util.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. # mako/util.py
  2. # Copyright 2006-2025 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. from ast import parse
  7. import codecs
  8. import collections
  9. import operator
  10. import os
  11. import re
  12. import timeit
  13. from .compat import importlib_metadata_get
  14. def update_wrapper(decorated, fn):
  15. decorated.__wrapped__ = fn
  16. decorated.__name__ = fn.__name__
  17. return decorated
  18. class PluginLoader:
  19. def __init__(self, group):
  20. self.group = group
  21. self.impls = {}
  22. def load(self, name):
  23. if name in self.impls:
  24. return self.impls[name]()
  25. for impl in importlib_metadata_get(self.group):
  26. if impl.name == name:
  27. self.impls[name] = impl.load
  28. return impl.load()
  29. from mako import exceptions
  30. raise exceptions.RuntimeException(
  31. "Can't load plugin %s %s" % (self.group, name)
  32. )
  33. def register(self, name, modulepath, objname):
  34. def load():
  35. mod = __import__(modulepath)
  36. for token in modulepath.split(".")[1:]:
  37. mod = getattr(mod, token)
  38. return getattr(mod, objname)
  39. self.impls[name] = load
  40. def verify_directory(dir_):
  41. """create and/or verify a filesystem directory."""
  42. tries = 0
  43. while not os.path.exists(dir_):
  44. try:
  45. tries += 1
  46. os.makedirs(dir_, 0o755)
  47. except:
  48. if tries > 5:
  49. raise
  50. def to_list(x, default=None):
  51. if x is None:
  52. return default
  53. if not isinstance(x, (list, tuple)):
  54. return [x]
  55. else:
  56. return x
  57. class memoized_property:
  58. """A read-only @property that is only evaluated once."""
  59. def __init__(self, fget, doc=None):
  60. self.fget = fget
  61. self.__doc__ = doc or fget.__doc__
  62. self.__name__ = fget.__name__
  63. def __get__(self, obj, cls):
  64. if obj is None:
  65. return self
  66. obj.__dict__[self.__name__] = result = self.fget(obj)
  67. return result
  68. class memoized_instancemethod:
  69. """Decorate a method memoize its return value.
  70. Best applied to no-arg methods: memoization is not sensitive to
  71. argument values, and will always return the same value even when
  72. called with different arguments.
  73. """
  74. def __init__(self, fget, doc=None):
  75. self.fget = fget
  76. self.__doc__ = doc or fget.__doc__
  77. self.__name__ = fget.__name__
  78. def __get__(self, obj, cls):
  79. if obj is None:
  80. return self
  81. def oneshot(*args, **kw):
  82. result = self.fget(obj, *args, **kw)
  83. def memo(*a, **kw):
  84. return result
  85. memo.__name__ = self.__name__
  86. memo.__doc__ = self.__doc__
  87. obj.__dict__[self.__name__] = memo
  88. return result
  89. oneshot.__name__ = self.__name__
  90. oneshot.__doc__ = self.__doc__
  91. return oneshot
  92. class SetLikeDict(dict):
  93. """a dictionary that has some setlike methods on it"""
  94. def union(self, other):
  95. """produce a 'union' of this dict and another (at the key level).
  96. values in the second dict take precedence over that of the first"""
  97. x = SetLikeDict(**self)
  98. x.update(other)
  99. return x
  100. class FastEncodingBuffer:
  101. """a very rudimentary buffer that is faster than StringIO,
  102. and supports unicode data."""
  103. def __init__(self, encoding=None, errors="strict"):
  104. self.data = collections.deque()
  105. self.encoding = encoding
  106. self.delim = ""
  107. self.errors = errors
  108. self.write = self.data.append
  109. def truncate(self):
  110. self.data = collections.deque()
  111. self.write = self.data.append
  112. def getvalue(self):
  113. if self.encoding:
  114. return self.delim.join(self.data).encode(
  115. self.encoding, self.errors
  116. )
  117. else:
  118. return self.delim.join(self.data)
  119. class LRUCache(dict):
  120. """A dictionary-like object that stores a limited number of items,
  121. discarding lesser used items periodically.
  122. this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
  123. paradigm so that synchronization is not really needed. the size management
  124. is inexact.
  125. """
  126. class _Item:
  127. def __init__(self, key, value):
  128. self.key = key
  129. self.value = value
  130. self.timestamp = timeit.default_timer()
  131. def __repr__(self):
  132. return repr(self.value)
  133. def __init__(self, capacity, threshold=0.5):
  134. self.capacity = capacity
  135. self.threshold = threshold
  136. def __getitem__(self, key):
  137. item = dict.__getitem__(self, key)
  138. item.timestamp = timeit.default_timer()
  139. return item.value
  140. def values(self):
  141. return [i.value for i in dict.values(self)]
  142. def setdefault(self, key, value):
  143. if key in self:
  144. return self[key]
  145. self[key] = value
  146. return value
  147. def __setitem__(self, key, value):
  148. item = dict.get(self, key)
  149. if item is None:
  150. item = self._Item(key, value)
  151. dict.__setitem__(self, key, item)
  152. else:
  153. item.value = value
  154. self._manage_size()
  155. def _manage_size(self):
  156. while len(self) > self.capacity + self.capacity * self.threshold:
  157. bytime = sorted(
  158. dict.values(self),
  159. key=operator.attrgetter("timestamp"),
  160. reverse=True,
  161. )
  162. for item in bytime[self.capacity :]:
  163. try:
  164. del self[item.key]
  165. except KeyError:
  166. # if we couldn't find a key, most likely some other thread
  167. # broke in on us. loop around and try again
  168. break
  169. # Regexp to match python magic encoding line
  170. _PYTHON_MAGIC_COMMENT_re = re.compile(
  171. r"[ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)", re.VERBOSE
  172. )
  173. def parse_encoding(fp):
  174. """Deduce the encoding of a Python source file (binary mode) from magic
  175. comment.
  176. It does this in the same way as the `Python interpreter`__
  177. .. __: http://docs.python.org/ref/encodings.html
  178. The ``fp`` argument should be a seekable file object in binary mode.
  179. """
  180. pos = fp.tell()
  181. fp.seek(0)
  182. try:
  183. line1 = fp.readline()
  184. has_bom = line1.startswith(codecs.BOM_UTF8)
  185. if has_bom:
  186. line1 = line1[len(codecs.BOM_UTF8) :]
  187. m = _PYTHON_MAGIC_COMMENT_re.match(line1.decode("ascii", "ignore"))
  188. if not m:
  189. try:
  190. parse(line1.decode("ascii", "ignore"))
  191. except (ImportError, SyntaxError):
  192. # Either it's a real syntax error, in which case the source
  193. # is not valid python source, or line2 is a continuation of
  194. # line1, in which case we don't want to scan line2 for a magic
  195. # comment.
  196. pass
  197. else:
  198. line2 = fp.readline()
  199. m = _PYTHON_MAGIC_COMMENT_re.match(
  200. line2.decode("ascii", "ignore")
  201. )
  202. if has_bom:
  203. if m:
  204. raise SyntaxError(
  205. "python refuses to compile code with both a UTF8"
  206. " byte-order-mark and a magic encoding comment"
  207. )
  208. return "utf_8"
  209. elif m:
  210. return m.group(1)
  211. else:
  212. return None
  213. finally:
  214. fp.seek(pos)
  215. def sorted_dict_repr(d):
  216. """repr() a dictionary with the keys in order.
  217. Used by the lexer unit test to compare parse trees based on strings.
  218. """
  219. keys = list(d.keys())
  220. keys.sort()
  221. return "{" + ", ".join("%r: %r" % (k, d[k]) for k in keys) + "}"
  222. def restore__ast(_ast):
  223. """Attempt to restore the required classes to the _ast module if it
  224. appears to be missing them
  225. """
  226. if hasattr(_ast, "AST"):
  227. return
  228. _ast.PyCF_ONLY_AST = 2 << 9
  229. m = compile(
  230. """\
  231. def foo(): pass
  232. class Bar: pass
  233. if False: pass
  234. baz = 'mako'
  235. 1 + 2 - 3 * 4 / 5
  236. 6 // 7 % 8 << 9 >> 10
  237. 11 & 12 ^ 13 | 14
  238. 15 and 16 or 17
  239. -baz + (not +18) - ~17
  240. baz and 'foo' or 'bar'
  241. (mako is baz == baz) is not baz != mako
  242. mako > baz < mako >= baz <= mako
  243. mako in baz not in mako""",
  244. "<unknown>",
  245. "exec",
  246. _ast.PyCF_ONLY_AST,
  247. )
  248. _ast.Module = type(m)
  249. for cls in _ast.Module.__mro__:
  250. if cls.__name__ == "mod":
  251. _ast.mod = cls
  252. elif cls.__name__ == "AST":
  253. _ast.AST = cls
  254. _ast.FunctionDef = type(m.body[0])
  255. _ast.ClassDef = type(m.body[1])
  256. _ast.If = type(m.body[2])
  257. _ast.Name = type(m.body[3].targets[0])
  258. _ast.Store = type(m.body[3].targets[0].ctx)
  259. _ast.Str = type(m.body[3].value)
  260. _ast.Sub = type(m.body[4].value.op)
  261. _ast.Add = type(m.body[4].value.left.op)
  262. _ast.Div = type(m.body[4].value.right.op)
  263. _ast.Mult = type(m.body[4].value.right.left.op)
  264. _ast.RShift = type(m.body[5].value.op)
  265. _ast.LShift = type(m.body[5].value.left.op)
  266. _ast.Mod = type(m.body[5].value.left.left.op)
  267. _ast.FloorDiv = type(m.body[5].value.left.left.left.op)
  268. _ast.BitOr = type(m.body[6].value.op)
  269. _ast.BitXor = type(m.body[6].value.left.op)
  270. _ast.BitAnd = type(m.body[6].value.left.left.op)
  271. _ast.Or = type(m.body[7].value.op)
  272. _ast.And = type(m.body[7].value.values[0].op)
  273. _ast.Invert = type(m.body[8].value.right.op)
  274. _ast.Not = type(m.body[8].value.left.right.op)
  275. _ast.UAdd = type(m.body[8].value.left.right.operand.op)
  276. _ast.USub = type(m.body[8].value.left.left.op)
  277. _ast.Or = type(m.body[9].value.op)
  278. _ast.And = type(m.body[9].value.values[0].op)
  279. _ast.IsNot = type(m.body[10].value.ops[0])
  280. _ast.NotEq = type(m.body[10].value.ops[1])
  281. _ast.Is = type(m.body[10].value.left.ops[0])
  282. _ast.Eq = type(m.body[10].value.left.ops[1])
  283. _ast.Gt = type(m.body[11].value.ops[0])
  284. _ast.Lt = type(m.body[11].value.ops[1])
  285. _ast.GtE = type(m.body[11].value.ops[2])
  286. _ast.LtE = type(m.body[11].value.ops[3])
  287. _ast.In = type(m.body[12].value.ops[0])
  288. _ast.NotIn = type(m.body[12].value.ops[1])
  289. def read_file(path, mode="rb"):
  290. with open(path, mode) as fp:
  291. return fp.read()
  292. def read_python_file(path):
  293. fp = open(path, "rb")
  294. try:
  295. encoding = parse_encoding(fp)
  296. data = fp.read()
  297. if encoding:
  298. data = data.decode(encoding)
  299. return data
  300. finally:
  301. fp.close()