diff options
Diffstat (limited to 'waflib/ConfigSet.py')
-rw-r--r-- | waflib/ConfigSet.py | 86 |
1 files changed, 34 insertions, 52 deletions
diff --git a/waflib/ConfigSet.py b/waflib/ConfigSet.py index b300bb56..2fac2166 100644 --- a/waflib/ConfigSet.py +++ b/waflib/ConfigSet.py @@ -1,12 +1,12 @@ #!/usr/bin/env python # encoding: utf-8 -# Thomas Nagy, 2005-2018 (ita) +# Thomas Nagy, 2005-2010 (ita) """ ConfigSet: a special dict -The values put in :py:class:`ConfigSet` must be serializable (dicts, lists, strings) +The values put in :py:class:`ConfigSet` must be lists """ import copy, re, os @@ -15,7 +15,7 @@ re_imp = re.compile('^(#)*?([^#=]*?)\ =\ (.*?)$', re.M) class ConfigSet(object): """ - A copy-on-write dict with human-readable serialized format. The serialization format + A dict that honor serialization and parent relationships. The serialization format is human-readable (python-like) and performed by using eval() and repr(). For high performance prefer pickle. Do not store functions as they are not serializable. @@ -39,20 +39,17 @@ class ConfigSet(object): def __contains__(self, key): """ - Enables the *in* syntax:: + Enable the *in* syntax:: if 'foo' in env: print(env['foo']) """ - if key in self.table: - return True - try: - return self.parent.__contains__(key) - except AttributeError: - return False # parent may not exist + if key in self.table: return True + try: return self.parent.__contains__(key) + except AttributeError: return False # parent may not exist def keys(self): - """Dict interface""" + """Dict interface (unknown purpose)""" keys = set() cur = self while cur: @@ -62,9 +59,6 @@ class ConfigSet(object): keys.sort() return keys - def __iter__(self): - return iter(self.keys()) - def __str__(self): """Text representation of the ConfigSet (for debugging purposes)""" return "\n".join(["%r %r" % (x, self.__getitem__(x)) for x in self.keys()]) @@ -79,7 +73,7 @@ class ConfigSet(object): """ try: while 1: - x = self.table.get(key) + x = self.table.get(key, None) if not x is None: return x self = self.parent @@ -88,13 +82,13 @@ class ConfigSet(object): def __setitem__(self, key, value): """ - Dictionary interface: set value from key + Dictionary interface: get value from key """ self.table[key] = value def __delitem__(self, key): """ - Dictionary interface: mark the value as missing + Dictionary interface: get value from key """ self[key] = [] @@ -107,7 +101,7 @@ class ConfigSet(object): conf.env['value'] """ if name in self.__slots__: - return object.__getattribute__(self, name) + return object.__getattr__(self, name) else: return self[name] @@ -158,7 +152,7 @@ class ConfigSet(object): def detach(self): """ - Detaches this instance from its parent (if present) + Detach self from its parent (if existing) Modifying the parent :py:class:`ConfigSet` will not change the current object Modifying this :py:class:`ConfigSet` will not modify the parent one. @@ -177,19 +171,18 @@ class ConfigSet(object): def get_flat(self, key): """ - Returns a value as a string. If the input is a list, the value returned is space-separated. + Return a value as a string. If the input is a list, the value returned is space-separated. :param key: key to use :type key: string """ s = self[key] - if isinstance(s, str): - return s + if isinstance(s, str): return s return ' '.join(s) def _get_list_value_for_modification(self, key): """ - Returns a list value for further modification. + Return a list value for further modification. The list may be modified inplace and there is no need to do this afterwards:: @@ -198,20 +191,16 @@ class ConfigSet(object): try: value = self.table[key] except KeyError: - try: - value = self.parent[key] - except AttributeError: - value = [] + try: value = self.parent[key] + except AttributeError: value = [] + if isinstance(value, list): + value = value[:] else: - if isinstance(value, list): - # force a copy - value = value[:] - else: - value = [value] - self.table[key] = value + value = [value] else: if not isinstance(value, list): - self.table[key] = value = [value] + value = [value] + self.table[key] = value return value def append_value(self, var, val): @@ -243,7 +232,7 @@ class ConfigSet(object): def append_unique(self, var, val): """ - Appends a value to the specified item only if it's not already present:: + Append a value to the specified item only if it's not already present:: def build(bld): bld.env.append_unique('CFLAGS', ['-O2', '-g']) @@ -260,7 +249,7 @@ class ConfigSet(object): def get_merged_dict(self): """ - Computes the merged dictionary from the fusion of self and all its parent + Compute the merged dictionary from the fusion of self and all its parent :rtype: a ConfigSet object """ @@ -268,10 +257,8 @@ class ConfigSet(object): env = self while 1: table_list.insert(0, env.table) - try: - env = env.parent - except AttributeError: - break + try: env = env.parent + except AttributeError: break merged_table = {} for table in table_list: merged_table.update(table) @@ -279,7 +266,7 @@ class ConfigSet(object): def store(self, filename): """ - Serializes the :py:class:`ConfigSet` data to a file. See :py:meth:`ConfigSet.load` for reading such files. + Write the :py:class:`ConfigSet` data into a file. See :py:meth:`ConfigSet.load` for reading such files. :param filename: file to use :type filename: string @@ -306,7 +293,7 @@ class ConfigSet(object): def load(self, filename): """ - Restores contents from a file (current values are not cleared). Files are written using :py:meth:`ConfigSet.store`. + Retrieve the :py:class:`ConfigSet` data from a file. See :py:meth:`ConfigSet.store` for writing such files :param filename: file to use :type filename: string @@ -316,20 +303,21 @@ class ConfigSet(object): for m in re_imp.finditer(code): g = m.group tbl[g(2)] = eval(g(3)) - Logs.debug('env: %s', self.table) + Logs.debug('env: %s' % str(self.table)) def update(self, d): """ - Dictionary interface: replace values with the ones from another dict + Dictionary interface: replace values from another dict :param d: object to use the value from :type d: dict-like object """ - self.table.update(d) + for k, v in d.items(): + self[k] = v def stash(self): """ - Stores the object state to provide transactionality semantics:: + Store the object state, to provide a kind of transaction support:: env = ConfigSet() env.stash() @@ -347,12 +335,6 @@ class ConfigSet(object): tbl[x] = copy.deepcopy(tbl[x]) self.undo_stack = self.undo_stack + [orig] - def commit(self): - """ - Commits transactional changes. See :py:meth:`ConfigSet.stash` - """ - self.undo_stack.pop(-1) - def revert(self): """ Reverts the object to a previous state. See :py:meth:`ConfigSet.stash` |