eric6/MicroPython/MicroPythonFileSystemUtilities.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8234
fcb6b4b96274
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
3 # Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing some file system utility functions.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import time
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import stat
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 def mtime2string(mtime):
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 Function to convert a time value to a string representation.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 @param mtime time value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 @return string representation of the given time
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 @rtype str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(mtime))
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 def mode2string(mode):
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 Function to convert a mode value to a string representation.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 @param mode mode value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @return string representation of the given mode value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @rtype str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 return stat.filemode(mode)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 def decoratedName(name, mode, isDir=False):
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 Function to decorate the given name according to the given mode.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @param name file or directory name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @type str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param mode mode value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @param isDir flag indicating that name is a directory
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @type bool
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @return decorated file or directory name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @rtype str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 if stat.S_ISDIR(mode) or isDir:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 # append a '/' for directories
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 return name + "/"
7084
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
55 elif stat.S_ISLNK(mode):
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
56 # append a '@' for links
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
57 return name + "@"
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 else:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 # no change
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 return name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
63 def isVisible(name, showHidden):
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 Function to check, if a filesystem entry is a hidden file or directory.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 @param name name to be checked
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 @type str
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
69 @param showHidden flag indicating to show hidden files as well
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
70 @type bool
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 @return flag indicating a visible filesystem entry
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 @rtype bool
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 """
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
74 return (
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
75 showHidden or
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
76 (not name.startswith(".") and not name.endswith("~"))
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
77 )
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def fstat(filename):
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 Function to get the stat() of file.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @param filename name of the file
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 @type str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 @return tuple containing the stat() result
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 @rtype tuple
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 try:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 rstat = os.lstat(filename)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 except Exception:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 rstat = os.stat(filename)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 return tuple(rstat)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
96 def listdirStat(dirname, showHidden=False):
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 Function to get a list of directory entries and associated stat() tuples.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @param dirname name of the directory to list
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @type str
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
102 @param showHidden flag indicating to show hidden files as well
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
103 @type bool
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 @return list of tuples containing the entry name and the associated
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 stat() tuple
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 @rtype list of tuple of (str, tuple)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 try:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 if dirname:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 files = os.listdir(dirname)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 else:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 files = os.listdir()
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 except OSError:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 return []
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 if dirname in ('', '/'):
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
117 return [(f, fstat(f)) for f in files if isVisible(f, showHidden)]
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 return [(f, fstat(os.path.join(dirname, f))) for f in files
7137
4ed2573947ff MicroPythonFileManagerWidget: added option to show hidden files to the local and device files list context menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7084
diff changeset
120 if isVisible(f, showHidden)]

eric ide

mercurial