-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.py
314 lines (219 loc) · 8.26 KB
/
directory.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Classes implimenting the simple filesystem syntax.
"""
import getpass
import os
import settings
from webnote import Webnote
class Directory(Webnote):
"""Provide directory services.
Services include generating lists of files by type as (link, text)
tuples. Has property attributes to provide lists of figures,
hi-res images, documents and other files by type.
The attribute baseurl is used to construct urls.
"""
address = None
dirpath = None
model = None
baseurl = None
sort = None
def __init__(self, dirpath, docroot=None, baseurl=None, sort=True):
"""Create a Directory object from a string path to directory.
The address is computed by cutting the docroot from the dirpath.
"""
if not os.path.isdir(dirpath):
raise self.ParseDirNotFound(dirpath)
if docroot:
if docroot[-1] == '/':
docroot = docroot[:-1]
if dirpath == docroot:
self.docroot = ''
self.address = ''
else:
self.docroot = docroot
self.address = dirpath.replace(docroot, '')
if baseurl:
self.baseurl = baseurl
self.dirpath = dirpath
self.sort = sort
self.model = self._parse_directory(dirpath)
class ParseDirNotFound(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def __unicode__(self):
return self.dirpath
def _parse_directory(self, dirpath):
"""Return a dictionary containing lists of files by type.
It looks for keys in the settings.SUFFIX variable. The output
dictionary will have elemements corresponding to the keys of
this dictionary. It will also contain elements "dirs",
"hidden" and "all".
Hidden files start with a period. Temporary files end with a
tilde.
"""
if not os.path.isdir(dirpath):
raise self.ParseDirNotFound(dirpath)
output = {
'dirs': [],
'hidden': [],
'temp': [],
'unknown': [],
}
for key in settings.SUFFIX:
output[key] = []
if self.sort:
listing = sorted(os.listdir(dirpath))
else:
listing = os.listdir(dirpath)
for item in listing:
if item[0] == '.':
output['hidden'].append(item)
elif item[-1] == '~':
output['temp'].append(item)
elif os.path.isdir(os.path.join(dirpath, item)):
output['dirs'].append(item)
else:
basename, ext = os.path.splitext(item)
found = False
for key in settings.SUFFIX:
if ext.lower() in settings.SUFFIX[key]:
output[key].append(item)
found = True
if not found:
output['unknown'].append(item)
output['all'] = listing
return output
def all_files(self, baseurl=None):
"""Return a list of (link, text) tuples identifying all files."""
targets = []
for item in self.model['all']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def clean_name(self):
steps = self.dirpath.split('/')
last = steps.pop()
name = last.replace('_', '')
if last == 'www':
name = '/home/' + getpass.getuser() + '/www/'
return name
def datafiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying data files."""
targets = []
for item in self.model['data']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def documents(self, baseurl=None):
"""Return a list of (link, text) tuples identifying document files."""
targets = []
for item in self.model['docs']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def figures(self, baseurl=''):
"""Return a list of (link, text) tuples identifying figure files."""
targets = []
if not baseurl:
if self.baseurl:
baseurl = self.baseurl
for item in self.model['figures']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def hiddenfiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying hidden files."""
targets = []
for item in self.model['hidden']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def hiresimages(self, baseurl=None):
"""Return a list of (link, text) tuples identifying image files."""
images = []
for item in self.model['img_hires']:
link = os.path.join(baseurl, self.address, item)
text = item
images.append((link, text))
return images
def htmlfiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying all html files."""
targets = []
for item in self.model['html']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def metafiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying meta files."""
targets = []
for item in self.model['']:
link = os.path.join(baseurl, item)
text = item
targets.append((link, text))
return targets
def pages(self, baseurl=None, suffix=None):
"""Return a list of (link, text) tuples identifying page files."""
targets = []
address = ''
if self.address:
address = self.address
if not baseurl:
if self.baseurl:
baseurl = os.path.join(self.baseurl, address)
else:
baseurl = ''
for item in self.model['page']:
(basename, ext) = os.path.splitext(item)
link = os.path.join(baseurl, basename) + '/'
if suffix:
text = item
else:
text = basename.replace('_', ' ')
targets.append((link, text))
return targets
def reference_text(self, text, baseurl=None):
"""Return an alterted text, and unreferenced figures.
Return a tuple: reftext, unref_figs, using the
reference_figures() method in the superclass. Requires only
the text to be processed, if the directory object has been
created with a baseurl.
"""
reftext = text
figures = []
if not baseurl:
baseurl = self.baseurl
for t in self.figures():
figures.append(t[0])
reftext, unref_figs = self.reference_figures(text, baseurl, figures)
return reftext, unref_figs
def tempfiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying temporary files."""
targets = []
for item in self.model['']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def textfiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying text files."""
targets = []
for item in self.model['text']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets
def unknownfiles(self, baseurl=None):
"""Return a list of (link, text) tuples identifying files."""
targets = []
for item in self.model['unknown']:
link = os.path.join(baseurl, self.address, item)
text = item
targets.append((link, text))
return targets