Skip to content

Commit

Permalink
处理传入文件的编码问题,wb_ampy采用CB2312编码,如果选中文件是utf-8编码会转为GB2312编码在传入,暂时对其他编码格式未…
Browse files Browse the repository at this point in the history
…作处理。
  • Loading branch information
BigCircleLaw committed Nov 13, 2019
1 parent 7327e22 commit 013720d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ ENV/

# Rope project settings
.ropeproject

try/
29 changes: 22 additions & 7 deletions ampy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def windows_full_port_name(portname):
help="Delay in seconds before entering RAW MODE (default 0). Can optionally specify with AMPY_DELAY environment variable.",
metavar="DELAY",
)
@click.version_option('1.1.0')
@click.version_option('1.1.1')
def cli(port, baud, delay):
"""ampy - Adafruit MicroPython Tool
Expand Down Expand Up @@ -248,19 +248,34 @@ def put(local, remote):
board_files.mkdir(remote_parent)
# Loop through all the files and put them on the board too.
for filename in child_files:
with open(os.path.join(parent, filename), "rb") as infile:
remote_filename = posixpath.join(remote_parent, filename)
board_files.put(remote_filename, infile.read())
try:
with open(os.path.join(parent, filename), "r", encoding = 'utf-8') as infile:
out_content = infile.read().encode('GB2312')
except:
with open(os.path.join(parent, filename), "rb") as infile:
out_content = infile.read()
# with open(os.path.join(parent, filename), "r") as infile:
remote_filename = posixpath.join(remote_parent, filename)
board_files.put(remote_filename, out_content)
except files.DirectoryExistsError:
# Ignore errors for directories that already exist.
pass

else:
# File copy, open the file and copy its contents to the board.
# Put the file on the board.
with open(local, "rb") as infile:
board_files = files.Files(_board)
board_files.put(remote, infile.read())
try:
with open(local, "r", encoding = 'utf-8') as infile:
out_content = infile.read().encode('GB2312')
# print('open(r)')
except:
with open(local, "rb") as infile:
out_content = infile.read()
# print('open(rb)')
# with open(local, "r") as infile:
board_files = files.Files(_board)
board_files.put(remote, out_content)
# print(out_content)


@cli.command()
Expand Down
9 changes: 7 additions & 2 deletions ampy/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ def run(self, filename, wait_output=True):
else:
# Read the file and run it using lower level pyboard functions that
# won't wait for it to finish or return output.
with open(filename, "rb") as infile:
self._pyboard.exec_raw_no_follow(infile.read())
try:
with open(filename, "r", encoding = 'utf-8') as infile:
out_content = infile.read().encode('GB2312')
except:
with open(filename, "rb") as infile:
out_content = infile.read()
self._pyboard.exec_raw_no_follow(out_content)
self._pyboard.exit_raw_repl()
return out

Expand Down

0 comments on commit 013720d

Please sign in to comment.