Cython 3.2.0__cp39-abi3-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (333) hide show
  1. Cython/Build/BuildExecutable.py +169 -0
  2. Cython/Build/Cache.py +199 -0
  3. Cython/Build/Cythonize.py +350 -0
  4. Cython/Build/Dependencies.py +1314 -0
  5. Cython/Build/Distutils.py +1 -0
  6. Cython/Build/Inline.py +463 -0
  7. Cython/Build/IpythonMagic.py +560 -0
  8. Cython/Build/SharedModule.py +94 -0
  9. Cython/Build/Tests/TestCyCache.py +194 -0
  10. Cython/Build/Tests/TestCythonizeArgsParser.py +481 -0
  11. Cython/Build/Tests/TestDependencies.py +133 -0
  12. Cython/Build/Tests/TestInline.py +177 -0
  13. Cython/Build/Tests/TestIpythonMagic.py +287 -0
  14. Cython/Build/Tests/TestRecythonize.py +212 -0
  15. Cython/Build/Tests/TestStripLiterals.py +155 -0
  16. Cython/Build/Tests/__init__.py +1 -0
  17. Cython/Build/__init__.py +11 -0
  18. Cython/CodeWriter.py +815 -0
  19. Cython/Compiler/AnalysedTreeTransforms.py +97 -0
  20. Cython/Compiler/Annotate.py +328 -0
  21. Cython/Compiler/AutoDocTransforms.py +320 -0
  22. Cython/Compiler/Buffer.py +680 -0
  23. Cython/Compiler/Builtin.py +984 -0
  24. Cython/Compiler/CmdLine.py +263 -0
  25. Cython/Compiler/Code.pxd +149 -0
  26. Cython/Compiler/Code.py +3746 -0
  27. Cython/Compiler/Code.pyd +0 -0
  28. Cython/Compiler/CodeGeneration.py +33 -0
  29. Cython/Compiler/CythonScope.py +191 -0
  30. Cython/Compiler/Dataclass.py +864 -0
  31. Cython/Compiler/DebugFlags.py +24 -0
  32. Cython/Compiler/Errors.py +297 -0
  33. Cython/Compiler/ExprNodes.py +15562 -0
  34. Cython/Compiler/FlowControl.pxd +97 -0
  35. Cython/Compiler/FlowControl.py +1451 -0
  36. Cython/Compiler/FlowControl.pyd +0 -0
  37. Cython/Compiler/FusedNode.py +971 -0
  38. Cython/Compiler/FusedNode.pyd +0 -0
  39. Cython/Compiler/Future.py +16 -0
  40. Cython/Compiler/Interpreter.py +57 -0
  41. Cython/Compiler/Lexicon.py +421 -0
  42. Cython/Compiler/LineTable.py +114 -0
  43. Cython/Compiler/LineTable.pyd +0 -0
  44. Cython/Compiler/Main.py +857 -0
  45. Cython/Compiler/MatchCaseNodes.py +259 -0
  46. Cython/Compiler/MemoryView.py +905 -0
  47. Cython/Compiler/ModuleNode.py +4235 -0
  48. Cython/Compiler/Naming.py +363 -0
  49. Cython/Compiler/Nodes.py +10831 -0
  50. Cython/Compiler/Optimize.py +5288 -0
  51. Cython/Compiler/Options.py +843 -0
  52. Cython/Compiler/ParseTreeTransforms.pxd +78 -0
  53. Cython/Compiler/ParseTreeTransforms.py +4638 -0
  54. Cython/Compiler/Parsing.pxd +9 -0
  55. Cython/Compiler/Parsing.py +4775 -0
  56. Cython/Compiler/Parsing.pyd +0 -0
  57. Cython/Compiler/Pipeline.py +439 -0
  58. Cython/Compiler/PyrexTypes.py +5870 -0
  59. Cython/Compiler/Pythran.py +232 -0
  60. Cython/Compiler/Scanning.pxd +48 -0
  61. Cython/Compiler/Scanning.py +701 -0
  62. Cython/Compiler/Scanning.pyd +0 -0
  63. Cython/Compiler/StringEncoding.py +298 -0
  64. Cython/Compiler/Symtab.py +3073 -0
  65. Cython/Compiler/Tests/TestBuffer.py +105 -0
  66. Cython/Compiler/Tests/TestBuiltin.py +72 -0
  67. Cython/Compiler/Tests/TestCmdLine.py +586 -0
  68. Cython/Compiler/Tests/TestCode.py +144 -0
  69. Cython/Compiler/Tests/TestFlowControl.py +65 -0
  70. Cython/Compiler/Tests/TestGrammar.py +202 -0
  71. Cython/Compiler/Tests/TestMemView.py +71 -0
  72. Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
  73. Cython/Compiler/Tests/TestScanning.py +134 -0
  74. Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
  75. Cython/Compiler/Tests/TestStringEncoding.py +21 -0
  76. Cython/Compiler/Tests/TestTreeFragment.py +63 -0
  77. Cython/Compiler/Tests/TestTreePath.py +103 -0
  78. Cython/Compiler/Tests/TestTypes.py +75 -0
  79. Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
  80. Cython/Compiler/Tests/TestVisitor.py +61 -0
  81. Cython/Compiler/Tests/Utils.py +36 -0
  82. Cython/Compiler/Tests/__init__.py +1 -0
  83. Cython/Compiler/TreeFragment.py +278 -0
  84. Cython/Compiler/TreePath.py +303 -0
  85. Cython/Compiler/TypeInference.py +591 -0
  86. Cython/Compiler/TypeSlots.py +1174 -0
  87. Cython/Compiler/UFuncs.py +311 -0
  88. Cython/Compiler/UtilNodes.py +389 -0
  89. Cython/Compiler/UtilityCode.py +344 -0
  90. Cython/Compiler/Version.py +8 -0
  91. Cython/Compiler/Visitor.pxd +53 -0
  92. Cython/Compiler/Visitor.py +861 -0
  93. Cython/Compiler/Visitor.pyd +0 -0
  94. Cython/Compiler/__init__.py +1 -0
  95. Cython/Coverage.py +448 -0
  96. Cython/Debugger/Cygdb.py +177 -0
  97. Cython/Debugger/DebugWriter.py +82 -0
  98. Cython/Debugger/Tests/TestLibCython.py +275 -0
  99. Cython/Debugger/Tests/__init__.py +1 -0
  100. Cython/Debugger/Tests/cfuncs.c +8 -0
  101. Cython/Debugger/Tests/codefile +49 -0
  102. Cython/Debugger/Tests/test_libcython_in_gdb.py +578 -0
  103. Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
  104. Cython/Debugger/__init__.py +1 -0
  105. Cython/Debugger/libcython.py +1548 -0
  106. Cython/Debugger/libpython.py +2821 -0
  107. Cython/Debugging.py +20 -0
  108. Cython/Distutils/__init__.py +2 -0
  109. Cython/Distutils/build_ext.py +139 -0
  110. Cython/Distutils/extension.py +96 -0
  111. Cython/Distutils/old_build_ext.py +351 -0
  112. Cython/Includes/cpython/__init__.pxd +173 -0
  113. Cython/Includes/cpython/array.pxd +178 -0
  114. Cython/Includes/cpython/bool.pxd +37 -0
  115. Cython/Includes/cpython/buffer.pxd +112 -0
  116. Cython/Includes/cpython/bytearray.pxd +33 -0
  117. Cython/Includes/cpython/bytes.pxd +200 -0
  118. Cython/Includes/cpython/cellobject.pxd +35 -0
  119. Cython/Includes/cpython/ceval.pxd +8 -0
  120. Cython/Includes/cpython/codecs.pxd +121 -0
  121. Cython/Includes/cpython/complex.pxd +60 -0
  122. Cython/Includes/cpython/contextvars.pxd +145 -0
  123. Cython/Includes/cpython/conversion.pxd +36 -0
  124. Cython/Includes/cpython/datetime.pxd +395 -0
  125. Cython/Includes/cpython/descr.pxd +26 -0
  126. Cython/Includes/cpython/dict.pxd +187 -0
  127. Cython/Includes/cpython/exc.pxd +263 -0
  128. Cython/Includes/cpython/fileobject.pxd +57 -0
  129. Cython/Includes/cpython/float.pxd +47 -0
  130. Cython/Includes/cpython/function.pxd +65 -0
  131. Cython/Includes/cpython/genobject.pxd +25 -0
  132. Cython/Includes/cpython/getargs.pxd +12 -0
  133. Cython/Includes/cpython/instance.pxd +25 -0
  134. Cython/Includes/cpython/iterator.pxd +36 -0
  135. Cython/Includes/cpython/iterobject.pxd +24 -0
  136. Cython/Includes/cpython/list.pxd +92 -0
  137. Cython/Includes/cpython/long.pxd +149 -0
  138. Cython/Includes/cpython/longintrepr.pxd +14 -0
  139. Cython/Includes/cpython/mapping.pxd +63 -0
  140. Cython/Includes/cpython/marshal.pxd +66 -0
  141. Cython/Includes/cpython/mem.pxd +120 -0
  142. Cython/Includes/cpython/memoryview.pxd +50 -0
  143. Cython/Includes/cpython/method.pxd +49 -0
  144. Cython/Includes/cpython/module.pxd +208 -0
  145. Cython/Includes/cpython/number.pxd +258 -0
  146. Cython/Includes/cpython/object.pxd +433 -0
  147. Cython/Includes/cpython/pycapsule.pxd +143 -0
  148. Cython/Includes/cpython/pylifecycle.pxd +68 -0
  149. Cython/Includes/cpython/pyport.pxd +8 -0
  150. Cython/Includes/cpython/pystate.pxd +95 -0
  151. Cython/Includes/cpython/pythread.pxd +53 -0
  152. Cython/Includes/cpython/ref.pxd +141 -0
  153. Cython/Includes/cpython/sequence.pxd +134 -0
  154. Cython/Includes/cpython/set.pxd +119 -0
  155. Cython/Includes/cpython/slice.pxd +70 -0
  156. Cython/Includes/cpython/time.pxd +129 -0
  157. Cython/Includes/cpython/tuple.pxd +72 -0
  158. Cython/Includes/cpython/type.pxd +53 -0
  159. Cython/Includes/cpython/unicode.pxd +639 -0
  160. Cython/Includes/cpython/version.pxd +32 -0
  161. Cython/Includes/cpython/weakref.pxd +78 -0
  162. Cython/Includes/libc/__init__.pxd +1 -0
  163. Cython/Includes/libc/complex.pxd +35 -0
  164. Cython/Includes/libc/errno.pxd +127 -0
  165. Cython/Includes/libc/float.pxd +43 -0
  166. Cython/Includes/libc/limits.pxd +28 -0
  167. Cython/Includes/libc/locale.pxd +46 -0
  168. Cython/Includes/libc/math.pxd +209 -0
  169. Cython/Includes/libc/setjmp.pxd +10 -0
  170. Cython/Includes/libc/signal.pxd +64 -0
  171. Cython/Includes/libc/stddef.pxd +9 -0
  172. Cython/Includes/libc/stdint.pxd +105 -0
  173. Cython/Includes/libc/stdio.pxd +80 -0
  174. Cython/Includes/libc/stdlib.pxd +72 -0
  175. Cython/Includes/libc/string.pxd +50 -0
  176. Cython/Includes/libc/threads.pxd +234 -0
  177. Cython/Includes/libc/time.pxd +52 -0
  178. Cython/Includes/libcpp/__init__.pxd +4 -0
  179. Cython/Includes/libcpp/algorithm.pxd +320 -0
  180. Cython/Includes/libcpp/any.pxd +16 -0
  181. Cython/Includes/libcpp/atomic.pxd +59 -0
  182. Cython/Includes/libcpp/barrier.pxd +22 -0
  183. Cython/Includes/libcpp/bit.pxd +29 -0
  184. Cython/Includes/libcpp/cast.pxd +12 -0
  185. Cython/Includes/libcpp/cmath.pxd +518 -0
  186. Cython/Includes/libcpp/complex.pxd +106 -0
  187. Cython/Includes/libcpp/condition_variable.pxd +322 -0
  188. Cython/Includes/libcpp/deque.pxd +165 -0
  189. Cython/Includes/libcpp/exception.pxd +86 -0
  190. Cython/Includes/libcpp/execution.pxd +15 -0
  191. Cython/Includes/libcpp/forward_list.pxd +63 -0
  192. Cython/Includes/libcpp/functional.pxd +26 -0
  193. Cython/Includes/libcpp/future.pxd +103 -0
  194. Cython/Includes/libcpp/iterator.pxd +34 -0
  195. Cython/Includes/libcpp/latch.pxd +17 -0
  196. Cython/Includes/libcpp/limits.pxd +61 -0
  197. Cython/Includes/libcpp/list.pxd +117 -0
  198. Cython/Includes/libcpp/map.pxd +252 -0
  199. Cython/Includes/libcpp/memory.pxd +115 -0
  200. Cython/Includes/libcpp/mutex.pxd +387 -0
  201. Cython/Includes/libcpp/numbers.pxd +15 -0
  202. Cython/Includes/libcpp/numeric.pxd +131 -0
  203. Cython/Includes/libcpp/optional.pxd +34 -0
  204. Cython/Includes/libcpp/pair.pxd +1 -0
  205. Cython/Includes/libcpp/queue.pxd +25 -0
  206. Cython/Includes/libcpp/random.pxd +166 -0
  207. Cython/Includes/libcpp/semaphore.pxd +43 -0
  208. Cython/Includes/libcpp/set.pxd +228 -0
  209. Cython/Includes/libcpp/shared_mutex.pxd +96 -0
  210. Cython/Includes/libcpp/span.pxd +87 -0
  211. Cython/Includes/libcpp/stack.pxd +11 -0
  212. Cython/Includes/libcpp/stop_token.pxd +117 -0
  213. Cython/Includes/libcpp/string.pxd +355 -0
  214. Cython/Includes/libcpp/string_view.pxd +183 -0
  215. Cython/Includes/libcpp/typeindex.pxd +15 -0
  216. Cython/Includes/libcpp/typeinfo.pxd +10 -0
  217. Cython/Includes/libcpp/unordered_map.pxd +193 -0
  218. Cython/Includes/libcpp/unordered_set.pxd +152 -0
  219. Cython/Includes/libcpp/utility.pxd +30 -0
  220. Cython/Includes/libcpp/vector.pxd +186 -0
  221. Cython/Includes/numpy/math.pxd +150 -0
  222. Cython/Includes/openmp.pxd +50 -0
  223. Cython/Includes/posix/__init__.pxd +1 -0
  224. Cython/Includes/posix/dlfcn.pxd +14 -0
  225. Cython/Includes/posix/fcntl.pxd +86 -0
  226. Cython/Includes/posix/ioctl.pxd +4 -0
  227. Cython/Includes/posix/mman.pxd +101 -0
  228. Cython/Includes/posix/resource.pxd +57 -0
  229. Cython/Includes/posix/select.pxd +21 -0
  230. Cython/Includes/posix/signal.pxd +73 -0
  231. Cython/Includes/posix/stat.pxd +98 -0
  232. Cython/Includes/posix/stdio.pxd +37 -0
  233. Cython/Includes/posix/stdlib.pxd +29 -0
  234. Cython/Includes/posix/strings.pxd +9 -0
  235. Cython/Includes/posix/time.pxd +71 -0
  236. Cython/Includes/posix/types.pxd +30 -0
  237. Cython/Includes/posix/uio.pxd +26 -0
  238. Cython/Includes/posix/unistd.pxd +271 -0
  239. Cython/Includes/posix/wait.pxd +38 -0
  240. Cython/Plex/Actions.pxd +24 -0
  241. Cython/Plex/Actions.py +119 -0
  242. Cython/Plex/Actions.pyd +0 -0
  243. Cython/Plex/DFA.pxd +14 -0
  244. Cython/Plex/DFA.py +164 -0
  245. Cython/Plex/DFA.pyd +0 -0
  246. Cython/Plex/Errors.py +48 -0
  247. Cython/Plex/Lexicons.py +178 -0
  248. Cython/Plex/Machines.pxd +36 -0
  249. Cython/Plex/Machines.py +238 -0
  250. Cython/Plex/Machines.pyd +0 -0
  251. Cython/Plex/Regexps.py +535 -0
  252. Cython/Plex/Scanners.pxd +45 -0
  253. Cython/Plex/Scanners.py +328 -0
  254. Cython/Plex/Scanners.pyd +0 -0
  255. Cython/Plex/Transitions.pxd +14 -0
  256. Cython/Plex/Transitions.py +239 -0
  257. Cython/Plex/Transitions.pyd +0 -0
  258. Cython/Plex/__init__.py +34 -0
  259. Cython/Runtime/__init__.py +1 -0
  260. Cython/Runtime/refnanny.pyd +0 -0
  261. Cython/Runtime/refnanny.pyx +237 -0
  262. Cython/Shadow.py +690 -0
  263. Cython/Shadow.pyi +521 -0
  264. Cython/StringIOTree.py +170 -0
  265. Cython/StringIOTree.pyd +0 -0
  266. Cython/Tempita/__init__.py +4 -0
  267. Cython/Tempita/_looper.py +154 -0
  268. Cython/Tempita/_tempita.py +1091 -0
  269. Cython/Tempita/_tempita.pyd +0 -0
  270. Cython/TestUtils.py +422 -0
  271. Cython/Tests/TestCodeWriter.py +128 -0
  272. Cython/Tests/TestCythonUtils.py +202 -0
  273. Cython/Tests/TestJediTyper.py +223 -0
  274. Cython/Tests/TestShadow.py +114 -0
  275. Cython/Tests/TestStringIOTree.py +67 -0
  276. Cython/Tests/TestTestUtils.py +90 -0
  277. Cython/Tests/__init__.py +1 -0
  278. Cython/Tests/xmlrunner.py +390 -0
  279. Cython/Utility/AsyncGen.c +1031 -0
  280. Cython/Utility/Buffer.c +865 -0
  281. Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
  282. Cython/Utility/Builtins.c +810 -0
  283. Cython/Utility/CConvert.pyx +134 -0
  284. Cython/Utility/CMath.c +104 -0
  285. Cython/Utility/CommonStructures.c +226 -0
  286. Cython/Utility/Complex.c +378 -0
  287. Cython/Utility/Coroutine.c +2300 -0
  288. Cython/Utility/CpdefEnums.pyx +103 -0
  289. Cython/Utility/CppConvert.pyx +282 -0
  290. Cython/Utility/CppSupport.cpp +151 -0
  291. Cython/Utility/CythonFunction.c +1832 -0
  292. Cython/Utility/Dataclasses.c +101 -0
  293. Cython/Utility/Embed.c +121 -0
  294. Cython/Utility/Exceptions.c +1016 -0
  295. Cython/Utility/ExtensionTypes.c +996 -0
  296. Cython/Utility/FunctionArguments.c +1043 -0
  297. Cython/Utility/FusedFunction.pyx +44 -0
  298. Cython/Utility/ImportExport.c +907 -0
  299. Cython/Utility/MemoryView.pxd +188 -0
  300. Cython/Utility/MemoryView.pyx +1482 -0
  301. Cython/Utility/MemoryView_C.c +927 -0
  302. Cython/Utility/ModuleSetupCode.c +3203 -0
  303. Cython/Utility/NumpyImportArray.c +46 -0
  304. Cython/Utility/ObjectHandling.c +3273 -0
  305. Cython/Utility/Optimize.c +1603 -0
  306. Cython/Utility/Overflow.c +384 -0
  307. Cython/Utility/Printing.c +86 -0
  308. Cython/Utility/Profile.c +732 -0
  309. Cython/Utility/StringTools.c +1379 -0
  310. Cython/Utility/Synchronization.c +399 -0
  311. Cython/Utility/TString.c +356 -0
  312. Cython/Utility/TestCyUtilityLoader.pyx +8 -0
  313. Cython/Utility/TestCythonScope.pyx +75 -0
  314. Cython/Utility/TestUtilityLoader.c +12 -0
  315. Cython/Utility/TypeConversion.c +1385 -0
  316. Cython/Utility/UFuncs.pyx +50 -0
  317. Cython/Utility/UFuncs_C.c +89 -0
  318. Cython/Utility/__init__.py +28 -0
  319. Cython/Utility/arrayarray.h +167 -0
  320. Cython/Utils.py +687 -0
  321. Cython/Utils.pyd +0 -0
  322. Cython/__init__.py +10 -0
  323. Cython/__init__.pyi +7 -0
  324. Cython/py.typed +0 -0
  325. cython-3.2.0.dist-info/METADATA +85 -0
  326. cython-3.2.0.dist-info/RECORD +333 -0
  327. cython-3.2.0.dist-info/WHEEL +5 -0
  328. cython-3.2.0.dist-info/entry_points.txt +4 -0
  329. cython-3.2.0.dist-info/top_level.txt +3 -0
  330. cython.py +29 -0
  331. pyximport/__init__.py +4 -0
  332. pyximport/pyxbuild.py +160 -0
  333. pyximport/pyximport.py +482 -0
@@ -0,0 +1,1091 @@
1
+ """
2
+ A small templating language
3
+
4
+ This implements a small templating language. This language implements
5
+ if/elif/else, for/continue/break, expressions, and blocks of Python
6
+ code. The syntax is::
7
+
8
+ {{any expression (function calls etc)}}
9
+ {{any expression | filter}}
10
+ {{for x in y}}...{{endfor}}
11
+ {{if x}}x{{elif y}}y{{else}}z{{endif}}
12
+ {{py:x=1}}
13
+ {{py:
14
+ def foo(bar):
15
+ return 'baz'
16
+ }}
17
+ {{default var = default_value}}
18
+ {{# comment}}
19
+
20
+ You use this with the ``Template`` class or the ``sub`` shortcut.
21
+ The ``Template`` class takes the template string and the name of
22
+ the template (for errors) and a default namespace. Then (like
23
+ ``string.Template``) you can call the ``tmpl.substitute(**kw)``
24
+ method to make a substitution (or ``tmpl.substitute(a_dict)``).
25
+
26
+ ``sub(content, **kw)`` substitutes the template immediately. You
27
+ can use ``__name='tmpl.html'`` to set the name of the template.
28
+
29
+ If there are syntax errors ``TemplateError`` will be raised.
30
+ """
31
+
32
+
33
+ import re
34
+ import sys
35
+ import os
36
+ import tokenize
37
+ from io import StringIO
38
+
39
+ from ._looper import looper
40
+
41
+ __all__ = ['TemplateError', 'Template', 'sub', 'bunch']
42
+
43
+ in_re = re.compile(r'\s+in\s+')
44
+ var_re = re.compile(r'^[a-z_][a-z0-9_]*$', re.I)
45
+
46
+ def coerce_text(v):
47
+ if not isinstance(v, str):
48
+ if hasattr(v, '__str__'):
49
+ return str(v)
50
+ else:
51
+ return bytes(v)
52
+ return v
53
+
54
+ class TemplateError(Exception):
55
+ """Exception raised while parsing a template
56
+ """
57
+
58
+ def __init__(self, message, position, name=None):
59
+ Exception.__init__(self, message)
60
+ self.position = position
61
+ self.name = name
62
+
63
+ def __str__(self):
64
+ msg = ' '.join(self.args)
65
+ if self.position:
66
+ msg = '%s at line %s column %s' % (
67
+ msg, self.position[0], self.position[1])
68
+ if self.name:
69
+ msg += ' in %s' % self.name
70
+ return msg
71
+
72
+
73
+ class _TemplateContinue(Exception):
74
+ pass
75
+
76
+
77
+ class _TemplateBreak(Exception):
78
+ pass
79
+
80
+
81
+ def get_file_template(name, from_template):
82
+ path = os.path.join(os.path.dirname(from_template.name), name)
83
+ return from_template.__class__.from_filename(
84
+ path, namespace=from_template.namespace,
85
+ get_template=from_template.get_template)
86
+
87
+
88
+ class Template:
89
+
90
+ default_namespace = {
91
+ 'start_braces': '{{',
92
+ 'end_braces': '}}',
93
+ 'looper': looper,
94
+ }
95
+
96
+ default_encoding = 'utf8'
97
+ default_inherit = None
98
+
99
+ def __init__(self, content, name=None, namespace=None, stacklevel=None,
100
+ get_template=None, default_inherit=None, line_offset=0,
101
+ delimiters=None, delimeters=None):
102
+ self.content = content
103
+
104
+ # set delimiters
105
+ if delimeters:
106
+ import warnings
107
+ warnings.warn(
108
+ "'delimeters' kwarg is being deprecated in favor of correctly"
109
+ " spelled 'delimiters'. Please adjust your code.",
110
+ DeprecationWarning
111
+ )
112
+ if delimiters is None:
113
+ delimiters = delimeters
114
+ if delimiters is None:
115
+ delimiters = (self.default_namespace['start_braces'],
116
+ self.default_namespace['end_braces'])
117
+ else:
118
+ #assert len(delimiters) == 2 and all([isinstance(delimiter, str)
119
+ # for delimiter in delimiters])
120
+ self.default_namespace = self.__class__.default_namespace.copy()
121
+ self.default_namespace['start_braces'] = delimiters[0]
122
+ self.default_namespace['end_braces'] = delimiters[1]
123
+ self.delimiters = self.delimeters = delimiters # Keep a legacy read-only copy, but don't use it.
124
+
125
+ self._unicode = isinstance(content, str)
126
+ if name is None and stacklevel is not None:
127
+ try:
128
+ caller = sys._getframe(stacklevel)
129
+ except ValueError:
130
+ pass
131
+ else:
132
+ globals = caller.f_globals
133
+ lineno = caller.f_lineno
134
+ if '__file__' in globals:
135
+ name = globals['__file__']
136
+ if name.endswith('.pyc') or name.endswith('.pyo'):
137
+ name = name[:-1]
138
+ elif '__name__' in globals:
139
+ name = globals['__name__']
140
+ else:
141
+ name = '<string>'
142
+ if lineno:
143
+ name += ':%s' % lineno
144
+ self.name = name
145
+ self._parsed = parse(content, name=name, line_offset=line_offset, delimiters=self.delimiters)
146
+ if namespace is None:
147
+ namespace = {}
148
+ self.namespace = namespace
149
+ self.get_template = get_template
150
+ if default_inherit is not None:
151
+ self.default_inherit = default_inherit
152
+
153
+ def from_filename(cls, filename, namespace=None, encoding=None,
154
+ default_inherit=None, get_template=get_file_template):
155
+ with open(filename, 'rb') as f:
156
+ c = f.read()
157
+ if encoding:
158
+ c = c.decode(encoding)
159
+ return cls(content=c, name=filename, namespace=namespace,
160
+ default_inherit=default_inherit, get_template=get_template)
161
+
162
+ from_filename = classmethod(from_filename)
163
+
164
+ def __repr__(self):
165
+ return '<%s %s name=%r>' % (
166
+ self.__class__.__name__,
167
+ hex(id(self))[2:], self.name)
168
+
169
+ def substitute(self, *args, **kw):
170
+ if args:
171
+ if kw:
172
+ raise TypeError(
173
+ "You can only give positional *or* keyword arguments")
174
+ if len(args) > 1:
175
+ raise TypeError(
176
+ "You can only give one positional argument")
177
+ if not hasattr(args[0], 'items'):
178
+ raise TypeError(
179
+ "If you pass in a single argument, you must pass in a dictionary-like object (with a .items() method); you gave %r"
180
+ % (args[0],))
181
+ kw = args[0]
182
+ ns = kw
183
+ ns['__template_name__'] = self.name
184
+ if self.namespace:
185
+ ns.update(self.namespace)
186
+ result, defs, inherit = self._interpret(ns)
187
+ if not inherit:
188
+ inherit = self.default_inherit
189
+ if inherit:
190
+ result = self._interpret_inherit(result, defs, inherit, ns)
191
+ return result
192
+
193
+ def _interpret(self, ns):
194
+ __traceback_hide__ = True
195
+ parts = []
196
+ defs = {}
197
+ self._interpret_codes(self._parsed, ns, out=parts, defs=defs)
198
+ if '__inherit__' in defs:
199
+ inherit = defs.pop('__inherit__')
200
+ else:
201
+ inherit = None
202
+ return ''.join(parts), defs, inherit
203
+
204
+ def _interpret_inherit(self, body, defs, inherit_template, ns):
205
+ __traceback_hide__ = True
206
+ if not self.get_template:
207
+ raise TemplateError(
208
+ 'You cannot use inheritance without passing in get_template',
209
+ position=None, name=self.name)
210
+ templ = self.get_template(inherit_template, self)
211
+ self_ = TemplateObject(self.name)
212
+ for name, value in defs.items():
213
+ setattr(self_, name, value)
214
+ self_.body = body
215
+ ns = ns.copy()
216
+ ns['self'] = self_
217
+ return templ.substitute(ns)
218
+
219
+ def _interpret_codes(self, codes, ns, out, defs):
220
+ __traceback_hide__ = True
221
+ for item in codes:
222
+ if isinstance(item, str):
223
+ out.append(item)
224
+ else:
225
+ self._interpret_code(item, ns, out, defs)
226
+
227
+ def _interpret_code(self, code, ns, out, defs):
228
+ __traceback_hide__ = True
229
+ name, pos = code[0], code[1]
230
+ if name == 'py':
231
+ self._exec(code[2], ns, pos)
232
+ elif name == 'continue':
233
+ raise _TemplateContinue()
234
+ elif name == 'break':
235
+ raise _TemplateBreak()
236
+ elif name == 'for':
237
+ vars, expr, content = code[2], code[3], code[4]
238
+ expr = self._eval(expr, ns, pos)
239
+ self._interpret_for(vars, expr, content, ns, out, defs)
240
+ elif name == 'cond':
241
+ parts = code[2:]
242
+ self._interpret_if(parts, ns, out, defs)
243
+ elif name == 'expr':
244
+ parts = code[2].split('|')
245
+ base = self._eval(parts[0], ns, pos)
246
+ for part in parts[1:]:
247
+ func = self._eval(part, ns, pos)
248
+ base = func(base)
249
+ out.append(self._repr(base, pos))
250
+ elif name == 'default':
251
+ var, expr = code[2], code[3]
252
+ if var not in ns:
253
+ result = self._eval(expr, ns, pos)
254
+ ns[var] = result
255
+ elif name == 'inherit':
256
+ expr = code[2]
257
+ value = self._eval(expr, ns, pos)
258
+ defs['__inherit__'] = value
259
+ elif name == 'def':
260
+ name = code[2]
261
+ signature = code[3]
262
+ parts = code[4]
263
+ ns[name] = defs[name] = TemplateDef(self, name, signature, body=parts, ns=ns,
264
+ pos=pos)
265
+ elif name == 'comment':
266
+ return
267
+ else:
268
+ assert 0, "Unknown code: %r" % name
269
+
270
+ def _interpret_for(self, vars, expr, content, ns, out, defs):
271
+ __traceback_hide__ = True
272
+ for item in expr:
273
+ if len(vars) == 1:
274
+ ns[vars[0]] = item
275
+ else:
276
+ if len(vars) != len(item):
277
+ raise ValueError(
278
+ 'Need %i items to unpack (got %i items)'
279
+ % (len(vars), len(item)))
280
+ for name, value in zip(vars, item):
281
+ ns[name] = value
282
+ try:
283
+ self._interpret_codes(content, ns, out, defs)
284
+ except _TemplateContinue:
285
+ continue
286
+ except _TemplateBreak:
287
+ break
288
+
289
+ def _interpret_if(self, parts, ns, out, defs):
290
+ __traceback_hide__ = True
291
+ # @@: if/else/else gets through
292
+ for part in parts:
293
+ assert not isinstance(part, str)
294
+ name, pos = part[0], part[1]
295
+ if name == 'else':
296
+ result = True
297
+ else:
298
+ result = self._eval(part[2], ns, pos)
299
+ if result:
300
+ self._interpret_codes(part[3], ns, out, defs)
301
+ break
302
+
303
+ def _eval(self, code, ns, pos):
304
+ __traceback_hide__ = True
305
+ try:
306
+ try:
307
+ value = eval(code, self.default_namespace, ns)
308
+ except SyntaxError as e:
309
+ raise SyntaxError(
310
+ 'invalid syntax in expression: %s' % code)
311
+ return value
312
+ except Exception as e:
313
+ if getattr(e, 'args', None):
314
+ arg0 = e.args[0]
315
+ else:
316
+ arg0 = coerce_text(e)
317
+ e.args = (self._add_line_info(arg0, pos),)
318
+ raise
319
+
320
+ def _exec(self, code, ns, pos):
321
+ __traceback_hide__ = True
322
+ try:
323
+ exec(code, self.default_namespace, ns)
324
+ except Exception as e:
325
+ if e.args:
326
+ e.args = (self._add_line_info(e.args[0], pos),)
327
+ else:
328
+ e.args = (self._add_line_info(None, pos),)
329
+ raise
330
+
331
+ def _repr(self, value, pos):
332
+ __traceback_hide__ = True
333
+ try:
334
+ if value is None:
335
+ return ''
336
+ if self._unicode:
337
+ try:
338
+ value = str(value)
339
+ except UnicodeDecodeError:
340
+ value = bytes(value)
341
+ else:
342
+ if not isinstance(value, str):
343
+ value = coerce_text(value)
344
+ if (isinstance(value, str)
345
+ and self.default_encoding):
346
+ value = value.encode(self.default_encoding)
347
+ except Exception as e:
348
+ e.args = (self._add_line_info(e.args[0], pos),)
349
+ raise
350
+ else:
351
+ if self._unicode and isinstance(value, bytes):
352
+ if not self.default_encoding:
353
+ raise UnicodeDecodeError(
354
+ 'Cannot decode bytes value %r into unicode '
355
+ '(no default_encoding provided)' % value)
356
+ try:
357
+ value = value.decode(self.default_encoding)
358
+ except UnicodeDecodeError as e:
359
+ raise UnicodeDecodeError(
360
+ e.encoding,
361
+ e.object,
362
+ e.start,
363
+ e.end,
364
+ e.reason + ' in string %r' % value)
365
+ elif not self._unicode and isinstance(value, str):
366
+ if not self.default_encoding:
367
+ raise UnicodeEncodeError(
368
+ 'Cannot encode unicode value %r into bytes '
369
+ '(no default_encoding provided)' % value)
370
+ value = value.encode(self.default_encoding)
371
+ return value
372
+
373
+ def _add_line_info(self, msg, pos):
374
+ msg = "%s at line %s column %s" % (
375
+ msg, pos[0], pos[1])
376
+ if self.name:
377
+ msg += " in file %s" % self.name
378
+ return msg
379
+
380
+
381
+ def sub(content, delimiters=None, **kw):
382
+ name = kw.get('__name')
383
+ delimeters = kw.pop('delimeters') if 'delimeters' in kw else None # for legacy code
384
+ tmpl = Template(content, name=name, delimiters=delimiters, delimeters=delimeters)
385
+ return tmpl.substitute(kw)
386
+
387
+
388
+ def paste_script_template_renderer(content, vars, filename=None):
389
+ tmpl = Template(content, name=filename)
390
+ return tmpl.substitute(vars)
391
+
392
+
393
+ class bunch(dict):
394
+
395
+ def __init__(self, **kw):
396
+ for name, value in kw.items():
397
+ setattr(self, name, value)
398
+
399
+ def __setattr__(self, name, value):
400
+ self[name] = value
401
+
402
+ def __getattr__(self, name):
403
+ try:
404
+ return self[name]
405
+ except KeyError:
406
+ raise AttributeError(name)
407
+
408
+ def __getitem__(self, key):
409
+ if 'default' in self:
410
+ try:
411
+ return dict.__getitem__(self, key)
412
+ except KeyError:
413
+ return dict.__getitem__(self, 'default')
414
+ else:
415
+ return dict.__getitem__(self, key)
416
+
417
+ def __repr__(self):
418
+ return '<%s %s>' % (
419
+ self.__class__.__name__,
420
+ ' '.join(['%s=%r' % (k, v) for k, v in sorted(self.items())]))
421
+
422
+
423
+ class TemplateDef:
424
+ def __init__(self, template, func_name, func_signature,
425
+ body, ns, pos, bound_self=None):
426
+ self._template = template
427
+ self._func_name = func_name
428
+ self._func_signature = func_signature
429
+ self._body = body
430
+ self._ns = ns
431
+ self._pos = pos
432
+ self._bound_self = bound_self
433
+
434
+ def __repr__(self):
435
+ return '<tempita function %s(%s) at %s:%s>' % (
436
+ self._func_name, self._func_signature,
437
+ self._template.name, self._pos)
438
+
439
+ def __str__(self):
440
+ return self()
441
+
442
+ def __call__(self, *args, **kw):
443
+ values = self._parse_signature(args, kw)
444
+ ns = self._ns.copy()
445
+ ns.update(values)
446
+ if self._bound_self is not None:
447
+ ns['self'] = self._bound_self
448
+ out = []
449
+ subdefs = {}
450
+ self._template._interpret_codes(self._body, ns, out, subdefs)
451
+ return ''.join(out)
452
+
453
+ def __get__(self, obj, type=None):
454
+ if obj is None:
455
+ return self
456
+ return self.__class__(
457
+ self._template, self._func_name, self._func_signature,
458
+ self._body, self._ns, self._pos, bound_self=obj)
459
+
460
+ def _parse_signature(self, args, kw):
461
+ values = {}
462
+ sig_args, var_args, var_kw, defaults = self._func_signature
463
+ extra_kw = {}
464
+ for name, value in kw.items():
465
+ if not var_kw and name not in sig_args:
466
+ raise TypeError(
467
+ 'Unexpected argument %s' % name)
468
+ if name in sig_args:
469
+ values[sig_args] = value
470
+ else:
471
+ extra_kw[name] = value
472
+ args = list(args)
473
+ sig_args = list(sig_args)
474
+ while args:
475
+ while sig_args and sig_args[0] in values:
476
+ sig_args.pop(0)
477
+ if sig_args:
478
+ name = sig_args.pop(0)
479
+ values[name] = args.pop(0)
480
+ elif var_args:
481
+ values[var_args] = tuple(args)
482
+ break
483
+ else:
484
+ raise TypeError(
485
+ 'Extra position arguments: %s'
486
+ % ', '.join([repr(v) for v in args]))
487
+ for name, value_expr in defaults.items():
488
+ if name not in values:
489
+ values[name] = self._template._eval(
490
+ value_expr, self._ns, self._pos)
491
+ for name in sig_args:
492
+ if name not in values:
493
+ raise TypeError(
494
+ 'Missing argument: %s' % name)
495
+ if var_kw:
496
+ values[var_kw] = extra_kw
497
+ return values
498
+
499
+
500
+ class TemplateObject:
501
+
502
+ def __init__(self, name):
503
+ self.__name = name
504
+ self.get = TemplateObjectGetter(self)
505
+
506
+ def __repr__(self):
507
+ return '<%s %s>' % (self.__class__.__name__, self.__name)
508
+
509
+
510
+ class TemplateObjectGetter:
511
+
512
+ def __init__(self, template_obj):
513
+ self.__template_obj = template_obj
514
+
515
+ def __getattr__(self, attr):
516
+ return getattr(self.__template_obj, attr, Empty)
517
+
518
+ def __repr__(self):
519
+ return '<%s around %r>' % (self.__class__.__name__, self.__template_obj)
520
+
521
+
522
+ class _Empty:
523
+ def __call__(self, *args, **kw):
524
+ return self
525
+
526
+ def __str__(self):
527
+ return ''
528
+
529
+ def __repr__(self):
530
+ return 'Empty'
531
+
532
+ def __unicode__(self):
533
+ return ''
534
+
535
+ def __iter__(self):
536
+ return iter(())
537
+
538
+ def __bool__(self):
539
+ return False
540
+
541
+ Empty = _Empty()
542
+ del _Empty
543
+
544
+ ############################################################
545
+ ## Lexing and Parsing
546
+ ############################################################
547
+
548
+
549
+ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimiters=None):
550
+ """
551
+ Lex a string into chunks:
552
+
553
+ >>> lex('hey')
554
+ ['hey']
555
+ >>> lex('hey {{you}}')
556
+ ['hey ', ('you', (1, 7))]
557
+ >>> lex('hey {{')
558
+ Traceback (most recent call last):
559
+ ...
560
+ TemplateError: No }} to finish last expression at line 1 column 7
561
+ >>> lex('hey }}')
562
+ Traceback (most recent call last):
563
+ ...
564
+ TemplateError: }} outside expression at line 1 column 7
565
+ >>> lex('hey {{ {{')
566
+ Traceback (most recent call last):
567
+ ...
568
+ TemplateError: {{ inside expression at line 1 column 10
569
+
570
+ """
571
+ if delimiters is None:
572
+ delimiters = ( Template.default_namespace['start_braces'],
573
+ Template.default_namespace['end_braces'] )
574
+ in_expr = False
575
+ chunks = []
576
+ last = 0
577
+ last_pos = (line_offset + 1, 1)
578
+
579
+ token_re = re.compile(r'%s|%s' % (re.escape(delimiters[0]),
580
+ re.escape(delimiters[1])))
581
+ for match in token_re.finditer(s):
582
+ expr = match.group(0)
583
+ pos = find_position(s, match.end(), last, last_pos)
584
+ if expr == delimiters[0] and in_expr:
585
+ raise TemplateError('%s inside expression' % delimiters[0],
586
+ position=pos,
587
+ name=name)
588
+ elif expr == delimiters[1] and not in_expr:
589
+ raise TemplateError('%s outside expression' % delimiters[1],
590
+ position=pos,
591
+ name=name)
592
+ if expr == delimiters[0]:
593
+ part = s[last:match.start()]
594
+ if part:
595
+ chunks.append(part)
596
+ in_expr = True
597
+ else:
598
+ chunks.append((s[last:match.start()], last_pos))
599
+ in_expr = False
600
+ last = match.end()
601
+ last_pos = pos
602
+ if in_expr:
603
+ raise TemplateError('No %s to finish last expression' % delimiters[1],
604
+ name=name, position=last_pos)
605
+ part = s[last:]
606
+ if part:
607
+ chunks.append(part)
608
+ if trim_whitespace:
609
+ chunks = trim_lex(chunks)
610
+ return chunks
611
+
612
+ statement_re = re.compile(r'^(?:if |elif |for |def |inherit |default |py:)')
613
+ single_statements = ['else', 'endif', 'endfor', 'enddef', 'continue', 'break']
614
+ trail_whitespace_re = re.compile(r'\n\r?[\t ]*$')
615
+ lead_whitespace_re = re.compile(r'^[\t ]*\n')
616
+
617
+
618
+ def trim_lex(tokens):
619
+ r"""
620
+ Takes a lexed set of tokens, and removes whitespace when there is
621
+ a directive on a line by itself:
622
+
623
+ >>> tokens = lex('{{if x}}\nx\n{{endif}}\ny', trim_whitespace=False)
624
+ >>> tokens
625
+ [('if x', (1, 3)), '\nx\n', ('endif', (3, 3)), '\ny']
626
+ >>> trim_lex(tokens)
627
+ [('if x', (1, 3)), 'x\n', ('endif', (3, 3)), 'y']
628
+ """
629
+ last_trim = None
630
+ for i, current in enumerate(tokens):
631
+ if isinstance(current, str):
632
+ # we don't trim this
633
+ continue
634
+ item = current[0]
635
+ if not statement_re.search(item) and item not in single_statements:
636
+ continue
637
+ if not i:
638
+ prev = ''
639
+ else:
640
+ prev = tokens[i - 1]
641
+ if i + 1 >= len(tokens):
642
+ next_chunk = ''
643
+ else:
644
+ next_chunk = tokens[i + 1]
645
+ if (not isinstance(next_chunk, str)
646
+ or not isinstance(prev, str)):
647
+ continue
648
+ prev_ok = not prev or trail_whitespace_re.search(prev)
649
+ if i == 1 and not prev.strip():
650
+ prev_ok = True
651
+ if last_trim is not None and last_trim + 2 == i and not prev.strip():
652
+ prev_ok = 'last'
653
+ if (prev_ok
654
+ and (not next_chunk or lead_whitespace_re.search(next_chunk)
655
+ or (i == len(tokens) - 2 and not next_chunk.strip()))):
656
+ if prev:
657
+ if ((i == 1 and not prev.strip())
658
+ or prev_ok == 'last'):
659
+ tokens[i - 1] = ''
660
+ else:
661
+ m = trail_whitespace_re.search(prev)
662
+ # +1 to leave the leading \n on:
663
+ prev = prev[:m.start() + 1]
664
+ tokens[i - 1] = prev
665
+ if next_chunk:
666
+ last_trim = i
667
+ if i == len(tokens) - 2 and not next_chunk.strip():
668
+ tokens[i + 1] = ''
669
+ else:
670
+ m = lead_whitespace_re.search(next_chunk)
671
+ next_chunk = next_chunk[m.end():]
672
+ tokens[i + 1] = next_chunk
673
+ return tokens
674
+
675
+
676
+ def find_position(string, index, last_index, last_pos):
677
+ """Given a string and index, return (line, column)"""
678
+ lines = string.count('\n', last_index, index)
679
+ if lines > 0:
680
+ column = index - string.rfind('\n', last_index, index)
681
+ else:
682
+ column = last_pos[1] + (index - last_index)
683
+ return (last_pos[0] + lines, column)
684
+
685
+
686
+ def parse(s, name=None, line_offset=0, delimiters=None):
687
+ r"""
688
+ Parses a string into a kind of AST
689
+
690
+ >>> parse('{{x}}')
691
+ [('expr', (1, 3), 'x')]
692
+ >>> parse('foo')
693
+ ['foo']
694
+ >>> parse('{{if x}}test{{endif}}')
695
+ [('cond', (1, 3), ('if', (1, 3), 'x', ['test']))]
696
+ >>> parse('series->{{for x in y}}x={{x}}{{endfor}}')
697
+ ['series->', ('for', (1, 11), ('x',), 'y', ['x=', ('expr', (1, 27), 'x')])]
698
+ >>> parse('{{for x, y in z:}}{{continue}}{{endfor}}')
699
+ [('for', (1, 3), ('x', 'y'), 'z', [('continue', (1, 21))])]
700
+ >>> parse('{{py:x=1}}')
701
+ [('py', (1, 3), 'x=1')]
702
+ >>> parse('{{if x}}a{{elif y}}b{{else}}c{{endif}}')
703
+ [('cond', (1, 3), ('if', (1, 3), 'x', ['a']), ('elif', (1, 12), 'y', ['b']), ('else', (1, 23), None, ['c']))]
704
+
705
+ Some exceptions::
706
+
707
+ >>> parse('{{continue}}')
708
+ Traceback (most recent call last):
709
+ ...
710
+ TemplateError: continue outside of for loop at line 1 column 3
711
+ >>> parse('{{if x}}foo')
712
+ Traceback (most recent call last):
713
+ ...
714
+ TemplateError: No {{endif}} at line 1 column 3
715
+ >>> parse('{{else}}')
716
+ Traceback (most recent call last):
717
+ ...
718
+ TemplateError: else outside of an if block at line 1 column 3
719
+ >>> parse('{{if x}}{{for x in y}}{{endif}}{{endfor}}')
720
+ Traceback (most recent call last):
721
+ ...
722
+ TemplateError: Unexpected endif at line 1 column 25
723
+ >>> parse('{{if}}{{endif}}')
724
+ Traceback (most recent call last):
725
+ ...
726
+ TemplateError: if with no expression at line 1 column 3
727
+ >>> parse('{{for x y}}{{endfor}}')
728
+ Traceback (most recent call last):
729
+ ...
730
+ TemplateError: Bad for (no "in") in 'x y' at line 1 column 3
731
+ >>> parse('{{py:x=1\ny=2}}')
732
+ Traceback (most recent call last):
733
+ ...
734
+ TemplateError: Multi-line py blocks must start with a newline at line 1 column 3
735
+ """
736
+ if delimiters is None:
737
+ delimiters = ( Template.default_namespace['start_braces'],
738
+ Template.default_namespace['end_braces'] )
739
+ tokens = lex(s, name=name, line_offset=line_offset, delimiters=delimiters)
740
+ result = []
741
+ while tokens:
742
+ next_chunk, tokens = parse_expr(tokens, name)
743
+ result.append(next_chunk)
744
+ return result
745
+
746
+
747
+ def parse_expr(tokens, name, context=()):
748
+ if isinstance(tokens[0], str):
749
+ return tokens[0], tokens[1:]
750
+ expr, pos = tokens[0]
751
+ expr = expr.strip()
752
+ if expr.startswith('py:'):
753
+ expr = expr[3:].lstrip(' \t')
754
+ if expr.startswith('\n') or expr.startswith('\r'):
755
+ expr = expr.lstrip('\r\n')
756
+ if '\r' in expr:
757
+ expr = expr.replace('\r\n', '\n')
758
+ expr = expr.replace('\r', '')
759
+ expr += '\n'
760
+ else:
761
+ if '\n' in expr:
762
+ raise TemplateError(
763
+ 'Multi-line py blocks must start with a newline',
764
+ position=pos, name=name)
765
+ return ('py', pos, expr), tokens[1:]
766
+ elif expr in ('continue', 'break'):
767
+ if 'for' not in context:
768
+ raise TemplateError(
769
+ 'continue outside of for loop',
770
+ position=pos, name=name)
771
+ return (expr, pos), tokens[1:]
772
+ elif expr.startswith('if '):
773
+ return parse_cond(tokens, name, context)
774
+ elif (expr.startswith('elif ')
775
+ or expr == 'else'):
776
+ raise TemplateError(
777
+ '%s outside of an if block' % expr.split()[0],
778
+ position=pos, name=name)
779
+ elif expr in ('if', 'elif', 'for'):
780
+ raise TemplateError(
781
+ '%s with no expression' % expr,
782
+ position=pos, name=name)
783
+ elif expr in ('endif', 'endfor', 'enddef'):
784
+ raise TemplateError(
785
+ 'Unexpected %s' % expr,
786
+ position=pos, name=name)
787
+ elif expr.startswith('for '):
788
+ return parse_for(tokens, name, context)
789
+ elif expr.startswith('default '):
790
+ return parse_default(tokens, name, context)
791
+ elif expr.startswith('inherit '):
792
+ return parse_inherit(tokens, name, context)
793
+ elif expr.startswith('def '):
794
+ return parse_def(tokens, name, context)
795
+ elif expr.startswith('#'):
796
+ return ('comment', pos, tokens[0][0]), tokens[1:]
797
+ return ('expr', pos, tokens[0][0]), tokens[1:]
798
+
799
+
800
+ def parse_cond(tokens, name, context):
801
+ start = tokens[0][1]
802
+ pieces = []
803
+ context = context + ('if',)
804
+ while 1:
805
+ if not tokens:
806
+ raise TemplateError(
807
+ 'Missing {{endif}}',
808
+ position=start, name=name)
809
+ if (isinstance(tokens[0], tuple)
810
+ and tokens[0][0] == 'endif'):
811
+ return ('cond', start) + tuple(pieces), tokens[1:]
812
+ next_chunk, tokens = parse_one_cond(tokens, name, context)
813
+ pieces.append(next_chunk)
814
+
815
+
816
+ def parse_one_cond(tokens, name, context):
817
+ (first, pos), tokens = tokens[0], tokens[1:]
818
+ content = []
819
+ if first.endswith(':'):
820
+ first = first[:-1]
821
+ if first.startswith('if '):
822
+ part = ('if', pos, first[3:].lstrip(), content)
823
+ elif first.startswith('elif '):
824
+ part = ('elif', pos, first[5:].lstrip(), content)
825
+ elif first == 'else':
826
+ part = ('else', pos, None, content)
827
+ else:
828
+ assert 0, "Unexpected token %r at %s" % (first, pos)
829
+ while 1:
830
+ if not tokens:
831
+ raise TemplateError(
832
+ 'No {{endif}}',
833
+ position=pos, name=name)
834
+ if (isinstance(tokens[0], tuple)
835
+ and (tokens[0][0] == 'endif'
836
+ or tokens[0][0].startswith('elif ')
837
+ or tokens[0][0] == 'else')):
838
+ return part, tokens
839
+ next_chunk, tokens = parse_expr(tokens, name, context)
840
+ content.append(next_chunk)
841
+
842
+
843
+ def parse_for(tokens, name, context):
844
+ first, pos = tokens[0]
845
+ tokens = tokens[1:]
846
+ context = ('for',) + context
847
+ content = []
848
+ assert first.startswith('for '), first
849
+ if first.endswith(':'):
850
+ first = first[:-1]
851
+ first = first[3:].strip()
852
+ match = in_re.search(first)
853
+ if not match:
854
+ raise TemplateError(
855
+ 'Bad for (no "in") in %r' % first,
856
+ position=pos, name=name)
857
+ vars = first[:match.start()]
858
+ if '(' in vars:
859
+ raise TemplateError(
860
+ 'You cannot have () in the variable section of a for loop (%r)'
861
+ % vars, position=pos, name=name)
862
+ vars = tuple([
863
+ v.strip() for v in first[:match.start()].split(',')
864
+ if v.strip()])
865
+ expr = first[match.end():]
866
+ while 1:
867
+ if not tokens:
868
+ raise TemplateError(
869
+ 'No {{endfor}}',
870
+ position=pos, name=name)
871
+ if (isinstance(tokens[0], tuple)
872
+ and tokens[0][0] == 'endfor'):
873
+ return ('for', pos, vars, expr, content), tokens[1:]
874
+ next_chunk, tokens = parse_expr(tokens, name, context)
875
+ content.append(next_chunk)
876
+
877
+
878
+ def parse_default(tokens, name, context):
879
+ first, pos = tokens[0]
880
+ assert first.startswith('default ')
881
+ first = first.split(None, 1)[1]
882
+ parts = first.split('=', 1)
883
+ if len(parts) == 1:
884
+ raise TemplateError(
885
+ "Expression must be {{default var=value}}; no = found in %r" % first,
886
+ position=pos, name=name)
887
+ var = parts[0].strip()
888
+ if ',' in var:
889
+ raise TemplateError(
890
+ "{{default x, y = ...}} is not supported",
891
+ position=pos, name=name)
892
+ if not var_re.search(var):
893
+ raise TemplateError(
894
+ "Not a valid variable name for {{default}}: %r"
895
+ % var, position=pos, name=name)
896
+ expr = parts[1].strip()
897
+ return ('default', pos, var, expr), tokens[1:]
898
+
899
+
900
+ def parse_inherit(tokens, name, context):
901
+ first, pos = tokens[0]
902
+ assert first.startswith('inherit ')
903
+ expr = first.split(None, 1)[1]
904
+ return ('inherit', pos, expr), tokens[1:]
905
+
906
+
907
+ def parse_def(tokens, name, context):
908
+ first, start = tokens[0]
909
+ tokens = tokens[1:]
910
+ assert first.startswith('def ')
911
+ first = first.split(None, 1)[1]
912
+ if first.endswith(':'):
913
+ first = first[:-1]
914
+ if '(' not in first:
915
+ func_name = first
916
+ sig = ((), None, None, {})
917
+ elif not first.endswith(')'):
918
+ raise TemplateError("Function definition doesn't end with ): %s" % first,
919
+ position=start, name=name)
920
+ else:
921
+ first = first[:-1]
922
+ func_name, sig_text = first.split('(', 1)
923
+ sig = parse_signature(sig_text, name, start)
924
+ context = context + ('def',)
925
+ content = []
926
+ while 1:
927
+ if not tokens:
928
+ raise TemplateError(
929
+ 'Missing {{enddef}}',
930
+ position=start, name=name)
931
+ if (isinstance(tokens[0], tuple)
932
+ and tokens[0][0] == 'enddef'):
933
+ return ('def', start, func_name, sig, content), tokens[1:]
934
+ next_chunk, tokens = parse_expr(tokens, name, context)
935
+ content.append(next_chunk)
936
+
937
+
938
+ def parse_signature(sig_text, name, pos):
939
+ tokens = tokenize.generate_tokens(StringIO(sig_text).readline)
940
+ sig_args = []
941
+ var_arg = None
942
+ var_kw = None
943
+ defaults = {}
944
+
945
+ def get_token(pos=False):
946
+ try:
947
+ tok_type, tok_string, (srow, scol), (erow, ecol), line = next(tokens)
948
+ except StopIteration:
949
+ return tokenize.ENDMARKER, ''
950
+ if pos:
951
+ return tok_type, tok_string, (srow, scol), (erow, ecol)
952
+ else:
953
+ return tok_type, tok_string
954
+ while 1:
955
+ var_arg_type = None
956
+ tok_type, tok_string = get_token()
957
+ if tok_type == tokenize.ENDMARKER:
958
+ break
959
+ if tok_type == tokenize.OP and (tok_string == '*' or tok_string == '**'):
960
+ var_arg_type = tok_string
961
+ tok_type, tok_string = get_token()
962
+ if tok_type != tokenize.NAME:
963
+ raise TemplateError('Invalid signature: (%s)' % sig_text,
964
+ position=pos, name=name)
965
+ var_name = tok_string
966
+ tok_type, tok_string = get_token()
967
+ if tok_type == tokenize.ENDMARKER or (tok_type == tokenize.OP and tok_string == ','):
968
+ if var_arg_type == '*':
969
+ var_arg = var_name
970
+ elif var_arg_type == '**':
971
+ var_kw = var_name
972
+ else:
973
+ sig_args.append(var_name)
974
+ if tok_type == tokenize.ENDMARKER:
975
+ break
976
+ continue
977
+ if var_arg_type is not None:
978
+ raise TemplateError('Invalid signature: (%s)' % sig_text,
979
+ position=pos, name=name)
980
+ if tok_type == tokenize.OP and tok_string == '=':
981
+ nest_type = None
982
+ unnest_type = None
983
+ nest_count = 0
984
+ start_pos = end_pos = None
985
+ parts = []
986
+ while 1:
987
+ tok_type, tok_string, s, e = get_token(True)
988
+ if start_pos is None:
989
+ start_pos = s
990
+ end_pos = e
991
+ if tok_type == tokenize.ENDMARKER and nest_count:
992
+ raise TemplateError('Invalid signature: (%s)' % sig_text,
993
+ position=pos, name=name)
994
+ if (not nest_count and
995
+ (tok_type == tokenize.ENDMARKER or (tok_type == tokenize.OP and tok_string == ','))):
996
+ default_expr = isolate_expression(sig_text, start_pos, end_pos)
997
+ defaults[var_name] = default_expr
998
+ sig_args.append(var_name)
999
+ break
1000
+ parts.append((tok_type, tok_string))
1001
+ if nest_count and tok_type == tokenize.OP and tok_string == nest_type:
1002
+ nest_count += 1
1003
+ elif nest_count and tok_type == tokenize.OP and tok_string == unnest_type:
1004
+ nest_count -= 1
1005
+ if not nest_count:
1006
+ nest_type = unnest_type = None
1007
+ elif not nest_count and tok_type == tokenize.OP and tok_string in ('(', '[', '{'):
1008
+ nest_type = tok_string
1009
+ nest_count = 1
1010
+ unnest_type = {'(': ')', '[': ']', '{': '}'}[nest_type]
1011
+ return sig_args, var_arg, var_kw, defaults
1012
+
1013
+
1014
+ def isolate_expression(string, start_pos, end_pos):
1015
+ srow, scol = start_pos
1016
+ srow -= 1
1017
+ erow, ecol = end_pos
1018
+ erow -= 1
1019
+ lines = string.splitlines(True)
1020
+ if srow == erow:
1021
+ return lines[srow][scol:ecol]
1022
+ parts = [lines[srow][scol:]]
1023
+ parts.extend(lines[srow+1:erow])
1024
+ if erow < len(lines):
1025
+ # It'll sometimes give (end_row_past_finish, 0)
1026
+ parts.append(lines[erow][:ecol])
1027
+ return ''.join(parts)
1028
+
1029
+ _fill_command_usage = """\
1030
+ %prog [OPTIONS] TEMPLATE arg=value
1031
+
1032
+ Use py:arg=value to set a Python value; otherwise all values are
1033
+ strings.
1034
+ """
1035
+
1036
+
1037
+ def fill_command(args=None):
1038
+ import sys
1039
+ import optparse
1040
+ import pkg_resources
1041
+ import os
1042
+ if args is None:
1043
+ args = sys.argv[1:]
1044
+ dist = pkg_resources.get_distribution('Paste')
1045
+ parser = optparse.OptionParser(
1046
+ version=coerce_text(dist),
1047
+ usage=_fill_command_usage)
1048
+ parser.add_option(
1049
+ '-o', '--output',
1050
+ dest='output',
1051
+ metavar="FILENAME",
1052
+ help="File to write output to (default stdout)")
1053
+ parser.add_option(
1054
+ '--env',
1055
+ dest='use_env',
1056
+ action='store_true',
1057
+ help="Put the environment in as top-level variables")
1058
+ options, args = parser.parse_args(args)
1059
+ if len(args) < 1:
1060
+ print('You must give a template filename')
1061
+ sys.exit(2)
1062
+ template_name = args[0]
1063
+ args = args[1:]
1064
+ vars = {}
1065
+ if options.use_env:
1066
+ vars.update(os.environ)
1067
+ for value in args:
1068
+ if '=' not in value:
1069
+ print('Bad argument: %r' % value)
1070
+ sys.exit(2)
1071
+ name, value = value.split('=', 1)
1072
+ if name.startswith('py:'):
1073
+ name = name[:3]
1074
+ value = eval(value)
1075
+ vars[name] = value
1076
+ if template_name == '-':
1077
+ template_content = sys.stdin.read()
1078
+ template_name = '<stdin>'
1079
+ else:
1080
+ with open(template_name, 'rb') as f:
1081
+ template_content = f.read()
1082
+ template = Template(template_content, name=template_name)
1083
+ result = template.substitute(vars)
1084
+ if options.output:
1085
+ with open(options.output, 'wb') as f:
1086
+ f.write(result)
1087
+ else:
1088
+ sys.stdout.write(result)
1089
+
1090
+ if __name__ == '__main__':
1091
+ fill_command()