Skip to content

Commit

Permalink
Implements #58.
Browse files Browse the repository at this point in the history
All local files can be now opened.
If they appear to be binary and built-in
editor is used, info dialog is displayed.
If it is remote file, it gets transfered
and same decision is made as for local
files.
  • Loading branch information
BetaRavener committed Aug 28, 2018
1 parent a5241f7 commit 2c004ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from src.helpers.ip_helper import IpHelper
from src.logic.file_transfer import FileTransfer
from src.utility.exceptions import PasswordException, NewPasswordException, OperationError, HostnameResolutionError
from src.utility.file_info import FileInfo
from src.utility.settings import Settings


Expand Down Expand Up @@ -221,12 +222,10 @@ def update_file_tree(self):
self.localFilesTreeView.setRootIndex(model.index(self._root_dir))

def serial_mcu_connection_valid(self):
file_list = []
try:
file_list = self._connection.list_files()
self._connection.list_files()
return True
except OperationError:
file_list = None
return False

def list_mcu_files(self):
Expand Down Expand Up @@ -373,17 +372,17 @@ def open_local_file(self, idx):

local_path = model.filePath(idx)
remote_path = local_path.rsplit("/", 1)[1]
if local_path.endswith(".py"):
if Settings().external_editor_path:
self.open_external_editor(local_path)
else:
with open(local_path) as f:
text = "".join(f.readlines())
self.open_code_editor()
self._code_editor.set_code(local_path, remote_path, text)

if Settings().external_editor_path:
self.open_external_editor(local_path)
else:
QMessageBox.information(self, "Unknown file", "Files without .py ending won't open"
" in editor, but can still be transferred.")
if FileInfo.is_file_binary(local_path):
QMessageBox.information(self, "Binary file detected", "Editor doesn't support binary files.")
return
with open(local_path) as f:
text = "".join(f.readlines())
self.open_code_editor()
self._code_editor.set_code(local_path, remote_path, text)

def mcu_file_selection_changed(self):
idx = self.mcuFilesListView.currentIndex()
Expand Down Expand Up @@ -487,8 +486,17 @@ def compile_files(self):
def finished_read_mcu_file(self, file_name, transfer):
assert isinstance(transfer, FileTransfer)
result = transfer.read_result
text = result.binary_data.decode("utf-8",
errors="replace") if result.binary_data is not None else "!Failed to read file!"

if result.binary_data:
try:
text = result.binary_data.decode("utf-8", "strict")
except UnicodeDecodeError:
QMessageBox.information(self, "Binary file detected", "Editor doesn't support binary files, "
"but these can still be transferred.")
return
else:
text = "! Failed to read file !"

self.open_code_editor()
self._code_editor.set_code(None, file_name, text)

Expand All @@ -497,10 +505,6 @@ def read_mcu_file(self, idx):
model = self.mcuFilesListView.model()
assert isinstance(model, QStringListModel)
file_name = model.data(idx, Qt.EditRole)
if not file_name.endswith(".py"):
QMessageBox.information(self, "Unknown file", "Files without .py ending won't open"
" in editor, but can still be transferred.")
return

progress_dlg = FileTransferDialog(FileTransferDialog.DOWNLOAD)
progress_dlg.finished.connect(lambda: self.finished_read_mcu_file(file_name, progress_dlg.transfer))
Expand Down
14 changes: 14 additions & 0 deletions src/utility/file_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class FileInfo:
@staticmethod
def is_file_binary(path: str) -> bool:
"""
Tests if file is binary.
:param path: Path to local file.
:return: True if file is binary (can't be decoded with utf-8), false otherwise
"""
with open(path, "rb") as f:
try:
f.read().decode("utf-8")
return False
except UnicodeDecodeError:
return True

0 comments on commit 2c004ea

Please sign in to comment.