This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathihih.py
515 lines (380 loc) · 10.9 KB
/
ihih.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/python
'''ihih - simple configuration parsers with dictionary-like interface
:Source code: `GitHub project <https://github.com/romain-dartigues/python-ihih>`_
:License: `BSD 3-Clause <http://opensource.org/licenses/BSD-3-Clause>`_
'''
import re
import os
__version__ = '0.1.1dev'
class IHIH(dict):
'''IHIH - simple configuration parser
One key/value pair per line.
'''
ignore_IOError = False
'''do not stop on IOError when reading sources'''
encoding = 'utf8'
'''define the encoding'''
_escape = r'(?<!\\)(?:\\\\)*'
'''regexp definition of the escape sequence'''
_escaped_chars = r'[\\\'\"\#/\\]'
'''regexp definition of characters to unconditionally un-escape'''
_escaped = r'(?:\\)(?P<char>%s)'
_comment = r'(\s*%(escape)s(?:\#|//))'
'''regexp definition of an in-line comment'''
_separator = r'\='
'''regexp definition of key/value separator
Must be a fixed-width expression.'''
_extract = r'''^\s*
(?P<key>.+?)
\s*%(separator)s\s*
(?P<value>.*)'''
'''extract ``key = [value]`` on a single line'''
_quote = r'["\']'
'''define what a quote might be'''
_quoted = r'%(escape)s(?P<quote>%(quote)s)(?P<value>.*?)%(escape)s(?P=quote)'
'''how to find a quoted value'''
_bool = r'^(?P<false>0|no|false|off|disabled)|(?P<true>1|yes|true|on|enabled)$'
'regexp definition of a boolean value (used by :meth:`get_bool`)'
def __init__(self, filenames, _ignore_IOError=False, *args, **kwargs):
'''attempt to parse a list of filenames
Parameters:
- `filenames` -- if is a string, it is treated as a single
file, otherwise it is treated as an iterable
- `_ignore_IOError` -- fail silently on IOError
- other parameters are passed to the :py:class:`dict` constructor
'''
dict.__init__(self, *args, **kwargs)
self._comment = self._comment % {
'escape': self._escape,
}
self.r_comment = re.compile(self._comment)
self.r_extract = re.compile(
self._extract % {
'separator': self._separator,
},
re.X
)
self.r_quoted = re.compile(
self._quoted % {
'escape': self._escape,
'quote': self._quote,
}
)
self.r_escaped = re.compile(
self._escaped % self._escaped_chars
)
self.r_bool = re.compile(self._bool, re.I)
if isinstance(filenames, basestring):
self.__source = (os.path.realpath(filenames),)
else:
self.__source = tuple(
os.path.realpath(filename)
for filename in filenames
)
self.__mtime = {}
self.ignore_IOError = _ignore_IOError
self.reload()
def reload(self, force=False, ignore_IOError=None):
'''call :meth:`parse` on each configuration file'''
for filename in self.__source:
self.parse(filename, force, ignore_IOError)
def parse(self, filename, force=False, ignore_IOError=None):
'''parse a configuration file
.. Note::
`filename` should be an absolute path.
'''
if ignore_IOError is None:
ignore_IOError = self.ignore_IOError
try:
mtime = os.stat(filename).st_mtime
except OSError:
# file not found / permission denied...
return False
if mtime <= self.__mtime.get(filename, 0) and not force:
# file did not change
return None
try:
fo = open(filename)
except IOError:
if ignore_IOError:
return False
raise
for line in fo:
results = self.r_extract.match(line)
if results:
self[results.group('key')] = results \
.group('value').rstrip()
self.__mtime[filename] = mtime
return True
def _unescape(self, value, quote=None):
'''remove escape prefix on "known escape"
See :attr:`_escaped_chars`.
This method attempt to utf8 encode :py:func:`unicode` objects.
'''
data = bytearray()
escaped = None
prev = 0
if isinstance(value, unicode):
enc = lambda s: s.encode(self.encoding)
else:
enc = lambda s: s
for escaped in self.r_escaped.finditer(value):
if escaped.start() > prev:
data+= enc(value[prev:escaped.start()])
data+= enc(escaped.group('char'))
prev = escaped.end()
if escaped and escaped.end() < len(value):
data+= enc(value[escaped.end():])
elif not escaped:
data+= enc(value)
return data
def _handle_fragment(self, fragment, quote=None):
'''handle a fragment of a value
Provided to help on subclassing.'''
return self._unescape(fragment, quote)
def _comment_at(self, value):
'return the position of the begining of a comment'
comment = self.r_comment.search(value)
return comment and comment.start()
def _parse_value(self, value, data):
'''parse the "value" part of a "key / value"
This function handle the quoted parts and the comments.
Parameters:
- `value` (:py:func:`basestring` instance): value to parse
- `data`: instance supporting ``+=`` operator
'''
quoted = None
prev = 0
for quoted in self.r_quoted.finditer(value):
if quoted.start() > prev:
# unquoted part before a quoted fragment
comment_at = self._comment_at(
value[prev:quoted.start()]
)
data+= self._handle_fragment(
value[
prev
:
quoted.start()
if comment_at is None
else comment_at
]
)
if comment_at is not None:
break
# quoted fragment
data+= self._handle_fragment(
quoted.group('value'),
quoted.group('quote')
)
prev = quoted.end()
if quoted and quoted.end() < len(value):
# there is unquoted string after the quoted one
data+= self._handle_fragment(
value[
quoted.end()
:
self._comment_at(value[quoted.end():])
]
)
elif not quoted:
# nothing was quoted
data+= self._handle_fragment(
value[:self._comment_at(value)]
)
return data
def _cast_str(self, value):
'''return a string representation of `value`'''
if isinstance(value, str):
return value
if isinstance(value, unicode):
return value.encode(self.encoding)
return str(value)
def __contains__(self, key):
'''True if self contains `key`
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
return dict.__contains__(self, self._cast_str(key))
def __setitem__(self, key, value):
'''set item `key` to `value`
.. Note::
Both variables will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
return dict.__setitem__(
self,
self._cast_str(key),
self._parse_value(
value
if isinstance(value, basestring)
else str(value),
bytearray()
)
)
def __getitem__(self, key):
'''return `key` value as internal type
You probably want to use one of the following:
:meth:`get_str`, :meth:`get_unicode`, :meth:`get_float`,
:meth:`get_int`.
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
return dict.__getitem__(self, self._cast_str(key))
def __delitem__(self, key):
'''delete `key` from dict
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
return dict.__delitem__(self, self._cast_str(key))
def get_str(self, key, default=None):
'''return `key` value as :py:func:`str`
or `default` if not found
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
if key in self:
return str(self[key])
return default
get = get_str
'''alias to :meth:`get_str`'''
def get_unicode(self, key, default=None, errors='strict'):
'''return `key` value as :py:func:`unicode` or `default`
if not found
The `errors` parameter is passed to :py:meth:`str.decode`.
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
if key in self:
return self[key].decode(self.encoding, errors)
return default
def get_float(self, key, default=None, errors='strict'):
'''return `key` value as :py:func:`float` or `default`
if not found
If `errors` is "ignore", return `default` value instead of
raising :py:exc:`~exceptions.TypeError` on failure.
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
if key in self:
try:
return float(self[key])
except TypeError:
if errors != 'ignore':
raise
return default
def get_int(self, key, default=None, errors='strict', base=10):
'''return `key` value as :py:func:`int` or `default`
if not found
If `errors` is "ignore", return `default` value instead of
raising :py:exc:`~exceptions.TypeError` on failure.
.. Note::
The `key` will be casted as :py:func:`str`
(see: :meth:`_cast_str`).
'''
if key in self:
try:
return int(self.get_str(key), base)
except TypeError:
if errors != 'ignore':
raise
return default
def get_bool(self, key, default=None):
'''attempt to coerce `key` value to a boolean
accordingly :attr:`_bool` rules
'''
if key in self:
match = self.r_bool.search(self.get_str(key))
if match:
return True if match.group('true') else False
return default
class IHIHI(IHIH):
'''IHIH Interpolate - :class:`IHIH` with variable interpolation
'''
_variable = r'%(escape)s\$(?P<value>\w+|%(escape)s\{(?P<unquoted>.+?)%(escape)s\})'
'''regexp definition of a "variable"'''
_escaped_chars = r'[\\\'\"\#/\\\$\{\}]'
def __init__(self, *args, **kwargs):
self.r_variable = re.compile(
self._variable % {'escape': self._escape},
re.UNICODE
)
super(IHIHI, self).__init__(*args, **kwargs)
def __setitem__(self, key, value):
return dict.__setitem__(
self,
self._cast_str(key),
self._parse_value(
value
if isinstance(value, basestring)
else str(value),
[]
)
)
def _handle_fragment(self, fragment, quote=None):
'''search for variables in `fragment`'''
var = None
data = []
prev = 0
if quote in (None, '"'):
for var in self.r_variable.finditer(fragment):
if var.start() > prev:
data.append((
self._unescape(
fragment[prev:var.start()],
quote
),
False
))
data.append((
self._unescape(
var.group('unquoted')
if var.group('unquoted')
else var.group('value')
),
True
))
prev = var.end()
if var and var.end() < len(fragment):
data.append((self._unescape(fragment[var.end():], quote), False))
elif not prev:
data = ((self._unescape(fragment, quote), False),)
return data
def __getkey(self, key, path=None):
'''return `key` value as internal type
with interpolated variables
For more informations, see: :meth:`~IHIH.__getitem__`.
'''
if path is None:
path = []
path.append(key)
data = super(IHIHI, self).__getitem__(key)
value = bytearray()
for sub, is_variable in data:
if is_variable:
if sub not in self:
continue
if sub in path:
value+= self._recursive(sub)
else:
value+= self.__getkey(sub, path)
else:
value+= sub
path.remove(key)
return value
def __getitem__(self, key):
return self.__getkey(key)
def _recursive(self, value):
'''recursive variable handler
Default: empty string
You can overwrite this function when subclassing
and chose to return a unexpended version of the variable,
raise an error or make a single, non recursive, lookup.
'''
return ''