-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_helpers.py
234 lines (188 loc) · 7.46 KB
/
file_helpers.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
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
from pathlib import Path
import hashlib
class BaseFile:
"""
Base class for all file objects. Not to be used directly.
Use HTMLfile, CSSfile, JSfile or OtherFile instead.
"""
# pylint: disable=too-many-instance-attributes
# Eight is reasonable in this case.
def __init__(self, path, config):
self._config = config
self._path = Path(path).relative_to(self._config['src_dir'])
self._stem = str(self._path.stem)
self._extension = str(self._path.suffix[1:]).lower()
self._filename = str(self._stem + '.' + self._extension)
self._parent = self._path.parent
self._src_path = self._config['src_dir'] / self._path
self._build_path = self._config["build_dir"] / self._path
self._upload_URL = None # URL of the upload form for file
self._link_URL = None # URL where the file will live
@property
def path(self):
""" Path of the file relative to src_dir. """
return self._path
# @property
# def config(self):
# """ Run configuration. """
# return self._config
@property
def filename(self):
''' Filename with extension. '''
return self._filename
@property
def extension(self):
''' File extension. '''
return self._extension
# @property
# def parent(self):
# ''' Path without the filename and terminal /. '''
# return self._parent
@property
def src_path(self):
''' Build path relative to src_dir. '''
return self._src_path
@property
def build_path(self):
''' Build path relative to build_dir. '''
return self._build_path
@property
def upload_URL(self):
''' URL of the upload form for this file. '''
return self._upload_URL
@property
def link_URL(self):
''' URL which can be used to link to this file. '''
return self._link_URL
class HTMLfile(BaseFile):
""" HTML file object. """
def __init__(self, path, config):
BaseFile.__init__(self, path, config)
self._upload_path = self._generate_upload_path()
self._upload_URL = self._generate_upload_URL()
self._link_URL = self._generate_link_URL()
def _generate_upload_path(self):
"""
Returns upload path, which is the part of the URL after team name
but before & and all. Includes / if required.
"""
# remove /index.html
upload_path = str(self.path.parent)
if upload_path == ".":
return ""
else:
return "/" + upload_path
def _generate_upload_URL(self):
"""
Returns the URL of the iGEM page where this file can be uploaded.
Private function. Use upload_URL to access instead.
"""
return 'https://2020.igem.org/wiki/index.php?title=Team:' + \
self._config["team"] + self._upload_path + '&action=edit'
def _generate_link_URL(self):
"""
Returns the iGEM URL where this page will be found and can be linked to.
Private function. Use link_URL to access instead.
"""
return 'https://2020.igem.org/Team:' + self._config['team'] + \
self._upload_path
class CSSfile(BaseFile):
def __init__(self, path, config):
BaseFile.__init__(self, path, config)
self._upload_path = self._generate_upload_path()
self._upload_URL = self._generate_upload_URL()
self._link_URL = self._generate_link_URL()
def _generate_upload_path(self):
"""
Returns upload path, which is the part of the URL after team name
but before & and ?. Includes / if required.
"""
# remove file extension
upload_path = self.path.parent / self.path.stem
# add 'CSS'
upload_path = str(upload_path).replace('.', '-') + 'CSS'
return "/" + upload_path
def _generate_upload_URL(self):
"""
Returns the URL of the iGEM page where this file can be uploaded.
Private function. Use upload_URL to access instead.
"""
return 'https://2020.igem.org/wiki/index.php?title=Template:' + self._config['team'] + \
self._upload_path + '&action=edit'
def _generate_link_URL(self):
"""
Returns the iGEM URL where this page will be found and can be linked to.
"""
return 'https://2020.igem.org/Template:' + self._config['team'] + \
self._upload_path + '?action=raw&ctype=text/css'
class JSfile(BaseFile):
def __init__(self, path, config):
BaseFile.__init__(self, path, config)
self._upload_path = self._generate_upload_path()
self._upload_URL = self._generate_upload_URL()
self._link_URL = self._generate_link_URL()
def _generate_upload_path(self):
"""
Returns upload path, which is the part of the URL after team name
but before & and all. Includes / if required.
"""
# remove file extension
upload_path = self.path.parent / self.path.stem
# add 'CSS'
upload_path = str(upload_path).replace('.', '-') + 'JS'
return "/" + upload_path
def _generate_upload_URL(self):
"""
Returns the URL of the iGEM page where this file can be uploaded.
Private function. Use upload_URL to access instead.
"""
return 'https://2020.igem.org/wiki/index.php?title=Template:' + self._config['team'] + \
self._upload_path + '&action=edit'
def _generate_link_URL(self):
"""
Returns the iGEM URL where this page will be found and can be linked to.
"""
return 'https://2020.igem.org/Template:' + self._config['team'] + \
self._upload_path + '?action=raw&ctype=text/javascript'
class OtherFile(BaseFile):
def __init__(self, path, config):
BaseFile.__init__(self, path, config)
self._upload_URL = "https://2020.igem.org/Special:Upload"
self._upload_filename = self._generate_upload_filename()
self._md5_hash = self._generate_md5_hash()
@property
def upload_filename(self):
''' Filename on iGEM servers. '''
return self._upload_filename
# Only OtherFile objects have this property because
# md5 hashes of other objects are hashes of the modified
# content, while OtherFiles don't require any modification.
# Besides, OtherFile md5_hash is a file hash while other
# hashes are computed on strings (modified file contents)
@property
def md5_hash(self):
''' MD5 hash of the file. '''
return self._md5_hash
def _generate_upload_filename(self):
if len(self._config['assets']) == 1:
return 'T--' + self._config['team'] + '--' + '--'.join(self.path.parts[1:])
else:
return 'T--' + self._config['team'] + '--' + '--'.join(self.path.parts)
def _generate_md5_hash(self):
"""
Returns the MD5 hash of the file
"""
# make a hash object
h = hashlib.sha1()
# open file for reading in binary mode
with open(self.src_path, 'rb') as file:
# loop till the end of the file
chunk = 0
while chunk != b'':
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)
# return the hex representation of digest
return h.hexdigest()
def set_upload_URL(self, url):
self._upload_URL = url