Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 16:f85dc4677c01
Implemented the real configuration dictionary with attribute access or
variable substitution
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Thu, 10 Mar 2016 09:39:35 +0100 |
| parents | |
| children | 94b5e94fae44 |
comparison
equal
deleted
inserted
replaced
| 15:0b1292e920af | 16:f85dc4677c01 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 r"""The unified configuration dictionary with attribute support | |
| 3 or variable substitution. | |
| 4 | |
| 5 """ | |
| 6 | |
| 7 from __future__ import division, absolute_import, print_function | |
| 8 | |
| 9 try: | |
| 10 from collections import OrderedDict as ConfigurationBase | |
| 11 except ImportError: | |
| 12 try: | |
| 13 from ordereddict import OrderedDict as ConfigurationBase | |
| 14 except ImportError: | |
| 15 ConfigurationBase = dict | |
| 16 | |
| 17 from .variables import lookup_varns, lookup_filter | |
| 18 from .compat import u | |
| 19 | |
| 20 | |
| 21 __all__ = ["Configuration"] | |
| 22 | |
| 23 | |
| 24 _MARKER = object() | |
| 25 | |
| 26 | |
| 27 class AttributeDict(ConfigurationBase): | |
| 28 | |
| 29 def __getattr__(self, name): | |
| 30 try: | |
| 31 v = self[name] | |
| 32 except KeyError: | |
| 33 raise AttributeError("%s has no attribute %r" % (type(self), name)) | |
| 34 else: | |
| 35 # Wrap a dict into another dict with attribute access support | |
| 36 if isinstance(v, dict): | |
| 37 return AttributeDict(v) | |
| 38 else: | |
| 39 return v | |
| 40 | |
| 41 | |
| 42 class Configuration(AttributeDict): | |
| 43 | |
| 44 def getvar(self, varname, default=_MARKER): | |
| 45 """Get a variable of the form ``[[ns1.]ns2.]name`` - including | |
| 46 variables from other namespaces. | |
| 47 | |
| 48 No variable expansion is done and no filters are applied. | |
| 49 """ | |
| 50 varns, varname = self._split_ns(varname) | |
| 51 try: | |
| 52 if not varns: | |
| 53 lookupfn = self._lookupvar | |
| 54 else: | |
| 55 lookupfn = lookup_varns(varns) | |
| 56 varvalue = lookupfn(varname) | |
| 57 except KeyError: | |
| 58 if default is _MARKER: | |
| 59 raise KeyError("Variable %r not found" % varname) | |
| 60 else: | |
| 61 return default | |
| 62 else: | |
| 63 return varvalue | |
| 64 | |
| 65 def getvar_s(self, varname, default=_MARKER): | |
| 66 """Get a variable - incliding variables from other namespaces. | |
| 67 | |
| 68 Variables will be substituted recursively in the result. | |
| 69 """ | |
| 70 try: | |
| 71 obj = self.getvar(varname) | |
| 72 return self.substitute_variables_in_obj(obj) | |
| 73 except KeyError: | |
| 74 if default is _MARKER: | |
| 75 raise | |
| 76 else: | |
| 77 return default | |
| 78 | |
| 79 def _split_ns(self, s): | |
| 80 nameparts = s.split(':', 1) | |
| 81 if len(nameparts) == 1: | |
| 82 return (None, s, ) | |
| 83 else: | |
| 84 return (nameparts[0], nameparts[1], ) | |
| 85 | |
| 86 def _split_filters(self, s): | |
| 87 nameparts = s.split('|') | |
| 88 if len(nameparts) == 1: | |
| 89 return (s, [], ) | |
| 90 else: | |
| 91 return (nameparts[0].rstrip(), nameparts[1:], ) | |
| 92 | |
| 93 def _lookupvar(self, key, default=_MARKER): | |
| 94 """Lookup a variable. | |
| 95 | |
| 96 If no default is given an unexisting `key` raises a `KeyError` | |
| 97 else `default` is returned. | |
| 98 """ | |
| 99 parts = key.split('.') | |
| 100 try: | |
| 101 v = self[parts[0]] | |
| 102 for p in parts[1:]: | |
| 103 v = v[p] | |
| 104 except KeyError: | |
| 105 if default is _MARKER: | |
| 106 raise KeyError("Configuration variable %r not found" % key) | |
| 107 else: | |
| 108 return default | |
| 109 return v | |
| 110 | |
| 111 # Speed | |
| 112 _TEXTTYPE = type(u("")) | |
| 113 | |
| 114 def substitute_variables_in_obj(self, obj): | |
| 115 """Recursively expand variables in the object tree `obj`.""" | |
| 116 if isinstance(obj, self._TEXTTYPE): | |
| 117 # a string - really replace the value | |
| 118 return self.expand_variable(obj) | |
| 119 elif isinstance(obj, list): | |
| 120 return [self.substitute_variables_in_obj(i) for i in obj] | |
| 121 elif isinstance(obj, tuple): | |
| 122 tmp = [self.substitute_variables_in_obj(i) for i in obj] | |
| 123 return type(obj)(tmp) | |
| 124 elif isinstance(obj, dict): | |
| 125 newdict = type(obj)() | |
| 126 for k in obj: | |
| 127 newdict[k] = self.substitute_variables_in_obj(obj[k]) | |
| 128 return newdict | |
| 129 elif isinstance(obj, set): | |
| 130 newset = type(obj)() | |
| 131 for i in obj: | |
| 132 newset.add(self.substitute_variables_in_obj(i)) | |
| 133 else: | |
| 134 return obj | |
| 135 | |
| 136 # Speed | |
| 137 _STARTTOK = u(b"{{") | |
| 138 _ENDTOK = u(b"}}") | |
| 139 | |
| 140 def expand_variable(self, s): | |
| 141 """Expand variables in a single string""" | |
| 142 start = s.find(self._STARTTOK, 0) | |
| 143 while start != -1: | |
| 144 end = s.find(self._ENDTOK, start) | |
| 145 if end < 0: | |
| 146 return s | |
| 147 varname, filters = self._split_filters(s[start+2:end]) | |
| 148 varvalue = self._apply_filters(filters, self.getvar_s(varname)) | |
| 149 s = u(b"{0}{1}{2}").format(s[:start], varvalue, s[end+2:]) | |
| 150 # don't re-evaluate because `self.getvar_s()` expands already | |
| 151 start = s.find(self._STARTTOK, start + len(varvalue)) | |
| 152 return s | |
| 153 | |
| 154 def _apply_filters(self, filters, value): | |
| 155 for name in filters: | |
| 156 try: | |
| 157 filterfn = lookup_filter(name) | |
| 158 except KeyError: | |
| 159 raise NameError("Filter %r not found" % name) | |
| 160 else: | |
| 161 value = filterfn(self, value) | |
| 162 return value |
