forked from gepd/uPiotMicroPythonTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampy_manager.py
More file actions
331 lines (230 loc) · 7.81 KB
/
Copy pathsampy_manager.py
File metadata and controls
331 lines (230 loc) · 7.81 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
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
# 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 os import path, mkdir
from ..tools import check_sidebar_folder, make_folder as mkfolder
from ..tools import message, serial, errors
from ..tools.sampy import Sampy
from ..tools.ampy import files, pyboard
txt = None
port = None
def start_sampy():
"""
Opens the sampy connection in the selected port. If already is a serial
connection running it will look into it and close it.
Returns:
Sampy -- Sampy object
"""
global txt
global port
port = serial.selected_port()
# close the current connection in open port
if(port in serial.in_use):
run_serial = serial.serial_dict[port]
run_serial.close()
# message printer
txt = message.open(port)
txt.set_focus()
if(port):
try:
return Sampy(port, data_consumer=txt.print)
except pyboard.PyboardError as e:
from sys import exit
if('failed to access' in str(e)):
txt.print(errors.serialError_noaccess)
exit(0)
def finished_action():
"""
This function will be called after an action is finished, and will
re-open the serial connection if was open.
"""
global port
serial.establish_connection(port)
# opens the console window
sublime.active_window().run_command('upiot_console_write')
def run_file(filepath):
"""Run file in device
Runs the given file in the selected device.
Arguments:
filepath {str} -- file path
"""
global txt
sampy = start_sampy()
# print command name
file = path.basename(filepath)
head = '\n\n>> Run {0}\n\n"ctrl+shift+c" to stop the script.\n---'
txt.print(head.format(file))
sampy.run(filepath)
txt.print("\n[done]")
sampy.close()
finished_action()
def list_files():
"""List of files in device
Shows the list of files (and folders) from the selected device
"""
sampy = start_sampy()
txt.print('\n\n>> sampy ls\n')
for filename in sampy.ls():
txt.print('\n' + filename)
sampy.close()
finished_action()
def get_file(filename):
"""Get file from device
Gets the given file from the selected device
Arguments:
filename {str} -- name of the file
"""
sampy = start_sampy()
txt.print('\n\n>> get {0}'.format(filename))
try:
output = sampy.get(filename)
except RuntimeError as e:
output = str(e)
output = output.replace('\r\n', '\n').replace('\r', '\n')
txt.print('\n\n' + output)
sampy.close()
finished_action()
def get_files(destination):
"""Get files from devices
Gets all the files in the device and stored in the selected destination
path.
"""
sampy = start_sampy()
destination = path.normpath(destination)
mkfolder(destination)
txt.print('\n\n>> get from device to {0}'.format(destination))
for filename in sampy.ls():
filepath = path.normpath(path.join(destination, filename))
if(filename.endswith('/')):
if(not path.exists(filepath)):
mkdir(filepath)
else:
with open(filepath, 'w') as file:
file.write(sampy.get(filename))
txt.print("\n[done]")
sampy.close()
finished_action()
if(check_sidebar_folder(destination)):
return
caption = "files retrieved, would you like to" \
"add the folder to your current proyect?"
answer = sublime.yes_no_cancel_dialog(caption, "Add", "Append")
if(answer == sublime.DIALOG_CANCEL):
return
if(answer == sublime.DIALOG_YES):
append = False
elif(answer == sublime.DIALOG_NO):
append = True
options = {'folder': destination, 'append': append}
sublime.active_window().run_command('upiot_add_project', options)
def put_file(filepath):
"""Put given file in device
Puts the given in the selected device
Arguments:
filepath {str} -- path of the file to put
"""
sampy = start_sampy()
file = path.basename(filepath)
txt.print('\n\n>> put {0}'.format(file))
try:
sampy.put(path.normpath(filepath))
output = '[done]'
except FileNotFoundError as e:
output = str(e)
txt.print('\n\n' + output)
sampy.close()
finished_action()
def remove_file(filepath):
"""Remove file in device
Removes the given file in the selected device
Arguments:
filepath {str} -- file to remove
"""
sampy = start_sampy()
file = path.basename(filepath)
txt.print('\n\n>> rm {0}'.format(file))
try:
sampy.rm(filepath)
output = '[done]'
except RuntimeError as e:
output = str(e)
txt.print('\n\n' + output)
sampy.close()
finished_action()
def make_folder(folder_name):
"""Create folder
Makes a folder in the selected device
Arguments:
folder_name {str} -- folder name
"""
sampy = start_sampy()
txt.print('\n\n>> mkdir {0}'.format(folder_name))
try:
sampy.mkdir(folder_name)
output = '[done]'
except files.DirectoryExistsError as e:
output = str(e)
txt.print('\n\n' + output)
sampy.close()
finished_action()
def remove_folder(folder_name):
"""Remove folder from device
Removes the given folder in the selected device
Arguments:
folder_name {str} -- folder to remvoe
"""
sampy = start_sampy()
txt.print('\n\n>> rmdir {0}'.format(folder_name))
try:
sampy.rmdir(folder_name)
output = '[done]'
except RuntimeError as e:
output = str(e)
txt.print('\n\n' + output)
sampy.close()
finished_action()
def help():
"""Show commands help
Displays the sampy command usage
"""
start_sampy()
_help = """\n\nUsage: sampy COMMAND [ARGS]...
sampy - Sublime Text version of ampy MicroPython Tool
Sampy is the Sublime Text version of the ampy tool developed
by Adafruit. It's a tool to control MicroPython boards over a
serial connection.
Sampy will allow you to manipulate files on the board's internal
filesystem and even run scripts from the console or from the
Sublime Text interface.
Commands:
get\t\t\tRetrieve a file from the board.
ls\t\t\tList the contens on the board.
mkdir\t\tCreate a directory on the board.
put\t\t\tPut a file or folder and its contents on the board.
reset\t\tPerform soft reset/reboot of the board.
rm\t\t\tRemove a file from the board.
rmdir\t\tForcefully remove a folder and all its content from board
run\t\t\tRun a script and print it's output
""".replace(' ', '')
txt.print(_help)