Skip to content

Commit

Permalink
Refactoring and preparations for listing remote FS.
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaRavener committed Nov 2, 2017
1 parent 6be476d commit f49f4c6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
18 changes: 17 additions & 1 deletion src/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,23 @@ def _get_remote_file_name(local_file_path):
return local_file_path.rsplit("/", 1)[1]

def list_files(self):
raise NotImplementedError()
success = True
self._auto_reader_lock.acquire()
self._auto_read_enabled = False
self.send_kill()
self.read_junk()
self.send_line("import os;os.listdir()")
ret = ""
try:
ret = self.read_to_next_prompt()
except TimeoutError:
success = False
self._auto_read_enabled = True
self._auto_reader_lock.release()
if success and ret:
return re.findall("'([^']+)'", ret)
else:
raise OperationError()

def _write_file_job(self, remote_name, content, transfer):
raise NotImplementedError()
Expand Down
21 changes: 0 additions & 21 deletions src/connection/serial_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,6 @@ def read_junk(self):
def read_one_byte(self):
return self._serial.read(1)

def list_files(self):
success = True
self._auto_reader_lock.acquire()
self._auto_read_enabled = False
self.send_kill()
self.read_junk()
self.send_line("import os; os.listdir()")
self._serial.flush()
ret = ""
try:
ret = self.read_to_next_prompt()
except TimeoutError:
success = False
self._auto_read_enabled = True
self._auto_reader_lock.release()

if success and ret:
return re.findall("'([^']+)'", ret)
else:
raise OperationError()

@staticmethod
def escape_characters(text):
ret = ""
Expand Down
19 changes: 0 additions & 19 deletions src/connection/wifi_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,6 @@ def send_line(self, line_text, ending="\r\n"):
assert isinstance(ending, str)
self.ws.write(line_text + ending)

#TODO: Could be merged with serial connection?
def list_files(self):
success = True
self._auto_reader_lock.acquire()
self._auto_read_enabled = False
self.read_junk()
self.ws.write("import os;os.listdir()\r\n")
ret = ""
try:
ret = self.read_to_next_prompt()
except TimeoutError:
success = False
self._auto_read_enabled = True
self._auto_reader_lock.release()
if success and ret:
return re.findall("'([^']+)'", ret)
else:
raise OperationError()

@staticmethod
def read_resp(ws):
data = ws.read(4)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def serial_mcu_connection_valid(self):

def list_mcu_files(self):
try:
self.remoteFilesTreeView.model.refresh()
self.remoteFilesTreeView.model().refresh(self._connection)
except OperationError:
QMessageBox().critical(self, "Operation failed", "Could not list files.", QMessageBox.Ok)
return
Expand Down
28 changes: 25 additions & 3 deletions src/logic/remote_file_system_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,37 @@
from PyQt5.QtGui import QStandardItem
from PyQt5.QtGui import QStandardItemModel

from src.connection.connection import Connection


class RemoteFileSystemModel(QStandardItemModel):
class _Data:
def __init__(self, path):
self.path = path

def __init__(self, parent=None):
super().__init__(parent)
self.setHorizontalHeaderLabels(["Name"])
self._setup_model_data()

def _setup_model_data(self):
def isDir(self, index):
return self.data(index, Qt.UserRole).path.endswith("/")

# Communication model
# x/\n <- dir (ends with /)
# 0\n
# 1\n
# A/\n <- empty dir
# \n <- empty line means ../
# \n
# y/\n
# 0\n
# \n
# \n <- end of FS
def refresh(self, connection):
assert isinstance(connection, Connection)
output = connection.list_files()
#TODO: Parse output
r = QStandardItem("root")
r.setData("path/path/path", Qt.UserRole)
r.setData(self._Data("path/path/path", self._Data.FILE), Qt.UserRole)
self.appendRow(r)
r.appendRow(QStandardItem("child"))

0 comments on commit f49f4c6

Please sign in to comment.