Skip to content

Fix loop cycles #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8975407
Add Python 3 support
TayHobbs Dec 29, 2015
08c457c
Allow blocks to be used in templates
TayHobbs Dec 29, 2015
0deac61
Merge pull request #4 from TayHobbs/python_3_compatibility
aromanovich Dec 30, 2015
bbfb277
Merge pull request #5 from TayHobbs/blocks
aromanovich Dec 30, 2015
f25cf4d
Allow includes and extend tags to be used in a template
TayHobbs Dec 29, 2015
27e821f
Include missing test template
TayHobbs Dec 30, 2015
beb6b95
Remove support for overriding blocks when using includes
TayHobbs Dec 30, 2015
cefe9e8
Merge pull request #6 from TayHobbs/inheritance
aromanovich Dec 31, 2015
2a01c86
Minor cleanup
aromanovich Dec 31, 2015
126ad42
Bump version to 0.1.2
aromanovich Dec 31, 2015
5b0ca0e
Convert readthedocs links for their .org -> .io migration for hosted …
Jun 13, 2016
d47eb56
Merge pull request #7 from adamchainz/readthedocs.io
aromanovich Jan 23, 2017
c78186d
Fix build error. Jinja2 2.9 change: Restricted test arguments
ubaumann Feb 20, 2017
d3a7414
Add value to model
ubaumann Feb 20, 2017
c76736b
Use default keyword in schema to work smoothly with angular-schema-form
ubaumann Feb 24, 2017
4d0587e
Merge pull request #8 from ubaumann/values
aromanovich Mar 23, 2017
1d63d0b
Bump version to 0.1.3
aromanovich Mar 23, 2017
02ac933
Add order number to make schema sortable
ubaumann Apr 4, 2017
406f292
Fix Python 3.X RuntimeError: dictionary changed size during iteration
ubaumann Apr 4, 2017
a70ef6c
Merge pull request #9 from ubaumann/order_number
aromanovich Apr 5, 2017
ca50530
Bump version to 0.1.4
aromanovich Apr 5, 2017
0717221
Debugging: short circuit loop cycle
Pachwenko Sep 26, 2019
324f14c
Merge branch 'master' into fix-loop-cycles
Pachwenko Sep 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.coverage
docs/build
**/__pycache__/*
*.pyc
*.egg-info*
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ License
.. _Jinja2: http://jinja.pocoo.org/docs/
.. _Demo: http://jinja2schema.aromanovich.ru/
.. _demo page: http://jinja2schema.aromanovich.ru/
.. _Documentation: http://jinja2schema.rtfd.org/
.. _Documentation: https://jinja2schema.readthedocs.io/
.. _GitHub: https://github.com/aromanovich/jinja2schema
.. _PyPI: https://pypi.python.org/pypi/jinja2schema
.. _BSD license: https://github.com/aromanovich/jinja2schema/blob/master/LICENSE
8 changes: 4 additions & 4 deletions jinja2schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

Type inference for Jinja2 templates.

See http://jinja2schema.rtfd.org/ for documentation.
See https://jinja2schema.readthedocs.io/ for documentation.

:copyright: (c) 2014 Anton Romanovich
:copyright: (c) 2017 Anton Romanovich
:license: BSD

"""

__title__ = 'jinja2schema'
__author__ = 'Anton Romanovich'
__license__ = 'BSD'
__copyright__ = 'Copyright 2014 Anton Romanovich'
__version__ = '0.1.1'
__copyright__ = 'Copyright 2017 Anton Romanovich'
__version__ = '0.1.4'
__version_info__ = tuple(int(i) for i in __version__.split('.'))


Expand Down
20 changes: 19 additions & 1 deletion jinja2schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
jinja2schema.config
~~~~~~~~~~~~~~~~~~~
"""
from .order_number import OrderNumber


class Config(object):
Expand Down Expand Up @@ -47,12 +48,26 @@ class Config(object):
this configuration is not needed.
"""

ORDER_NUMBER = False
"""Add a order number to each node

Add a order number to make schema sortable.
"""

ORDER_NUMBER_SUB_COUNTER = True
"""Independent subsection order numbers

Use a separate counter in subsections as order number creator.
"""

def __init__(self,
TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE='dictionary',
TYPE_OF_VARIABLE_INDEXED_WITH_INTEGER_TYPE='list',
BOOLEAN_CONDITIONS=False,
PACKAGE_NAME='',
TEMPLATE_DIR='templates'):
TEMPLATE_DIR='templates',
ORDER_NUMBER=False,
ORDER_NUMBER_SUB_COUNTER=True):
if TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE not in ('dictionary', 'list'):
raise ValueError('TYPE_OF_VARIABLE_INDEXED_WITH_VARIABLE_TYPE must be'
'either "dictionary" or "list"')
Expand All @@ -64,6 +79,9 @@ def __init__(self,
self.BOOLEAN_CONDITIONS = BOOLEAN_CONDITIONS
self.PACKAGE_NAME = PACKAGE_NAME
self.TEMPLATE_DIR = TEMPLATE_DIR
self.ORDER_NUMBER = ORDER_NUMBER
self.ORDER_OBJECT = OrderNumber(number=1, enabled=self.ORDER_NUMBER,
sub_counter_enabled=ORDER_NUMBER_SUB_COUNTER)


default_config = Config()
4 changes: 4 additions & 0 deletions jinja2schema/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def encode_common_attrs(self, var):
rv = {}
if var.label:
rv['title'] = var.label
if var.value and var.used_with_default:
rv['default'] = var.value
if var.order_nr:
rv['order_number'] = var.order_nr
return rv

def encode(self, var):
Expand Down
3 changes: 3 additions & 0 deletions jinja2schema/mergers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def merge(fst, snd, custom_merger=None):
result.used_with_default = fst.used_with_default and snd.used_with_default
result.checked_as_defined = fst.checked_as_defined and snd.checked_as_defined
result.checked_as_undefined = fst.checked_as_undefined and snd.checked_as_undefined
if fst.value == snd.value:
result.value = fst.value
result.order_nr = fst.order_nr
if callable(custom_merger):
result = custom_merger(fst, snd, result)
return result
Expand Down
15 changes: 12 additions & 3 deletions jinja2schema/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ class Variable(object):

Is true if the variable occurs within ``{% if %}`` block which condition checks
if the variable is defined.

.. attribute:: value

Value of the variable in template. Set by default filter or assignment.
"""
def __init__(self, label=None, linenos=None, constant=False,
may_be_defined=False, used_with_default=False,
checked_as_undefined=False, checked_as_defined=False):
checked_as_undefined=False, checked_as_defined=False,
value=None, order_nr=None):
self.label = label
self.linenos = linenos if linenos is not None else []
self.constant = constant
self.may_be_defined = may_be_defined
self.used_with_default = used_with_default
self.checked_as_undefined = checked_as_undefined
self.checked_as_defined = checked_as_defined
self.value = value
self.order_nr = order_nr

def clone(self):
cls = type(self)
Expand All @@ -68,6 +75,7 @@ def _get_kwargs_from_ast(cls, ast):
return {
'linenos': [ast.lineno],
'label': ast.name if isinstance(ast, nodes.Name) else None,
'value': ast.value if hasattr(ast, 'value') else None,
}

@classmethod
Expand All @@ -77,7 +85,7 @@ def from_ast(cls, ast, **kwargs):
:param ast: AST node
:type ast: :class:`jinja2.nodes.Node`
"""
for k, v in kwargs.items():
for k, v in list(kwargs.items()):
if v is None:
del kwargs[k]
kwargs = dict(cls._get_kwargs_from_ast(ast), **kwargs)
Expand All @@ -97,7 +105,8 @@ def __eq__(self, other):
self.used_with_default == other.used_with_default and
self.checked_as_undefined == other.checked_as_undefined and
self.checked_as_defined == other.checked_as_defined and
self.required == other.required
self.required == other.required and
self.value == other.value
)

def __ne__(self, other):
Expand Down
42 changes: 42 additions & 0 deletions jinja2schema/order_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# coding: utf-8
"""
jinja2schema.order_number
~~~~~~~~~~~~~~~~~~
"""
from contextlib import contextmanager


class OrderNumber(object):
"""A base Order Number class.

.. attribute:: number

Initial counter value.

.. attribute:: enabled

Counter enabled or return None.

"""

def __init__(self, number=0, enabled=False, sub_counter_enabled=True):
self.start = number
self.number = self.start
self.order_enabled = enabled
self.sub_counter_enabled = sub_counter_enabled

def get_next(self):
if self.order_enabled:
self.number += 1
return self.number
return None

@contextmanager
def sub_counter(self):
if self.sub_counter_enabled:
counter = self.number
self.number = self.start
yield
self.number = counter
return
yield
Loading