eric6/MicroPython/MicroPythonFileSystemUtilities.py

Fri, 26 Jul 2019 20:05:49 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 26 Jul 2019 20:05:49 +0200
branch
micropython
changeset 7084
3eddfc540614
parent 7081
ed510767c096
child 7137
4ed2573947ff
permissions
-rw-r--r--

MicroPython: continued implementing the file manager widget.

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
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
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 from __future__ import unicode_literals
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import time
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import stat
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import os
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
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 def mtime2string(mtime):
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 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
20
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 @param mtime time value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 @return string representation of the given time
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @rtype str
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 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
27
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 def mode2string(mode):
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 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
32
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param mode mode value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @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
36 @rtype str
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 return stat.filemode(mode)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
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 def decoratedName(name, mode, isDir=False):
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 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
44
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param name file or directory name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @param mode mode value
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @type int
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @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
50 @type bool
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @return decorated file or directory name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @rtype str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 if stat.S_ISDIR(mode) or isDir:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # append a '/' for directories
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 return name + "/"
7084
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
57 elif stat.S_ISLNK(mode):
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
58 # append a '@' for links
3eddfc540614 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7081
diff changeset
59 return name + "@"
7081
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 else:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 # no change
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 return name
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
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 def isVisible(name):
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 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
68
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 @param name name to be checked
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 @type str
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 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 return not name.startswith(".") and not name.endswith("~")
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 def fstat(filename):
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 Function to get the stat() of file.
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @param filename name of the file
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @type str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @return tuple containing the stat() result
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @rtype tuple
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 try:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 rstat = os.lstat(filename)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 except Exception:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 rstat = os.stat(filename)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 return tuple(rstat)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 def listdirStat(dirname):
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 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
96
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 @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
98 @type str
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 @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
100 stat() tuple
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @rtype list of tuple of (str, tuple)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 """
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 try:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 if dirname:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 files = os.listdir(dirname)
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 else:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 files = os.listdir()
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 except OSError:
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 return []
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 if dirname in ('', '/'):
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 return [(f, fstat(f)) for f in files if isVisible(f)]
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 return [(f, fstat(os.path.join(dirname, f))) for f in files
ed510767c096 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 if isVisible(f)]

eric ide

mercurial