class ConfigNamespace(object): """Abstract class representing the interface of Config objects.
A ConfigNamespace is a collection of names mapped to values, where
the values may be nested namespaces. Values can be accessed via
container notation - obj[key] - or via dotted notation - obj.key.
Both these access methods are equivalent.
To minimize name conflicts between namespace keys andclass members,
the number of class members should be minimized, and the names of
all class members should start with an underscore.
Subclasses must implement the methods for container-like access, and this class will automatically provide dotted access.
# Machinery for converting dotted access into container access, # and automatically creating new sections/namespaces. # # To distinguish between accesses of class members and namespace # keys, we first call object.__getattribute__(). If that succeeds, # the name is assumed to be a class member. Otherwise it is # treated as a namespace key. # # Therefore, member variables should be defined in the class, # not just in the __init__() function. See BasicNamespace for # an example.
# During unpickling, Python checks if the class has a __setstate__ # method. But, the data dicts have not been initialised yet, which # leads to _getitem and hence __getattr__ raising an exception. So # we explicitly impement default __setstate__ behavior. def __setstate__(self, state):
self.__dict__.update(state)
class Undefined(object): """Helper class used to hold undefined names until assignment.
This class helps create any undefined subsections when an
assignment is made to a nested value. For example, if the
statement is"cfg.a.b.c = 42", but "cfg.a.b" does not exist yet. """
def _readfp(self, fp): whileTrue:
line = fp.readline() ifnot line: break
line = line.strip() ifnot line: continue if line[0] == '#': continue
data = line.split('=', 1) if len(data) == 1:
name = line
value = None else:
name = data[0].strip()
value = data[1].strip()
name_components = name.split('.')
ns = self for n in name_components[:-1]: if n in ns:
ns = ns[n] ifnot isinstance(ns, ConfigNamespace): raise TypeError('value-namespace conflict', n) else:
ns = ns._new_namespace(n)
ns[name_components[-1]] = value
# ---- Utility functions
def update_config(target, source): """Imports values from source into target.
Recursively walks the <source> ConfigNamespace and inserts values
into the <target> ConfigNamespace. For example:
>>> from iniparse import ini
>>> i = ini.INIConfig()
>>> update_config(i, n)
>>> print(i)
[playlist]
expand_playlist = True
<BLANKLINE>
[ui]
display_clock = True
display_qlength = True
width = 150
""" for name in sorted(source):
value = source[name] if isinstance(value, ConfigNamespace): if name in target:
myns = target[name] ifnot isinstance(myns, ConfigNamespace): raise TypeError('value-namespace conflict') else:
myns = target._new_namespace(name)
update_config(myns, value) else:
target[name] = value
Messung V0.5
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.