src/eric7/SystemUtilities/FileSystemUtilities.py

Tue, 18 Mar 2025 18:14:12 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 18 Mar 2025 18:14:12 +0100
branch
eric7
changeset 11173
d63911a89570
parent 11090
f5f5f5803935
permissions
-rw-r--r--

Streamlined some code in the filesystem utilities module.

9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
11090
f5f5f5803935 Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10991
diff changeset
3 # Copyright (c) 2022 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing file system related utility functions.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import contextlib
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import fnmatch
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import pathlib
10791
ca9ece290f71 Changed some code to use 'shutil.which()'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10680
diff changeset
14 import shutil
10170
6cf1ee737d8f Corrected some more code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10108
diff changeset
15 import subprocess # secok
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from eric7.SystemUtilities import OSUtilities
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def toNativeSeparators(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Function returning a path, that is using native separator characters.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param path path to be converted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @return path with converted separator characters
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @rtype str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 return str(pathlib.PurePath(path)) if bool(path) else ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 def fromNativeSeparators(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Function returning a path, that is using "/" separator characters.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param path path to be converted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @return path with converted separator characters
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 @rtype str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return pathlib.PurePath(path).as_posix() if bool(path) else ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 def normcasepath(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 Function returning a path, that is normalized with respect to its case
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 and references.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
49 @param path file path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
50 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
51 @return case normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
52 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 return os.path.normcase(os.path.normpath(path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 def normcaseabspath(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 Function returning an absolute path, that is normalized with respect to
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 its case and references.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
62 @param path file path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
63 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
64 @return absolute, normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
65 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 return os.path.normcase(os.path.abspath(path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 def normjoinpath(a, *p):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 Function returning a normalized path of the joined parts passed into it.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
74 @param a first path to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
75 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
76 @param p variable number of path parts to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
77 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
78 @return normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
79 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 return os.path.normpath(os.path.join(a, *p))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 def normabsjoinpath(a, *p):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Function returning a normalized, absolute path of the joined parts passed
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 into it.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
89 @param a first path to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
90 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
91 @param p variable number of path parts to be joined
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
92 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
93 @return absolute, normalized path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
94 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 return os.path.abspath(os.path.join(a, *p))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 def isinpath(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 Function to check for an executable file.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
103 @param file filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
104 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
105 @return flag indicating, if the executable file is accessible via the executable
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
106 search path defined by the PATH environment variable.
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
107 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 """
10791
ca9ece290f71 Changed some code to use 'shutil.which()'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10680
diff changeset
109 return bool(shutil.which(file))
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
112 def startsWithPath(path, start):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 Function to check, if a path starts with a given start path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 @param path path to be checked
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 @param start start path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 @return flag indicating that the path starts with the given start
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 @rtype bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 """
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
124 start1 = start if start.endswith(os.sep) else f"{start}{os.sep}"
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 return bool(start) and (
10668
2be6fadcac40 Fixed a little issue in the file system utilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
126 path == start or normcasepath(path).startswith(normcasepath(start1))
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 )
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 def relativeUniversalPath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 Function to convert a file path to a path relative to a start path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 with universal separators.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
135 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
136 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
137 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
138 @type str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 @return relative path or unchanged path, if path does not start with
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
140 the start path with universal separators
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
141 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 return fromNativeSeparators(os.path.relpath(path, start))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 def absolutePath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 Public method to convert a path relative to a start path to an
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 absolute path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
151 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
152 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
153 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
154 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
155 @return absolute path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
156 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 if not os.path.isabs(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 path = os.path.normpath(os.path.join(start, path))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 def absoluteUniversalPath(path, start):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 Public method to convert a path relative to a start path with
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 universal separators to an absolute path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
168 @param path file or directory name to convert
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
169 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
170 @param start start path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
171 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
172 @return absolute path with native separators
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
173 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 if not os.path.isabs(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 path = toNativeSeparators(os.path.normpath(os.path.join(start, path)))
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 def getExecutablePath(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 Function to build the full path of an executable file from the environment.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
184 @param file filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
185 @type str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 @return full executable name, if the executable file is accessible
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
187 via the executable search path defined by the PATH environment variable, or an
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 empty string otherwise.
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
189 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
10791
ca9ece290f71 Changed some code to use 'shutil.which()'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10680
diff changeset
191 exe = shutil.which(file)
ca9ece290f71 Changed some code to use 'shutil.which()'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10680
diff changeset
192 return exe if bool(exe) else ""
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 def getExecutablePaths(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 Function to build all full path of an executable file from the environment.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
199 @param file filename of the executable
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
200 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
201 @return list of full executable names, if the executable file is accessible via
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
202 the executable search path defined by the PATH environment variable, or an
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
203 empty list otherwise.
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
204 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 paths = []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 if os.path.isabs(file):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 if os.access(file, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 return [file]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 return []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 cur_path = os.path.join(os.curdir, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 paths.append(cur_path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 path = os.getenv("PATH")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 # environment variable not defined
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 if path is not None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 dirs = path.split(os.pathsep)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 for directory in dirs:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 exe = os.path.join(directory, file)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 if os.access(exe, os.X_OK) and exe not in paths:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 paths.append(exe)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 return paths
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 def isExecutable(exe):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 Function to check, if a file is executable.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
235 @param exe filename of the executable to check
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
236 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
237 @return flag indicating executable status
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
238 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 return os.access(exe, os.X_OK)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 def isDrive(path):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 Function to check, if a path is a Windows drive.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 @param path path name to be checked
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 @return flag indicating a Windows drive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 @rtype bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 isWindowsDrive = False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 drive, directory = os.path.splitdrive(path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 if (
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 drive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 and len(drive) == 2
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 and drive.endswith(":")
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 and directory in ["", "\\", "/"]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 isWindowsDrive = True
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 return isWindowsDrive
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
265 def samepath(f1, f2, followSymlinks=True):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 Function to compare two paths.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
269 @param f1 first filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
270 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
271 @param f2 second filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
272 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
273 @param followSymlinks flag indicating to respect symbolic links for the comparison
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
274 (i.e. compare the real paths) (defaults to True)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
275 @type bool (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 @return flag indicating whether the two paths represent the
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
277 same path on disk
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
278 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 if f1 is None or f2 is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282
10561
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
283 if isPlainFileName(f1) and isPlainFileName(f2):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
284 if followSymlinks:
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
285 if normcaseabspath(os.path.realpath(f1)) == normcaseabspath(
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
286 os.path.realpath(f2)
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
287 ):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
288 return True
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
289 else:
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
290 if normcaseabspath(f1) == normcaseabspath(f2):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
291 return True
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
292
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
293 else:
10561
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
294 return f1 == f2
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
299 def samefilepath(f1, f2, followSymlinks=True):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 Function to compare two paths. Strips the filename.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
303 @param f1 first filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
304 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
305 @param f2 second filepath for the compare
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
306 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
307 @param followSymlinks flag indicating to respect symbolic links for the comparison
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
308 (i.e. compare the real paths) (defaults to True)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
309 @type bool (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 @return flag indicating whether the two paths represent the
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
311 same path on disk
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
312 @rtype bool
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 if f1 is None or f2 is None:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
10561
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
317 if isPlainFileName(f1) and isPlainFileName(f2):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
318 if followSymlinks:
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
319 if normcaseabspath(
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
320 os.path.dirname(os.path.realpath(f1))
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
321 ) == normcaseabspath(os.path.dirname(os.path.realpath(f2))):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
322 return True
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
323 else:
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
324 if normcaseabspath(os.path.dirname(f1)) == normcaseabspath(
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
325 os.path.dirname(f2)
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
326 ):
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
327 return True
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
328
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
329 else:
10561
be23a662d709 Implemented (most) of the eric-ide server debugging functionality.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10521
diff changeset
330 return os.path.dirname(f1) == os.path.dirname(f2)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 return False
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
334
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 try:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
336 EXTSEP = os.extsep
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 except AttributeError:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
338 EXTSEP = "."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 def splitPath(name):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 Function to split a pathname into a directory part and a file part.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
345 @param name path name
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
346 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
347 @return tuple containing directory name and file name
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
348 @rtype tuple of (str, str)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 if os.path.isdir(name):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 dn = os.path.abspath(name)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 fn = "."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 else:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 dn, fn = os.path.split(name)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 return (dn, fn)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 def joinext(prefix, ext):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 Function to join a file extension to a path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362 The leading "." of ext is replaced by a platform specific extension
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 separator if necessary.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
365 @param prefix the basepart of the filename
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
366 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
367 @param ext the extension part
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
368 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
369 @return the complete filename
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
370 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 if ext[0] != ".":
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 ext = ".{0}".format(ext)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 # require leading separator to match os.path.splitext
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 return prefix + EXTSEP + ext[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378 def compactPath(path, width, measure=len):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
380 Function to return a compacted path fitting inside the given width.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
381
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
382 @param path path to be compacted
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
383 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
384 @param width width for the compacted path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
385 @type int
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
386 @param measure reference to a function used to measure the length of the
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
387 string
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
388 @type function(str)
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
389 @return compacted path
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
390 @rtype str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
391 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
392 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
393 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
394
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
395 ellipsis = "..."
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
396
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
397 head, tail = os.path.split(path)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
398 mid = len(head) // 2
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
399 head1 = head[:mid]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
400 head2 = head[mid:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
401 while head1:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
402 # head1 is same size as head2 or one shorter
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
403 path = os.path.join("{0}{1}{2}".format(head1, ellipsis, head2), tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
404 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
405 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
406 head1 = head1[:-1]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
407 head2 = head2[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
408 path = os.path.join(ellipsis, tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
409 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
410 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
411 while tail:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
412 path = "{0}{1}".format(ellipsis, tail)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
413 if measure(path) <= width:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
414 return path
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
415 tail = tail[1:]
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 return ""
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
419 def direntries(
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
420 path,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
421 filesonly=False,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
422 pattern=None,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
423 followsymlinks=True,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
424 checkStop=None,
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
425 ignore=None,
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
426 recursive=True,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
427 dirsonly=False,
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
429 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
430 Function returning a list of all files and directories.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
431
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
432 @param path root of the tree to check
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
433 @type str
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
434 @param filesonly flag indicating that only files are wanted (defaults to False)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
435 @type bool (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
436 @param pattern a filename pattern or list of filename patterns to check
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
437 against (defaults to None)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
438 @type str or list of str (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
439 @param followsymlinks flag indicating whether symbolic links
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
440 should be followed (defaults to True)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
441 @type bool (optional)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
442 @param checkStop function to be called to check for a stop (defaults to None)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
443 @type function (optional)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
444 @param ignore list of entries to be ignored (defaults to None)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
445 @type list of str (optional)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
446 @param recursive flag indicating a recursive search (defaults to True)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
447 @type bool (optional)
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
448 @param dirsonly flag indicating to return only directories. When True it has
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
449 precedence over the 'filesonly' parameter ((defaults to False)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
450 @type bool
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
451 @return list of all files and directories in the tree rooted
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
452 at path. The names are expanded to start with path.
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
453 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 patterns = pattern if isinstance(pattern, list) else [pattern]
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
456 files = [] if (filesonly and not dirsonly) else [path]
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
457 ignoreList = [
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
458 ".svn",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
459 ".hg",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
460 ".git",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
461 ".ropeproject",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
462 ".eric7project",
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
463 ".jedi",
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
464 "__pycache__",
9639
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
465 ]
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
466 if ignore is not None:
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
467 ignoreList.extend(ignore)
9e66fd88193c Corrected the logic for the project creation if the project directory already contains files and an embedded environment (see issue 480).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9624
diff changeset
468
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
469 with contextlib.suppress(OSError, UnicodeDecodeError), os.scandir(
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
470 path
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
471 ) as dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
472 for dirEntry in dirEntriesIterator:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
473 if checkStop and checkStop():
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
474 break
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
475
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
476 if dirEntry.name in ignoreList:
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
477 continue
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
478
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
479 if (
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
480 pattern
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
481 and not dirEntry.is_dir()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
482 and not any(fnmatch.fnmatch(dirEntry.name, p) for p in patterns)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
483 ):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
484 # entry doesn't fit the given pattern
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
485 continue
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
486
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
487 if dirEntry.is_dir():
10597
fbe93720ee9f Corrected a few formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10583
diff changeset
488 if dirEntry.path in ignoreList or (
fbe93720ee9f Corrected a few formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10583
diff changeset
489 dirEntry.is_symlink() and not followsymlinks
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
490 ):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
491 continue
10583
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
492 if recursive:
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
493 files += direntries(
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
494 dirEntry.path,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
495 filesonly=filesonly,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
496 pattern=pattern,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
497 followsymlinks=followsymlinks,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
498 checkStop=checkStop,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
499 ignore=ignore,
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
500 )
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
501 elif dirsonly:
2114cc7275e8 Adapted the UML Class diagram and the Package diagram to support the 'eric-ide' server.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10561
diff changeset
502 files.append(dirEntry.path)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
503 else:
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
504 files.append(dirEntry.path)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
505 return files
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
506
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
507
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
508 def getDirs(path, excludeDirs):
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
509 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
510 Function returning a list of all directories below path.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
511
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
512 @param path root of the tree to check
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
513 @type str
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
514 @param excludeDirs base name of directories to ignore
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
515 @type list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
516 @return list of all directories found
10393
434e1b0bc49e Extended functionality in FileSystemUtilities.py and modified the doc strings to the newer format.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10388
diff changeset
517 @rtype list of str
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
518 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
519 try:
9646
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
520 dirs = []
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
521 with os.scandir(path) as dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
522 for dirEntry in dirEntriesIterator:
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
523 if (
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
524 dirEntry.is_dir()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
525 and not dirEntry.is_symlink()
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
526 and dirEntry.name not in excludeDirs
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
527 ):
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
528 dirs.append(dirEntry.path)
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
529 dirs.extend(getDirs(dirEntry.path, excludeDirs))
ab5678db972f Modernize the code by using os.scandir() instead of os.listdir().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9645
diff changeset
530 return dirs
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
531 except OSError:
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
532 return []
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
533
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
534
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
535 def findVolume(volumeName, findAll=False, markerFile=None):
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
536 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
537 Function to find the directory belonging to a given volume name.
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
538
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
539 @param volumeName name of the volume to search for
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
540 @type str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
541 @param findAll flag indicating to get the directories for all volumes
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
542 starting with the given name (defaults to False)
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
543 @type bool (optional)
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
544 @param markerFile name of a file to check for its existence (defaults to None)
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
545 @type str (optional)
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
546 @return directory path or list of directory paths for the given volume
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
547 name
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
548 @rtype str or list of str
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
549 """
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
550 if OSUtilities.isWindowsPlatform():
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
551 # we are on a Windows platform
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
552 drives = []
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
553 output = subprocess.run(
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
554 [
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
555 "wmic",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
556 "PATH",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
557 "Win32_LogicalDisk",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
558 "get",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
559 "DeviceID,",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
560 "DriveType,",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
561 "FileSystem,",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
562 "VolumeName",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
563 ],
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
564 check=True,
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
565 capture_output=True,
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
566 text=True,
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
567 encoding="utf-8",
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
568 ).stdout.splitlines()
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
569
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
570 for line in output:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
571 words = line.split()
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
572 if len(words) >= 4 and words[1] == "2" and words[2] == "FAT":
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
573 drive = words[0]
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
574 volume = " ".join(words[3:])
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
575 if findAll:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
576 if volume.startswith(volumeName):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
577 drives.append(f"{drive}\\")
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
578 else:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
579 if volume == volumeName:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
580 return f"{drive}\\"
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
581
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
582 return drives
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
583 else:
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
584 # we are on a Linux, FreeBSD or macOS platform
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
585 # FreeBSD needs a marker file because it does not use the volume name.
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
586 directories = []
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
587 if OSUtilities.isMacPlatform():
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
588 # macOS
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
589 mountPathStart = "/Volumes"
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
590 elif OSUtilities.isLinuxPlatform():
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
591 # Linux
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
592 mountPathStart = "/media/{0}/".format(OSUtilities.getUserName())
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
593 if not os.path.isdir(mountPathStart):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
594 # no user mount available
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
595 return [] if findAll else None
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
596 elif OSUtilities.isFreeBsdPlatform():
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
597 # FreeBSD
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
598 mountPathStart = "/media/"
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
599 else:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
600 # unsupported platform
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
601 return [] if findAll else None
9624
b47dfa7a137d Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
602
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
603 for d in os.listdir(mountPathStart):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
604 dPath = os.path.join(mountPathStart, d)
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
605 if findAll:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
606 if d.startswith(volumeName) or (
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
607 markerFile is not None
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
608 and os.path.exists(os.path.join(dPath, markerFile))
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
609 ):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
610 directories.append(dPath)
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
611 else:
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
612 if d == volumeName or (
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
613 markerFile is not None
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
614 and os.path.exists(os.path.join(dPath, markerFile))
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
615 ):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
616 return dPath
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
617
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
618 return directories
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
619
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
620
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
621 def getUserMounts():
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
622 """
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
623 Function to determine all available user mounts.
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
624
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
625 Note: On Windows platforms all available drives are returned.
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
626
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
627 @return list of user mounts or drives
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
628 @rtype list of str
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
629 """
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
630 if OSUtilities.isWindowsPlatform():
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
631 # we are on a Windows platform
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
632 return [
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
633 f"{disk}:\\"
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
634 for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
635 if os.path.exists(f"{disk}:\\")
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
636 ]
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
637 else:
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
638 # we are on a Linux, FreeBSD or macOS platform
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
639 if OSUtilities.isMacPlatform():
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
640 # macOS
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
641 mountPathStart = "/Volumes/"
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
642 elif OSUtilities.isLinuxPlatform():
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
643 # Linux
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
644 mountPathStart = "/media/{0}/".format(OSUtilities.getUserName())
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
645 if not os.path.isdir(mountPathStart):
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
646 # no user mount available
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
647 return []
10339
446d22fa1aea Renamed "isBsdPlatform()" to "isFreeBsdPlatform()" to express its real test.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10338
diff changeset
648 elif OSUtilities.isFreeBsdPlatform():
10338
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
649 # FreeBSD
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
650 mountPathStart = "/media/"
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
651 else:
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
652 # unsupported platform
7329b8d78f7b Modified the "getUserMounts()" function of FileSystemUtilities.py to work on FreeBSD as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10170
diff changeset
653 return []
9944
011ae0edbcff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9923
diff changeset
654
11173
d63911a89570 Streamlined some code in the filesystem utilities module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 11090
diff changeset
655 return [os.path.join(mountPathStart, d) for d in os.listdir(mountPathStart)]
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
656
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
657
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
658 def startfile(filePath):
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
659 """
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
660 Function to open the given file path with the system default application.
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
661
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
662 @param filePath file path to be opened
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
663 @type str or Path
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
664 @return flag indicating a successful start of the associated application
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
665 @rtype bool
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
666 """
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
667 filePath = str(filePath)
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
668
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
669 with contextlib.suppress(OSError):
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
670 if OSUtilities.isWindowsPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
671 os.startfile(filePath) # secok
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
672 return True
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
673
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
674 elif OSUtilities.isMacPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
675 return subprocess.call(("open", filePath)) == 0 # secok
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
676
10397
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
677 elif OSUtilities.isLinuxPlatform() or OSUtilities.isFreeBsdPlatform():
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
678 return subprocess.call(("xdg-open", filePath)) == 0 # secok
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
679
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
680 # unsupported platform or OSError
f60464a5f7ea Modified the last implementation to work on Windows as well and made the code more robust against errors.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10396
diff changeset
681 return False
10396
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
682
2f72e9330af2 File Browser
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10393
diff changeset
683
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
684 ################################################################################
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
685 ## Functions below handle (MicroPython) device and remote file names.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
686 ################################################################################
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
687
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
688
10991
e93595883b7d Changed the device and remote markers to a better readable string.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10974
diff changeset
689 _DeviceFileMarker = "device::"
e93595883b7d Changed the device and remote markers to a better readable string.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10974
diff changeset
690 _RemoteFileMarker = "remote::"
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
691
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
692
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
693 def deviceFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
694 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
695 Function to create a device (MicroPython) file name given a plain file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
696
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
697 @param fileName plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
698 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
699 @return device file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
700 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
701 """
10603
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
702 if fileName.startswith(_DeviceFileMarker):
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
703 # it is already a device file name
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
704 return fileName
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
705 else:
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
706 return f"{_DeviceFileMarker}{fileName}"
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
707
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
708
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
709 def isDeviceFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
710 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
711 Function to check, if the given file name is a device file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
712
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
713 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
714 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
715 @return flag indicating a device file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
716 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
717 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
718 return fileName.startswith(_DeviceFileMarker)
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
719
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
720
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
721 def remoteFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
722 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
723 Function to create a remote file name given a plain file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
724
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
725 @param fileName plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
726 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
727 @return remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
728 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
729 """
10603
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
730 if fileName.startswith(_RemoteFileMarker):
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
731 # it is already a remote file name
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
732 return fileName
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
733 else:
8093b9e14b75 Made some conversion function in FileSystemUtilities more resilient.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10597
diff changeset
734 return f"{_RemoteFileMarker}{fileName}"
10388
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
735
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
736
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
737 def isRemoteFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
738 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
739 Function to check, if the given file name is a remote file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
740
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
741 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
742 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
743 @return flag indicating a remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
744 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
745 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
746 return fileName.startswith(_RemoteFileMarker)
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
747
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
748
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
749 def isPlainFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
750 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
751 Function to check, if the given file name is a plain (i.e. local) file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
752
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
753 @param fileName file name to be checked
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
754 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
755 @return flag indicating a local file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
756 @rtype bool
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
757 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
758 return not fileName.startswith((_DeviceFileMarker, _RemoteFileMarker))
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
759
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
760
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
761 def plainFileName(fileName):
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
762 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
763 Function to create a plain file name given a device or remote file name.
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
764
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
765 @param fileName device or remote file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
766 @type str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
767 @return plain file name
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
768 @rtype str
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
769 """
a34ce7f42e8b Made the code dealing with "device:" and "remote:" file names generally available in FileSystemUtilities.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10339
diff changeset
770 return fileName.replace(_DeviceFileMarker, "").replace(_RemoteFileMarker, "")

eric ide

mercurial