Module ncs.template
This module implements classes to simplify template processing.
Classes
class Template (service, path=None)
-
Class to simplify applying of templates in a NCS service callback.
Initialize a Template object.
The 'service' argument is the 'service' variable received in decorated cb_create method in a service class. ('service' can in fact be any maagic.Node (except a Root node) instance with an underlying Transaction). It is also possible to provide a maapi.Transaction instance for the 'service' argument in which case 'path' must also be provided.
Example use:
vars = ncs.template.Variables() vars.add('VAR1', 'foo') vars.add('VAR2', 'bar') vars.add('VAR3', 42) template = ncs.template.Template(service) template.apply('my-service-template', vars)
Methods
def apply(self, name, vars=None, flags=0)
-
Apply the template 'name'.
The optional argument 'vars' may be provided in form of a Variables instance.
Arguments:
- name – template name (str)
- vars – template variables (template.Variables)
- flags – template flags (int, optional)
class Variables (init=None)
-
Class to simplify passing of variables when applying a template.
Initialize a Variables object.
The optional argument 'init' can be any iterable yielding a 2-tuple in the form (name, value).
Ancestors
Methods
def add(self, name, value)
-
Add a value for the variable 'name'.
The value will be quoted before adding it to the internal list.
Quoting works like this: If value contains ' all occurrences of " will be replaced by ' and the final value will be quoted with ". Otherwise, the final value will be quoted with '.
Arguments:
- name – service variable name (str)
- value – variable value (str, int, boolean)
def add_plain(self, name, value)
-
Add a value for the variable 'name'.
It's up to the caller to do proper quoting of value.
For arguments, see Variables.add()