18
18
"""
19
19
20
20
import argparse
21
- import os , sys
21
+ import os
22
+ import sys
22
23
from collections .abc import Mapping
23
24
from textwrap import dedent
24
25
25
- from .commands import (
26
- CommandError ,
27
- do_connect ,
28
- do_disconnect ,
29
- do_edit ,
30
- do_filesystem ,
31
- do_mount ,
32
- do_umount ,
33
- do_exec ,
34
- do_eval ,
35
- do_run ,
36
- do_resume ,
37
- do_soft_reset ,
38
- )
26
+ from .commands import (CommandError , do_connect , do_disconnect , do_edit ,
27
+ do_eval , do_exec , do_filesystem , do_mount , do_resume ,
28
+ do_run , do_soft_reset , do_umount )
39
29
from .mip import do_mip
40
30
from .repl import do_repl
41
31
@@ -49,8 +39,12 @@ def print_commands_help(cmds, help_key):
49
39
help_message_lines = dedent (help_key (cmds [cmd ])).split ("\n " )
50
40
help_message = help_message_lines [0 ]
51
41
for line in help_message_lines [1 :]:
52
- help_message = "{}\n {}{}" .format (help_message , " " * (max_command_len + 4 ), line )
53
- print (" " , cmd , " " * (max_command_len - len (cmd ) + 2 ), help_message , sep = "" )
42
+ help_message = "{}\n {}{}" .format (
43
+ help_message , " " * (max_command_len + 4 ), line
44
+ )
45
+ print (
46
+ " " , cmd , " " * (max_command_len - len (cmd ) + 2 ), help_message , sep = ""
47
+ )
54
48
55
49
print (_PROG , "-- MicroPython remote control" )
56
50
print ("See https://docs.micropython.org/en/latest/reference/mpremote.html" )
@@ -61,7 +55,9 @@ def print_commands_help(cmds, help_key):
61
55
) # extract description from argparse
62
56
63
57
print ("\n List of shortcuts:" )
64
- print_commands_help (_command_expansions , lambda x : x [2 ]) # (args, sub, help_message)
58
+ print_commands_help (
59
+ _command_expansions , lambda x : x [2 ]
60
+ ) # (args, sub, help_message)
65
61
66
62
sys .exit (0 )
67
63
@@ -93,7 +89,9 @@ def _bool_flag(cmd_parser, name, short_name, default, description):
93
89
def argparse_connect ():
94
90
cmd_parser = argparse .ArgumentParser (description = "connect to given device" )
95
91
cmd_parser .add_argument (
96
- "device" , nargs = 1 , help = "Either list, auto, id:x, port:x, or any valid device name/path"
92
+ "device" ,
93
+ nargs = 1 ,
94
+ help = "Either list, auto, id:x, port:x, or any valid device name/path" ,
97
95
)
98
96
return cmd_parser
99
97
@@ -126,7 +124,10 @@ def argparse_repl():
126
124
help = "saves a copy of the REPL session to the specified path" ,
127
125
)
128
126
cmd_parser .add_argument (
129
- "--inject-code" , type = str , required = False , help = "code to be run when Ctrl-J is pressed"
127
+ "--inject-code" ,
128
+ type = str ,
129
+ required = False ,
130
+ help = "code to be run when Ctrl-J is pressed" ,
130
131
)
131
132
cmd_parser .add_argument (
132
133
"--inject-file" ,
@@ -146,7 +147,11 @@ def argparse_eval():
146
147
def argparse_exec ():
147
67E6
td>148
cmd_parser = argparse .ArgumentParser (description = "execute the string" )
148
149
_bool_flag (
149
- cmd_parser , "follow" , "f" , True , "follow output until the expression completes (default)"
150
+ cmd_parser ,
151
+ "follow" ,
152
+ "f" ,
153
+ True ,
154
+ "follow output until the expression completes (default)" ,
150
155
)
151
156
cmd_parser .add_argument ("expr" , nargs = 1 , help = "expression to execute" )
152
157
return cmd_parser
@@ -155,15 +160,23 @@ def argparse_exec():
155
160
def argparse_run ():
156
161
cmd_parser = argparse .ArgumentParser (description = "run the given local script" )
157
162
_bool_flag (
158
- cmd_parser , "follow" , "f" , True , "follow output until the script completes (default)"
163
+ cmd_parser ,
164
+ "follow" ,
165
+ "f" ,
166
+ True ,
167
+ "follow output until the script completes (default)" ,
159
168
)
160
169
cmd_parser .add_argument ("path" , nargs = 1 , help = "path to script to execute" )
161
170
return cmd_parser
162
171
163
172
164
173
def argparse_filesystem ():
165
- cmd_parser = argparse .ArgumentParser (description = "execute filesystem commands on the device" )
166
- _bool_flag (cmd_parser , "recursive" , "r" , False , "recursive copy (for cp command only)" )
174
+ cmd_parser = argparse .ArgumentParser (
175
+ description = "execute filesystem commands on the device"
176
+ )
177
+ _bool_flag (
178
+ cmd_parser , "recursive" , "r" , False , "recursive copy (for cp command only)"
179
+ )
167
180
_bool_flag (
168
181
cmd_parser ,
169
182
"verbose" ,
@@ -182,7 +195,9 @@ def argparse_mip():
182
195
cmd_parser = argparse .ArgumentParser (
183
196
description = "install packages from micropython-lib or third-party sources"
184
197
)
185
- _bool_flag (cmd_parser , "mpy" , "m" , True , "download as compiled .mpy files (default)" )
198
+ _bool_flag (
199
+ cmd_parser , "mpy" , "m" , True , "download as compiled .mpy files (default)"
200
+ )
186
201
cmd_parser .add_argument (
187
202
"--target" , type = str , required = False , help ="destination direction on the device"
188
203
)
@@ -333,13 +348,15 @@ def load_user_config():
333
348
config .commands = {}
334
349
335
350
# Get config file name.
336
- path = os .getenv ("XDG_CONFIG_HOME" )
351
+ path = None
352
+ for var in ["XDG_CONFIG_HOME" , "HOME" , "USERPROFILE" ]:
353
+ path = os .getenv (var )
354
+ if path :
355
+ path = os .path .join (path , ".config" )
356
+ path = os .path .join (path , _PROG )
357
+ break
337
358
if path is None :
338
- path = os .getenv ("HOME" )
339
- if path is None :
340
- return config
341
- path = os .path .join (path , ".config" )
342
- path = os .path .join (path , _PROG )
359
+ return config
343
360
config_file = os .path .join (path , "config.py" )
344
361
345
362
# Check if config file exists.
0 commit comments