Giter Site home page Giter Site logo

jinja2schema's People

Contributors

aromanovich avatar tayhobbs avatar ubaumann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jinja2schema's Issues

fails to parse assignment of list filtered with 'list' built-in filter

I have this one line of jinja2 that contains no html. It is a valid template that jinja2 is able to parse. I can additionally write some additional parts of the template to actually use the assigned value here and am able to render it

{% set filteredList = ThisIsAListOfObjects | selectattr("SomeProperty", "equalto", "SomeValue") | list %}

When inferring this I receive the following stack trace:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 116, in meet
    merge(self.predicted_struct, actual_struct)
  File "C:\Python34\lib\site-packages\jinja2schema\mergers.py", line 66, in merge
    raise MergeException(fst, snd)
jinja2schema.exceptions.MergeException: unnamed variable (used as scalar on lines 1) conflicts with unnamed variable (used as list on lines: )

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print(jinja2schema.infer(data))
  File "C:\Python34\lib\site-packages\jinja2schema\core.py", line 66, in infer
    return infer_from_ast(parse(template), config=config, ignore_constants=True)
  File "C:\Python34\lib\site-packages\jinja2schema\core.py", line 49, in infer_from_ast
    rv = visit(ast, {}, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 20, in visit
    structure = visit_many(node.body, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 37, in visit_many
    structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\util.py", line 14, in visit
    structure = visit_stmt(node, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\stmt.py", line 54, in visit_stmt
    return visitor(ast, macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\stmt.py", line 134, in visit_assign
    predicted_struct=Unknown.from_ast(var_ast, order_nr=config.ORDER_OBJECT.get_next())), macroses, config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 156, in visit_expr
    return visitor(ast, ctx, macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 534, in visit_filter
    ), macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 156, in visit_expr
    return visitor(ast, ctx, macroses, config=config)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 509, in visit_filter
    ctx.meet(List(Unknown()), ast)
  File "C:\Python34\lib\site-packages\jinja2schema\visitors\expr.py", line 118, in meet
    raise UnexpectedExpression(self.predicted_struct, actual_ast, actual_struct)
jinja2schema.exceptions.UnexpectedExpression: conflict on the line 1
got: AST node jinja2.nodes.Filter of structure [<unknown>]
expected structure: <scalar>

Here is the AST jinja2 gives from parsing:

Template(
  body=[
    Assign(
      target=Name(
        name='filteredList', 
        ctx='store'
      ), 
      node=Filter(
        node=Filter(
          node=Name(
            name='ThisIsAListOfObjects', 
            ctx='load'
          ), 
          name='selectattr', 
          args=[
            Const(value='SomeProperty'), 
            Const(value='equalto'), 
            Const(value='SomeValue')
          ], 
          kwargs=[], 
          dyn_args=None, 
          dyn_kwargs=None
        ), 
        name='list', 
        args=[], 
        kwargs=[], 
        dyn_args=None, 
        dyn_kwargs=None
      )
    )
  ]
)

question about int/float filter

Hello, thanks for this package!

I wonder why its not Number instead of Scalar here

node_struct = Number.from_ast(ast.node, order_nr=config.ORDER_OBJECT.get_next())
instead of
node_struct = Scalar.from_ast(ast.node, order_nr=config.ORDER_OBJECT.get_next())

Thanks!

Required set incorrectly in nested if loop

{% if logging is defined %}
{%     if logging.console is defined %}
  console: {{ logging.console }}
{%     endif %}
{%     if logging.monitor is defined %}
  monitor: {{ logging.monitor }}
{%     endif %}
{% endif %}

Results in:

>>> print(json.dumps(schema, indent=2))
{
  "type": "object",
  "properties": {
    "logging": {
      "title": "logging",
      "type": "object",
      "properties": {
        "monitor": {
          "title": "monitor",
          "anyOf": [
            {
              "type": "boolean"
            },
            {
              "type": "null"
            },
            {
              "type": "number"
            },
            {
              "type": "string"
            }
          ]
        },
        "console": {
          "title": "console",
          "anyOf": [
            {
              "type": "boolean"
            },
            {
              "type": "null"
            },
            {
              "type": "number"
            },
            {
              "type": "string"
            }
          ]
        }
      },
      "required": [
        "monitor",
        "console"
      ]
    }
  },
  "required": []
}

Instead of:

      "required": [
        "monitor",
        "console"
      ]

it rather should be empty.

visitors/stmt.py contains Python 3 incompatible code

In the visitors/stmt.py there is a couple spots that contain the iteritems method. I hit this code when I have an includes in my Jinja2 template. If I edit the code in my virtualenvironment to just use items then I run into this error: stmt visitor for <class 'jinja2.nodes.Include'> is not found.

The iteritems is here: https://github.com/aromanovich/jinja2schema/blob/master/jinja2schema/visitors/stmt.py#L49

And I run into the exception here: https://github.com/aromanovich/jinja2schema/blob/master/jinja2schema/visitors/stmt.py#L53

Thank you

How to insert description to the variable

In the situation of using jinja2schema + jsonforms (https://github.com/brutusin/json-forms) to generate an html form, is it possible to describe this field? Or generate "description" for this variable field title?

Currently using {{variable_description | default("description") }}, which generates a changeable form including the descriptions. It works but a non-changeable field is more desirable.

Thanks!

arg expansion not suppported

Apparently jinja2schema does not expand argument expansion using tuples.
jinja2schema.infer on the following throws an AttributeError: 'Tuple' object has no attribute 'name':

CREATE TABLE IF NOT EXISTS `{{ table_name }}` (
{%- for field, (type, not_null, is_primary_key, default_value) in fields.items() %}
    `{{ field }}` {{ type }}
    {%- if not_null %} NOT NULL{% endif -%}
    {%- if is_primary_key %} PRIMARY KEY{% endif -%}
    {%- if default_value %} DEFAULT {{ default_value }}{% endif -%}
    {%- if not loop.last %},{% endif %}
{%- endfor %}
);

Unsupported Jinja2 builtin filters

Apparently a few of the Jinja2 builtin filters are not supported by jinja2schema. I came across this while trying to explore jinja2schema and using my templates with "lower" filter.

How to reproduce

The following simple snippet can be used to reproduce the issue.

from jinja2schema import infer
infer("{{var|lower}}")

Looking at the list of jinja2 builtin filters, and jinja2schema visit_filters method, here is the list of filters for which support is not available: 'lower', 'max', 'min', 'reverse', 'tojson', 'unique'.

System Configuration

Python version - 3.7.6
Jinja2 version - 2.11.2
jinja2schema versio - 0.1.4
Windows 10

Error when PACKAGE_NAME=''

image
image
The commented line gives me :
image
And when commenting it out and using PACKAGE_NAME='keepEmpty' with
image
I get what I want :
image

InvalidExpression on jinja `loop`

Is there a way to list things for jinja2schema to ignore as it goes through the AST?

I was able to get it working if I hacked in

@visits_expr(nodes.Call)
def visit_call(ast, ctx, macroses=None, config=default_config):
    // my code to short circuit evaluating the node
    if ast.node.node.name in ('loop'):
        return Unknown(), Unknown()

in jinja2schema/visitors/expr.py

Stacktrace

return infer(template_source, config=Config(PACKAGE_NAME='documents', TEMPLATE_DIR=settings.TEMPLATE_PATH))
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/core.py", line 66, in infer
    return infer_from_ast(parse(template), config=config, ignore_constants=True)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/core.py", line 49, in infer_from_ast
    rv = visit(ast, {}, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 19, in visit
    structure = visit_many(node.body, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
    structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 14, in visit
    structure = visit_stmt(node, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 54, in visit_stmt
    return visitor(ast, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 59, in visit_for
    body_struct = visit_many(ast.body, macroses, config, predicted_struct_cls=Scalar)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
    structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 14, in visit
    structure = visit_stmt(node, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 54, in visit_stmt
    return visitor(ast, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/stmt.py", line 153, in visit_output
    return visit_many(ast.nodes, macroses, config, predicted_struct_cls=Scalar)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 36, in visit_many
    structure = visit(node, macroses, config, predicted_struct_cls, return_struct_cls)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/util.py", line 17, in visit
    _, structure = visit_expr(node, ctx, macroses, config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/expr.py", line 156, in visit_expr
    return visitor(ast, ctx, macroses, config=config)
  File "/home/user/local/lib/python3.4/site-packages/jinja2schema/visitors/expr.py", line 402, in visit_call
    raise InvalidExpression(ast, '"{0}" call is not supported'.format(ast.node.attr))
jinja2schema.exceptions.InvalidExpression: line 367: "cycle" call is not supported

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.