This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
subl_pipenv.py
356 lines (241 loc) · 10.5 KB
/
subl_pipenv.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
import os
import sublime
import sublime_plugin
from .vendor import requests
from .vendor import parse
from .vendor import pipenvlib
TEMPLATE = "<a href='{0}'>{0}</a><br/>"
ALL_PACKAGES = list()
def plugin_loaded():
pass
class PipenvIsEnabledMixin:
def is_enabled(self):
open_files = [view.file_name() for view in sublime.active_window().views()]
for o_f in open_files:
o_f = os.path.abspath(o_f)
dirname = os.path.dirname(o_f)
dirname = os.path.sep.join([dirname, '..', '..'])
for root, dirs, files in os.walk(dirname, followlinks=True):
if 'Pipfile' in files:
return True
return False
class InstallHandler(sublime_plugin.ListInputHandler):
def __init__(self):
super(InstallHandler, self).__init__()
def _yield_packages(self):
# Set the status message.
sublime.status_message("Fetching all available packages from PyPi (just a sec!)…")
# Make the HTTP Request.
r = requests.get('https://pypi.python.org/simple/')
sublime.status_message("")
# Yield results.
for result in parse.findall(TEMPLATE, r.text):
yield result[0]
@property
def _all_packages(self):
return ALL_PACKAGES
@_all_packages.setter
def function(self, value):
global ALL_PACKAGES
ALL_PACKAGES = value
@property
def all_packages(self):
if self._all_packages:
return self._all_packages
for package in self._yield_packages():
self._all_packages.append(package)
# List special packages first, because I can.
kr_packages = (
'requests', 'requests-html', 'maya', 'records', 'httpbin', 'crayons',
'delegator.py', 'tablib', 'background', 'clint', 'xerox'
)
# Special shout-outs.
kr_favorites = ('django', 'flask', 'docopt', 'parse', 'apistar')
kr_favorites = list(kr_packages + kr_favorites)
# Reverse order.
kr_favorites.reverse()
for kr_package in kr_favorites:
package = self._all_packages.pop(self._all_packages.index(kr_package))
self._all_packages.insert(0, package)
return self._all_packages
def list_items(self):
return self.all_packages
def initial_text(self, *args):
return ""
class pipenv_install(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_install, self).__init__(text)
# def is_enabled(self):
# return super(pipenv_install, self).is_enabled()
def input(self, *args):
return InstallHandler()
def run(self, install_handler):
# The package to install.
package = install_handler
# The home directory for the current file name.
home = os.path.dirname(sublime.active_window().active_view().file_name())
p = pipenvlib.PipenvProject(home)
# Update package status.
sublime.status_message("Installing {!r} with Pipenv…".format(package))
# Show the console.
sublime.active_window().active_view().window().run_command('show_panel', {'panel': 'console'})
# Run the install command.
c = p.run('install {}'.format(package), block=False)
# Update the status bar.
sublime.status_message("Waiting for {!r} to install…".format(package))
# Block on subprocess…
c.block()
# Print results to console.
print(c.out)
# Assure that the intallation was successful.
try:
# Ensure installation was successful.
assert c.return_code == 0
# Update the status bar.
sublime.status_message("Success installing {!r}!".format(package))
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile')
# Hide the console.
sublime.active_window().active_view().window().run_command('hide_panel', {'panel': 'console'})
except AssertionError:
# Update the status bar.
sublime.status_message("Error installing {!r}!".format(package))
# Report the error.
print(c.err)
class pipenv_install_dev(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_install_dev, self).__init__(text)
# def is_enabled(self):
# return super(pipenv_install, self).is_enabled()
def input(self, *args):
return InstallHandler()
def run(self, install_handler):
# The package to install.
package = install_handler
# The home directory for the current file name.
home = os.path.dirname(sublime.active_window().active_view().file_name())
p = pipenvlib.PipenvProject(home)
# Update package status.
sublime.status_message("Installing {!r} with Pipenv…".format(package))
# Show the console.
sublime.active_window().active_view().window().run_command('show_panel', {'panel': 'console'})
# Run the install command.
c = p.run('install --dev {}'.format(package), block=False)
# Update the status bar.
sublime.status_message("Waiting for {!r} to install…".format(package))
# Block on subprocess…
c.block()
# Print results to console.
print(c.out)
# Assure that the intallation was successful.
try:
# Ensure installation was successful.
assert c.return_code == 0
# Update the status bar.
sublime.status_message("Success installing {!r}!".format(package))
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile')
# Hide the console.
sublime.active_window().active_view().window().run_command('hide_panel', {'panel': 'console'})
except AssertionError:
# Update the status bar.
sublime.status_message("Error installing {!r}!".format(package))
# Report the error.
print(c.err)
class UninstallHandler(sublime_plugin.ListInputHandler):
def __init__(self):
super(UninstallHandler, self).__init__()
def list_items(self):
home = os.path.dirname(sublime.active_window().active_view().file_name())
p = pipenvlib.PipenvProject(home)
return list(set([p.name for p in p.packages + p.dev_packages]))
def initial_text(self, *args):
return ""
class pipenv_uninstall(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_uninstall, self).__init__(text)
# def is_enabled(self):
# return super(pipenv_uninstall, self).is_enabled()
def input(self, *args):
return UninstallHandler()
def run(self, uninstall_handler):
# The package to install.
package = uninstall_handler
# The home directory for the current file name.
home = os.path.dirname(sublime.active_window().active_view().file_name())
p = pipenvlib.PipenvProject(home)
# Update package status.
sublime.status_message("Un–installing {!r} with Pipenv…".format(package))
# Show the console.
sublime.active_window().active_view().window().run_command('show_panel', {'panel': 'console'})
# Run the uninstall command.
c = p.run('uninstall {}'.format(package), block=False)
# Update the status bar.
sublime.status_message("Waiting for {!r} to un–install…".format(package))
# Block on subprocess…
c.block()
# Print results to console.
print(c.out)
# Assure that the intallation was successful.
try:
# Ensure installation was successful.
assert c.return_code == 0
# Update the status bar.
sublime.status_message("Success un–installing {!r}!".format(package))
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile')
# Hide the console.
sublime.active_window().active_view().window().run_command('hide_panel', {'panel': 'console'})
except AssertionError:
# Update the status bar.
sublime.status_message("Error un–installing {!r}!".format(package))
# Report the error.
print(c.err)
class pipenv_open_pipfile(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_open_pipfile, self).__init__(text)
# def is_enabled(self):
# return super(pipenv_open_pipfile, self).is_enabled()
def run(self):
# Update package status.
sublime.status_message("Opening {!r} with Pipenv…".format('Pipfile'))
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile')
class pipenv_open_pipfile_lock(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_open_pipfile_lock, self).__init__(text)
def is_enabled(self):
return super(pipenv_open_pipfile_lock, self).is_enabled()
def run(self):
# Update package status.
sublime.status_message("Opening {!r} with Pipenv…".format('Pipfile.lock'))
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile.lock')
class pipenv_lock(PipenvIsEnabledMixin, sublime_plugin.WindowCommand):
def __init__(self, text):
super(pipenv_lock, self).__init__(text)
# def is_enabled(self):
# return super(pipenv_lock, self).is_enabled()
def run(self):
# Update package status.
sublime.status_message("Locking {!r} with Pipenv…".format('Pipfile'))
home = os.path.dirname(sublime.active_window().active_view().file_name())
p = pipenvlib.PipenvProject(home)
c = p.run('lock', block=False)
c.block()
try:
assert c.return_code == 0
# Update locking status.
sublime.status_message("Success!")
# Open the Pipfile.
sublime.active_window().active_view().window().open_file('Pipfile.lock')
sublime.status_message("")
except AssertionError:
# Show the console.
sublime.active_window().active_view().window().run_command('show_panel', {'panel': 'console'})
# Update locking status.
sublime.status_message("Error while locking!")
print(c.err)
if __name__ == '__main__':
if sublime.version() < '3000':
plugin_loaded()