Package jsontemplate :: Module _jsontemplate

Module _jsontemplate

source code

Python implementation of json-template.

JSON Template is a minimal and powerful templating language for transforming a
JSON dictionary to arbitrary text.

To use this module, you will typically use the Template constructor, and catch
various exceptions thrown.  You may also want to use the FromFile/FromString
methods, which allow Template constructor options to be embedded in the template
string itself.

Other functions are exposed for tools which may want to process templates.




Author: Andy Chu

Classes
  Error
Base class for all exceptions in this module.
  CompilationError
Base class for errors that happen during the compilation stage.
  EvaluationError
Base class for errors that happen when expanding the template.
  BadFormatter
A bad formatter was specified, e.g.
  BadPredicate
A bad predicate was specified, e.g.
  MissingFormatter
Raised when formatters are required, and a variable is missing a formatter.
  ConfigurationError
Raised when the Template options are invalid and it can't even be compiled.
  TemplateSyntaxError
Syntax error in the template text.
  UndefinedVariable
The template contains a variable not defined by the data dictionary.
  Template
Represents a compiled template.
Functions
 
CompileTemplate(template_str, builder=None, meta='{}', format_char='|', more_formatters=<function <lambda> at 0x833a5dc>, more_predicates=<function <lambda> at 0x833a614>, default_formatter='str')
Compile the template string, calling methods on the 'program builder'.
source code
 
FromString(s, more_formatters=<function <lambda> at 0x833a684>, _constructor=None)
Like FromFile, but takes a string.
source code
 
FromFile(f, more_formatters=<function <lambda> at 0x833a6f4>, _constructor=None)
Parse a template from a file, using a simple file format.
source code
 
expand(template_str, dictionary, **kwargs)
Free function to expands a template string with a data dictionary.
source code
Function Details

CompileTemplate(template_str, builder=None, meta='{}', format_char='|', more_formatters=<function <lambda> at 0x833a5dc>, more_predicates=<function <lambda> at 0x833a614>, default_formatter='str')

source code 
Compile the template string, calling methods on the 'program builder'.

Args:
  template_str: The template string.  It should not have any compilation
      options in the header -- those are parsed by FromString/FromFile

  builder: The interface of _ProgramBuilder isn't fixed.  Use at your own
      risk.

  meta: The metacharacters to use, e.g. '{}', '[]'.

  more_formatters:
      Something that can map format strings to formatter functions.  One of:
        - A plain dictionary of names -> functions  e.g. {'html': cgi.escape}
        - A higher-order function which takes format strings and returns
          formatter functions.  Useful for when formatters have parsed
          arguments.
        - A FunctionRegistry instance for the most control.  This allows
          formatters which takes contexts as well.

  more_predicates:
      Like more_formatters, but for predicates.

  default_formatter: The formatter to use for substitutions that are missing a
      formatter.  The 'str' formatter the "default default" -- it just tries
      to convert the context value to a string in some unspecified manner.

Returns:
  The compiled program (obtained from the builder)

Raises:
  The various subclasses of CompilationError.  For example, if
  default_formatter=None, and a variable is missing a formatter, then
  MissingFormatter is raised.

This function is public so it can be used by other tools, e.g. a syntax
checking tool run before submitting a template to source control.

FromFile(f, more_formatters=<function <lambda> at 0x833a6f4>, _constructor=None)

source code 
Parse a template from a file, using a simple file format.

This is useful when you want to include template options in a data file,
rather than in the source code.

The format is similar to HTTP or E-mail headers.  The first lines of the file
can specify template options, such as the metacharacters to use.  One blank
line must separate the options from the template body.

Example:

  default-formatter: none
  meta: {{}}
  format-char: :
  <blank line required>
  Template goes here: {{variable:html}}

Args:
  f: A file handle to read from.  Caller is responsible for opening and
  closing it.

expand(template_str, dictionary, **kwargs)

source code 
Free function to expands a template string with a data dictionary.

This is useful for cases where you don't care about saving the result of
compilation (similar to re.match('.*', s) vs DOT_STAR.match(s))