-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
162 lines (97 loc) · 3.86 KB
/
files.py
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
"""
"""
import os
import io
import re
from config import *
class Files():
def _prepare_file_name(self, file):
return file + ".waiting"
def _append_file_name(self, file):
return file + ".writing"
def _is_file(self, file):
return os.path.isfile(file)
def _get_size(self, file):
return os.path.getsize(file)
def _remove_file(self, file):
return os.remove(file)
def _rename_file(self, ofile, nfile):
return os.rename(ofile, nfile)
def _path_exist(self, path):
return os.path.exists(path)
def _make_dir(self, dir):
return os.makedirs(dir)
def _files(self, type = 'list'):
path = self.full_path()
r = []
if os.path.isdir(path):
for f in os.listdir(path):
file_path = self.full_path(f)
if self._is_file(file_path):
if type == 'size':
r.append(self._get_size(file_path))
else:
r.append(f)
return r
def full_path(self, file=None):
return os.path.join(PATH, file) if file != None else PATH
def file_name_check(self, name):
return re.findall(r'[^A-Za-z0-9_\-\\]',name)
def file_write(self, file, data, type='wt', seek=None): #w-write, a-append, t-text, b-binary
try:
with io.open(self.full_path(file), type) as f:
if not seek == None:
f.seek(int(seek))
f.write(unicode(data))
return True
except IOError as e:
return False
def file_read(self, file, type='rt', rtype='read'): #r-read, t-text, b-binary
with open(self.full_path(file), type) as f:
return f.readlines() if rtype == 'lines' else f.read() #f.readline()
def file_check(self, file, size = 1):
file_path = self.full_path(file)
if self._is_file(file_path):
if self._get_size(file_path) >= int(size):
return True
return False
def dir_check(self, path):
if not self._path_exist(path):
self._make_dir(path)
return self._path_exist(path)
def prepare_file(self, file, size):
file = self._prepare_file_name(file)
self.file_write(file, b"\0", 'wb', int(size)-1)
return self.file_check(file, int(size))
def append_file(self, file, data, seek):
prepare_file = self._prepare_file_name(file)
append_file = self._append_file_name(file)
if self.file_check(prepare_file):
self.rename_file(prepare_file, append_file)
if not self.file_check(append_file):
return 'FAIL'
if not self.file_write(append_file, data, 'r+b', seek):
return 'FAIL'
if self.get_file_size(append_file) == len(data) + seek:
self.rename_file(append_file, file)
return 'DONE'
return 'WAIT'
def is_prepared_file(self, file, size):
return self.file_check(self._prepare_file_name(file), size) or self.file_check(self._append_file_name(file), size)
def get_files_list(self):
return self._files('list')
def get_file_size(self, file):
file_path = self.full_path(file)
return self._get_size(file_path)
def rename_file(self, ofile, nfile):
ofile = self.full_path(ofile)
nfile = self.full_path(nfile)
return self._rename_file(ofile, nfile)
def get_files_size(self):
return sum(self._files('size'))
def remove_file(self, file):
file_path = self.full_path(file)
if self._is_file(file_path):
self._remove_file(file_path)
return True
return False