[50fdf12] | 1 | # |
---|
| 2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
[b0fa2ae] | 3 | # Copyright 2010-2016 Chris Johns (chrisj@rtems.org) |
---|
[50fdf12] | 4 | # All rights reserved. |
---|
| 5 | # |
---|
| 6 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
| 7 | # |
---|
| 8 | # Redistribution and use in source and binary forms, with or without |
---|
| 9 | # modification, are permitted provided that the following conditions are met: |
---|
| 10 | # |
---|
| 11 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
| 12 | # this list of conditions and the following disclaimer. |
---|
| 13 | # |
---|
| 14 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | # this list of conditions and the following disclaimer in the documentation |
---|
| 16 | # and/or other materials provided with the distribution. |
---|
| 17 | # |
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 22 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 28 | # POSSIBILITY OF SUCH DAMAGE. |
---|
| 29 | # |
---|
| 30 | |
---|
| 31 | # |
---|
| 32 | # Macro tables. |
---|
| 33 | # |
---|
| 34 | |
---|
[b0fa2ae] | 35 | from __future__ import print_function |
---|
| 36 | |
---|
[50fdf12] | 37 | import copy |
---|
[b7d48ef] | 38 | import inspect |
---|
[50fdf12] | 39 | import re |
---|
| 40 | import os |
---|
| 41 | import string |
---|
| 42 | |
---|
[7e5cdea] | 43 | from rtemstoolkit import error |
---|
| 44 | from rtemstoolkit import log |
---|
| 45 | from rtemstoolkit import path |
---|
[50fdf12] | 46 | |
---|
| 47 | # |
---|
| 48 | # Macro tables |
---|
| 49 | # |
---|
| 50 | class macros: |
---|
| 51 | |
---|
| 52 | class macro_iterator: |
---|
| 53 | def __init__(self, keys): |
---|
| 54 | self.keys = keys |
---|
| 55 | self.index = 0 |
---|
| 56 | |
---|
| 57 | def __iter__(self): |
---|
| 58 | return self |
---|
| 59 | |
---|
[b0fa2ae] | 60 | def __next__(self): |
---|
[50fdf12] | 61 | if self.index < len(self.keys): |
---|
| 62 | key = self.keys[self.index] |
---|
| 63 | self.index += 1 |
---|
| 64 | return key |
---|
| 65 | raise StopIteration |
---|
| 66 | |
---|
| 67 | def iterkeys(self): |
---|
| 68 | return self.keys |
---|
| 69 | |
---|
[b0fa2ae] | 70 | def _unicode_to_str(self, us): |
---|
| 71 | try: |
---|
| 72 | if type(us) == unicode: |
---|
| 73 | return us.encode('ascii', 'replace') |
---|
| 74 | except: |
---|
| 75 | pass |
---|
| 76 | try: |
---|
| 77 | if type(us) == bytes: |
---|
| 78 | return us.encode('ascii', 'replace') |
---|
| 79 | except: |
---|
| 80 | pass |
---|
| 81 | return us |
---|
| 82 | |
---|
[50fdf12] | 83 | def __init__(self, name = None, original = None, rtdir = '.'): |
---|
| 84 | self.files = [] |
---|
| 85 | self.macro_filter = re.compile(r'%{[^}]+}') |
---|
| 86 | if original is None: |
---|
| 87 | self.macros = {} |
---|
| 88 | self.read_maps = [] |
---|
| 89 | self.read_map_locked = False |
---|
| 90 | self.write_map = 'global' |
---|
[b7d48ef] | 91 | self.rtpath = path.abspath(path.dirname(inspect.getfile(macros))) |
---|
| 92 | if path.dirname(self.rtpath).endswith('/share/rtems'): |
---|
| 93 | self.prefix = path.dirname(self.rtpath)[:-len('/share/rtems')] |
---|
| 94 | else: |
---|
| 95 | self.prefix = '.' |
---|
[50fdf12] | 96 | self.macros['global'] = {} |
---|
| 97 | self.macros['global']['nil'] = ('none', 'none', '') |
---|
[b7d48ef] | 98 | self.macros['global']['_cwd'] = ('dir', |
---|
| 99 | 'required', |
---|
| 100 | path.abspath(os.getcwd())) |
---|
| 101 | self.macros['global']['_prefix'] = ('dir', 'required', self.prefix) |
---|
| 102 | self.macros['global']['_rtdir'] = ('dir', |
---|
| 103 | 'required', |
---|
| 104 | path.abspath(self.expand(rtdir))) |
---|
| 105 | self.macros['global']['_rttop'] = ('dir', 'required', self.prefix) |
---|
[50fdf12] | 106 | else: |
---|
| 107 | self.macros = {} |
---|
| 108 | for m in original.macros: |
---|
| 109 | if m not in self.macros: |
---|
| 110 | self.macros[m] = {} |
---|
| 111 | for k in original.macros[m]: |
---|
| 112 | self.macros[m][k] = copy.copy(original.macros[m][k]) |
---|
| 113 | self.read_maps = sorted(copy.copy(original.read_maps)) |
---|
| 114 | self.read_map_locked = copy.copy(original.read_map_locked) |
---|
| 115 | self.write_map = copy.copy(original.write_map) |
---|
| 116 | if name is not None: |
---|
| 117 | self.load(name) |
---|
| 118 | |
---|
| 119 | def __copy__(self): |
---|
| 120 | return macros(original = self) |
---|
| 121 | |
---|
| 122 | def __str__(self): |
---|
| 123 | text_len = 80 |
---|
| 124 | text = '' |
---|
| 125 | for f in self.files: |
---|
| 126 | text += '> %s%s' % (f, os.linesep) |
---|
| 127 | for map in self.macros: |
---|
[bf58911] | 128 | rm = '-' |
---|
| 129 | for rmap in self.read_maps: |
---|
| 130 | if rmap[4:] == '___%s' % (map): |
---|
| 131 | if self.read_map_locked: |
---|
| 132 | rm = 'R[%s]' % (rmap[:4]) |
---|
| 133 | else: |
---|
| 134 | rm = 'r[%s]' % (rmap[:4]) |
---|
| 135 | break |
---|
[50fdf12] | 136 | if map == self.write_map: |
---|
| 137 | wm = 'w' |
---|
| 138 | else: |
---|
| 139 | wm = '-' |
---|
[bf58911] | 140 | text += '[%s] %s,%s%s' % (map, wm, rm, os.linesep) |
---|
[50fdf12] | 141 | for k in sorted(self.macros[map].keys()): |
---|
| 142 | d = self.macros[map][k] |
---|
| 143 | text += " %s:%s '%s'%s '%s'%s" % \ |
---|
| 144 | (k, ' ' * (20 - len(k)), |
---|
| 145 | d[0], ' ' * (8 - len(d[0])), |
---|
| 146 | d[1], ' ' * (10 - len(d[1]))) |
---|
| 147 | if len(d[2]) == 0: |
---|
| 148 | text += "''%s" % (os.linesep) |
---|
| 149 | else: |
---|
| 150 | if '\n' in d[2]: |
---|
| 151 | text += "'''" |
---|
| 152 | else: |
---|
| 153 | text += "'" |
---|
| 154 | indent = False |
---|
| 155 | ds = d[2].split('\n') |
---|
| 156 | lc = 0 |
---|
| 157 | for l in ds: |
---|
| 158 | lc += 1 |
---|
| 159 | while len(l): |
---|
| 160 | if indent: |
---|
| 161 | text += ' %21s %10s %12s' % (' ', ' ', ' ') |
---|
| 162 | text += l[0:text_len] |
---|
| 163 | l = l[text_len:] |
---|
| 164 | if len(l): |
---|
| 165 | text += ' \\' |
---|
| 166 | elif lc == len(ds): |
---|
| 167 | if len(ds) > 1: |
---|
| 168 | text += "'''" |
---|
| 169 | else: |
---|
| 170 | text += "'" |
---|
| 171 | text += '%s' % (os.linesep) |
---|
| 172 | indent = True |
---|
| 173 | return text |
---|
| 174 | |
---|
| 175 | def __iter__(self): |
---|
| 176 | return macros.macro_iterator(self.keys()) |
---|
| 177 | |
---|
| 178 | def __getitem__(self, key): |
---|
| 179 | macro = self.get(key) |
---|
| 180 | if macro is None or macro[1] == 'undefine': |
---|
| 181 | raise IndexError('key: %s' % (key)) |
---|
| 182 | return macro[2] |
---|
| 183 | |
---|
| 184 | def __setitem__(self, key, value): |
---|
[2de37f3] | 185 | key = self._unicode_to_str(key) |
---|
[50fdf12] | 186 | if type(key) is not str: |
---|
| 187 | raise TypeError('bad key type (want str): %s' % (type(key))) |
---|
[b0fa2ae] | 188 | if type(value) is not tuple: |
---|
| 189 | value = self._unicode_to_str(value) |
---|
[50fdf12] | 190 | if type(value) is str: |
---|
| 191 | value = ('none', 'none', value) |
---|
| 192 | if type(value) is not tuple: |
---|
| 193 | raise TypeError('bad value type (want tuple): %s' % (type(value))) |
---|
| 194 | if len(value) != 3: |
---|
| 195 | raise TypeError('bad value tuple (len not 3): %d' % (len(value))) |
---|
[b0fa2ae] | 196 | value = (self._unicode_to_str(value[0]), |
---|
| 197 | self._unicode_to_str(value[1]), |
---|
| 198 | self._unicode_to_str(value[2])) |
---|
[50fdf12] | 199 | if type(value[0]) is not str: |
---|
| 200 | raise TypeError('bad value tuple type field: %s' % (type(value[0]))) |
---|
| 201 | if type(value[1]) is not str: |
---|
| 202 | raise TypeError('bad value tuple attrib field: %s' % (type(value[1]))) |
---|
| 203 | if type(value[2]) is not str: |
---|
| 204 | raise TypeError('bad value tuple value field: %s' % (type(value[2]))) |
---|
| 205 | if value[0] not in ['none', 'triplet', 'dir', 'file', 'exe']: |
---|
| 206 | raise TypeError('bad value tuple (type field): %s' % (value[0])) |
---|
| 207 | if value[1] not in ['none', 'optional', 'required', |
---|
| 208 | 'override', 'undefine', 'convert']: |
---|
| 209 | raise TypeError('bad value tuple (attrib field): %s' % (value[1])) |
---|
| 210 | if value[1] == 'convert': |
---|
| 211 | value = self.expand(value) |
---|
| 212 | self.macros[self.write_map][self.key_filter(key)] = value |
---|
| 213 | |
---|
| 214 | def __delitem__(self, key): |
---|
| 215 | self.undefine(key) |
---|
| 216 | |
---|
| 217 | def __contains__(self, key): |
---|
| 218 | return self.has_key(key) |
---|
| 219 | |
---|
| 220 | def __len__(self): |
---|
[b0fa2ae] | 221 | return len(list(self.keys())) |
---|
[50fdf12] | 222 | |
---|
| 223 | def keys(self): |
---|
[b0fa2ae] | 224 | keys = list(self.macros['global'].keys()) |
---|
[50fdf12] | 225 | for rm in self.get_read_maps(): |
---|
| 226 | for mk in self.macros[rm]: |
---|
| 227 | if self.macros[rm][mk][1] == 'undefine': |
---|
| 228 | if mk in keys: |
---|
| 229 | keys.remove(mk) |
---|
| 230 | else: |
---|
| 231 | keys.append(mk) |
---|
| 232 | return sorted(set(keys)) |
---|
| 233 | |
---|
| 234 | def has_key(self, key): |
---|
[cbff4c8] | 235 | key = self._unicode_to_str(key) |
---|
[50fdf12] | 236 | if type(key) is not str: |
---|
| 237 | raise TypeError('bad key type (want str): %s' % (type(key))) |
---|
[b0fa2ae] | 238 | if self.key_filter(key) not in list(self.keys()): |
---|
[50fdf12] | 239 | return False |
---|
| 240 | return True |
---|
| 241 | |
---|
| 242 | def maps(self): |
---|
| 243 | return self.macros.keys() |
---|
| 244 | |
---|
| 245 | def get_read_maps(self): |
---|
[bf58911] | 246 | return [rm[7:] for rm in self.read_maps] |
---|
[50fdf12] | 247 | |
---|
| 248 | def key_filter(self, key): |
---|
| 249 | if key.startswith('%{') and key[-1] is '}': |
---|
| 250 | key = key[2:-1] |
---|
| 251 | return key.lower() |
---|
| 252 | |
---|
| 253 | def parse(self, lines): |
---|
| 254 | |
---|
| 255 | def _clean(l): |
---|
| 256 | if '#' in l: |
---|
| 257 | l = l[:l.index('#')] |
---|
| 258 | if '\r' in l: |
---|
| 259 | l = l[:l.index('r')] |
---|
| 260 | if '\n' in l: |
---|
| 261 | l = l[:l.index('\n')] |
---|
| 262 | return l.strip() |
---|
| 263 | |
---|
| 264 | trace_me = False |
---|
| 265 | if trace_me: |
---|
[04a5204] | 266 | print('[[[[]]]] parsing macros') |
---|
[50fdf12] | 267 | orig_macros = copy.copy(self.macros) |
---|
| 268 | map = 'global' |
---|
| 269 | lc = 0 |
---|
| 270 | state = 'key' |
---|
| 271 | token = '' |
---|
| 272 | macro = [] |
---|
| 273 | for l in lines: |
---|
| 274 | lc += 1 |
---|
| 275 | #print 'l:%s' % (l[:-1]) |
---|
| 276 | if len(l) == 0: |
---|
| 277 | continue |
---|
| 278 | l_remaining = l |
---|
| 279 | for c in l: |
---|
| 280 | if trace_me: |
---|
[04a5204] | 281 | print(']]]]]]]] c:%s(%d) s:%s t:"%s" m:%r M:%s' % \ |
---|
| 282 | (c, ord(c), state, token, macro, map)) |
---|
[50fdf12] | 283 | l_remaining = l_remaining[1:] |
---|
| 284 | if c is '#' and not state.startswith('value'): |
---|
| 285 | break |
---|
| 286 | if c == '\n' or c == '\r': |
---|
| 287 | if not (state is 'key' and len(token) == 0) and \ |
---|
| 288 | not state.startswith('value-multiline'): |
---|
| 289 | self.macros = orig_macros |
---|
| 290 | raise error.general('malformed macro line:%d: %s' % (lc, l)) |
---|
| 291 | if state is 'key': |
---|
| 292 | if c not in string.whitespace: |
---|
| 293 | if c is '[': |
---|
| 294 | state = 'map' |
---|
| 295 | elif c is '%': |
---|
| 296 | state = 'directive' |
---|
| 297 | elif c is ':': |
---|
| 298 | macro += [token] |
---|
| 299 | token = '' |
---|
| 300 | state = 'attribs' |
---|
| 301 | elif c is '#': |
---|
| 302 | break |
---|
| 303 | else: |
---|
| 304 | token += c |
---|
| 305 | elif state is 'map': |
---|
| 306 | if c is ']': |
---|
| 307 | if token not in self.macros: |
---|
| 308 | self.macros[token] = {} |
---|
| 309 | map = token |
---|
| 310 | token = '' |
---|
| 311 | state = 'key' |
---|
| 312 | elif c in string.printable and c not in string.whitespace: |
---|
| 313 | token += c |
---|
| 314 | else: |
---|
| 315 | self.macros = orig_macros |
---|
| 316 | raise error.general('invalid macro map:%d: %s' % (lc, l)) |
---|
| 317 | elif state is 'directive': |
---|
| 318 | if c in string.whitespace: |
---|
| 319 | if token == 'include': |
---|
| 320 | self.load(_clean(l_remaining)) |
---|
| 321 | token = '' |
---|
| 322 | state = 'key' |
---|
| 323 | break |
---|
| 324 | elif c in string.printable and c not in string.whitespace: |
---|
| 325 | token += c |
---|
| 326 | else: |
---|
| 327 | self.macros = orig_macros |
---|
| 328 | raise error.general('invalid macro directive:%d: %s' % (lc, l)) |
---|
| 329 | elif state is 'include': |
---|
| 330 | if c is string.whitespace: |
---|
| 331 | if token == 'include': |
---|
| 332 | state = 'include' |
---|
| 333 | elif c in string.printable and c not in string.whitespace: |
---|
| 334 | token += c |
---|
| 335 | else: |
---|
| 336 | self.macros = orig_macros |
---|
| 337 | raise error.general('invalid macro directive:%d: %s' % (lc, l)) |
---|
| 338 | elif state is 'attribs': |
---|
| 339 | if c not in string.whitespace: |
---|
| 340 | if c is ',': |
---|
| 341 | macro += [token] |
---|
| 342 | token = '' |
---|
| 343 | if len(macro) == 3: |
---|
| 344 | state = 'value-start' |
---|
| 345 | else: |
---|
| 346 | token += c |
---|
| 347 | elif state is 'value-start': |
---|
| 348 | if c is "'": |
---|
| 349 | state = 'value-line-start' |
---|
| 350 | elif state is 'value-line-start': |
---|
| 351 | if c is "'": |
---|
| 352 | state = 'value-multiline-start' |
---|
| 353 | else: |
---|
| 354 | state = 'value-line' |
---|
| 355 | token += c |
---|
| 356 | elif state is 'value-multiline-start': |
---|
| 357 | if c is "'": |
---|
| 358 | state = 'value-multiline' |
---|
| 359 | else: |
---|
| 360 | macro += [token] |
---|
| 361 | state = 'macro' |
---|
| 362 | elif state is 'value-line': |
---|
| 363 | if c is "'": |
---|
| 364 | macro += [token] |
---|
| 365 | state = 'macro' |
---|
| 366 | else: |
---|
| 367 | token += c |
---|
| 368 | elif state is 'value-multiline': |
---|
| 369 | if c is "'": |
---|
| 370 | state = 'value-multiline-end' |
---|
| 371 | else: |
---|
| 372 | token += c |
---|
| 373 | elif state is 'value-multiline-end': |
---|
| 374 | if c is "'": |
---|
| 375 | state = 'value-multiline-end-end' |
---|
| 376 | else: |
---|
| 377 | state = 'value-multiline' |
---|
| 378 | token += "'" + c |
---|
| 379 | elif state is 'value-multiline-end-end': |
---|
| 380 | if c is "'": |
---|
| 381 | macro += [token] |
---|
| 382 | state = 'macro' |
---|
| 383 | else: |
---|
| 384 | state = 'value-multiline' |
---|
| 385 | token += "''" + c |
---|
| 386 | else: |
---|
| 387 | self.macros = orig_macros |
---|
| 388 | raise error.internal('bad state: %s' % (state)) |
---|
| 389 | if state is 'macro': |
---|
| 390 | self.macros[map][macro[0].lower()] = (macro[1], macro[2], macro[3]) |
---|
| 391 | macro = [] |
---|
| 392 | token = '' |
---|
| 393 | state = 'key' |
---|
| 394 | |
---|
| 395 | def load(self, name): |
---|
| 396 | names = self.expand(name).split(':') |
---|
| 397 | for n in names: |
---|
[1676b9c] | 398 | log.trace('opening: %s' % (n)) |
---|
[50fdf12] | 399 | if path.exists(n): |
---|
| 400 | try: |
---|
| 401 | mc = open(path.host(n), 'r') |
---|
| 402 | macros = self.parse(mc) |
---|
| 403 | mc.close() |
---|
| 404 | self.files += [n] |
---|
| 405 | return |
---|
[04a5204] | 406 | except IOError as err: |
---|
[50fdf12] | 407 | pass |
---|
| 408 | raise error.general('opening macro file: %s' % \ |
---|
| 409 | (path.host(self.expand(name)))) |
---|
| 410 | |
---|
| 411 | def get(self, key): |
---|
[cbff4c8] | 412 | key = self._unicode_to_str(key) |
---|
[50fdf12] | 413 | if type(key) is not str: |
---|
| 414 | raise TypeError('bad key type: %s' % (type(key))) |
---|
| 415 | key = self.key_filter(key) |
---|
| 416 | for rm in self.get_read_maps(): |
---|
| 417 | if key in self.macros[rm]: |
---|
| 418 | return self.macros[rm][key] |
---|
| 419 | if key in self.macros['global']: |
---|
| 420 | return self.macros['global'][key] |
---|
| 421 | return None |
---|
| 422 | |
---|
| 423 | def get_type(self, key): |
---|
| 424 | m = self.get(key) |
---|
| 425 | if m is None: |
---|
| 426 | return None |
---|
| 427 | return m[0] |
---|
| 428 | |
---|
| 429 | def get_attribute(self, key): |
---|
| 430 | m = self.get(key) |
---|
| 431 | if m is None: |
---|
| 432 | return None |
---|
| 433 | return m[1] |
---|
| 434 | |
---|
| 435 | def get_value(self, key): |
---|
| 436 | m = self.get(key) |
---|
| 437 | if m is None: |
---|
| 438 | return None |
---|
| 439 | return m[2] |
---|
| 440 | |
---|
| 441 | def overridden(self, key): |
---|
| 442 | return self.get_attribute(key) == 'override' |
---|
| 443 | |
---|
| 444 | def define(self, key, value = '1'): |
---|
| 445 | if type(key) is not str: |
---|
| 446 | raise TypeError('bad key type: %s' % (type(key))) |
---|
| 447 | self.__setitem__(key, ('none', 'none', value)) |
---|
| 448 | |
---|
| 449 | def undefine(self, key): |
---|
| 450 | if type(key) is not str: |
---|
| 451 | raise TypeError('bad key type: %s' % (type(key))) |
---|
| 452 | key = self.key_filter(key) |
---|
| 453 | for map in self.macros: |
---|
| 454 | if key in self.macros[map]: |
---|
| 455 | del self.macros[map][key] |
---|
| 456 | |
---|
| 457 | def expand(self, _str): |
---|
| 458 | """Simple basic expander of config file macros.""" |
---|
[b7d48ef] | 459 | start_str = _str |
---|
[50fdf12] | 460 | expanded = True |
---|
[b7d48ef] | 461 | count = 0 |
---|
[50fdf12] | 462 | while expanded: |
---|
[b7d48ef] | 463 | count += 1 |
---|
| 464 | if count > 1000: |
---|
| 465 | raise error.general('expansion looped over 1000 times "%s"' % |
---|
| 466 | (start_str)) |
---|
[50fdf12] | 467 | expanded = False |
---|
| 468 | for m in self.macro_filter.findall(_str): |
---|
| 469 | name = m[2:-1] |
---|
| 470 | macro = self.get(name) |
---|
| 471 | if macro is None: |
---|
| 472 | raise error.general('cannot expand default macro: %s in "%s"' % |
---|
| 473 | (m, _str)) |
---|
| 474 | _str = _str.replace(m, macro[2]) |
---|
| 475 | expanded = True |
---|
| 476 | return _str |
---|
| 477 | |
---|
| 478 | def find(self, regex): |
---|
| 479 | what = re.compile(regex) |
---|
| 480 | keys = [] |
---|
| 481 | for key in self.keys(): |
---|
| 482 | if what.match(key): |
---|
| 483 | keys += [key] |
---|
| 484 | return keys |
---|
| 485 | |
---|
| 486 | def set_read_map(self, _map): |
---|
| 487 | if not self.read_map_locked: |
---|
| 488 | if _map in self.macros: |
---|
| 489 | if _map not in self.get_read_maps(): |
---|
[bf58911] | 490 | rm = '%04d___%s' % (len(self.read_maps), _map) |
---|
[50fdf12] | 491 | self.read_maps = sorted(self.read_maps + [rm]) |
---|
| 492 | return True |
---|
| 493 | return False |
---|
| 494 | |
---|
| 495 | def unset_read_map(self, _map): |
---|
| 496 | if not self.read_map_locked: |
---|
| 497 | if _map in self.get_read_maps(): |
---|
| 498 | for i in range(0, len(self.read_maps)): |
---|
[bf58911] | 499 | if '%04d___%s' % (i, _map) == self.read_maps[i]: |
---|
[50fdf12] | 500 | self.read_maps.pop(i) |
---|
| 501 | return True |
---|
| 502 | return False |
---|
| 503 | |
---|
[bf58911] | 504 | def set_write_map(self, _map, add = False): |
---|
| 505 | if _map in self.macros: |
---|
| 506 | self.write_map = _map |
---|
| 507 | return True |
---|
| 508 | elif add: |
---|
| 509 | self.write_map = _map |
---|
| 510 | self.macros[_map] = {} |
---|
[50fdf12] | 511 | return True |
---|
| 512 | return False |
---|
| 513 | |
---|
[bf58911] | 514 | def unset_write_map(self): |
---|
| 515 | self.write_map = 'global' |
---|
| 516 | return True |
---|
| 517 | |
---|
[50fdf12] | 518 | def lock_read_map(self): |
---|
| 519 | self.read_map_locked = True |
---|
| 520 | |
---|
| 521 | def unlock_read_map(self): |
---|
| 522 | self.read_map_locked = False |
---|
| 523 | |
---|
| 524 | if __name__ == "__main__": |
---|
| 525 | import copy |
---|
| 526 | import sys |
---|
[04a5204] | 527 | print(inspect.getfile(macros)) |
---|
[b0fa2ae] | 528 | m = macros() |
---|
[50fdf12] | 529 | d = copy.copy(m) |
---|
| 530 | m['test1'] = 'something' |
---|
| 531 | if d.has_key('test1'): |
---|
[04a5204] | 532 | print('error: copy failed.') |
---|
[50fdf12] | 533 | sys.exit(1) |
---|
| 534 | m.parse("[test]\n" \ |
---|
| 535 | "test1: none, undefine, ''\n" \ |
---|
| 536 | "name: none, override, 'pink'\n") |
---|
[04a5204] | 537 | print('set test:', m.set_read_map('test')) |
---|
[50fdf12] | 538 | if m['name'] != 'pink': |
---|
[04a5204] | 539 | print('error: override failed. name is %s' % (m['name'])) |
---|
[50fdf12] | 540 | sys.exit(1) |
---|
| 541 | if m.has_key('test1'): |
---|
[04a5204] | 542 | print('error: map undefine failed.') |
---|
[50fdf12] | 543 | sys.exit(1) |
---|
[04a5204] | 544 | print('unset test:', m.unset_read_map('test')) |
---|
| 545 | print(m) |
---|
| 546 | print(m.keys()) |
---|