-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfcFileSystemItemLoader.py
More file actions
254 lines (232 loc) · 8.13 KB
/
fcFileSystemItemLoader.py
File metadata and controls
254 lines (232 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os;
class cFileSystemItemStandIn(object):
bSupportsZipFiles = False;
def __init__(oSelf, sPath):
oSelf.sPath = os.path.normpath(sPath);
@property
def sName(oSelf):
return os.path.basename(oSelf.sPath);
@property
def sWindowsPath(oSelf):
return oSelf.sPath;
def fbIsFile(oSelf, bThrowErrors = False):
try:
return os.path.isfile(oSelf.sPath);
except Exception as oException:
if bThrowErrors:
raise;
return False;
def fbIsFolder(oSelf, bThrowErrors = False):
try:
return os.path.isdir(oSelf.sPath);
except Exception as oException:
if bThrowErrors:
raise;
return False;
def foMustBeFile(oSelf):
assert oSelf.fbIsFile(), \
"%s is not a file" % oSelf.sPath;
return oSelf;
def foMustBeFolder(oSelf):
assert oSelf.fbIsFolder(), \
"%s is not a folder" % oSelf.sPath;
return oSelf;
def fSetAsCurrentWorkingDirectory(oSelf):
assert oSelf.fbSetAsCurrentWorkingDirectory(bThrowErrors = True);
def fbSetAsCurrentWorkingDirectory(oSelf, bThrowErrors = False):
try:
os.chdir(oSelf.sPath);
except Exception:
if bThrowErrors:
raise;
return False;
assert os.getcwd().lower() == oSelf.sPath.lower(), \
"Changed working directory to %s but got %s" % (repr(oSelf.sPath), repr(os.getcwd()));
return True;
@property
def o0Parent(oSelf):
sParentPath = os.path.dirname(oSelf.sPath);
return oSelf.__class__(sParentPath) if sParentPath != oSelf.sPath else None;
def fsGetRelativePathTo(oSelf, oDescendant):
try:
return os.path.relpath(oDescendant.sPath, oSelf.sPath);
except ValueError:
return None;
def fCreateAsFolder(oSelf, bCreateParents = False):
assert oSelf.fbCreateAsFolder(
bCreateParents = bCreateParents,
bThrowErrors = True,
);
def fbCreateAsFolder(oSelf, bCreateParents = False, bThrowErrors = False):
if oSelf.o0Parent and not oSelf.o0Parent.fbExists(bThrowErrors = bThrowErrors):
if not bCreateParents:
assert not bThrowErrors, \
"Cannot create folder %s when its parent does not exist!" % oSelf.sPath;
return False;
if not oSelf.o0Parent or not oSelf.o0Parent.fbCreateAsParent(bThrowErrors = bThrowErrors):
return False;
try:
os.makedirs(oSelf.sWindowsPath);
if not os.path.isdir(oSelf.sWindowsPath):
return False;
except Exception as oException:
if bThrowErrors:
raise;
return False;
return True;
def fCreateAsFile(oSelf, sbData = b"", bCreateParents = False):
assert oSelf.fbCreateAsFile(
sbData = sbData,
bCreateParents = bCreateParents,
bThrowErrors = True,
);
def fbCreateAsFile(oSelf, sbData = b"", bCreateParents = False, bThrowErrors = False):
try:
assert oSelf.o0Parent, \
"Cannot create file %s as a root node!" % oSelf.sPath;
assert not oSelf.fbIsFolder(bThrowErrors = bThrowErrors), \
"Cannot create file %s if it already exists as a folder!" % oSelf.sPath;
except AssertionError:
if bThrowErrors:
raise;
return False;
if not oSelf.o0Parent.fbExists(bThrowErrors = bThrowErrors):
if not bCreateParents:
assert not bThrowErrors, \
"Cannot create file %s when its parent does not exist!" % oSelf.sPath;
return False;
if not oSelf.o0Parent.fbCreateAsParent(bThrowErrors = bThrowErrors):
return False;
try:
with open(oSelf.sWindowsPath, "wb") as o0PyFile:
o0PyFile.write(sbData);
except Exception as oException:
if bThrowErrors:
raise;
return False;
return True;
def foGetChild(oSelf, sName):
return oSelf.fo0GetChild(sName, bThrowErrors = True);
def fo0GetChild(oSelf, sName, bThrowErrors = False):
sNormalizedName = os.path.normpath(sName);
if (
sNormalizedName != sName
or os.sep in sName
or os.altsep in sName
or sName in [".", ".."]
or sName.endswith(".")
):
assert not bThrowErrors, \
"Invalid child name %s!" % sName;
return None;
oChild = oSelf.__class__(os.path.join(oSelf.sPath, sName));
return oChild;
def faoGetChildren(oSelf):
return oSelf.fa0oGetChildren(bThrowErrors = True);
def fa0oGetChildren(oSelf, bThrowErrors = False):
try:
asChildNames = os.listdir(oSelf.sPath);
except:
if bThrowErrors:
raise;
return None;
ao0Chilren = [
oSelf.fo0GetChild(sChildName, bThrowErrors = bThrowErrors)
for sChildName in asChildNames
];
return [o0Child for o0Child in ao0Chilren if o0Child is not None];
def foGetDescendant(oSelf, sRelativePath):
return oSelf.fo0GetDescendant(
sRelativePath,
bThrowErrors = True,
);
def fo0GetDescendant(oSelf, sRelativePath, bThrowErrors = False):
sNormalizedRelativePath= os.path.normpath(sRelativePath);
if (
sNormalizedRelativePath != sRelativePath
or sRelativePath in [".", ".."]
or sRelativePath.endswith(".")
):
assert not bThrowErrors, \
"Invalid descendant path %s!" % sRelativePath;
return None;
return oSelf.__class__(os.path.join(oSelf.sPath, sRelativePath));
def faoGetDescendants(oSelf):
return oSelf.fa0oGetDescendants(bThrowErrors = True);
def fa0oGetDescendants(oSelf, bThrowErrors = False):
assert bThrowErrors, \
"Errors are always thrown by %s" % oSelf.__class__.__name__;
a0oChildren = oSelf.fa0oGetChildren(bThrowErrors = bThrowErrors);
if a0oChildren is None:
return None;
aoDescendants = [];
for oChild in a0oChildren:
aoDescendants.append(oChild);
if oChild.fbIsFolder(bThrowErrors = bThrowErrors):
aoDescendants += oChild.fa0oGetDescendants(bThrowErrors = bThrowErrors) or [];
return aoDescendants;
def fsbRead(oSelf):
with open(oSelf.sPath, "rb") as oFile:
return oFile.read();
def fsb0Read(oSelf, bThrowErrors = False):
assert bThrowErrors, \
"Errors are always thrown by %s" % oSelf.__class__.__name__;
with open(oSelf.sPath, "rb") as oFile:
return oFile.read();
def fWrite(oSelf, sbContent):
return oSelf.fbWrite(sbContent, bThrowErrors = True);
def fbWrite(oSelf, sbContent, bThrowErrors = False):
assert bThrowErrors, \
"Errors are always thrown by %s" % oSelf.__class__.__name__;
with open(oSelf.sPath, "wb") as oFile:
oFile.write(sbContent);
return True;
def fDeleteDescendants(oSelf):
return oSelf.fbDeleteDescendants(bThrowErrors = True);
def fbDeleteDescendants(oSelf, bThrowErrors = False):
a0oChildren = oSelf.fa0oGetChildren(bThrowErrors = bThrowErrors);
if a0oChildren is None:
return False;
for oChild in a0oChildren:
if not oChild.fbDelete(bThrowErrors = bThrowErrors):
return False;
return True;
def fDelete(oSelf):
return oSelf.fbDelete(bThrowErrors = True);
def fbDelete(oSelf, bThrowErrors = False):
assert bThrowErrors, \
"Errors are always thrown by %s" % oSelf.__class__.__name__;
if oSelf.fbIsFile():
os.remove(oSelf.sPath);
else:
if not oSelf.fbDeleteDescendants(bThrowErrors = bThrowErrors):
return False;
os.rmdir(oSelf.sPath);
return True;
def fuGetSize(oSelf):
return oSelf.fu0GetSize(bThrowErrors = True);
def fu0GetSize(oSelf, bThrowErrors = False):
try:
return os.path.getsize(oSelf.sWindowsPath);
except Exception as oException:
if bThrowErrors:
raise;
return None;
def __repr__(oSelf):
return "<%s %s #%d>" % (oSelf.__class__.__name__, oSelf, id(oSelf));
def fsToString(oSelf):
return "%s{%s#%d}" % (oSelf.__class__.__name__, oSelf.sPath, id(oSelf));
def __str__(oSelf):
return oSelf.sPath;
gc0FileSystemItem = None;
def fcFileSystemItemLoader():
global gc0FileSystemItem;
if gc0FileSystemItem is None:
try:
from mFileSystemItem import cFileSystemItem;
except Exception as oException:
print("Using stand-in cFileSystemItem (%s)!" % oException);
gcFileSystemItem = cFileSystemItemStandIn;
else:
gcFileSystemItem = cFileSystemItem;
return gcFileSystemItem;