326 @return flag indicating success |
328 @return flag indicating success |
327 @rtype bool |
329 @rtype bool |
328 @exception IOError raised to indicate an issue with the device |
330 @exception IOError raised to indicate an issue with the device |
329 """ |
331 """ |
330 # TODO: not implemented yet |
332 # TODO: not implemented yet |
331 |
333 |
332 ################################################################## |
334 |
333 ## Utility methods below |
335 class MicroPythonFileManager(QObject): |
334 ################################################################## |
336 """ |
335 |
337 Class implementing an interface to the device file system commands with |
336 def mtime2string(self, mtime): |
338 some additional sugar. |
337 """ |
339 |
338 Public method to convert a time value to a string representation. |
340 @signal longListFiles(result) emitted with a tuple of tuples containing the |
339 |
341 name, mode, size and time for each directory entry |
340 @param mtime time value |
342 @signal currentDir(dirname) emitted to report the current directory of the |
341 @type int |
343 device |
342 @return string representation of the given time |
344 |
343 @rtype str |
345 @signal longListFilesFailed(exc) emitted with a failure message to indicate |
344 """ |
346 a failed long listing operation |
345 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime)) |
347 @signal currentDirFailed(exc) emitted with a failure message to indicate |
346 |
348 that the current directory is not available |
347 def mode2string(self, mode): |
349 """ |
348 """ |
350 longListFiles = pyqtSignal(tuple) |
349 Public method to convert a mode value to a string representation. |
351 currentDir = pyqtSignal(str) |
350 |
352 |
351 @param mode mode value |
353 longListFilesFailed = pyqtSignal(str) |
352 @type int |
354 currentDirFailed = pyqtSignal(str) |
353 @return string representation of the given mode value |
355 |
354 @rtype str |
356 def __init__(self, port, parent=None): |
355 """ |
357 """ |
356 return stat.filemode(mode) |
358 Constructor |
357 # TODO: remove this |
359 |
358 ## |
360 @param port port name of the device |
359 ##if __name__ == "__main__": |
361 @type str |
360 ## from PyQt5.QtCore import QCoreApplication, QTimer |
362 @param parent reference to the parent object |
361 ## from MicroPythonSerialPort import MicroPythonSerialPort |
363 @type QObject |
362 ## |
364 """ |
363 ## app = QCoreApplication([]) |
365 super(MicroPythonFileManager, self).__init__(parent) |
364 ## |
366 |
365 ## serial = MicroPythonSerialPort() |
367 self.__serialPort = port |
366 ## serial.openSerialLink("/dev/ttyUSB0") |
368 self.__serial = MicroPythonSerialPort(parent=self) |
367 ## fs = MicroPythonFileSystem() |
369 self.__fs = MicroPythonFileSystem(parent=self) |
368 ## fs.setSerial(serial) |
370 |
369 ## |
371 @pyqtSlot() |
370 ## def tf(): |
372 def connect(self): |
371 ## fs.cd("/flash") |
373 """ |
372 ## print(fs.pwd()) |
374 Public slot to start the manager. |
373 ## fs.cd("odroid_go") |
375 """ |
374 ## print(fs.pwd()) |
376 self.__serial.openSerialLink(self.__serialPort) |
375 ## ll = fs.lls() |
377 self.__fs.setSerial(self.__serial) |
376 ## print(ll) |
378 |
377 ## for f, (m, s, t) in ll: |
379 @pyqtSlot() |
378 ## print(fs.mode2string(m), s, fs.mtime2string(t), f) |
380 def disconnect(self): |
379 ## fs.cd("..") |
381 """ |
380 ## print(fs.pwd()) |
382 Public slot to stop the thread. |
381 ## ll = fs.lls("odroid_go") |
383 """ |
382 ## print(ll) |
384 self.__serial.closeSerialLink() |
383 ## for f, (m, s, t) in ll: |
385 |
384 ## print(fs.mode2string(m), s, fs.mtime2string(t), f) |
386 @pyqtSlot(str) |
385 ## |
387 def lls(self, dirname): |
386 ## QTimer.singleShot(0, tf) |
388 """ |
387 ## app.exec_() |
389 Public method to get a long listing of the given directory. |
|
390 |
|
391 @param dirname name of the directory to list |
|
392 @type str |
|
393 """ |
|
394 try: |
|
395 filesList = self.__fs.lls(dirname) |
|
396 result = [(decoratedName(name, mode), |
|
397 mode2string(mode), |
|
398 str(size), |
|
399 mtime2string(time)) for |
|
400 name, (mode, size, time) in filesList] |
|
401 self.longListFiles.emit(tuple(result)) |
|
402 except Exception as exc: |
|
403 self.longListFilesFailed.emit(str(exc)) |
|
404 |
|
405 @pyqtSlot() |
|
406 def pwd(self): |
|
407 """ |
|
408 Public method to get the current directory of the device. |
|
409 """ |
|
410 try: |
|
411 pwd = self.__fs.pwd() |
|
412 self.currentDir.emit(pwd) |
|
413 except Exception as exc: |
|
414 self.currentDirFailed.emit(str(exc)) |
|
415 |
|
416 ################################################################## |
|
417 ## Utility methods below |
|
418 ################################################################## |
|
419 |
|
420 |
|
421 def mtime2string(mtime): |
|
422 """ |
|
423 Function to convert a time value to a string representation. |
|
424 |
|
425 @param mtime time value |
|
426 @type int |
|
427 @return string representation of the given time |
|
428 @rtype str |
|
429 """ |
|
430 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime)) |
|
431 |
|
432 |
|
433 def mode2string(mode): |
|
434 """ |
|
435 Function to convert a mode value to a string representation. |
|
436 |
|
437 @param mode mode value |
|
438 @type int |
|
439 @return string representation of the given mode value |
|
440 @rtype str |
|
441 """ |
|
442 return stat.filemode(mode) |
|
443 |
|
444 |
|
445 def decoratedName(name, mode): |
|
446 """ |
|
447 Function to decorate the given name according to the given mode. |
|
448 |
|
449 @param name file or directory name |
|
450 @type str |
|
451 @param mode mode value |
|
452 @type int |
|
453 @return decorated file or directory name |
|
454 @rtype str |
|
455 """ |
|
456 if stat.S_ISDIR(mode): |
|
457 # append a '/' for directories |
|
458 return name + "/" |
|
459 else: |
|
460 # no change |
|
461 return name |