-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
SideBarAPI.py
855 lines (737 loc) · 28.7 KB
/
SideBarAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# coding=utf8
import sublime
import os
import re
import shutil
class Object:
pass
def expandVars(path):
for k, v in list(os.environ.items()):
path = path.replace("%" + k + "%", v).replace("%" + k.lower() + "%", v)
return path
def escapeCMDWindows(string):
return string.replace("^", "^^")
BINARY = re.compile(
"\.(psd|ai|cdr|ico|cache|sublime-package|eot|svgz|ttf|woff|zip|tar|gz|rar|bz2|jar|xpi|mov|mpeg|avi|mpg|flv|wmv|mp3|wav|aif|aiff|snd|wma|asf|asx|pcm|pdf|doc|docx|xls|xlsx|ppt|pptx|rtf|sqlite|sqlitedb|fla|swf|exe)$",
re.I,
)
class SideBarSelection:
def __init__(self, paths=[]):
if not paths or len(paths) < 1:
try:
path = sublime.active_window().active_view().file_name()
if self.isNone(path):
paths = []
else:
paths = [path]
except:
paths = []
self._paths = paths
self._paths.sort()
self._obtained_selection_information_basic = False
self._obtained_selection_information_extended = False
def len(self):
return len(self._paths)
def hasDirectories(self):
self._obtainSelectionInformationBasic()
return self._has_directories
def hasFiles(self):
self._obtainSelectionInformationBasic()
return self._has_files
def hasOnlyDirectories(self):
self._obtainSelectionInformationBasic()
return self._only_directories
def hasOnlyFiles(self):
self._obtainSelectionInformationBasic()
return self._only_files
def hasProjectDirectories(self):
if self.hasDirectories():
project_directories = SideBarProject().getDirectories()
for item in self.getSelectedDirectories():
if item.path() in project_directories:
return True
return False
else:
return False
def hasItemsUnderProject(self):
for item in self.getSelectedItems():
if item.isUnderCurrentProject():
return True
return False
def hasImages(self):
return self.hasFilesWithExtension("gif|jpg|jpeg|png")
def hasFilesWithExtension(self, extensions):
extensions = re.compile("(" + extensions + ")$", re.I)
for item in self.getSelectedFiles():
if extensions.search(item.path()):
return True
return False
def getSelectedItems(self):
self._obtainSelectionInformationExtended()
return self._files + self._directories
def getSelectedItemsWithoutChildItems(self):
self._obtainSelectionInformationExtended()
items = []
for item in self._items_without_containing_child_items:
items.append(SideBarItem(item, os.path.isdir(item)))
return items
def getSelectedDirectories(self):
self._obtainSelectionInformationExtended()
return self._directories
def getSelectedFiles(self):
self._obtainSelectionInformationExtended()
return self._files
def getSelectedDirectoriesOrDirnames(self):
self._obtainSelectionInformationExtended()
return self._directories_or_dirnames
def getSelectedImages(self):
return self.getSelectedFilesWithExtension("gif|jpg|jpeg|png")
def getSelectedFilesWithExtension(self, extensions):
items = []
extensions = re.compile("(" + extensions + ")$", re.I)
for item in self.getSelectedFiles():
if extensions.search(item.path()):
items.append(item)
return items
def _obtainSelectionInformationBasic(self):
if not self._obtained_selection_information_basic:
self._obtained_selection_information_basic = True
self._has_directories = False
self._has_files = False
self._only_directories = False
self._only_files = False
for path in self._paths:
if self._has_directories == False and os.path.isdir(path):
self._has_directories = True
if self._has_files == False and os.path.isdir(path) == False:
self._has_files = True
if self._has_files and self._has_directories:
break
if self._has_files and self._has_directories:
self._only_directories = False
self._only_files = False
elif self._has_files:
self._only_files = True
elif self._has_directories:
self._only_directories = True
def _obtainSelectionInformationExtended(self):
if not self._obtained_selection_information_extended:
self._obtained_selection_information_extended = True
self._directories = []
self._files = []
self._directories_or_dirnames = []
self._items_without_containing_child_items = []
_directories = []
_files = []
_directories_or_dirnames = []
_items_without_containing_child_items = []
for path in self._paths:
if os.path.isdir(path):
item = SideBarItem(path, True)
if item.path() not in _directories:
_directories.append(item.path())
self._directories.append(item)
if item.path() not in _directories_or_dirnames:
_directories_or_dirnames.append(item.path())
self._directories_or_dirnames.append(item)
_items_without_containing_child_items = (
self._itemsWithoutContainingChildItems(
_items_without_containing_child_items, item.path()
)
)
else:
item = SideBarItem(path, False)
if item.path() not in _files:
_files.append(item.path())
self._files.append(item)
_items_without_containing_child_items = (
self._itemsWithoutContainingChildItems(
_items_without_containing_child_items, item.path()
)
)
item = SideBarItem(os.path.dirname(path), True)
if item.path() not in _directories_or_dirnames:
_directories_or_dirnames.append(item.path())
self._directories_or_dirnames.append(item)
self._items_without_containing_child_items = (
_items_without_containing_child_items
)
def _itemsWithoutContainingChildItems(self, items, item):
new_list = []
add = True
for i in items:
if i.find(item + "\\") == 0 or i.find(item + "/") == 0:
continue
else:
new_list.append(i)
if (item + "\\").find(i + "\\") == 0 or (item + "/").find(i + "/") == 0:
add = False
if add:
new_list.append(item)
return new_list
def isNone(self, path):
if (
path == None
or path == ""
or path == "."
or path == ".."
or path == "./"
or path == "../"
or path == "/"
or path == "//"
or path == "\\"
or path == "\\\\"
or path == "\\\\\\\\"
or path == "\\\\?\\"
or path == "\\\\?"
or path == "\\\\\\\\?\\\\"
):
return True
else:
return False
class SideBarProject:
def getDirectories(self):
return sublime.active_window().folders()
def hasDirectories(self):
return len(self.getDirectories()) > 0
def hasOpenedProject(self):
return self.getProjectFile() != None
def getDirectoryFromPath(self, path):
for directory in self.getDirectories():
maybe_path = path.replace(directory, "", 1)
if maybe_path != path:
return directory
def getProjectFile(self):
return sublime.active_window().project_file_name()
def getProjectJson(self):
return sublime.active_window().project_data()
def setProjectJson(self, data):
return sublime.active_window().set_project_data(data)
def excludeDirectory(self, path, exclude):
data = self.getProjectJson()
for folder in data["folders"]:
project_folder = folder["path"]
if project_folder == ".":
project_folder = SideBarItem(self.getProjectFile(), False).dirname()
if path.find(project_folder) == 0:
try:
folder["folder_exclude_patterns"].append(exclude)
except:
folder["folder_exclude_patterns"] = [exclude]
self.setProjectJson(data)
def excludeFile(self, path, exclude):
data = self.getProjectJson()
for folder in data["folders"]:
project_folder = folder["path"]
if project_folder == ".":
project_folder = SideBarItem(self.getProjectFile(), False).dirname()
if path.find(project_folder) == 0:
try:
folder["file_exclude_patterns"].append(exclude)
except:
folder["file_exclude_patterns"] = [exclude]
self.setProjectJson(data)
def add(self, path):
data = self.getProjectJson()
if data:
data["folders"].append({"follow_symlinks": True, "path": path})
else:
data = {"folders": [{"follow_symlinks": True, "path": path}]}
self.setProjectJson(data)
def refresh(self):
sublime.active_window().run_command("refresh_folder_list")
class SideBarItem:
def __init__(self, path, is_directory):
self._path = path
self._is_directory = is_directory
def path(self, path=""):
if path == "":
return self._path
else:
self._path = path
self._is_directory = os.path.isdir(path)
return path
def pathWithoutProject(self):
path = self.path()
for directory in SideBarProject().getDirectories():
path = path.replace(directory, "", 1)
return path.replace("\\", "/")
def pathProject(self):
path = self.path()
for directory in SideBarProject().getDirectories():
path2 = path.replace(directory, "", 1)
if path2 != path:
return directory
return False
def url(self, type):
filenames = []
# scans a la htaccess
item = SideBarItem(self.path(), self.isDirectory())
while not os.path.exists(item.join(".sublime/SideBarEnhancements.json")):
if item.dirname() == item.path():
break
item.path(item.dirname())
item = SideBarItem(item.join(".sublime/SideBarEnhancements.json"), False)
if item.exists():
filenames.append(item.path())
filenames.append(
os.path.dirname(sublime.packages_path())
+ "/Settings/SideBarEnhancements.json"
)
import collections
for filename in filenames:
if os.path.lexists(filename):
import json
data = open(filename, "r").read()
data = (
data.replace("\t", " ")
.replace("\\", "/")
.replace("\\", "/")
.replace("//", "/")
.replace("//", "/")
.replace("http:/", "http://")
.replace("https:/", "https://")
)
data = json.loads(
data, strict=False, object_pairs_hook=collections.OrderedDict
)
for key in list(data.keys()):
# print('-------------------------------------------------------')
# print(key);
if filename == filenames[len(filenames) - 1]:
base = expandVars(key)
else:
base = os.path.normpath(expandVars(key))
base = (
base.replace("\\", "/")
.replace("\\", "/")
.replace("//", "/")
.replace("//", "/")
)
# print(base)
current = (
self.path()
.replace("\\", "/")
.replace("\\", "/")
.replace("//", "/")
.replace("//", "/")
)
# print(current)
url_path = re.sub(
re.compile("^" + re.escape(base), re.IGNORECASE), "", current
)
# print(url_path)
if url_path != current:
url = data[key][type]
if url:
if url[-1:] != "/":
url = url + "/"
import urllib.request, urllib.parse, urllib.error
return url + (re.sub("^/", "", urllib.parse.quote(url_path)))
return False
def isUnderCurrentProject(self):
path = self.path()
path2 = self.path()
for directory in SideBarProject().getDirectories():
path2 = path2.replace(directory, "", 1)
return path != path2
def pathRelativeFromProject(self):
return re.sub("^/+", "", self.pathWithoutProject())
def pathRelativeFromProjectEncoded(self):
import urllib.request, urllib.parse, urllib.error
return urllib.parse.quote(self.pathRelativeFromProject())
def pathRelativeFromView(self):
path = sublime.active_window().active_view().file_name()
if path:
return os.path.relpath(self.path(), os.path.dirname(path)).replace(
"\\", "/"
)
else:
return None
def pathRelativeFromViewEncoded(self):
import urllib.request, urllib.parse, urllib.error
return urllib.parse.quote(
os.path.relpath(
self.path(),
os.path.dirname(sublime.active_window().active_view().file_name()),
).replace("\\", "/")
)
def pathAbsoluteFromProject(self):
return self.pathWithoutProject()
def pathAbsoluteFromProjectEncoded(self):
import urllib.request, urllib.parse, urllib.error
return urllib.parse.quote(self.pathAbsoluteFromProject())
def uri(self):
uri = "file:" + (self.path().replace("\\", "/").replace("//", "/"))
return uri
def join(self, name):
return os.path.join(self.path(), name)
def dirname(self):
branch, leaf = os.path.split(self.path())
return branch
def forCwdSystemPath(self):
if self.isDirectory():
return self.path()
else:
return self.dirname()
def forCwdSystemName(self):
if self.isDirectory():
return "."
else:
path = self.path()
branch = self.dirname()
leaf = path.replace(branch, "", 1).replace("\\", "").replace("/", "")
return leaf
def forCwdSystemPathRelativeFrom(self, relativeFrom):
relative = SideBarItem(relativeFrom, os.path.isdir(relativeFrom))
path = self.path().replace(relative.path(), "", 1).replace("\\", "/")
if path == "":
return "."
else:
return re.sub("^/+", "", path)
def forCwdSystemPathRelativeFromRecursive(self, relativeFrom):
relative = SideBarItem(relativeFrom, os.path.isdir(relativeFrom))
path = self.path().replace(relative.path(), "", 1).replace("\\", "/")
if path == "":
return "."
else:
if self.isDirectory():
return re.sub("^/+", "", path) + "/"
else:
return re.sub("^/+", "", path)
def dirnameCreate(self):
try:
self._makedirs(self.dirname())
except:
pass
def name(self):
branch, leaf = os.path.split(self.path())
return leaf
def nameEncoded(self):
import urllib.request, urllib.parse, urllib.error
return urllib.parse.quote(self.name())
def namePretty(self):
return (
self.name()
.replace(self.extension(), "")
.replace("-", " ")
.replace("_", " ")
.strip()
)
def open(self, use_powershell=True, command=""):
if self.isDirectory():
import subprocess
if sublime.platform() == "osx":
subprocess.Popen(
[
"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal",
".",
],
cwd=self.forCwdSystemPath(),
)
elif sublime.platform() == "windows":
if command:
command = [command] + ["."]
flat_command = [item for sublist in command for item in sublist]
subprocess.Popen(
flat_command, cwd=self.forCwdSystemPath(), shell=True
)
elif use_powershell:
try:
subprocess.Popen(
["start", "powershell"],
cwd=self.forCwdSystemPath(),
shell=True,
)
except:
subprocess.Popen(
[
"start",
"%AppData%/../Local/Microsoft/WindowsApps/wt.exe",
"-d",
".",
],
cwd=self.forCwdSystemPath(),
shell=True,
)
else:
subprocess.Popen(
[
"start",
"%AppData%/../Local/Microsoft/WindowsApps/wt.exe",
"-d",
".",
],
cwd=self.forCwdSystemPath(),
shell=True,
)
elif sublime.platform() == "linux":
subprocess.Popen(["gnome-terminal", "."], cwd=self.forCwdSystemPath())
else:
if sublime.platform() == "osx":
import subprocess
subprocess.Popen(["open", self.name()], cwd=self.dirname())
elif sublime.platform() == "windows":
import subprocess
subprocess.Popen(
[
"start",
"%AppData%/../Local/Microsoft/WindowsApps/wt.exe",
escapeCMDWindows(self.path()),
],
cwd=self.dirname(),
shell=True,
)
else:
from . import desktop
desktop.open(self.path())
def edit(self):
if BINARY.search(self.path()):
return None
else:
view = sublime.active_window().open_file(self.path())
view.settings().set("open_with_edit", True)
return view
def isDirectory(self):
return self._is_directory
def isFile(self):
return self.isDirectory() == False
def contentUTF8(self):
return open(self.path(), "r", newline="", encoding="utf-8").read()
def contentBinary(self):
return open(self.path(), "rb").read()
def contentBase64(self):
import base64
base64text = base64.b64encode(self.contentBinary()).decode("utf-8")
return (
"data:"
+ self.mime()
+ ";charset=utf-8;base64,"
+ (base64text.replace("\n", ""))
)
def reveal(self):
if sublime.platform() == "windows":
import subprocess
if self.isDirectory():
subprocess.Popen(["explorer", escapeCMDWindows(self.path())])
else:
subprocess.Popen(
["explorer", "/select,", escapeCMDWindows(self.path())]
)
else:
sublime.active_window().run_command(
"open_dir", {"dir": self.dirname(), "file": self.name()}
)
def write(self, content):
with open(self.path(), "w+", encoding="utf8", newline="") as f:
f.write(str(content))
if 3000 <= int(sublime.version()) < 3088:
# Fixes as best as possible a new file permissions issue
# See https://github.com/titoBouzout/SideBarEnhancements/issues/203
# See https://github.com/SublimeTextIssues/Core/issues/239
oldmask = os.umask(0o000)
if oldmask == 0:
os.chmod(self.path(), 0o644)
os.umask(oldmask)
def mime(self):
import mimetypes
return mimetypes.guess_type(self.path())[0] or "application/octet-stream"
def extension(self):
try:
return (
re.compile("(\.[^\.]+(\.[^\.]{2,4})?)$")
.findall("name" + self.name())[0][0]
.lower()
)
except:
return os.path.splitext("name" + self.name())[1].lower()
def exists(self):
return os.path.isdir(self.path()) or os.path.isfile(self.path())
def overwrite(self):
overwrite = sublime.ok_cancel_dialog(
"Destination exists", "Delete, and overwrite"
)
if overwrite:
from SideBarEnhancements.send2trash import send2trash
send2trash(self.path())
return True
else:
return False
def create(self):
if self.isDirectory():
self.dirnameCreate()
self._makedirs(self.path())
else:
self.dirnameCreate()
self.write("")
def _makedirs(self, path):
if 3000 <= int(sublime.version()) < 3088:
# Fixes as best as possible a new directory permissions issue
# See https://github.com/titoBouzout/SideBarEnhancements/issues/203
# See https://github.com/SublimeTextIssues/Core/issues/239
oldmask = os.umask(0o000)
if oldmask == 0:
os.makedirs(path, 0o755)
else:
os.makedirs(path)
os.umask(oldmask)
else:
os.makedirs(path)
def copy(self, location, replace=False):
location = SideBarItem(location, os.path.isdir(location))
if location.exists() and replace == False:
return False
elif location.exists() and location.isFile():
os.remove(location.path())
location.dirnameCreate()
if self.isDirectory():
if location.exists():
self.copyRecursive(self.path(), location.path())
else:
shutil.copytree(self.path(), location.path())
else:
shutil.copy2(self.path(), location.path())
return True
def copyRecursive(self, _from, _to):
if os.path.isfile(_from) or os.path.islink(_from):
try:
self._makedirs(os.path.dirname(_to))
except:
pass
if os.path.exists(_to):
os.remove(_to)
shutil.copy2(_from, _to)
else:
try:
self._makedirs(_to)
except:
pass
for content in os.listdir(_from):
__from = os.path.join(_from, content)
__to = os.path.join(_to, content)
self.copyRecursive(__from, __to)
def move(self, location, replace=False):
location = SideBarItem(location, os.path.isdir(location))
if location.exists() and replace == False:
if self.path().lower() == location.path().lower():
pass
else:
return False
elif location.exists() and location.isFile():
os.remove(location.path())
if self.path().lower() == location.path().lower():
location.dirnameCreate()
os.rename(self.path(), location.path() + ".sublime-temp")
os.rename(location.path() + ".sublime-temp", location.path())
self._moveMoveViews(self.path(), location.path())
else:
location.dirnameCreate()
if location.exists():
self.moveRecursive(self.path(), location.path())
else:
shutil.move(self.path(), location.path())
self._moveMoveViews(self.path(), location.path())
return True
def moveRecursive(self, _from, _to):
if os.path.isfile(_from) or os.path.islink(_from):
try:
self._makedirs(os.path.dirname(_to))
except:
pass
if os.path.exists(_to):
os.remove(_to)
shutil.move(_from, _to)
else:
try:
self._makedirs(_to)
except:
pass
for content in os.listdir(_from):
__from = os.path.join(_from, content)
__to = os.path.join(_to, content)
self.moveRecursive(__from, __to)
os.rmdir(_from)
def _moveMoveViews(self, old, location):
for window in sublime.windows():
active_view = window.active_view()
views = []
for view in window.views():
if view.file_name():
views.append(view)
views.reverse()
for view in views:
if old == view.file_name():
active_view = self._moveMoveView(
window, view, location, active_view
)
elif view.file_name().find(old + "\\") == 0:
active_view = self._moveMoveView(
window,
view,
view.file_name().replace(old + "\\", location + "\\", 1),
active_view,
)
elif view.file_name().find(old + "/") == 0:
active_view = self._moveMoveView(
window,
view,
view.file_name().replace(old + "/", location + "/", 1),
active_view,
)
def _moveMoveView(self, window, view, location, active_view):
view.retarget(location)
def closeViews(self):
path = self.path()
closed_items = []
for window in sublime.windows():
active_view = window.active_view()
views = []
for view in window.views():
if view.file_name():
views.append(view)
views.reverse()
for view in views:
if (
path == view.file_name()
or view.file_name().find(path + "\\") == 0
or view.file_name().find(path + "/") == 0
):
if view.window():
closed_items.append(
[
view.file_name(),
view.window(),
view.window().get_view_index(view),
]
)
if len(window.views()) == 1:
window.new_file()
view.set_scratch(True)
view.close()
# try to repaint
try:
window.focus_view(active_view)
window.focus_view(window.active_view())
except:
try:
window.focus_view(window.active_view())
except:
pass
return closed_items
def views(self):
path = self.path()
views = []
if self.isDirectory():
for window in sublime.windows():
for view in window.views():
if view.file_name() and (
view.file_name().find(path + "\\") == 0
or view.file_name().find(path + "/") == 0
):
views.append(view)
else:
for window in sublime.windows():
for view in window.views():
if view.file_name() and path == view.file_name():
views.append(view)
return views