pypage 2.1.0__py3-none-any.whl → 2.2.1__py3-none-any.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.
@@ -1,9 +1,9 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: pypage
3
- Version: 2.1.0
4
- Summary: Light-weight Python Templating Engine
3
+ Version: 2.2.1
4
+ Summary: Light-weight Python Templating Engine
5
5
  Home-page: https://github.com/arjun-menon/pypage
6
- Download-URL: https://github.com/arjun-menon/pypage/archive/v2.1.0.tar.gz
6
+ Download-URL: https://github.com/arjun-menon/pypage/archive/v2.2.1.tar.gz
7
7
  Author: Arjun G. Menon
8
8
  Author-email: contact@arjungmenon.com
9
9
  License: Apache-2.0
@@ -24,6 +24,16 @@ Classifier: Programming Language :: Python :: Implementation :: PyPy
24
24
  Classifier: Programming Language :: Python :: 2
25
25
  Classifier: Programming Language :: Python :: 3
26
26
  License-File: LICENSE.txt
27
+ Dynamic: author
28
+ Dynamic: author-email
29
+ Dynamic: classifier
30
+ Dynamic: description
31
+ Dynamic: download-url
32
+ Dynamic: home-page
33
+ Dynamic: keywords
34
+ Dynamic: license
35
+ Dynamic: license-file
36
+ Dynamic: summary
27
37
 
28
38
  PyPage |pypi| |docs| |test|
29
39
  ===========================
@@ -331,10 +341,23 @@ tag:
331
341
  hello {{"bob"}}
332
342
  {% %}
333
343
 
334
- The above tag will not yield any output, but rather a new variable ``x``
344
+ The tag above will not yield any output, but rather a new variable ``x``
335
345
  will be created that captures the output of everything enclosed by it
336
346
  (which in this case is ``"hello bob"``).
337
347
 
348
+ Function Blocks
349
+ ^^^^^^^^^^^^^^^
350
+
351
+ You can define functions using the ``def`` tag:
352
+
353
+ .. code:: python
354
+
355
+ {% def anchor name href %}
356
+ <a href="{{href}}">{{name}}</a>
357
+ {% %}
358
+
359
+ The tag above will not yield any output, but rather create a new function ``anchor`` that behaves like a function, and returns the output of everything enclosed by it, with the named positional arguments injected (in an effective stacked local scope) as expected. In this example, we can invoke it with ``{{ anchor('Wikipedia', 'https://en.wikipedia.org') }}``.
360
+
338
361
  Finer Details
339
362
  -------------
340
363
 
@@ -0,0 +1,7 @@
1
+ pypage.py,sha256=70F8Lg3KOyGVeHCJIvCGY3o223EQp1Pvy3qDA5vI7zo,30890
2
+ pypage-2.2.1.dist-info/licenses/LICENSE.txt,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
+ pypage-2.2.1.dist-info/METADATA,sha256=tDgfsis6WJfZ904RMOtsaH5Fa94CKlxiAj3AIvC1OUU,18890
4
+ pypage-2.2.1.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
5
+ pypage-2.2.1.dist-info/entry_points.txt,sha256=iHPN6EfIUdv-njh8GV04yDTzkcCLGNcXvdbA43s_8mo,39
6
+ pypage-2.2.1.dist-info/top_level.txt,sha256=8AAB15dVQEt9xcwH_eLcVUZEbFlc__81RA5mOrYtJiQ,7
7
+ pypage-2.2.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (72.1.0)
2
+ Generator: setuptools (78.1.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
pypage.py CHANGED
@@ -18,7 +18,7 @@
18
18
  from __future__ import print_function
19
19
  import string, sys, time, os, json
20
20
 
21
- pypage_version = '2.1.0'
21
+ pypage_version = '2.2.1'
22
22
 
23
23
  class RootNode(object):
24
24
  """
@@ -303,6 +303,51 @@ class WhileBlock(BlockTag):
303
303
 
304
304
  return output
305
305
 
306
+ class DefBlock(BlockTag):
307
+ """
308
+ The function tag. {% def foo a b c %}
309
+ """
310
+ tag_startswith = 'def '
311
+
312
+ @staticmethod
313
+ def identify(src):
314
+ return src.strip().startswith(DefBlock.tag_startswith)
315
+
316
+ def __init__(self, node):
317
+ super(DefBlock, self).__init__(node.loc)
318
+ self.src = node.src.strip()
319
+
320
+ name_and_args = self.src[len(self.tag_startswith):].strip().split()
321
+ for name in name_and_args:
322
+ if not isidentifier(name):
323
+ raise InvalidDefBlockFunctionOrArgName(name)
324
+
325
+ self.funcname = name_and_args[0]
326
+ self.argnames = name_and_args[1:]
327
+
328
+ def run(self, pe):
329
+ def invoke(args):
330
+ if len(args) != len(self.argnames):
331
+ raise InvalidDefBlockMismatchingArgCount(self.argnames, args)
332
+
333
+ conflicting = set(pe.env.keys()) & set(self.argnames)
334
+ backup = { x : pe.env[x] for x in conflicting }
335
+
336
+ local_vars = dict(zip(self.argnames, args))
337
+ pe.env.update(local_vars)
338
+
339
+ output = exec_tree(self, pe)
340
+
341
+ for argname in self.argnames:
342
+ if argname in pe.env:
343
+ del pe.env[argname]
344
+
345
+ pe.env.update(backup)
346
+ return output
347
+
348
+ pe.env[self.funcname] = lambda *args: invoke(args)
349
+ return ""
350
+
306
351
  class CaptureBlock(BlockTag):
307
352
  """
308
353
  Capture all content within this tag, and bind it to a variable.
@@ -449,6 +494,15 @@ class InvalidCaptureBlockVariableName(PypageSyntaxError):
449
494
  def __init__(self, varname):
450
495
  self.description = "Incorrect CaptureBlock: '%s' is not a valid Python variable name." % varname
451
496
 
497
+ class InvalidDefBlockFunctionOrArgName(PypageSyntaxError):
498
+ def __init__(self, varname):
499
+ self.description = "Incorrect DefBlock: '%s' is not a valid function or argument name." % varname
500
+
501
+ class InvalidDefBlockMismatchingArgCount(PypageSyntaxError):
502
+ def __init__(self, argnames, args):
503
+ self.description = "Incorrect DefBlock function call: expected %d arguments (%s) but received %d arguments (%s) instead." % (
504
+ len(argnames), ', '.join(argnames), len(args), ', '.join(map(repr, args)))
505
+
452
506
  class UnknownTag(PypageSyntaxError):
453
507
  def __init__(self, node):
454
508
  self.description = "Unknown tag '%s%s%s' at line %d, column %d." % (
@@ -1,7 +0,0 @@
1
- pypage.py,sha256=oEEr3pCIbKbDoAIYL8vANUtRb4Wbll9IvYMWID0xEng,29037
2
- pypage-2.1.0.dist-info/LICENSE.txt,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
- pypage-2.1.0.dist-info/METADATA,sha256=TvOBibltTKqFxxy_4-sZ0Y8lHIrZVPo2wX0do03nVF4,18152
4
- pypage-2.1.0.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
5
- pypage-2.1.0.dist-info/entry_points.txt,sha256=iHPN6EfIUdv-njh8GV04yDTzkcCLGNcXvdbA43s_8mo,39
6
- pypage-2.1.0.dist-info/top_level.txt,sha256=8AAB15dVQEt9xcwH_eLcVUZEbFlc__81RA5mOrYtJiQ,7
7
- pypage-2.1.0.dist-info/RECORD,,