Tue, 23 Jul 2019 19:43:14 +0200
MicroPython: continued implementing the file manager widget.
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
3 | # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing some file system commands for MicroPython. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | from __future__ import unicode_literals |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
12 | import ast |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
13 | import time |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
14 | import os |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
15 | import stat |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
16 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
17 | from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QThread |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
18 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
19 | from .MicroPythonSerialPort import MicroPythonSerialPort |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
20 | from .MicroPythonFileSystemUtilities import ( |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
21 | mtime2string, mode2string, decoratedName, listdirStat |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
22 | ) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | class MicroPythonFileSystem(QObject): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | Class implementing some file system commands for MicroPython. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
29 | Commands are provided to perform operations on the file system of a |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
30 | connected MicroPython device. Supported commands are: |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | <ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | <li>ls: directory listing</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | <li>lls: directory listing with meta data</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | <li>cd: change directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | <li>pwd: get the current directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | <li>put: copy a file to the connected device</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | <li>get: get a file from the connected device</li> |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
38 | <li>rm: remove a file from the connected device</li> |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
39 | <li>rmrf: remove a file/directory recursively (like 'rm -rf' in bash) |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
40 | <li>mkdir: create a new directory</li> |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
41 | <li>rmdir: remove an empty directory</li> |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
42 | </ul> |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
43 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
44 | There are additional commands related to time and version. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
45 | <ul> |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
46 | <li>version: get version info about MicroPython</li> |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
47 | <li>syncTime: synchronize the time of the connected device</li> |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
48 | <li>showTime: show the current time of the connected device</li> |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | </ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | def __init__(self, parent=None): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | Constructor |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | @param parent reference to the parent object |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | @type QObject |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | super(MicroPythonFileSystem, self).__init__(parent) |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
59 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
60 | self.__serial = None |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
62 | def setSerial(self, serial): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
63 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
64 | Public method to set the serial port to be used. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
65 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
66 | Note: The serial port should be initialized and open already. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
67 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
68 | @param serial open serial port |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
69 | @type MicroPythonSerialPort |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
70 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
71 | self.__serial = serial |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
72 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
73 | def __rawOn(self): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
74 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
75 | Private method to switch the connected device to 'raw' mode. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
76 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
77 | Note: switching to raw mode is done with synchroneous writes. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
78 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
79 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
80 | return |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
81 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
82 | rawReplMessage = b"raw REPL; CTRL-B to exit\r\n" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
83 | softRebootMessage = b"soft reboot\r\n" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
84 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
85 | self.__serial.write(b"\x02") # end raw mode if required |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
86 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
87 | for _i in range(3): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
88 | # CTRL-C three times to break out of loops |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
89 | self.__serial.write(b"\r\x03") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
90 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
91 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
92 | self.__serial.readAll() # read all data and discard it |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
93 | self.__serial.write(b"\r\x01") # send CTRL-A to enter raw mode |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
94 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
95 | self.__serial.write(b"\x04") # send CTRL-D to soft reset |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
96 | self.__serial.readUntil(softRebootMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
97 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
98 | # some MicroPython devices seem to need to be convinced in some |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
99 | # special way |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
100 | data = self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
101 | if not data.endswith(rawReplMessage): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
102 | self.__serial.write(b"\r\x01") # send CTRL-A again |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
103 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
104 | self.__serial.readAll() # read all data and discard it |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
105 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
106 | def __rawOff(self): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
107 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
108 | Private method to switch 'raw' mode off. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
109 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
110 | if self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
111 | self.__serial.write(b"\x02") # send CTRL-B to cancel raw mode |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
112 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
113 | def __execute(self, commands): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
114 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
115 | Private method to send commands to the connected device and return the |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
116 | result. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
117 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
118 | If no serial connection is available, empty results will be returned. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
119 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
120 | @param commands list of commands to be executed |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
121 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
122 | @return tuple containing stdout and stderr output of the device |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
123 | @rtype tuple of (bytes, bytes) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
124 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
125 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
126 | return b"", b"" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
127 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
128 | result = bytearray() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
129 | err = b"" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
130 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
131 | self.__rawOn() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
132 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
133 | for command in commands: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
134 | if command: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
135 | commandBytes = command.encode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
136 | self.__serial.write(commandBytes + b"\x04") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
137 | # read until prompt |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
138 | response = self.__serial.readUntil(b"\x04>") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
139 | # split stdout, stderr |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
140 | out, err = response[2:-2].split(b"\x04") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
141 | result += out |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
142 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
143 | return b"", err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
144 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
145 | self.__rawOff() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
146 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
147 | return bytes(result), err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
148 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
149 | def __shortError(self, error): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
150 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
151 | Private method to create a shortened error message. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
152 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
153 | @param error verbose error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
154 | @type bytes |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
155 | @return shortened error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
156 | @rtype str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
157 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
158 | if error: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
159 | decodedError = error.decode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
160 | try: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
161 | return decodedError.split["\r\n"][-2] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
162 | except Exception: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
163 | return decodedError |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
164 | return self.tr("Detected an error without indications.") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
165 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
166 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
167 | ## Methods below implement the file system commands |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
168 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
169 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
170 | def ls(self, dirname=""): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
171 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
172 | Public method to get a directory listing of the connected device. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
173 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
174 | @param dirname name of the directory to be listed |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
175 | @type str |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
176 | @return tuple containg the directory listing |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
177 | @rtype tuple of str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
178 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
179 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
180 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
181 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
182 | "print(os.listdir('{0}'))".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
183 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
184 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
185 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
186 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
187 | return ast.literal_eval(out.decode("utf-8")) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
188 | |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
189 | def lls(self, dirname="", fullstat=False): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
190 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
191 | Public method to get a long directory listing of the connected device |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
192 | including meta data. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
193 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
194 | @param dirname name of the directory to be listed |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
195 | @type str |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
196 | @param fullstat flag indicating to return the full stat() tuple |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
197 | @type bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
198 | @return list containing the directory listing with tuple entries of |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
199 | the name and and a tuple of mode, size and time (if fullstat is |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
200 | false) or the complete stat() tuple. 'None' is returned in case the |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
201 | directory doesn't exist. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
202 | @rtype tuple of (str, tuple) |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
203 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
204 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
205 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
206 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
207 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
208 | "def stat(filename):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
209 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
210 | " rstat = os.lstat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
211 | " except:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
212 | " rstat = os.stat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
213 | " return tuple(rstat)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
214 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
215 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
216 | "def listdir_stat(dirname):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
217 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
218 | " files = os.listdir(dirname)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
219 | " except OSError:", |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
220 | " return None", |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
221 | " if dirname in ('', '/'):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
222 | " return list((f, stat(f)) for f in files)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
223 | " return list((f, stat(dirname + '/' + f)) for f in files)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
224 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
225 | "print(listdir_stat('{0}'))".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
226 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
227 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
228 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
229 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
230 | fileslist = ast.literal_eval(out.decode("utf-8")) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
231 | if fileslist is None: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
232 | return None |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
233 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
234 | if fullstat: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
235 | return fileslist |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
236 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
237 | return [(f, (s[0], s[6], s[8])) for f, s in fileslist] |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
238 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
239 | def cd(self, dirname): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
240 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
241 | Public method to change the current directory on the connected device. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
242 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
243 | @param dirname directory to change to |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
244 | @type str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
245 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
246 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
247 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
248 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
249 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
250 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
251 | "os.chdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
252 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
253 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
254 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
255 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
256 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
257 | def pwd(self): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
258 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
259 | Public method to get the current directory of the connected device. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
260 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
261 | @return current directory |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
262 | @rtype str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
263 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
264 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
265 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
266 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
267 | "print(os.getcwd())", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
268 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
269 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
270 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
271 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
272 | return out.decode("utf-8").strip() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
273 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
274 | def rm(self, filename): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
275 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
276 | Public method to remove a file from the connected device. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
277 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
278 | @param filename name of the file to be removed |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
279 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
280 | @exception IOError raised to indicate an issue with the device |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
281 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
282 | assert filename |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
283 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
284 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
285 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
286 | "os.remove('{0}')".format(filename), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
287 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
288 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
289 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
290 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
291 | |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
292 | def rmrf(self, name, recursive=False, force=False): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
293 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
294 | Public method to remove a file or directory recursively. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
295 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
296 | @param name of the file or directory to remove |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
297 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
298 | @param recursive flag indicating a recursive deletion |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
299 | @type bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
300 | @param force flag indicating to ignore errors |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
301 | @type bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
302 | @return flag indicating success |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
303 | @rtype bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
304 | @exception IOError raised to indicate an issue with the device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
305 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
306 | assert name |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
307 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
308 | commands = [ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
309 | "import os" |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
310 | "\n".join([ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
311 | "def remove_file(name, recursive=False, force=False):", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
312 | " try:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
313 | " mode = os.stat(name)[0]", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
314 | " if mode & 0x4000 != 0:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
315 | " if recursive:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
316 | " for file in os.listdir(name):", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
317 | " success = remove_file(name + '/' + file," |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
318 | " recursive, force)", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
319 | " if not success and not force:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
320 | " return False", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
321 | " os.rmdir(name)", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
322 | " else:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
323 | " if not force:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
324 | " return False", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
325 | " else:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
326 | " os.remove(name)", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
327 | " except:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
328 | " if not force:", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
329 | " return False", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
330 | " return True", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
331 | ]), |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
332 | "print(remove_file('{0}', {1}, {2}))".format(name, recursive, |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
333 | force), |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
334 | ] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
335 | out, err = self.__execute(commands) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
336 | if err: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
337 | raise IOError(self.__shortError(err)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
338 | return ast.literal_eval(out.decode("utf-8")) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
339 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
340 | def mkdir(self, dirname): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
341 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
342 | Public method to create a new directory. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
343 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
344 | @param dirname name of the directory to create |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
345 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
346 | @exception IOError raised to indicate an issue with the device |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
347 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
348 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
349 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
350 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
351 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
352 | "os.mkdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
353 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
354 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
355 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
356 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
357 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
358 | def rmdir(self, dirname): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
359 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
360 | Public method to remove a directory. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
361 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
362 | @param dirname name of the directory to be removed |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
363 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
364 | @exception IOError raised to indicate an issue with the device |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
365 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
366 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
367 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
368 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
369 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
370 | "os.rmdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
371 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
372 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
373 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
374 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
375 | |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
376 | def put(self, hostFileName, deviceFileName=None): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
377 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
378 | Public method to copy a local file to the connected device. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
379 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
380 | @param hostFileName name of the file to be copied |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
381 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
382 | @param deviceFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
383 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
384 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
385 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
386 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
387 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
388 | if not os.path.isfile(hostFileName): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
389 | raise IOError("No such file: {0}".format(hostFileName)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
390 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
391 | with open(hostFileName, "rb") as hostFile: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
392 | content = hostFile.read() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
393 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
394 | if not deviceFileName: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
395 | deviceFileName = os.path.basename(hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
396 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
397 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
398 | "fd = open('{0}', 'wb')".format(deviceFileName), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
399 | "f = fd.write", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
400 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
401 | while content: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
402 | chunk = content[:64] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
403 | commands.append("f(" + repr(chunk) + ")") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
404 | content = content[64:] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
405 | commands.append("fd.close()") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
406 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
407 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
408 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
409 | raise IOError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
410 | return True |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
411 | |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
412 | def get(self, deviceFileName, hostFileName=None): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
413 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
414 | Public method to copy a file from the connected device. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
415 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
416 | @param deviceFileName name of the file to copy |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
417 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
418 | @param hostFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
419 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
420 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
421 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
422 | @exception IOError raised to indicate an issue with the device |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
423 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
424 | if not hostFileName: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
425 | hostFileName = deviceFileName |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
426 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
427 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
428 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
429 | "try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
430 | " from microbit import uart as u", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
431 | "except ImportError:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
432 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
433 | " from machine import UART", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
434 | " u = UART(0, {0})".format(115200), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
435 | " except Exception:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
436 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
437 | " from sys import stdout as u", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
438 | " except Exception:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
439 | " raise Exception('Could not find UART module in" |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
440 | " device.')", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
441 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
442 | "f = open('{0}', 'rb')".format(deviceFileName), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
443 | "r = f.read", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
444 | "result = True", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
445 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
446 | "while result:", |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
447 | " result = r(32)", |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
448 | " if result:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
449 | " u.write(result)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
450 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
451 | "f.close()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
452 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
453 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
454 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
455 | raise IOError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
456 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
457 | # write the received bytes to the local file |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
458 | with open(hostFileName, "wb") as hostFile: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
459 | hostFile.write(out) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
460 | return True |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
461 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
462 | def version(self): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
463 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
464 | Public method to get the MicroPython version information of the |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
465 | connected device. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
466 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
467 | @return dictionary containing the version information |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
468 | @rtype dict |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
469 | @exception ValueError raised to indicate that the device might not be |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
470 | running MicroPython or there was an issue parsing the output |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
471 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
472 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
473 | "import os", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
474 | "print(os.uname())", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
475 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
476 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
477 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
478 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
479 | raise ValueError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
480 | except ValueError: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
481 | # just re-raise it |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
482 | raise |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
483 | except Exception: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
484 | # Raise a value error to indicate being unable to find something |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
485 | # on the device that will return parseable information about the |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
486 | # version. It doesn't matter what the error is, it just needs to |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
487 | # report a failure with the expected ValueError exception. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
488 | raise ValueError("Unable to determine version information.") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
489 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
490 | rawOutput = out.decode("utf-8").strip() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
491 | rawOutput = rawOutput[1:-1] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
492 | items = rawOutput.split(",") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
493 | result = {} |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
494 | for item in items: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
495 | key, value = item.strip().split("=") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
496 | result[key.strip()] = value.strip()[1:-1] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
497 | return result |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
498 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
499 | def syncTime(self): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
500 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
501 | Public method to set the time of the connected device to the local |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
502 | computer's time. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
503 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
504 | @exception IOError raised to indicate an issue with the device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
505 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
506 | now = time.localtime(time.time()) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
507 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
508 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
509 | "def set_time(rtc_time):", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
510 | " rtc = None", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
511 | " try:", # Pyboard (it doesn't have machine.RTC()) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
512 | " import pyb", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
513 | " rtc = pyb.RTC()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
514 | " rtc.datetime(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
515 | " except:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
516 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
517 | " import machine", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
518 | " rtc = machine.RTC()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
519 | " try:", # ESP8266 uses rtc.datetime() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
520 | " rtc.datetime(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
521 | " except:", # ESP32 uses rtc.init() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
522 | " rtc.init(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
523 | " except:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
524 | " pass", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
525 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
526 | "set_time({0})".format((now.tm_year, now.tm_mon, now.tm_mday, |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
527 | now.tm_wday + 1, now.tm_hour, now.tm_min, |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
528 | now.tm_sec, 0)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
529 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
530 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
531 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
532 | raise IOError(self.__shortError(err)) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
533 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
534 | def showTime(self): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
535 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
536 | Public method to get the current time of the device. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
537 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
538 | @return time of the device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
539 | @rtype str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
540 | @exception IOError raised to indicate an issue with the device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
541 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
542 | commands = [ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
543 | "import time", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
544 | "print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))", |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
545 | # __IGNORE_WARNING_M601__ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
546 | ] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
547 | out, err = self.__execute(commands) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
548 | if err: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
549 | raise IOError(self.__shortError(err)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
550 | return out.decode("utf-8").strip() |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
551 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
552 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
553 | class MicroPythonFileManager(QObject): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
554 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
555 | Class implementing an interface to the device file system commands with |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
556 | some additional sugar. |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
557 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
558 | @signal longListFiles(result) emitted with a tuple of tuples containing the |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
559 | name, mode, size and time for each directory entry |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
560 | @signal currentDir(dirname) emitted to report the current directory of the |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
561 | device |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
562 | @signal currentDirChanged(dirname) emitted to report back a change of the |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
563 | current directory |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
564 | @signal getFileDone(deviceFile, localFile) emitted after the file was |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
565 | fetched from the connected device and written to the local file system |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
566 | @signal putFileDone(localFile, deviceFile) emitted after the file was |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
567 | copied to the connected device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
568 | @signal deleteFileDone(deviceFile) emitted after the file has been deleted |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
569 | on the connected device |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
570 | @signal rsyncDone(localName, deviceName) emitted after the rsync operation |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
571 | has been completed |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
572 | @signal rsyncMessages(list) emitted with a list of messages |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
573 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
574 | @signal longListFilesFailed(exc) emitted with a failure message to indicate |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
575 | a failed long listing operation |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
576 | @signal currentDirFailed(exc) emitted with a failure message to indicate |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
577 | that the current directory is not available |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
578 | @signal currentDirChangeFailed(exc) emitted with a failure message to |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
579 | indicate that the current directory could not be changed |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
580 | @signal getFileFailed(exc) emitted with a failure message to indicate that |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
581 | the file could not be fetched |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
582 | @signal putFileFailed(exc) emitted with a failure message to indicate that |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
583 | the file could not be copied |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
584 | @signal deleteFileFailed(exc) emitted with a failure message to indicate |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
585 | that the file could not be deleted on the device |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
586 | @signal rsyncFailed(exc) emitted with a failure message to indicate that |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
587 | the rsync operation could not be completed |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
588 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
589 | longListFiles = pyqtSignal(tuple) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
590 | currentDir = pyqtSignal(str) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
591 | currentDirChanged = pyqtSignal(str) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
592 | getFileDone = pyqtSignal(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
593 | putFileDone = pyqtSignal(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
594 | deleteFileDone = pyqtSignal(str) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
595 | rsyncDone = pyqtSignal(str, str) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
596 | rsyncMessages = pyqtSignal(list) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
597 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
598 | longListFilesFailed = pyqtSignal(str) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
599 | currentDirFailed = pyqtSignal(str) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
600 | currentDirChangeFailed = pyqtSignal(str) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
601 | getFileFailed = pyqtSignal(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
602 | putFileFailed = pyqtSignal(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
603 | deleteFileFailed = pyqtSignal(str) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
604 | rsyncFailed = pyqtSignal(str) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
605 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
606 | def __init__(self, port, parent=None): |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
607 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
608 | Constructor |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
609 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
610 | @param port port name of the device |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
611 | @type str |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
612 | @param parent reference to the parent object |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
613 | @type QObject |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
614 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
615 | super(MicroPythonFileManager, self).__init__(parent) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
616 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
617 | self.__serialPort = port |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
618 | self.__serial = MicroPythonSerialPort(parent=self) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
619 | self.__fs = MicroPythonFileSystem(parent=self) |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
620 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
621 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
622 | def connect(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
623 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
624 | Public slot to start the manager. |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
625 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
626 | self.__serial.openSerialLink(self.__serialPort) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
627 | self.__fs.setSerial(self.__serial) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
628 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
629 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
630 | def disconnect(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
631 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
632 | Public slot to stop the thread. |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
633 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
634 | self.__serial.closeSerialLink() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
635 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
636 | @pyqtSlot(str) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
637 | def lls(self, dirname): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
638 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
639 | Public slot to get a long listing of the given directory. |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
640 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
641 | @param dirname name of the directory to list |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
642 | @type str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
643 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
644 | try: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
645 | filesList = self.__fs.lls(dirname) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
646 | result = [(decoratedName(name, mode), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
647 | mode2string(mode), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
648 | str(size), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
649 | mtime2string(time)) for |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
650 | name, (mode, size, time) in filesList] |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
651 | self.longListFiles.emit(tuple(result)) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
652 | except Exception as exc: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
653 | self.longListFilesFailed.emit(str(exc)) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
654 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
655 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
656 | def pwd(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
657 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
658 | Public slot to get the current directory of the device. |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
659 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
660 | try: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
661 | pwd = self.__fs.pwd() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
662 | self.currentDir.emit(pwd) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
663 | except Exception as exc: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
664 | self.currentDirFailed.emit(str(exc)) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
665 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
666 | @pyqtSlot(str) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
667 | def cd(self, dirname): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
668 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
669 | Public slot to change the current directory of the device. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
670 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
671 | @param dirname name of the desired current directory |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
672 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
673 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
674 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
675 | self.__fs.cd(dirname) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
676 | self.currentDirChanged.emit(dirname) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
677 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
678 | self.currentDirChangeFailed.emit(str(exc)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
679 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
680 | @pyqtSlot(str) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
681 | @pyqtSlot(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
682 | def get(self, deviceFileName, hostFileName=""): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
683 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
684 | Public slot to get a file from the connected device. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
685 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
686 | @param deviceFileName name of the file on the device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
687 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
688 | @param hostFileName name of the local file |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
689 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
690 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
691 | if hostFileName and os.path.isdir(hostFileName): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
692 | # only a local directory was given |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
693 | hostFileName = os.path.join(hostFileName, |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
694 | os.path.basename(deviceFileName)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
695 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
696 | self.__fs.get(deviceFileName, hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
697 | self.getFileDone.emit(deviceFileName, hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
698 | except Exception as exc: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
699 | self.getFileFailed.emit(str(exc)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
700 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
701 | @pyqtSlot(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
702 | @pyqtSlot(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
703 | def put(self, hostFileName, deviceFileName=""): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
704 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
705 | Public slot to put a file onto the device. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
706 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
707 | @param hostFileName name of the local file |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
708 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
709 | @param deviceFileName name of the file on the connected device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
710 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
711 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
712 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
713 | self.__fs.put(hostFileName, deviceFileName) |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
714 | self.putFileDone.emit(hostFileName, deviceFileName) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
715 | except Exception as exc: |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
716 | self.putFileFailed.emit(str(exc)) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
717 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
718 | @pyqtSlot(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
719 | def delete(self, deviceFileName): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
720 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
721 | Public slot to delete a file on the device. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
722 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
723 | @param deviceFileName name of the file on the connected device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
724 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
725 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
726 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
727 | self.__fs.rm(deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
728 | self.deleteFileDone.emit(deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
729 | except Exception as exc: |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
730 | self.deleteFileFailed.emit(str(exc)) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
731 | |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
732 | def __rsync(self, hostDirectory, deviceDirectory, mirror=True): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
733 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
734 | Private method to synchronize a local directory to the device. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
735 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
736 | @param hostDirectory name of the local directory |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
737 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
738 | @param deviceDirectory name of the directory on the device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
739 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
740 | @param mirror flag indicating to mirror the local directory to |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
741 | the device directory |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
742 | @type bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
743 | @return tuple containing a list of messages and list of errors |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
744 | @rtype tuple of (list of str, list of str) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
745 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
746 | messages = [] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
747 | errors = [] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
748 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
749 | if not os.isdir(hostDirectory): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
750 | return ([], [self.tr( |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
751 | "The given name '{0}' is not a directory or does not exist.") |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
752 | .format(hostDirectory) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
753 | ]) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
754 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
755 | sourceDict = {} |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
756 | sourceFiles = listdirStat(hostDirectory) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
757 | for name, nstat in sourceFiles: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
758 | sourceDict[name] = nstat |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
759 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
760 | destinationDict = {} |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
761 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
762 | destinationFiles = self.__fs.lls(deviceDirectory, fullstat=True) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
763 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
764 | return ([], [str(exc)]) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
765 | if destinationFiles is None: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
766 | # the destination directory does not exist |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
767 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
768 | self.__fs.mkdir(deviceDirectory) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
769 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
770 | return ([], [str(exc)]) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
771 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
772 | for name, nstat in destinationFiles: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
773 | destinationDict[name] = nstat |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
774 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
775 | destinationSet = set(destinationDict.keys()) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
776 | sourceSet = set(sourceDict.keys()) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
777 | toAdd = sourceSet - destinationSet # add to dev |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
778 | toDelete = destinationSet - sourceSet # delete from dev |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
779 | toUpdate = destinationSet.intersection(sourceSet) # update files |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
780 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
781 | for sourceBasename in toAdd: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
782 | # name exists in source but not in device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
783 | sourceFilename = os.path.join(hostDirectory, sourceBasename) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
784 | destFilename = deviceDirectory + "/" + sourceBasename |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
785 | if os.path.isfile(sourceFilename): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
786 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
787 | self.__fs.put(sourceFilename, destFilename) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
788 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
789 | messages.append(str(exc)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
790 | if os.path.isdir(sourceFilename): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
791 | # recurse |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
792 | msg, err = self.__rsync(sourceFilename, destFilename, |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
793 | mirror=mirror) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
794 | messages.extend(msg) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
795 | errors.extend(err) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
796 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
797 | if mirror: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
798 | for destBasename in toDelete: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
799 | # name exists in device but not local, delete |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
800 | destFilename = deviceDirectory + "/" + destBasename |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
801 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
802 | self.__fs.rmrf(destFilename, recursive=True, force=True) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
803 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
804 | # ignore errors here |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
805 | messages.append(str(exc)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
806 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
807 | for sourceBasename in toUpdate: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
808 | # names exist in both; do an update |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
809 | sourceStat = sourceDict[sourceBasename] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
810 | destStat = destinationDict[sourceBasename] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
811 | sourceFilename = os.path.join(hostDirectory, sourceBasename) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
812 | destFilename = deviceDirectory + "/" + sourceBasename |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
813 | destMode = destStat[0] |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
814 | if os.path.isdir(sourceFilename): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
815 | if stat.S_ISDIR(destMode): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
816 | # both are directories => recurse |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
817 | msg, err = self.__rsync(sourceFilename, destFilename, |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
818 | mirror=mirror) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
819 | messages.extend(msg) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
820 | errors.extend(err) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
821 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
822 | messages.append(self.tr( |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
823 | "Source '{0}' is a directory and destination '{1}'" |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
824 | " is a file. Ignoring it." |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
825 | ).format(sourceFilename, destFilename)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
826 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
827 | if stat.S_ISDIR(destMode): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
828 | messages.append(self.tr( |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
829 | "Source '{0}' is a file and destination '{1}' is" |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
830 | " a directory. Ignoring it." |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
831 | ).format(sourceFilename, destFilename)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
832 | else: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
833 | if sourceStat[8] > destStat[8]: # mtime |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
834 | messages.append(self.tr( |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
835 | "'{0}' is newer than '{1}' - copying" |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
836 | ).format(sourceFilename, destFilename)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
837 | try: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
838 | self.__fs.put(sourceFilename, destFilename) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
839 | except Exception as exc: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
840 | messages.append(str(exc)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
841 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
842 | return messages, errors |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
843 | |
7081
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
844 | def rsync(self, hostDirectory, deviceDirectory, mirror=True): |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
845 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
846 | Public method to synchronize a local directory to the device. |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
847 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
848 | @param hostDirectory name of the local directory |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
849 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
850 | @param deviceDirectory name of the directory on the device |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
851 | @type str |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
852 | @param mirror flag indicating to mirror the local directory to |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
853 | the device directory |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
854 | @type bool |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
855 | """ |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
856 | messages, errors = self.__rsync(hostDirectory, deviceDirectory, |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
857 | mirror=mirror) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
858 | if errors: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
859 | self.rsyncFailed.emit("\n".join(errors)) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
860 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
861 | if messages: |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
862 | self.rsyncMessages.emit(messages) |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
863 | |
ed510767c096
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7080
diff
changeset
|
864 | self.rsyncDone.emit(hostDirectory, deviceDirectory) |