forked from gepd/uPiotMicroPythonTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
248 lines (189 loc) · 6.48 KB
/
__init__.py
File metadata and controls
248 lines (189 loc) · 6.48 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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# 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
import requests
from sublime_plugin import WindowCommand
from package_control import events
from ..tools.paths import plugin_name
from os import path, makedirs
from ..tools.boards import boards_list
from ..tools.quick_panel import quick_panel
VERSION = (0, 1, 6, '-alpha')
ACTIVE_VIEW = None
SETTINGS_NAME = 'upiot.sublime-settings'
DOWNLOAD = 1
BURN = 2
def versionize(raw_version):
"""Semantic Versioning
Convert the given version in the semantic versioning format
Arguments:
raw_version {tuple} -- plugin version in a tuple
Returns:
[str] -- Semantic Versioning string
"""
version = ".".join([str(s) for s in raw_version[:3]])
if(len(raw_version) > 3):
version += raw_version[3]
return version
__all__ = ["boards_list",
"run_command",
"quick_panel"]
def message_upgrade():
package_name = plugin_name()
if(events.post_upgrade(package_name)):
message_dialog("uPiot Updated, please restart Sublime Text")
def get_headers():
"""
headers for urllib request
"""
user_agent = 'uPIOT/{0} (Sublime-Text/{1})'.format(__version__,
sublime.version())
headers = {'User-Agent': user_agent}
return headers
def download_file(file_url, dst_path, callback=None):
"""download file
Download and save a file from a given url
Arguments:
file_url {str} -- url with the file to download
dst_path {str} -- where file will be stored (including file name)
callback {obj} -- callback to show the progress of the download
Returns:
bool -- true if the file was successfully downloaded
"""
downloaded = 0
progress_qty = 5 # numbers of symbols to show when it downloading (total)
headers = get_headers()
with open(dst_path, 'wb') as file:
try:
req = requests.get(file_url, stream=True, headers=headers)
except:
return False
total_length = req.headers.get('content-length')
# File status
if(req.status_code != 200):
return False
if total_length is None:
file.write(req.content)
else:
for chunk in req.iter_content(1024):
downloaded += len(chunk)
file.write(chunk)
done = int(progress_qty * downloaded / int(total_length))
percent = int(100 * downloaded) / int(total_length)
if(callback):
current_prog = '■' * done
new_prog = ' ' * (progress_qty - done)
callback("Downloading Firmware {0:.0f}% [{1}{2}]".format(
percent, current_prog, new_prog))
return True
def erase_flash():
"""Erase flash memory
Erase the flash memory from the current selected device
"""
from ..tools.command import Command
from ..tools import serial
port = serial.selected_port(request_port=True)
if(not port):
return
options = ['--port', port, 'erase_flash']
Command().run(options, port=port)
def make_folder(folder_path):
"""make foler
Make a folder in the given path
Arguments:
folder_path {str} -- folder to make
Raises:
exc -- [description]
"""
if(not path.exists(folder_path)):
import errno
try:
makedirs(folder_path)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise exc
pass
def find_view(view_name):
"""
Search a specific view in the list of available views
Arguments:
view_name {string}
Name of the view to search
"""
opened_view = None
found = False
fwindows = sublime.windows()
for window in fwindows:
views = window.views()
for view in views:
name = view.name()
if(view_name in name):
opened_view = view
found = True
break
if found:
break
return (window, opened_view)
def check_sidebar_folder(folder):
"""check folder in sidebar
Checks if the given folder already is in the current project
Arguments:
folder {str} -- folder to search
Returns:
bool -- true if already is false if not
"""
data = sublime.active_window().project_data()
if(not data):
return False
else:
paths = [path['path'] for path in data["folders"]]
if(folder in paths):
return True
return False
def set_status(text):
"""Set the status bar
Sets a message/text in the status bar
Arguments:
text {str} -- text to add
"""
if(ACTIVE_VIEW):
ACTIVE_VIEW.set_status('_upiot_', text)
def clean_status():
"""Remove status bar text
Removes the status bar text related to the plugin
"""
if(ACTIVE_VIEW):
ACTIVE_VIEW.erase_status('_upiot_')
def str_cmd_serial(cmd):
"""String command to serial
Convert a string in a serial command, it means handles scape character and
convert it to by as is required by the serial.
Arguments:
cmd {str} -- serial command string
"""
data = cmd.replace('\\x03', '\x03')
return data.encode('utf-8', 'replace')
__version__ = versionize(VERSION)