eric6/MicroPython/MicroPythonFileSystemUtilities.py

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

eric ide

mercurial