Skip to content
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

implement basic logging and update doc #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ $ jinja_to_js --help
usage: jinja_to_js [-h] [-o [OUTFILE]] [-m [JS_MODULE_FORMAT]]
[-r [RUNTIME_PATH]] [-p [INCLUDE_PREFIX]]
[-i [INCLUDE_EXT]]
[-f [CUSTOM_FILTERS [CUSTOM_FILTERS ...]]] [-v]
template_root template_name

Convert Jinja templates into JavaScript functions.
Expand Down Expand Up @@ -81,6 +82,9 @@ optional arguments:
Specifies the prefix to use for included templates.
-i [INCLUDE_EXT], --include-ext [INCLUDE_EXT]
Specifies the extension to use for included templates.
-f [CUSTOM_FILTERS [CUSTOM_FILTERS ...]], --filters [CUSTOM_FILTERS [CUSTOM_FILTERS ...]]
Specifies custom filters to be allowed.
-v, --verbose Log to standard out. Use two for debug.
```

## Supported Features
Expand Down
5 changes: 5 additions & 0 deletions jinja_to_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import unicode_literals

import contextlib
import logging
import json
import re
import os
Expand Down Expand Up @@ -244,6 +245,7 @@ def __init__(self,
'The js_module_format option must be one of: %s' % JS_MODULE_FORMATS.keys()
)

logging.info('Parsing template: {}'.format(self.template_name))
self.ast = self.environment.parse(template_string)

try:
Expand Down Expand Up @@ -317,6 +319,7 @@ def _process_extends(self, node, **kwargs):
"""
Processes an extends block e.g. `{% extends "some/template.jinja" %}`
"""
logging.debug('Extends parent template: {}'.format(node.template.value))

# find all the blocks in this template
for b in self.ast.find_all(nodes.Block):
Expand Down Expand Up @@ -354,6 +357,7 @@ def _process_block(self, node, **kwargs):
"""
Processes a block e.g. `{% block my_block %}{% endblock %}`
"""
logging.debug('Processing block: %s', node.name)

# check if this node already has a 'super_block' attribute
if not hasattr(node, 'super_block'):
Expand Down Expand Up @@ -635,6 +639,7 @@ def _process_call(self, node, super_block=None, **kwargs):
# special case for the super() method which is available inside blocks
if not super_block:
raise Exception('super() called outside of a block with a parent.')
logging.debug('explicit call to super(): %s', super_block.name)
self._process_node(super_block, **kwargs)

else:
Expand Down
19 changes: 18 additions & 1 deletion jinja_to_js/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys

import argparse
import logging

from . import JinjaToJS

Expand Down Expand Up @@ -75,20 +76,36 @@ def get_arg_parser():
dest="custom_filters"
)

parser.add_argument(
'-v', '--verbose', action='count',
help="Log to standard out. Use -vv for debug level.",
default=0,
dest="verbose"
)

return parser


def setup_logging(options):
if options.verbose:
level = logging.INFO
if options.verbose > 1:
level = logging.DEBUG
logging.basicConfig(format='%(levelname)-8s:%(message)s', level=level)


def get_init_kwargs(options):
kwargs = {}
for key, value in vars(options).items():
if key != 'outfile':
if key not in ('outfile', 'verbose'):
kwargs[key] = value
return kwargs


def main():
parser = get_arg_parser()
options = parser.parse_args()
setup_logging(options)
compiler = JinjaToJS(**get_init_kwargs(options))
options.outfile.write(compiler.get_output())
return 0