forked from gepd/Deviot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviotStarter.py
641 lines (460 loc) · 17.3 KB
/
DeviotStarter.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
import os
import glob
import time
import sublime
import sublime_plugin
import threading
from shutil import rmtree
try:
from .libs import Paths, Tools
from .libs.Menu import Menu
from .libs.Messages import Console
from .libs.PlatformioCLI import PlatformioCLI
from .libs.Preferences import Preferences
from .libs.QuickPanel import quickPanel
from .libs import Libraries
from .libs.I18n import I18n
from .libs import Serial
from .libs import Messages
from .libs.Progress import ThreadProgress
from .libs.Install import PioInstall
except:
from libs import Paths
from libs import Tools
from libs.Menu import Menu
from libs.Messages import Console
from libs.PlatformioCLI import PlatformioCLI
from libs.Preferences import Preferences
from libs.QuickPanel import quickPanel
from libs import Libraries
from libs.I18n import I18n
from libs import Serial
from libs import Messages
from libs.Progress import ThreadProgress
from libs.Install import PioInstall
_ = I18n().translate
package_name = 'Deviot'
def plugin_loaded():
protected = Preferences().get('protected')
if(not protected):
thread = threading.Thread(target=PioInstall().checkPio)
thread.start()
ThreadProgress(thread, _('processing'), _('done'))
else:
# creating files
Tools.createCompletions()
Tools.createSyntaxFile()
Menu().createMainMenu()
Menu().createLibraryImportMenu()
Menu().createLibraryExamplesMenu()
# Run serial port listener
Serial_Lib = Serial.SerialListener(func=Menu().createSerialPortsMenu)
Serial_Lib.start()
def plugin_unloaded():
try:
from package_control import events
if events.remove(package_name):
Tools.removePreferences()
except:
pass
# Compat with ST2
if(int(sublime.version()) < 3000):
sublime.set_timeout(plugin_loaded, 300)
unload_handler = plugin_unloaded
class DeviotListener(sublime_plugin.EventListener):
"""
This is the first class to run when the plugin is excecuted
Extends: sublime_plugin.EventListener
"""
def on_activated(self, view):
"""
Set the current version of Deviot
Arguments: view {ST object} -- Sublime Text Object
"""
PlatformioCLI(view, command=False).checkInitFile()
Tools.setStatus(view)
Tools.userPreferencesStatus(view)
def on_close(self, view):
"""
When a sketch is closed, temp files are deleted
Arguments: view {ST object} -- Sublime Text Object
"""
# Serial Monitor
monitor_module = Serial
if Messages.isMonitorView(view):
name = view.name()
serial_port = name.split('-')[1].strip()
if serial_port in monitor_module.serials_in_use:
cur_serial_monitor = monitor_module.serial_monitor_dict.get(
serial_port, None)
if cur_serial_monitor:
cur_serial_monitor.stop()
monitor_module.serials_in_use.remove(serial_port)
# Remove cache
keep_cache = Preferences().get('keep_cache', False)
if(keep_cache):
return
file_path = Tools.getPathFromView(view)
if(not file_path):
return
file_name = Tools.getFileNameFromPath(file_path, ext=False)
tmp_path = Paths.getTempPath()
tmp_all = os.path.join(tmp_path, '*')
tmp_all = glob.glob(tmp_all)
for content in tmp_all:
if file_name in content:
tmp_path = os.path.join(tmp_path, content)
rmtree(tmp_path, ignore_errors=False)
Preferences().set('builded_sketch', False)
# Empty enviroment menu
Menu().createEnvironmentMenu(empty=True)
class DeviotNewSketchCommand(sublime_plugin.WindowCommand):
def run(self):
caption = _('caption_new_sketch')
self.window.show_input_panel(caption, '', self.on_done, None, None)
def on_done(self, sketch_name):
Paths.selectDir(self.window, key=sketch_name, func=Tools.createSketch)
class DeviotSelectBoardCommand(sublime_plugin.WindowCommand):
"""
This class trigger two methods to know what board(s)
were chosen and to store it in a preference file.
Extends: sublime_plugin.WindowCommand
"""
def run(self, board_id):
native = Preferences().get('native', False)
remove = Preferences().boardSelected(board_id)
if(remove):
PlatformioCLI().removeEnvFromFile(board_id)
if(native and not remove):
view = self.window.active_view()
console_name = 'Deviot|Init' + str(time.time())
console = Console(view.window(), name=console_name)
Preferences().set('init_queue', board_id)
PlatformioCLI(view, console).openInThread('init', chosen=board_id)
Menu().createEnvironmentMenu()
def is_checked(self, board_id):
check = Preferences().checkBoard(board_id)
return check
def is_enabled(self):
return Preferences().get('enable_menu', False)
class SelectEnvCommand(sublime_plugin.WindowCommand):
"""
Stores the environment option selected by the user in
the preferences files
Extends: sublime_plugin.WindowCommand
"""
def run(self, board_id):
native = Preferences().get('native', False)
key = 'env_selected'
if(native):
key = 'native_env_selected'
Preferences().set(key, board_id)
Tools.userPreferencesStatus(self.window.active_view())
def is_checked(self, board_id):
native = Preferences().get('native', False)
key = 'env_selected'
if(native):
key = 'native_env_selected'
check = Preferences().get(key, False)
return board_id == check
def is_enabled(self):
return Preferences().get('enable_menu', False)
class SearchLibraryCommand(sublime_plugin.WindowCommand):
"""
Command to search a library in the platformio API
Extends: sublime_plugin.WindowCommand
"""
def run(self):
caption = _('search_query')
self.window.show_input_panel(caption, '', self.on_done, None, None)
def on_done(self, result):
Libraries.openInThread('download', self.window, result)
class ShowResultsCommand(sublime_plugin.WindowCommand):
"""
The results of the SearchLibraryCommand query in a quick_panel.
When one of the result is selected, it's installed by CLI
Extends: sublime_plugin.WindowCommand
"""
def run(self):
choose = Libraries.Libraries().getList()
quickPanel(self.window, choose, self.on_done)
def on_done(self, result):
if(result != -1):
Libraries.openInThread('install', self.window, result)
class RemoveLibraryCommand(sublime_plugin.WindowCommand):
"""
Remove a library by the CLI
Extends: sublime_plugin.WindowCommand
"""
def run(self):
Libraries.openInThread('list', self.window)
class ShowRemoveListCommand(sublime_plugin.WindowCommand):
"""
Show the list with all the installed libraries, and what you can remove
Extends: sublime_plugin.WindowCommand
"""
def run(self):
choose = Libraries.Libraries(self.window).installedList()
quickPanel(self.window, choose, self.on_done)
def on_done(self, result):
if(result != -1):
Libraries.openInThread('remove', self.window, result)
class AddLibraryCommand(sublime_plugin.TextCommand):
"""
Include the header(s) from the selected library into a sketch
Extends: sublime_plugin.TextCommand
"""
def run(self, edit, library_path):
Tools.addLibraryToSketch(self.view, edit, library_path)
class OpenExampleCommand(sublime_plugin.WindowCommand):
"""
Open the selected example from the deviot menu
Extends: sublime_plugin.WindowCommand
"""
def run(self, example_path):
Tools.openExample(example_path, self.window)
class OpenLibraryFolderCommand(sublime_plugin.TextCommand):
"""
Open a new window where the user libreries must be installed
Extends: sublime_plugin.TextCommand
"""
def run(self, edit):
library = Paths.getPioLibrary()
url = Paths.getOpenFolderPath(library)
sublime.run_command('open_url', {'url': url})
class BuildSketchCommand(sublime_plugin.TextCommand):
"""
Trigger a method to build the files in the current
view, initializes the console to show the state of
the process
Extends: sublime_plugin.TextCommand
"""
def run(self, edit):
view = self.view
console_name = 'Deviot|Build' + str(time.time())
console = Console(view.window(), name=console_name)
PlatformioCLI(view, console).openInThread('build')
def is_enabled(self):
return Preferences().get('enable_menu', False)
class UploadSketchCommand(sublime_plugin.TextCommand):
"""
Trigger a method to upload the files in the current
view, initializes the console to show the state of
the process
Extends: sublime_plugin.TextCommand
"""
def run(self, edit):
view = self.view
console_name = 'Deviot|Upload' + str(time.time())
console = Console(view.window(), name=console_name)
PlatformioCLI(view, console).openInThread('upload')
def is_enabled(self):
return Preferences().get('enable_menu')
class CleanSketchCommand(sublime_plugin.TextCommand):
"""
Trigger a method to delete firmware/program binaries compiled
if a sketch has been built previously
Extends: sublime_plugin.TextCommand
"""
def run(self, edit):
view = self.view
console_name = 'Deviot|Clean' + str(time.time())
console = Console(view.window(), name=console_name)
PlatformioCLI(view, console).openInThread('clean')
def is_enabled(self):
is_enabled = Preferences().get('builded_sketch')
if(not Preferences().get('enable_menu', False)):
is_enabled = False
return is_enabled
class HideConsoleCommand(sublime_plugin.WindowCommand):
"""
Hide the deviot console
Extends: sublime_plugin.WindowCommand
"""
def run(self):
self.window.run_command("hide_panel", {"panel": "output.exec"})
class SelectPortCommand(sublime_plugin.WindowCommand):
"""
Saves the port COM selected by the user in the
preferences file.
Extends: sublime_plugin.WindowCommand
"""
def run(self, id_port):
Preferences().set('id_port', id_port)
def is_checked(self, id_port):
saved_id_port = Preferences().get('id_port')
return saved_id_port == id_port
class AddSerialIpCommand(sublime_plugin.WindowCommand):
"""
Add a IP to the list of COM ports
Extends: sublime_plugin.WindowCommand
"""
def run(self):
caption = _('add_ip_caption')
self.window.show_input_panel(caption, '', self.on_done, None, None)
def on_done(self, result):
if(result != -1):
result = (result if result != 0 else '')
Preferences().set('ip_port', result)
Menu().createSerialPortsMenu()
class SerialMonitorRunCommand(sublime_plugin.WindowCommand):
"""
Run a selected serial monitor and show the messages in a new window
Extends: sublime_plugin.WindowCommand
"""
def run(self):
Tools.toggleSerialMonitor(self.window)
def is_checked(self):
monitor_module = Serial
state = False
serial_port = Preferences().get('id_port', '')
if serial_port in monitor_module.serials_in_use:
serial_monitor = monitor_module.serial_monitor_dict.get(
serial_port)
if serial_monitor and serial_monitor.isRunning():
state = True
return state
class SendMessageSerialCommand(sublime_plugin.WindowCommand):
"""
Send a text over the selected serial port
Extends: sublime_plugin.WindowCommand
"""
def run(self):
caption = _('send')
self.window.show_input_panel(caption, '', self.on_done, None, None)
def on_done(self, text):
if(text):
Tools.sendSerialMessage(text)
self.window.run_command('send_message_serial')
class ChooseBaudrateItemCommand(sublime_plugin.WindowCommand):
"""
Stores the baudrate selected for the user and save it in
the preferences file.
Extends: sublime_plugin.WindowCommand
"""
def run(self, baudrate_item):
Preferences().set('baudrate', baudrate_item)
def is_checked(self, baudrate_item):
target_baudrate = Preferences().get('baudrate', 9600)
return baudrate_item == target_baudrate
class ChooseLineEndingItemCommand(sublime_plugin.WindowCommand):
"""
Stores the Line ending selected for the user and save it in
the preferences file.
Extends: sublime_plugin.WindowCommand
"""
def run(self, line_ending_item):
Preferences().set('line_ending', line_ending_item)
def is_checked(self, line_ending_item):
target_line_ending = Preferences().get('line_ending', '\n')
return line_ending_item == target_line_ending
class ChooseDisplayModeItemCommand(sublime_plugin.WindowCommand):
"""
Stores the display mode selected for the user and save it in
the preferences file.
Extends: sublime_plugin.WindowCommand
"""
def run(self, display_mode_item):
Preferences().set('display_mode', display_mode_item)
def is_checked(self, display_mode_item):
target_display_mode = Preferences().get('display_mode', 'Text')
return display_mode_item == target_display_mode
class UpgradePioCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
console_name = 'Deviot|Upgrade' + str(time.time())
console = Console(view.window(), name=console_name)
PlatformioCLI(view, console, install=True).openInThread('upgrade')
class UpdateBoardListCommand(sublime_plugin.WindowCommand):
"""
Update the board list, extracting the info from platformIO
ecosystem
Extends: sublime_plugin.WindowCommand
"""
def run(self):
PlatformioCLI().saveAPIBoards(update_method=Menu().createMainMenu)
class ToggleVerboseCommand(sublime_plugin.WindowCommand):
"""
Saves the verbose output option selected by the user in the
preferences file.
Extends: sublime_plugin.WindowCommand
"""
def run(self):
verbose = Preferences().get('verbose_output', False)
Preferences().set('verbose_output', not verbose)
def is_checked(self):
return Preferences().get('verbose_output', False)
class RemoveUserFilesCommand(sublime_plugin.WindowCommand):
def run(self):
confirm = sublime.ok_cancel_dialog(
_('confirm_del_pref'), _('continue'))
if(confirm):
Tools.removePreferences()
class KeepTempFilesCommand(sublime_plugin.WindowCommand):
"""
When is select avoid to remove the cache from the temporal folder.
Extends: sublime_plugin.WindowCommand
"""
def run(self):
keep = Preferences().get('keep_cache', False)
Preferences().set('keep_cache', not keep)
def is_checked(self):
return Preferences().get('keep_cache', False)
class OpenBuildFolderCommand(sublime_plugin.TextCommand):
"""
Open a new window where the user libreries must be installed
Extends: sublime_plugin.TextCommand
"""
def run(self, edit):
temp = Paths.getTempPath()
url = Paths.getOpenFolderPath(temp)
sublime.run_command('open_url', {'url': url})
class ChangeBuildFolderCommand(sublime_plugin.WindowCommand):
def run(self):
Paths.selectDir(self.window, key='build_dir', func=Preferences().set)
class UseCppTemplate(sublime_plugin.WindowCommand):
def run(self):
keep = Preferences().get('use_cpp', False)
Preferences().set('use_cpp', not keep)
def is_checked(self):
return Preferences().get('use_cpp', False)
class SelectLanguageCommand(sublime_plugin.WindowCommand):
def run(self, id_lang):
Preferences().set('id_lang', id_lang)
def is_checked(self, id_lang):
saved_id_lang = Preferences().get('id_lang')
return saved_id_lang == id_lang
class DonateDeviotCommand(sublime_plugin.WindowCommand):
"""
Show the Deviot github site.
Extends: sublime_plugin.WindowCommand
"""
def run(self):
sublime.run_command('open_url', {'url': 'https://goo.gl/LqdDrC'})
class AboutDeviotCommand(sublime_plugin.WindowCommand):
"""
Show the Deviot github site.
Extends: sublime_plugin.WindowCommand
"""
def run(self):
sublime.run_command('open_url', {'url': 'https://goo.gl/c41EXS'})
class AboutPioCommand(sublime_plugin.WindowCommand):
"""
Show the Deviot github site.
Extends: sublime_plugin.WindowCommand
"""
def run(self):
sublime.run_command('open_url', {'url': 'http://goo.gl/66BHnk'})
class AddStatusCommand(sublime_plugin.TextCommand):
"""
Add a message in the status bar
Extends: sublime_plugin.TextCommand
"""
def run(self, edit, text, erase_time):
Tools.setStatus(self.view, text, erase_time)