This repository was archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
166 lines (124 loc) · 4.4 KB
/
Copy pathfile.py
File metadata and controls
166 lines (124 loc) · 4.4 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
import os
import shutil
import hashlib
class File:
def __init__(self, filePath):
if not isinstance(filePath, str):
raise TypeError(f"the file path must be a string, not '{filePath}' ({type(filePath)})")
if os.path.isabs(filePath):
self.__absPath = os.path.abspath(filePath).replace("\\", "/")
else:
self.__absPath = os.path.abspath(os.path.join(os.getcwd(), filePath)).replace("\\", "/")
@property
def isDirectory(self):
return os.path.isdir(self.path)
@property
def isFile(self):
return os.path.isfile(self.path)
@property
def exists(self):
return os.path.exists(self.path)
def rename(self, newName):
os.rename(self.path, self.parent.append(newName).path)
@property
def content(self):
if not self.exists:
raise FileNotFoundError(f"'{self.path}' was not found")
if self.isDirectory:
raise IsADirectoryError(f"'{self.path}' was not a file")
with open(self.path, "r", encoding="utf-8") as f:
return f.read()
@content.setter
def content(self, content):
if self.exists and self.isDirectory:
raise IsADirectoryError(f"'{self.path}' was not a file")
with open(self.path, "w+", encoding="utf-8") as f:
f.write(content)
@property
def name(self):
return os.path.basename(self.path)
@property
def length(self):
if not self.exists:
raise FileNotFoundError(f"'{self.path}' was not found")
if not self.isFile:
raise IsADirectoryError(f"'{self.path}' was not a file")
return os.path.getsize(self.path)
@property
def files(self):
if not self.exists:
raise FileNotFoundError(f"'{self.path}' was not found")
if self.isFile:
raise NotADirectoryError(f"'{self.path}' was not a Directory")
files = [
File(os.path.join(self.path, f))
for f in os.listdir(self.path)
]
return files
@property
def path(self):
return self.__absPath.replace("\\", "/")
def relPath(self, baseDir=None):
if baseDir is None:
return os.path.relpath(self.path).replace("\\", "/")
bd = baseDir if isinstance(baseDir, File) else File(baseDir)
if bd.isDirectory:
return os.path.relpath(self.path, bd.path).replace("\\", "/")
return self.path
@property
def parent(self):
return File(os.path.dirname(self.path))
@property
def modified(self):
return int(os.path.getmtime(self.path))
def delete(self):
if self.exists:
if self.isFile:
os.remove(self.path)
if self.isDirectory:
shutil.rmtree(self.path)
def append(self, relPath):
if self.isFile:
raise NotADirectoryError(f"'{self.path}' was not a Directory, can not get content in subfolder")
return File(os.path.join(self.path, relPath))
@property
def sha1(self):
if not self.exists:
raise FileNotFoundError(f"'{self.path}' was not found")
if self.isDirectory:
raise IsADirectoryError(f"'{self.path}' was not a file")
with open(self.path, 'rb') as f:
sha1obj = hashlib.sha1()
sha1obj.update(f.read())
return sha1obj.hexdigest()
@property
def hash(self):
return self.sha1
class Iter:
def __init__(self, obj):
self.files = obj.files
self.index = 0
self.end = len(self.files)
def __next__(self):
if self.index < self.end:
ret = self.files[self.index]
self.index += 1
return ret
else:
raise StopIteration
def __call__(self, relPath):
if not isinstance(relPath, str):
raise TypeError(f"The key must be a string, not '{relPath}' ({type(relPath)})")
return self.append(relPath)
def __getitem__(self, key):
return self.__call__(key)
def __add__(self, other):
return self.__call__(other)
def __contains__(self, item):
return os.path.exists(self.append(item).path)
def __len__(self):
return len(self.files)
def __iter__(self):
return self.Iter(self)
def __repr__(self):
return str(__class__)+': '+self.name