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