ncs.template Module

This module implements classes to simplify template processing.

Classes

class Template

Class to simplify applying of templates in a NCS service callback.

Template(service, path=None)

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)

Members:

chevron-rightapply(...)hashtag

Method:

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

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).

Members:

chevron-rightadd(...)hashtag

Method:

Add a value for the variable 'name'.

The value will be normalized and quoted as a valid XPath string literal before adding it to the internal list.

Quoting rules:

  • No quotes in value: wrapped in single quotes ('value')

  • Single quotes only: wrapped in double quotes ("it's")

  • Double quotes only: wrapped in single quotes ('say "hello"')

  • Both quote types: uses XPath concat() to combine safely quoted segments, e.g. concat('A',"'","'",'B','"') for A'B"

Arguments:

  • name -- service variable name (str)

  • value -- variable value (str, int, boolean)

chevron-rightadd_plain(...)hashtag

Method:

Add a value for the variable 'name'.

It's up to the caller to do proper quoting of value.

For arguments, see Variables.add()

chevron-rightappend(...)hashtag

Method:

Append object to the end of the list.

chevron-rightclear(...)hashtag

Method:

Remove all items from list.

chevron-rightcopy(...)hashtag

Method:

Return a shallow copy of the list.

chevron-rightcount(...)hashtag

Method:

Return number of occurrences of value.

chevron-rightextend(...)hashtag

Method:

Extend list by appending elements from the iterable.

chevron-rightindex(...)hashtag

Method:

Return first index of value.

Raises ValueError if the value is not present.

chevron-rightinsert(...)hashtag

Method:

Insert object before index.

chevron-rightpop(...)hashtag

Method:

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

chevron-rightremove(...)hashtag

Method:

Remove first occurrence of value.

Raises ValueError if the value is not present.

chevron-rightreverse(...)hashtag

Method:

Reverse IN PLACE.

chevron-rightsort(...)hashtag

Method:

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

Last updated

Was this helpful?