comparison configmix/config.py @ 647:df58983f28a2

Allow to disable the internal caching in configmix. Also allow to re-enable internal caching.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 11 Mar 2022 01:53:08 +0100
parents d35f41e15404
children e8f3e970e411
comparison
equal deleted inserted replaced
646:f8cb74b447de 647:df58983f28a2
449 449
450 def __init__(self, *args, **kwds): 450 def __init__(self, *args, **kwds):
451 # 451 #
452 # PY2.7 compat: must be set before calling the superclass' __init__ 452 # PY2.7 compat: must be set before calling the superclass' __init__
453 # 453 #
454 self.enable_cache()
455 super(Configuration, self).__init__(*args, **kwds)
456
457 def clear_cache(self):
458 """Clear the internal lookup cache and the interpolation cache"""
459 if self.__lookup_cache is not None:
460 self.__lookup_cache.clear()
461 if self.__interpolation_cache is not None:
462 self.__interpolation_cache.clear()
463
464 def disable_cache(self):
465 self.__lookup_cache = None
466 self.__interpolation_cache = None
467
468 def enable_cache(self):
454 self.__lookup_cache = {} 469 self.__lookup_cache = {}
455 self.__interpolation_cache = {} 470 self.__interpolation_cache = {}
456 super(Configuration, self).__init__(*args, **kwds)
457
458 def clear_cache(self):
459 """Clear the internal lookup cache and the interpolation cache"""
460 self.__lookup_cache.clear()
461 self.__interpolation_cache.clear()
462 471
463 def __getitem__(self, key): 472 def __getitem__(self, key):
464 """Mapping and list interface that forwards to :meth:`~.getvarl_s` 473 """Mapping and list interface that forwards to :meth:`~.getvarl_s`
465 474
466 """ 475 """
832 :raise KeyError: An unexisting `path` raises a `KeyError` 841 :raise KeyError: An unexisting `path` raises a `KeyError`
833 842
834 """ 843 """
835 if not path: 844 if not path:
836 return self 845 return self
837 v = self.__lookup_cache.get(path, _MARKER) 846 use_cache = self.__lookup_cache is not None
838 if v is not _MARKER: 847 if use_cache:
839 if v is _MISSING: 848 v = self.__lookup_cache.get(path, _MARKER)
840 raise KeyError( 849 if v is not _MARKER:
841 "Configuration variable %r not found" 850 if v is _MISSING:
842 " (negative internal cache value)" % (path,)) 851 raise KeyError(
843 else: 852 "Configuration variable %r not found"
844 return v 853 " (negative internal cache value)" % (path,))
854 else:
855 return v
845 eiref = self.expand_if_reference 856 eiref = self.expand_if_reference
846 try: 857 try:
847 v = eiref(super(Configuration, self).__getitem__(path[0])) 858 v = eiref(super(Configuration, self).__getitem__(path[0]))
848 for p in path[1:]: 859 for p in path[1:]:
849 v = eiref(v[p]) 860 v = eiref(v[p])
850 except TypeError: 861 except TypeError:
851 self.__lookup_cache[path] = _MISSING 862 if use_cache:
863 self.__lookup_cache[path] = _MISSING
852 raise KeyError( 864 raise KeyError(
853 "Configuration variable %r not found" 865 "Configuration variable %r not found"
854 "(missing intermediate keys?)" % (path,)) 866 "(missing intermediate keys?)" % (path,))
855 except KeyError: 867 except KeyError:
856 self.__lookup_cache[path] = _MISSING 868 if use_cache:
869 self.__lookup_cache[path] = _MISSING
857 raise 870 raise
858 self.__lookup_cache[path] = v 871 if use_cache:
872 self.__lookup_cache[path] = v
859 return v 873 return v
860 874
861 def _lookupref(self, key): 875 def _lookupref(self, key):
862 """ 876 """
863 877
934 if s == DEL_VALUE: 948 if s == DEL_VALUE:
935 return s 949 return s
936 start = s.find(_STARTTOK, 0) 950 start = s.find(_STARTTOK, 0)
937 if start < 0: 951 if start < 0:
938 return s 952 return s
939 res = self.__interpolation_cache.get(s, _MARKER) 953 use_cache = self.__interpolation_cache is not None
940 if res is not _MARKER: 954 if use_cache:
941 if res is _MISSING: 955 res = self.__interpolation_cache.get(s, _MARKER)
942 warnings.warn("Cannot interpolate variables in string " 956 if res is not _MARKER:
943 "%r (cached)" % (s, ), 957 if res is _MISSING:
944 UserWarning, 958 warnings.warn("Cannot interpolate variables in string "
945 stacklevel=1) 959 "%r (cached)" % (s, ),
946 raise KeyError("Cannot interpolate variables in string " 960 UserWarning,
947 "%r (cached)" % (s, )) 961 stacklevel=1)
948 else: 962 raise KeyError("Cannot interpolate variables in string "
949 return res 963 "%r (cached)" % (s, ))
964 else:
965 return res
950 res = [] 966 res = []
951 res_append = res.append 967 res_append = res.append
952 rest = 0 968 rest = 0
953 use_cache = True 969 cacheable = True
954 while start != -1: 970 while start != -1:
955 res_append(s[rest:start]) 971 res_append(s[rest:start])
956 end = s.find(_ENDTOK, start) 972 end = s.find(_ENDTOK, start)
957 if end < 0: 973 if end < 0:
958 rest = start 974 rest = start
966 if NONE_FILTER in filters: 982 if NONE_FILTER in filters:
967 varvalue = None 983 varvalue = None
968 elif EMPTY_FILTER in filters: 984 elif EMPTY_FILTER in filters:
969 varvalue = _EMPTY_STR 985 varvalue = _EMPTY_STR
970 else: 986 else:
971 self.__interpolation_cache[s] = _MISSING 987 if use_cache and cacheable:
988 self.__interpolation_cache[s] = _MISSING
972 warnings.warn("Cannot interpolate variable %r in string " 989 warnings.warn("Cannot interpolate variable %r in string "
973 "%r" % (varname, s, ), 990 "%r" % (varname, s, ),
974 UserWarning, 991 UserWarning,
975 stacklevel=1) 992 stacklevel=1)
976 raise 993 raise
977 if not cacheable: 994 if not cacheable:
978 use_cache = False 995 cacheable = False
979 varvalue = self._apply_filters(filters, varvalue) 996 varvalue = self._apply_filters(filters, varvalue)
980 rest = end + 2 997 rest = end + 2
981 # 998 #
982 # Dont apply and type conversions to the variable value if 999 # Dont apply and type conversions to the variable value if
983 # the whole `s` is just one expansion 1000 # the whole `s` is just one expansion
984 # 1001 #
985 if (start == 0) and (rest == len_s): 1002 if (start == 0) and (rest == len_s):
986 if use_cache: 1003 if use_cache and cacheable:
987 self.__interpolation_cache[s] = varvalue 1004 self.__interpolation_cache[s] = varvalue
988 return varvalue 1005 return varvalue
989 if varvalue is None: 1006 if varvalue is None:
990 pass 1007 pass
991 else: 1008 else:
992 res_append(str_and_u(varvalue)) 1009 res_append(str_and_u(varvalue))
993 # don't re-evaluate because `self.getvar_s()` expands already 1010 # don't re-evaluate because `self.getvar_s()` expands already
994 start = s.find(_STARTTOK, rest) 1011 start = s.find(_STARTTOK, rest)
995 res_append(s[rest:]) 1012 res_append(s[rest:])
996 res = _EMPTY_STR.join(res) 1013 res = _EMPTY_STR.join(res)
997 if use_cache: 1014 if use_cache and cacheable:
998 self.__interpolation_cache[s] = res 1015 self.__interpolation_cache[s] = res
999 return res 1016 return res
1000 1017
1001 if _fast_interpolate_variables: 1018 if _fast_interpolate_variables:
1002 1019