Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 527:08924499db3c
Implement a simple interpolation cache for text types
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Mon, 20 Dec 2021 13:12:14 +0100 |
| parents | 48990863b905 |
| children | 54a8d020f5d5 |
comparison
equal
deleted
inserted
replaced
| 526:48990863b905 | 527:08924499db3c |
|---|---|
| 333 def __init__(self, *args, **kwds): | 333 def __init__(self, *args, **kwds): |
| 334 # | 334 # |
| 335 # PY2.7 compat: must be set before calling the superclass' __init__ | 335 # PY2.7 compat: must be set before calling the superclass' __init__ |
| 336 # | 336 # |
| 337 self.__lookup_cache = {} | 337 self.__lookup_cache = {} |
| 338 self.__interpolation_cache = {} | |
| 338 super(Configuration, self).__init__(*args, **kwds) | 339 super(Configuration, self).__init__(*args, **kwds) |
| 339 | 340 |
| 340 def clear_cache(self): | 341 def clear_cache(self): |
| 341 """Clear the internal lookup cache""" | 342 """Clear the internal lookup cache""" |
| 342 self.__lookup_cache.clear() | 343 self.__lookup_cache.clear() |
| 344 self.__interpolation_cache.clear() | |
| 343 | 345 |
| 344 def __getitem__(self, key): | 346 def __getitem__(self, key): |
| 345 """Mapping and list interface that forwards to :meth:`~.getvarl_s` | 347 """Mapping and list interface that forwards to :meth:`~.getvarl_s` |
| 346 | 348 |
| 347 """ | 349 """ |
| 773 def interpolate_variables(self, s): | 775 def interpolate_variables(self, s): |
| 774 """Expand all variables in the single string `s`""" | 776 """Expand all variables in the single string `s`""" |
| 775 start = s.find(_STARTTOK, 0) | 777 start = s.find(_STARTTOK, 0) |
| 776 if start < 0: | 778 if start < 0: |
| 777 return s | 779 return s |
| 780 res = self.__interpolation_cache.get(s, _MARKER) | |
| 781 if res is not _MARKER: | |
| 782 return res | |
| 778 len_s = len(s) | 783 len_s = len(s) |
| 779 res = [] | 784 res = [] |
| 780 res_append = res.append | 785 res_append = res.append |
| 781 rest = 0 | 786 rest = 0 |
| 782 while start != -1: | 787 while start != -1: |
| 808 # | 813 # |
| 809 # Dont apply and type conversions to the variable value if | 814 # Dont apply and type conversions to the variable value if |
| 810 # the whole `s` is just one expansion | 815 # the whole `s` is just one expansion |
| 811 # | 816 # |
| 812 if (start == 0) and (rest == len_s): | 817 if (start == 0) and (rest == len_s): |
| 818 self.__interpolation_cache[s] = varvalue | |
| 813 return varvalue | 819 return varvalue |
| 814 if varvalue is None: | 820 if varvalue is None: |
| 815 pass | 821 pass |
| 816 else: | 822 else: |
| 817 res_append(str_and_u(varvalue)) | 823 res_append(str_and_u(varvalue)) |
| 818 # don't re-evaluate because `self.getvar_s()` expands already | 824 # don't re-evaluate because `self.getvar_s()` expands already |
| 819 start = s.find(_STARTTOK, rest) | 825 start = s.find(_STARTTOK, rest) |
| 820 res_append(s[rest:]) | 826 res_append(s[rest:]) |
| 821 return _EMPTY_STR.join(res) | 827 res = _EMPTY_STR.join(res) |
| 828 self.__interpolation_cache[s] = res | |
| 829 return res | |
| 822 | 830 |
| 823 def _apply_filters(self, filters, value): | 831 def _apply_filters(self, filters, value): |
| 824 for name in filters: | 832 for name in filters: |
| 825 try: | 833 try: |
| 826 filterfn = lookup_filter(name) | 834 filterfn = lookup_filter(name) |
