-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtractSublimePackage.py
More file actions
125 lines (103 loc) · 4.01 KB
/
Copy pathExtractSublimePackage.py
File metadata and controls
125 lines (103 loc) · 4.01 KB
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import collections
import contextlib
import errno
import os
import sublime, sublime_plugin
import sys
import zipfile
PLUGIN_NAME = "Extract Sublime Package"
PACKAGE_EXT = ".sublime-package"
str_type = str if sys.version_info[0] >= 3 else unicode
# decorator that manages function-level "globals" (like static in C functions)
def functionlocals(spec):
if isinstance(spec, str_type):
spec = spec.split(" ")
class _FuncLocals(object):
def __setattr__(self, name, attr):
if name in spec:
object.__setattr__(self, name, attr)
else:
raise AttributeError("invalid attribute '%s'" % name)
def _make_functionlocals(f):
_locals = _FuncLocals()
def _wrapper(*args, **kwargs):
return f(_locals, *args, **kwargs)
return _wrapper
return _make_functionlocals
def is_a_package(filename):
return filename is not None and os.path.splitext(filename)[1] == PACKAGE_EXT
# get and cache Packages mode
@functionlocals("dir_mode")
def packages_dir_mode(funclocals):
try:
return funclocals.dir_mode
except AttributeError:
p = sublime.packages_path()
funclocals.dir_mode = os.stat(p).st_mode
return funclocals.dir_mode
def _msg(msg):
sublime.status_message("%s: %s" % (PLUGIN_NAME, msg))
def extract_package(filename):
base_name = os.path.basename(filename)
dir_base_name = os.path.splitext(base_name)[0]
outdir = os.path.join(sublime.packages_path(), dir_base_name)
try:
arc = zipfile.ZipFile(filename)
except zipfile.BadZipfile:
# can't open the file
_msg("package file %s is invalid" % base_name)
with contextlib.closing(arc):
try:
os.mkdir(outdir, packages_dir_mode())
except OSError:
if os.path.isdir(outdir) and len(os.listdir(outdir)) == 0:
# directory existed, but was empty; continuing
pass
else:
# so this isn't great, let's abort
_msg("directory Packages/%s couldn't be created or wasn't empty" %
dir_base_name)
return
try:
arc.extractall(outdir)
except:
# so something else altogether went wrong
_msg("extracting '%s' failed" % base_name)
return
_msg("%s extracted" % base_name)
class ExtractCurrentPackageFileCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
if view is not None:
extract_package(view.file_name())
def is_enabled(self):
view = self.window.active_view()
return is_a_package(view.file_name()) if view is not None else False
def is_visible(self):
return self.is_enabled()
class ExtractSinglePackageFileCommand(sublime_plugin.WindowCommand):
def run(self, files):
extract_package(files[0])
def is_enabled(self, files):
return len(files) == 1 and is_a_package(files[0])
def is_visible(self, files):
return self.is_enabled(files)
class ExtractPackageFilesCommand(sublime_plugin.WindowCommand):
def run(self, files):
for filename in files:
extract_package(filename)
def is_enabled(self, files):
return len(files) > 1 and all(is_a_package(f) for f in files)
def is_visible(self, files):
return self.is_enabled(files)
class ExtractLoadedPackageFileEventListener(sublime_plugin.EventListener):
def on_load(self, view):
global_settings = sublime.load_settings("Preferences.sublime-settings")
ask_on_open = global_settings.get("extract_sublime_package_ask_on_open", False)
filename = view.file_name()
if is_a_package(filename) and ask_on_open:
if sublime.ok_cancel_dialog("Extract %s to the Packages directory?"
% os.path.basename(filename)):
extract_package(filename)