This repository was archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
325 lines (264 loc) · 9.24 KB
/
Copy pathutil.py
File metadata and controls
325 lines (264 loc) · 9.24 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
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
315
316
317
318
319
320
321
322
323
324
325
### util.py --- Miscellaneous utilities
## Copyright (C) 2005, 2006 Brailcom, o.p.s.
##
## Author: Milan Zamazal <pdm@brailcom.org>
##
## COPYRIGHT NOTICE
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the Free
## Software Foundation; either version 2 of the License, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
## more details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import copy
import os
import re
import charseq
# Very basic utilities which should be present in any standard language library
def sort (sequence, compfunc=None):
"""Return a sorted copy of 'sequence'.
Use 'compfunc' if given as in 'list.sort'.
"""
sequence_copy = copy.copy (sequence)
sequence_copy.sort (compfunc)
return sequence_copy
def remove (sequence, object):
"""Return a copy of 'sequence' with 'object' element removed.
"""
sequence_copy = list (copy.copy (sequence))
sequence_copy.remove (object)
if isinstance (sequence, tuple):
sequence_copy = tuple (sequence_copy)
return sequence_copy
def if_ (condition, then_expression, else_expression):
"""Very limited emulation of the IF operator.
"""
if condition:
result = then_expression
else:
result = else_expression
return result
def class_name (object):
"""Return class name of 'object's class as a string.
The string includes full module qualification.
If 'object' is not a class instance, return None.
If 'object' is a class itself, return its name.
"""
if not hasattr (object, '__class__'):
return None
if hasattr (object, '__mro__'):
cls = object
else:
cls = object.__class__
module_name = cls.__module__
class_name = cls.__name__
if module_name != '__builtin__':
class_name = module_name + '.' + class_name
return class_name
def is_sequence (object):
"""Return true iff 'object' is a tuple or a list.
"""
return isinstance (object, tuple) or isinstance (object, list)
def concatenate (*sequences):
"""Return concatenation of 'sequences'.
"""
result = []
for s in sequences:
result = result + list (s)
return result
def str_ (object):
"""Same as 'charseq.str' except it applies 'str_' to sequence elements too.
"""
if isinstance (object, list):
result = '['
for x in object[:1]:
result = result + str_ (x)
for x in object[1:]:
result = result + ', ' + str_ (x)
result = result + ']'
elif isinstance (object, tuple):
result = '('
for x in object[:1]:
result = result + str_ (x) + ','
for x in object[1:]:
result = result + ' ' + str_ (x) + ','
result = result + ')'
else:
result = charseq.str (object)
return result
class Variable (object):
"""Simple value holder.
Useful for civilized access to non-local values in local functions.
"""
def __init__ (self, value):
self._value = value
def get (self):
"""Return value.
"""
return self._value
def set (self, value):
"""Set value to 'value'.
"""
self._value = value
def __nonzero__ (self):
"""Return nonzero value of the stored value.
"""
if self.get ():
return True
else:
return False
undefined_argument = object ()
"""Used for default optional argument values.
It indicates no value was given to the argument.
"""
# File utilities
def directory (directory, pattern=None):
"""Return base names of all files in 'directory'.
If 'pattern' is not None, it is a string containing regular expression and
only the files matching the regular expression are returned.
"""
regexp = pattern and re.compile (pattern)
return [f for f in os.listdir (directory) if not regexp or regexp.match (f)]
def copy_file (source_file_name, target_file_name):
"""Copy file 'source_file_name' to 'target_file_name'.
"""
in_ = open (source_file_name, 'r')
out = open (target_file_name, 'w')
while True:
data = in_.read (65536)
if not data:
break
out.write (data)
out.close ()
in_.close ()
def rename_file (original_file_name, new_file_name):
"""Rename file 'original_file_name' to 'new_file_name'.
Unlike os.remove, this function works accross file systems.
"""
try:
os.rename (original_file_name, new_file_name)
except OSError:
copy_file (original_file_name, new_file_name)
os.remove (original_file_name)
def read_stream (stream):
"""Read the whole stream and return the read data as a string.
"""
data = ''
while True:
next_data = stream.read ()
if not next_data:
break
data = data + next_data
return data
# Very primitive generic function emulation
class No_Applicable_Method_Exception (Exception):
"""Raised when no applicable method is found in a generic function call.
"""
class defgeneric (object):
"""Generic function emulation.
By creating an instance of this class, you define new generic function.
Generic function methods can be created using the 'defmethod' method.
You can invoke the generic function by calling the class instance.
"""
def __init__ (self):
self._methods = {}
self._cache = {}
def defmethod (self, function, classes):
"""Define 'function' as the method for args specified by 'classes'.
'classes' must be a non-empty sequence of classes corresponding to the
'function' arguments.
"""
classes = tuple (classes)
methods = self._methods
for c in classes[:-1]:
try:
methods = methods[c]
except KeyError:
methods = methods[c] = {}
methods[classes[-1]] = function
self._cache = {}
def __call__ (self, *args):
"""Call the generic function with 'args'.
The first argument is considered to be most important for determining
the corresponding method, the second argument the second most
important, etc.
If no applicable method is found 'No_Applicable_Method_Exception' is
raised.
"""
method = self._method (args)
return method (*args)
def _method (self, args):
classes = tuple ([a.__class__ for a in args])
method = self._cache.get (classes)
if method is None:
method = self._find_method (classes)
self._cache[classes] = method
return method
def _find_method (self, classes):
def matching_methods (cls, methods):
for c in cls.__mro__:
if methods.has_key (c):
return methods[c]
return None
methods = self._methods
for c in classes:
methods = matching_methods (c, methods)
if methods is None:
raise No_Applicable_Method_Exception (classes)
elif type (methods) != type ({}):
return methods
raise No_Applicable_Method_Exception (classes)
# Simple data structures
class Structure (object):
"""Simple data structures.
Attribute names of the instance are listed in the sequence '_attributes'.
Each element of '_attributes' is a tuple of the form
(attribute_name, documentation, default_value). default_value may be
omitted, in such a case the attribute value must be provided to the
constructor call.
"""
_attributes = ()
def __init__ (self, **args):
for a in self._attributes:
name = a[0]
if len (a) > 2:
if args.has_key (name):
value = args[name]
else:
value = a[2]
else:
value = args[name]
setattr (self, name, value)
def __str__ (self):
result = '<%s:' % (self.__class__.__name__,)
for a in self._attributes:
name = a[0]
result = result + (' %s=%s;' % (name, str_ (getattr (self, name)),))
result = result + '>'
return result
class Enumeration (object):
"""Object containing only constant attributes.
The attribute names are specified in the constructor call.
"""
def __init__ (self, _documentation, *constants):
"""Create new enumeration instance.
'documentation' is the enumeration documentation.
'constants' is a list of pairs
(constant_name, constant_documentation,).
"""
self._constants = constants
for c in constants:
name = c[0]
setattr (self, name, name)
def all_values (self):
"""Return sequence of all enumeration constants.
"""
return [c[0] for c in self._constants]