Sat, 20 Jul 2019 14:48:09 +0200
Started to implement the device file system interface.
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 |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
15 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
16 | from PyQt5.QtCore import QObject, QThread |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | class MicroPythonFileSystem(QObject): |
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 | Class implementing some file system commands for MicroPython. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | 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
|
24 | system of a connected MicroPython device. Supported commands are: |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | <ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | <li>ls: directory listing</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | <li>lls: directory listing with meta data</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | <li>cd: change directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | <li>pwd: get the current directory</li> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | <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
|
31 | <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
|
32 | <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
|
33 | <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
|
34 | <li>rmdir: remove an empty directory</li> |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | </ul> |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | def __init__(self, parent=None): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | Constructor |
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 | @param parent reference to the parent object |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | @type QObject |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | 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
|
45 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
46 | self.__serial = None |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
48 | def setSerial(self, serial): |
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 | 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
|
51 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
52 | 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
|
53 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
54 | @param serial open serial port |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
55 | @type MicroPythonSerialPort |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
56 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
57 | self.__serial = serial |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
58 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
59 | def __rawOn(self): |
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 | 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
|
62 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
63 | 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
|
64 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
65 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
66 | return |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
67 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
68 | 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
|
69 | 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
|
70 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
71 | 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
|
72 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
73 | for _i in range(3): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
74 | # 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
|
75 | 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
|
76 | self.__serial.waitForBytesWritten() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
77 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
78 | 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
|
79 | 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
|
80 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
81 | 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
|
82 | self.__serial.readUntil(softRebootMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
83 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
84 | # 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
|
85 | # special way |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
86 | data = self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
87 | if not data.endswith(rawReplMessage): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
88 | 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
|
89 | self.__serial.readUntil(rawReplMessage) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
90 | 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
|
91 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
92 | def __rawOff(self): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
93 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
94 | 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
|
95 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
96 | if self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
97 | 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
|
98 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
99 | def __execute(self, commands): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
100 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
101 | 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
|
102 | result. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
103 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
104 | 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
|
105 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
106 | @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
|
107 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
108 | @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
|
109 | @rtype tuple of (bytes, bytes) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
110 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
111 | if not self.__serial: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
112 | return b"", b"" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
113 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
114 | result = bytearray() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
115 | err = b"" |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
116 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
117 | self.__rawOn() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
118 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
119 | for command in commands: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
120 | if command: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
121 | commandBytes = command.encode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
122 | 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
|
123 | # read until prompt |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
124 | 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
|
125 | # split stdout, stderr |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
126 | 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
|
127 | result += out |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
128 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
129 | return b"", err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
130 | QThread.msleep(10) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
131 | self.__rawOff() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
132 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
133 | return bytes(result), err |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
134 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
135 | def __shortError(self, error): |
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 | 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
|
138 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
139 | @param error verbose error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
140 | @type bytes |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
141 | @return shortened error message |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
142 | @rtype str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
143 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
144 | if error: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
145 | decodedError = error.decode("utf-8") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
146 | try: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
147 | 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
|
148 | except Exception: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
149 | return decodedError |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
150 | 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
|
151 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
152 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
153 | ## 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
|
154 | ################################################################## |
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 | def ls(self, dirname=""): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
157 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
158 | 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
|
159 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
160 | @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
|
161 | @type str |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
162 | @return tuple containg the directory listing |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
163 | @rtype tuple of str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
164 | @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
|
165 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
166 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
167 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
168 | "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
|
169 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
170 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
171 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
172 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
173 | return ast.literal_eval(out.decode("utf-8")) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
174 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
175 | def lls(self, dirname=""): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
176 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
177 | 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
|
178 | including meta data. |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
179 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
180 | @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
|
181 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
182 | @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
|
183 | 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
|
184 | @rtype tuple of str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
185 | @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
|
186 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
187 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
188 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
189 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
190 | "def stat(filename):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
191 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
192 | " rstat = os.lstat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
193 | " except:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
194 | " rstat = os.stat(filename)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
195 | " return tuple(rstat)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
196 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
197 | "\n".join([ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
198 | "def listdir_stat(dirname):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
199 | " try:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
200 | " files = os.listdir(dirname)", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
201 | " except OSError:", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
202 | " return []", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
203 | " if dirname in ('', '/'):", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
204 | " 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
|
205 | " 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
|
206 | ]), |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
207 | "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
|
208 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
209 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
210 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
211 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
212 | 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
|
213 | 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
|
214 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
215 | def cd(self, dirname): |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
216 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
217 | 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
|
218 | |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
219 | @param dirname directory to change to |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
220 | @type str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
221 | @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
|
222 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
223 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
224 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
225 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
226 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
227 | "os.chdir('{0}')".format(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 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
230 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
231 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
232 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
233 | def pwd(self): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
234 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
235 | 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
|
236 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
237 | @return current directory |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
238 | @rtype str |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
239 | @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
|
240 | """ |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
241 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
242 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
243 | "print(os.getcwd())", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
244 | ] |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
245 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
246 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
247 | raise IOError(self.__shortError(err)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
248 | 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
|
249 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
250 | def rm(self, filename): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
251 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
252 | 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
|
253 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
254 | @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
|
255 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
256 | @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
|
257 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
258 | assert filename |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
259 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
260 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
261 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
262 | "os.remove('{0}')".format(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 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
265 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
266 | raise IOError(self.__shortError(err)) |
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 | def mkdir(self, dirname): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
269 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
270 | 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
|
271 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
272 | @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
|
273 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
274 | @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
|
275 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
276 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
277 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
278 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
279 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
280 | "os.mkdir('{0}')".format(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 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
283 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
284 | raise IOError(self.__shortError(err)) |
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 | def rmdir(self, dirname): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
287 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
288 | 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
|
289 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
290 | @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
|
291 | @type str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
292 | @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
|
293 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
294 | assert dirname |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
295 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
296 | commands = [ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
297 | "import os", |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
298 | "os.rmdir('{0}')".format(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 | out, err = self.__execute(commands) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
301 | if err: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
302 | raise IOError(self.__shortError(err)) |
7067
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
303 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
304 | def put(self, hostFileName, deviceFileName): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
305 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
306 | 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
|
307 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
308 | @param hostFileName name of the file to be copied |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
309 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
310 | @param deviceFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
311 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
312 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
313 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
314 | @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
|
315 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
316 | # TODO: not implemented yet |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
317 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
318 | def get(self, deviceFileName, hostFileName): |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
319 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
320 | 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
|
321 | |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
322 | @param deviceFileName name of the file to copy |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
323 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
324 | @param hostFileName name of the file to copy to |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
325 | @type str |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
326 | @return flag indicating success |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
327 | @rtype bool |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
328 | @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
|
329 | """ |
3fc4082fc6ba
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
330 | # TODO: not implemented yet |
7070
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
331 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
332 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
333 | ## Utility methods below |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
334 | ################################################################## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
335 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
336 | def mtime2string(self, mtime): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
337 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
338 | Public method to convert a time value to a string representation. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
339 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
340 | @param mtime time value |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
341 | @type int |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
342 | @return string representation of the given time |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
343 | @rtype str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
344 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
345 | return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime)) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
346 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
347 | def mode2string(self, mode): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
348 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
349 | Public method to convert a mode value to a string representation. |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
350 | |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
351 | @param mode mode value |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
352 | @type int |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
353 | @return string representation of the given mode value |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
354 | @rtype str |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
355 | """ |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
356 | return stat.filemode(mode) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
357 | # TODO: remove this |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
358 | ## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
359 | ##if __name__ == "__main__": |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
360 | ## from PyQt5.QtCore import QCoreApplication, QTimer |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
361 | ## from MicroPythonSerialPort import MicroPythonSerialPort |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
362 | ## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
363 | ## app = QCoreApplication([]) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
364 | ## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
365 | ## serial = MicroPythonSerialPort() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
366 | ## serial.openSerialLink("/dev/ttyUSB0") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
367 | ## fs = MicroPythonFileSystem() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
368 | ## fs.setSerial(serial) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
369 | ## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
370 | ## def tf(): |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
371 | ## fs.cd("/flash") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
372 | ## print(fs.pwd()) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
373 | ## fs.cd("odroid_go") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
374 | ## print(fs.pwd()) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
375 | ## ll = fs.lls() |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
376 | ## print(ll) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
377 | ## for f, (m, s, t) in ll: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
378 | ## print(fs.mode2string(m), s, fs.mtime2string(t), f) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
379 | ## fs.cd("..") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
380 | ## print(fs.pwd()) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
381 | ## ll = fs.lls("odroid_go") |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
382 | ## print(ll) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
383 | ## for f, (m, s, t) in ll: |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
384 | ## print(fs.mode2string(m), s, fs.mtime2string(t), f) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
385 | ## |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
386 | ## QTimer.singleShot(0, tf) |
3368ce0e7879
Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7067
diff
changeset
|
387 | ## app.exec_() |