Mon, 22 Jul 2019 20:17:33 +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 |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
14 | import stat |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
15 | import os |
7070
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 |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | class MicroPythonFileSystem(QObject): |
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 | Class implementing some file system commands for MicroPython. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | Some FTP like commands are provided to perform operations on the file |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | system of a connected MicroPython device. Supported commands are: |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | <ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | <li>ls: directory listing</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | <li>lls: directory listing with meta data</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | <li>cd: change directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | <li>pwd: get the current directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | <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
|
34 | <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
|
35 | <li>rm: remove a file from the connected device</li> |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
36 | <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
|
37 | <li>rmdir: remove an empty directory</li> |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
38 | <li>version: get version info about MicroPython</li> |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | </ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | def __init__(self, parent=None): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | Constructor |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | @param parent reference to the parent object |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | @type QObject |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | 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
|
49 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
50 | self.__serial = None |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
52 | def setSerial(self, serial): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
53 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
54 | 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
|
55 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
56 | 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
|
57 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
58 | @param serial open serial port |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
59 | @type MicroPythonSerialPort |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
60 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
61 | self.__serial = serial |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
62 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
63 | def __rawOn(self): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
64 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
65 | 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
|
66 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
67 | 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
|
68 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
69 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
70 | return |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
71 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
72 | 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
|
73 | 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
|
74 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
75 | 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
|
76 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
77 | for _i in range(3): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
78 | # 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
|
79 | 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
|
80 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
81 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
82 | 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
|
83 | 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
|
84 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
85 | 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
|
86 | self.__serial.readUntil(softRebootMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
87 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
88 | # 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
|
89 | # special way |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
90 | data = self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
91 | if not data.endswith(rawReplMessage): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
92 | 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
|
93 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
94 | 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
|
95 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
96 | def __rawOff(self): |
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 | 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
|
99 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
100 | if self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
101 | 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
|
102 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
103 | def __execute(self, commands): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
104 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
105 | 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
|
106 | result. |
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 | 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
|
109 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
110 | @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
|
111 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
112 | @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
|
113 | @rtype tuple of (bytes, bytes) |
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 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
116 | return b"", b"" |
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 | result = bytearray() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
119 | err = b"" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
120 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
121 | self.__rawOn() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
122 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
123 | for command in commands: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
124 | if command: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
125 | commandBytes = command.encode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
126 | 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
|
127 | # read until prompt |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
128 | 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
|
129 | # split stdout, stderr |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
130 | 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
|
131 | result += out |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
132 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
133 | return b"", err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
134 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
135 | self.__rawOff() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
136 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
137 | return bytes(result), err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
138 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
139 | def __shortError(self, error): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
140 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
141 | 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
|
142 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
143 | @param error verbose error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
144 | @type bytes |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
145 | @return shortened error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
146 | @rtype str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
147 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
148 | if error: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
149 | decodedError = error.decode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
150 | try: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
151 | 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
|
152 | except Exception: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
153 | return decodedError |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
154 | 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
|
155 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
156 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
157 | ## 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
|
158 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
159 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
160 | def ls(self, dirname=""): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
161 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
162 | 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
|
163 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
164 | @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
|
165 | @type str |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
166 | @return tuple containg the directory listing |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | @rtype tuple of str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
168 | @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
|
169 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
170 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
171 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
172 | "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
|
173 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
174 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
175 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
176 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
177 | return ast.literal_eval(out.decode("utf-8")) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
178 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
179 | def lls(self, dirname=""): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
180 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
181 | 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
|
182 | including meta data. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
183 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
184 | @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
|
185 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
186 | @return list containing the the directory listing with tuple entries |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
187 | of the name and and a tuple of mode, size and time |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
188 | @rtype tuple of str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
189 | @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
|
190 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
191 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
192 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
193 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
194 | "def stat(filename):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
195 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
196 | " rstat = os.lstat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
197 | " except:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
198 | " rstat = os.stat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
199 | " return tuple(rstat)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
200 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
201 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
202 | "def listdir_stat(dirname):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
203 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
204 | " files = os.listdir(dirname)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
205 | " except OSError:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
206 | " return []", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
207 | " if dirname in ('', '/'):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
208 | " 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
|
209 | " 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
|
210 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
211 | "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
|
212 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
213 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
214 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
215 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
216 | fileslist = ast.literal_eval(out.decode("utf-8")) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
217 | 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
|
218 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
219 | def cd(self, dirname): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
220 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
221 | 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
|
222 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
223 | @param dirname directory to change to |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
224 | @type str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
225 | @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
|
226 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
227 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
228 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
229 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
230 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
231 | "os.chdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
232 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
233 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
234 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
235 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
236 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
237 | def pwd(self): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
238 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
239 | 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
|
240 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
241 | @return current directory |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
242 | @rtype str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
243 | @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
|
244 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
245 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
246 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
247 | "print(os.getcwd())", |
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 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
250 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
251 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
252 | 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
|
253 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
254 | def rm(self, filename): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
255 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
256 | 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
|
257 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
258 | @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
|
259 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
260 | @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
|
261 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
262 | assert filename |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
263 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
264 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
265 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
266 | "os.remove('{0}')".format(filename), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
267 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
268 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
269 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
270 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
271 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
272 | def mkdir(self, dirname): |
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 | 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
|
275 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
276 | @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
|
277 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
278 | @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
|
279 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
280 | assert dirname |
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 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
283 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
284 | "os.mkdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
285 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
286 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
287 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
288 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
289 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
290 | def rmdir(self, dirname): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
291 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
292 | 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
|
293 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
294 | @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
|
295 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
296 | @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
|
297 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
298 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
299 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
300 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
301 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
302 | "os.rmdir('{0}')".format(dirname), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
303 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
304 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
305 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
306 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
307 | |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
308 | def put(self, hostFileName, deviceFileName=None): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
309 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
310 | 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
|
311 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
312 | @param hostFileName name of the file to be copied |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
313 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
314 | @param deviceFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
315 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
316 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
317 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
318 | @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
|
319 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
320 | if not os.path.isfile(hostFileName): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
321 | 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
|
322 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
323 | with open(hostFileName, "rb") as hostFile: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
324 | content = hostFile.read() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
325 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
326 | if not deviceFileName: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
327 | deviceFileName = os.path.basename(hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
328 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
329 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
330 | "fd = open('{0}', 'wb')".format(deviceFileName), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
331 | "f = fd.write", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
332 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
333 | while content: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
334 | chunk = content[:64] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
335 | commands.append("f(" + repr(chunk) + ")") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
336 | content = content[64:] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
337 | commands.append("fd.close()") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
338 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
339 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
340 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
341 | raise IOError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
342 | return True |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
343 | |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
344 | def get(self, deviceFileName, hostFileName=None): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
345 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
346 | 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
|
347 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
348 | @param deviceFileName name of the file to copy |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
349 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
350 | @param hostFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
351 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
352 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
353 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
354 | @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
|
355 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
356 | if not hostFileName: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
357 | hostFileName = deviceFileName |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
358 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
359 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
360 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
361 | "try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
362 | " from microbit import uart as u", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
363 | "except ImportError:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
364 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
365 | " from machine import UART", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
366 | " u = UART(0, {0})".format(115200), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
367 | " except Exception:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
368 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
369 | " from sys import stdout as u", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
370 | " except Exception:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
371 | " 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
|
372 | " device.')", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
373 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
374 | "f = open('{0}', 'rb')".format(deviceFileName), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
375 | "r = f.read", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
376 | "result = True", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
377 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
378 | "while result:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
379 | " result = r(32)" |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
380 | " if result:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
381 | " u.write(result)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
382 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
383 | "f.close()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
384 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
385 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
386 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
387 | raise IOError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
388 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
389 | # 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
|
390 | with open(hostFileName, "wb") as hostFile: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
391 | hostFile.write(out) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
392 | return True |
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 | # TODO: add rsync function |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
395 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
396 | def version(self): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
397 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
398 | 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
|
399 | connected device. |
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 | @return dictionary containing the version information |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
402 | @rtype dict |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
403 | @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
|
404 | 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
|
405 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
406 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
407 | "import os", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
408 | "print(os.uname())", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
409 | ] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
410 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
411 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
412 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
413 | raise ValueError(self.__shortError(err)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
414 | except ValueError: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
415 | # just re-raise it |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
416 | raise |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
417 | except Exception: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
418 | # 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
|
419 | # 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
|
420 | # 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
|
421 | # 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
|
422 | 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
|
423 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
424 | rawOutput = out.decode("utf-8").strip() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
425 | rawOutput = rawOutput[1:-1] |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
426 | items = rawOutput.split(",") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
427 | result = {} |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
428 | for item in items: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
429 | key, value = item.strip().split("=") |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
430 | 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
|
431 | return result |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
432 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
433 | def syncTime(self): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
434 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
435 | 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
|
436 | computer's time. |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
437 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
438 | @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
|
439 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
440 | now = time.localtime(time.time()) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
441 | commands = [ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
442 | "\n".join([ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
443 | "def set_time(rtc_time):", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
444 | " rtc = None", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
445 | " 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
|
446 | " import pyb", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
447 | " rtc = pyb.RTC()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
448 | " rtc.datetime(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
449 | " except:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
450 | " try:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
451 | " import machine", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
452 | " rtc = machine.RTC()", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
453 | " try:", # ESP8266 uses rtc.datetime() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
454 | " rtc.datetime(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
455 | " except:", # ESP32 uses rtc.init() |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
456 | " rtc.init(rtc_time)", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
457 | " except:", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
458 | " pass", |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
459 | ]), |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
460 | "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
|
461 | 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
|
462 | now.tm_sec, 0)) |
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 | out, err = self.__execute(commands) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
465 | if err: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
466 | raise IOError(self.__shortError(err)) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
467 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
468 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
469 | class MicroPythonFileManager(QObject): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
470 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
471 | 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
|
472 | some additional sugar. |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
473 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
474 | @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
|
475 | 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
|
476 | @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
|
477 | device |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
478 | @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
|
479 | 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
|
480 | @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
|
481 | copied to the connected device |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
482 | @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
|
483 | on the connected device |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
484 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
485 | @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
|
486 | a failed long listing operation |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
487 | @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
|
488 | that the current directory is not available |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
489 | @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
|
490 | the file could not be fetched |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
491 | @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
|
492 | the file could not be copied |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
493 | @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
|
494 | that the file could not be deleted on the device |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
495 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
496 | longListFiles = pyqtSignal(tuple) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
497 | currentDir = pyqtSignal(str) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
498 | getFileDone = pyqtSignal(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
499 | putFileDone = pyqtSignal(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
500 | deleteFileDone = pyqtSignal(str) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
501 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
502 | longListFilesFailed = pyqtSignal(str) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
503 | currentDirFailed = pyqtSignal(str) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
504 | getFileFailed = pyqtSignal(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
505 | putFileFailed = pyqtSignal(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
506 | deleteFileFailed = pyqtSignal(str) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
507 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
508 | 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
|
509 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
510 | Constructor |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
511 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
512 | @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
|
513 | @type str |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
514 | @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
|
515 | @type QObject |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
516 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
517 | super(MicroPythonFileManager, self).__init__(parent) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
518 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
519 | self.__serialPort = port |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
520 | self.__serial = MicroPythonSerialPort(parent=self) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
521 | 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
|
522 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
523 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
524 | def connect(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
525 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
526 | 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
|
527 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
528 | self.__serial.openSerialLink(self.__serialPort) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
529 | self.__fs.setSerial(self.__serial) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
530 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
531 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
532 | def disconnect(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
533 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
534 | 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
|
535 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
536 | self.__serial.closeSerialLink() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
537 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
538 | @pyqtSlot(str) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
539 | def lls(self, dirname): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
540 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
541 | 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
|
542 | |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
543 | @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
|
544 | @type str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
545 | """ |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
546 | try: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
547 | filesList = self.__fs.lls(dirname) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
548 | result = [(decoratedName(name, mode), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
549 | mode2string(mode), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
550 | str(size), |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
551 | mtime2string(time)) for |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
552 | 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
|
553 | self.longListFiles.emit(tuple(result)) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
554 | except Exception as exc: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
555 | self.longListFilesFailed.emit(str(exc)) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
556 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
557 | @pyqtSlot() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
558 | def pwd(self): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
559 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
560 | 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
|
561 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
562 | try: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
563 | pwd = self.__fs.pwd() |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
564 | self.currentDir.emit(pwd) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
565 | except Exception as exc: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
566 | self.currentDirFailed.emit(str(exc)) |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
567 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
568 | @pyqtSlot(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
569 | @pyqtSlot(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
570 | def get(self, deviceFileName, hostFileName=""): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
571 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
572 | 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
|
573 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
574 | @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
|
575 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
576 | @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
|
577 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
578 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
579 | 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
|
580 | # only a local directory was given |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
581 | hostFileName = os.path.join(hostFileName, |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
582 | os.path.basename(deviceFileName)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
583 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
584 | self.__fs.get(deviceFileName, hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
585 | self.getFileDone.emit(deviceFileName, hostFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
586 | except Exception as exc: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
587 | self.getFileFailed.emit(str(exc)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
588 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
589 | @pyqtSlot(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
590 | @pyqtSlot(str, str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
591 | def put(self, hostFileName, deviceFileName=""): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
592 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
593 | 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
|
594 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
595 | @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
|
596 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
597 | @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
|
598 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
599 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
600 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
601 | self.__fs.put(hostFileName, deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
602 | self.putFileDone(hostFileName, deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
603 | except Exception as exc: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
604 | self.putFileFailed(str(exc)) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
605 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
606 | @pyqtSlot(str) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
607 | def delete(self, deviceFileName): |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
608 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
609 | 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
|
610 | |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
611 | @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
|
612 | @type str |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
613 | """ |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
614 | try: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
615 | self.__fs.rm(deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
616 | self.deleteFileDone.emit(deviceFileName) |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
617 | except Exception as exc: |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
618 | self.deleteFileFailed(str(exc)) |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
619 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
620 | ################################################################## |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
621 | ## Utility methods below |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
622 | ################################################################## |
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 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
625 | def mtime2string(mtime): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
626 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
627 | Function to convert a time value to a string representation. |
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 | @param mtime time value |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
630 | @type int |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
631 | @return string representation of the given time |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
632 | @rtype str |
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 | return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime)) |
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 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
637 | def mode2string(mode): |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
638 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
639 | Function to convert a mode value to a string representation. |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
640 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
641 | @param mode mode value |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
642 | @type int |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
643 | @return string representation of the given mode value |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
644 | @rtype str |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
645 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
646 | return stat.filemode(mode) |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
647 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
648 | |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
649 | def decoratedName(name, mode, isDir=False): |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
650 | """ |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
651 | Function to decorate the given name according to the given mode. |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
652 | |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
653 | @param name file or directory name |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
654 | @type str |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
655 | @param mode mode value |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
656 | @type int |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
657 | @param isDir flag indicating that name is a directory |
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
658 | @type bool |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
659 | @return decorated file or directory name |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
660 | @rtype str |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
661 | """ |
7080
9a3adf033f90
MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
662 | if stat.S_ISDIR(mode) or isDir: |
7077
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
663 | # append a '/' for directories |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
664 | return name + "/" |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
665 | else: |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
666 | # no change |
3b7475b7a1ef
MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7070
diff
changeset
|
667 | return name |