Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/variables.py @ 101:f6f2dc7cf053
Better docu of the configmix.variables module
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Thu, 22 Mar 2018 16:55:12 +0100 |
| parents | e3289a56ba80 |
| children | b35837427a7a |
comparison
equal
deleted
inserted
replaced
| 100:e3289a56ba80 | 101:f6f2dc7cf053 |
|---|---|
| 72 | 72 |
| 73 _filter_registry = {} | 73 _filter_registry = {} |
| 74 | 74 |
| 75 | 75 |
| 76 def add_filter(name, fn): | 76 def add_filter(name, fn): |
| 77 """Register a variable filter with name `name` and implementation `fn` | |
| 78 | |
| 79 """ | |
| 77 _filter_registry[_normalized(name)] = fn | 80 _filter_registry[_normalized(name)] = fn |
| 78 | 81 |
| 79 | 82 |
| 80 def lookup_filter(name): | 83 def lookup_filter(name): |
| 84 """Lookup a variable filter with name `name` and return it's implementation | |
| 85 function | |
| 86 | |
| 87 """ | |
| 81 return _filter_registry[_normalized(name)] | 88 return _filter_registry[_normalized(name)] |
| 82 | 89 |
| 83 | 90 |
| 84 def filter(name): | 91 def filter(name): |
| 85 """Decorator for a filter function""" | 92 """Decorator for a filter function. |
| 93 | |
| 94 Example usage:: | |
| 95 | |
| 96 @filter("myfilter") | |
| 97 def myfilter_impl(appconfig, variable_value): | |
| 98 filtered_value = ... | |
| 99 return filtered_value | |
| 100 | |
| 101 | |
| 102 """ | |
| 86 | 103 |
| 87 def _decorator(f): | 104 def _decorator(f): |
| 88 | 105 |
| 89 @wraps(f) | 106 @wraps(f) |
| 90 def _f(appconfig, v): | 107 def _f(appconfig, v): |
| 105 # | 122 # |
| 106 if PY2: | 123 if PY2: |
| 107 | 124 |
| 108 @filter("urlquote") | 125 @filter("urlquote") |
| 109 def urlquote(config, v): | 126 def urlquote(config, v): |
| 110 """Replace all special characters in string using the ``%xx`` escape""" | 127 """Filter function to replace all special characters in string using |
| 128 the ``%xx`` escape | |
| 129 | |
| 130 """ | |
| 111 from urllib import quote | 131 from urllib import quote |
| 112 return quote(v.encode("utf-8"), safe=b"").decode("utf-8") | 132 return quote(v.encode("utf-8"), safe=b"").decode("utf-8") |
| 113 | 133 |
| 114 else: | 134 else: |
| 115 | 135 |
| 116 @filter("urlquote") | 136 @filter("urlquote") |
| 117 def urlquote(config, v): | 137 def urlquote(config, v): |
| 118 """Replace all special characters in string using the ``%xx`` escape""" | 138 """Filter function to replace all special characters in string using |
| 139 the ``%xx`` escape | |
| 140 | |
| 141 """ | |
| 119 from urllib.parse import quote | 142 from urllib.parse import quote |
| 120 return quote(v, safe="") | 143 return quote(v, safe="") |
| 121 | 144 |
| 122 | 145 |
| 123 @filter("saslprep") | 146 @filter("saslprep") |
| 124 def saslprep(config, v): | 147 def saslprep(config, v): |
| 125 """Do a `SASLprep` according to :rfc:`4013` on `v`. | 148 """Filter function to perform a `SASLprep` according to :rfc:`4013` on |
| 149 `v`. | |
| 126 | 150 |
| 127 This is a Stringprep Profile for usernames and passwords | 151 This is a Stringprep Profile for usernames and passwords |
| 128 | 152 |
| 129 """ | 153 """ |
| 130 import passlib.utils | 154 import passlib.utils |
| 131 return passlib.utils.saslprep(v) | 155 return passlib.utils.saslprep(v) |
| 132 | 156 |
| 133 | 157 |
| 134 @filter("normpath") | 158 @filter("normpath") |
| 135 def normpath_impl(config, v): | 159 def normpath_impl(config, v): |
| 160 """Implementation of the `normpath` filter function""" | |
| 136 return os.path.normpath(v) | 161 return os.path.normpath(v) |
| 137 | 162 |
| 138 | 163 |
| 139 @filter("abspath") | 164 @filter("abspath") |
| 140 def abspath_impl(config, v): | 165 def abspath_impl(config, v): |
| 166 """Implementation of the `abspath` filter function""" | |
| 141 return os.path.abspath(v) | 167 return os.path.abspath(v) |
| 142 | 168 |
| 143 | 169 |
| 144 @filter("posixpath") | 170 @filter("posixpath") |
| 145 def posixpath_impl(config, v): | 171 def posixpath_impl(config, v): |
| 172 """Implementation of the `posixpath` filter function""" | |
| 146 return v.replace(u('\\'), u('/')) | 173 return v.replace(u('\\'), u('/')) |
| 147 | 174 |
| 148 | 175 |
| 149 @filter("lower") | 176 @filter("lower") |
| 150 def lower_impl(config, v): | 177 def lower_impl(config, v): |
| 178 """Implementation of the `lower` filter function""" | |
| 151 return v.lower() | 179 return v.lower() |
| 152 | 180 |
| 153 | 181 |
| 154 @filter("upper") | 182 @filter("upper") |
| 155 def upper_impl(config, v): | 183 def upper_impl(config, v): |
| 184 """Implementation of the `upper` filter function""" | |
| 156 return v.upper() | 185 return v.upper() |
