mirror of
https://git.rtems.org/rtems-source-builder
synced 2024-10-09 07:15:10 +08:00
Macros updates to support multiple maps.
Add a read and write map pointer. This means you can read from a user defined map through to the global map while pointing all write to only the global map therefore supporting overrides cleanly. Print the list of loaded files when printing. Provide helper calls for type and attributes.
This commit is contained in:
parent
06dad0acd8
commit
9bd29bbaa6
@ -52,13 +52,15 @@ class macros:
|
|||||||
return self.keys
|
return self.keys
|
||||||
|
|
||||||
def __init__(self, name = None, original = None, sbdir = '.'):
|
def __init__(self, name = None, original = None, sbdir = '.'):
|
||||||
|
self.files = []
|
||||||
self.macro_filter = re.compile(r'%{[^}]+}')
|
self.macro_filter = re.compile(r'%{[^}]+}')
|
||||||
if original is None:
|
if original is None:
|
||||||
self.macros = {}
|
self.macros = {}
|
||||||
self.map = 'global'
|
self.read_map = 'global'
|
||||||
self.macros[self.map] = {}
|
self.write_map = 'global'
|
||||||
self.macros[self.map]['_cwd'] = ('dir', 'required', path.shell(os.getcwd()))
|
self.macros[self.read_map] = {}
|
||||||
self.macros[self.map]['_sbdir'] = ('dir', 'required', path.shell(sbdir))
|
self.macros[self.read_map]['_cwd'] = ('dir', 'required', path.shell(os.getcwd()))
|
||||||
|
self.macros[self.read_map]['_sbdir'] = ('dir', 'required', path.shell(sbdir))
|
||||||
else:
|
else:
|
||||||
self.macros = {}
|
self.macros = {}
|
||||||
for m in original.macros:
|
for m in original.macros:
|
||||||
@ -66,7 +68,8 @@ class macros:
|
|||||||
self.macros[m] = {}
|
self.macros[m] = {}
|
||||||
for k in original.macros[m]:
|
for k in original.macros[m]:
|
||||||
self.macros[m][k] = original.macros[m][k]
|
self.macros[m][k] = original.macros[m][k]
|
||||||
self.map = original.map
|
self.read_map = original.read_map
|
||||||
|
self.write_map = original.write_map
|
||||||
if name is not None:
|
if name is not None:
|
||||||
self.load(name)
|
self.load(name)
|
||||||
|
|
||||||
@ -76,6 +79,8 @@ class macros:
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
text_len = 80
|
text_len = 80
|
||||||
text = ''
|
text = ''
|
||||||
|
for f in self.files:
|
||||||
|
text += '> %s%s' % (f, os.linesep)
|
||||||
for map in self.macros:
|
for map in self.macros:
|
||||||
text += '[%s]%s' % (map, os.linesep)
|
text += '[%s]%s' % (map, os.linesep)
|
||||||
for k in sorted(self.macros[map].keys()):
|
for k in sorted(self.macros[map].keys()):
|
||||||
@ -113,7 +118,7 @@ class macros:
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return macros.macro_iterator(self.macros[self.map].keys())
|
return macros.macro_iterator(self.macros[self.read_map].keys())
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
macro = self.get(key)
|
macro = self.get(key)
|
||||||
@ -140,7 +145,7 @@ class macros:
|
|||||||
raise TypeError('bad value tuple (type field): %s' % (value[0]))
|
raise TypeError('bad value tuple (type field): %s' % (value[0]))
|
||||||
if value[1] not in ['none', 'optional', 'required', 'override']:
|
if value[1] not in ['none', 'optional', 'required', 'override']:
|
||||||
raise TypeError('bad value tuple (attrib field): %s' % (value[1]))
|
raise TypeError('bad value tuple (attrib field): %s' % (value[1]))
|
||||||
self.macros[self.map][self.key_filter(key)] = value
|
self.macros[self.write_map][self.key_filter(key)] = value
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
self.undefine(key)
|
self.undefine(key)
|
||||||
@ -152,7 +157,7 @@ class macros:
|
|||||||
return len(self.keys())
|
return len(self.keys())
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
k = self.macros[self.map].keys()
|
k = self.macros[self.read_map].keys()
|
||||||
if map is not 'global':
|
if map is not 'global':
|
||||||
k += self.macros['global'].keys()
|
k += self.macros['global'].keys()
|
||||||
return sorted(set(k))
|
return sorted(set(k))
|
||||||
@ -161,11 +166,14 @@ class macros:
|
|||||||
if type(key) is not str:
|
if type(key) is not str:
|
||||||
raise TypeError('bad key type (want str): %s' % (type(key)))
|
raise TypeError('bad key type (want str): %s' % (type(key)))
|
||||||
key = self.key_filter(key)
|
key = self.key_filter(key)
|
||||||
if key not in self.macros[self.map].keys():
|
if key not in self.macros[self.read_map].keys():
|
||||||
if key not in self.macros['global'].keys():
|
if key not in self.macros['global'].keys():
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def maps(self):
|
||||||
|
return self.macros.keys()
|
||||||
|
|
||||||
def key_filter(self, key):
|
def key_filter(self, key):
|
||||||
if key.startswith('%{') and key[-1] is '}':
|
if key.startswith('%{') and key[-1] is '}':
|
||||||
key = key[2:-1]
|
key = key[2:-1]
|
||||||
@ -210,7 +218,7 @@ class macros:
|
|||||||
map = token
|
map = token
|
||||||
token = ''
|
token = ''
|
||||||
state = 'key'
|
state = 'key'
|
||||||
elif c in string.ascii_letters or c in string.digits:
|
elif c in string.printable and c not in string.whitespace:
|
||||||
token += c
|
token += c
|
||||||
else:
|
else:
|
||||||
raise error.general('invalid macro map:%d: %s' % (lc, l))
|
raise error.general('invalid macro map:%d: %s' % (lc, l))
|
||||||
@ -265,7 +273,7 @@ class macros:
|
|||||||
else:
|
else:
|
||||||
raise error.internal('bad state: %s' % (state))
|
raise error.internal('bad state: %s' % (state))
|
||||||
if state is 'macro':
|
if state is 'macro':
|
||||||
macros[map][macro[0]] = (macro[1], macro[2], macro[3])
|
macros[map][macro[0].lower()] = (macro[1], macro[2], macro[3])
|
||||||
macro = []
|
macro = []
|
||||||
token = ''
|
token = ''
|
||||||
state = 'key'
|
state = 'key'
|
||||||
@ -278,21 +286,39 @@ class macros:
|
|||||||
except IOError, err:
|
except IOError, err:
|
||||||
raise error.general('opening macro file: %s' % (path.host(name)))
|
raise error.general('opening macro file: %s' % (path.host(name)))
|
||||||
macros = self.parse(mc)
|
macros = self.parse(mc)
|
||||||
|
mc.close()
|
||||||
for m in macros:
|
for m in macros:
|
||||||
|
if m not in self.macros:
|
||||||
|
self.macros[m] = {}
|
||||||
for mm in macros[m]:
|
for mm in macros[m]:
|
||||||
self.macros[m][mm] = macros[m][mm]
|
self.macros[m][mm] = macros[m][mm]
|
||||||
mc.close()
|
self.files += [name]
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
if type(key) is not str:
|
if type(key) is not str:
|
||||||
raise TypeError('bad key type: %s' % (type(key)))
|
raise TypeError('bad key type: %s' % (type(key)))
|
||||||
key = self.key_filter(key)
|
key = self.key_filter(key)
|
||||||
if self.map is not 'global'and key in self.macros[self.map]:
|
if self.read_map is not 'global'and key in self.macros[self.read_map]:
|
||||||
return self.macros[self.map][key]
|
return self.macros[self.read_map][key]
|
||||||
if key in self.macros['global']:
|
if key in self.macros['global']:
|
||||||
return self.macros['global'][key]
|
return self.macros['global'][key]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_type(self, key):
|
||||||
|
m = self.get(key)
|
||||||
|
if m is None:
|
||||||
|
return None
|
||||||
|
return m[0]
|
||||||
|
|
||||||
|
def get_attribute(self, key):
|
||||||
|
m = self.get(key)
|
||||||
|
if m is None:
|
||||||
|
return None
|
||||||
|
return m[1]
|
||||||
|
|
||||||
|
def overridden(self, key):
|
||||||
|
return self.get_attribute(key) == 'override'
|
||||||
|
|
||||||
def define(self, key, value = '1'):
|
def define(self, key, value = '1'):
|
||||||
if type(key) is not str:
|
if type(key) is not str:
|
||||||
raise TypeError('bad key type: %s' % (type(key)))
|
raise TypeError('bad key type: %s' % (type(key)))
|
||||||
@ -321,6 +347,26 @@ class macros:
|
|||||||
expanded = True
|
expanded = True
|
||||||
return _str
|
return _str
|
||||||
|
|
||||||
|
def find(self, regex):
|
||||||
|
what = re.compile(regex)
|
||||||
|
keys = []
|
||||||
|
for key in self.keys():
|
||||||
|
if what.match(key):
|
||||||
|
keys += [key]
|
||||||
|
return keys
|
||||||
|
|
||||||
|
def set_read_map(self, map):
|
||||||
|
if map in self.macros:
|
||||||
|
self.read_map = map
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def set_write_map(self, map):
|
||||||
|
if map in self.macros:
|
||||||
|
self.write_map = map
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import copy
|
import copy
|
||||||
import sys
|
import sys
|
||||||
|
Loading…
x
Reference in New Issue
Block a user