forked from gepd/uPiotMicroPythonTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_write.py
More file actions
123 lines (97 loc) · 4.05 KB
/
console_write.py
File metadata and controls
123 lines (97 loc) · 4.05 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
# This file is part of the uPiot project, https://github.com/gepd/upiot/
#
# MIT License
#
# Copyright (c) 2017 GEPD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sublime
from sublime_plugin import WindowCommand
from threading import Thread
from ..tools import serial
class upiotConsoleWriteCommand(WindowCommand):
port = None
def run(self):
self.window.show_input_panel('>>>', '', self.callback, None, None)
def is_eable(self):
return bool(serial.in_use)
def callback(self, data):
"""Console write callback
callback called after write any command in the ST input, if there
is the 'sampy' prefix, a sampy (ampy) function will be call, otherwise
the string enter will be write in the current serial connection
Arguments:
data {str} -- command to run or data to write
"""
# check if there is a port available
self.port = serial.selected_port(request_port=True)
if(not self.port):
return
# run sampy commands
if(data.startswith(('sampy', '--help'))):
data = data.split()
try:
cmd = data[1]
except IndexError:
cmd = data[0]
arg = data[2] if(len(data) > 2) else None
th = Thread(target=self.sampy_commands, args=(cmd, arg))
th.start()
self.window.run_command('upiot_console_write')
return
link = serial.serial_dict[self.port]
# destroy connection
if(data == '--close'):
from ..tools import message
link.disconnect()
link.destroy()
txt = message.open(self.port)
txt.print("\n\nConnection to port {0} closed.".format(self.port))
return
link.writable(data)
self.window.run_command('upiot_console_write')
def sampy_commands(self, option, arg=None):
"""Console commands
Executes the commands called from the console, this command
are called with the 'sampy' prefix
Arguments:
option {str} -- command option (ls, run, get, etc)
Keyword Arguments:
arg {str} -- command argument required in some commands like
the filename (default: {None})
"""
from ..tools import sampy_manager
commands = {}
commands['ls'] = sampy_manager.list_files
commands['run'] = sampy_manager.run_file
commands['get'] = sampy_manager.get_file
commands['put'] = sampy_manager.put_file
commands['rm'] = sampy_manager.remove_file
commands['mkdir'] = sampy_manager.make_folder
commands['rmdir'] = sampy_manager.remove_folder
commands['--help'] = sampy_manager.help
try:
commands[option]()
except:
try:
commands[option](arg)
except KeyError:
from ..tools import message
txt = message.open(self.port)
txt.print('\n\n>> CommandError: "{}" not found'.format(option))