Asterisk - The Open Source Telephony Project  18.5.0
Public Member Functions | Data Fields
Transform Class Reference
Inheritance diagram for Transform:
Inheritance graph
[legend]
Collaboration diagram for Transform:
Collaboration graph
[legend]

Public Member Functions

def __init__ (self, template_file, dest_file_template_str, overwrite=True)
 
def render (self, renderer, model, dest_dir)
 

Data Fields

 dest_file_template
 
 overwrite
 
 template
 

Detailed Description

Transformation for template to code.

Definition at line 31 of file transform.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  template_file,
  dest_file_template_str,
  overwrite = True 
)
Ctor.

@param template_file: Filename of the mustache template.
@param dest_file_template_str: Destination file name. This is a
    mustache template, so each resource can write to a unique file.
@param overwrite: If True, destination file is ovewritten if it exists.

Definition at line 34 of file transform.py.

References transform.unicode().

34  def __init__(self, template_file, dest_file_template_str, overwrite=True):
35  """Ctor.
36 
37  @param template_file: Filename of the mustache template.
38  @param dest_file_template_str: Destination file name. This is a
39  mustache template, so each resource can write to a unique file.
40  @param overwrite: If True, destination file is ovewritten if it exists.
41  """
42  template_str = unicode(open(template_file, "r").read())
43  self.template = pystache.parse(template_str)
44  dest_file_template_str = unicode(dest_file_template_str)
45  self.dest_file_template = pystache.parse(dest_file_template_str)
46  self.overwrite = overwrite
47 
def unicode(v)
Definition: transform.py:27

Member Function Documentation

◆ render()

def render (   self,
  renderer,
  model,
  dest_dir 
)
Render a model according to this transformation.

@param render: Pystache renderer.
@param model: Model object to render.
@param dest_dir: Destination directory to write generated code.

Definition at line 48 of file transform.py.

References Transform.dest_file_template, Transform.overwrite, Transform.template, and phoneprov_file.template.

48  def render(self, renderer, model, dest_dir):
49  """Render a model according to this transformation.
50 
51  @param render: Pystache renderer.
52  @param model: Model object to render.
53  @param dest_dir: Destination directory to write generated code.
54  """
55  dest_file = pystache.render(self.dest_file_template, model)
56  dest_file = os.path.join(dest_dir, dest_file)
57  dest_exists = os.path.exists(dest_file)
58  if dest_exists and not self.overwrite:
59  return
60  with tempfile.NamedTemporaryFile(mode='w+') as out:
61  out.write(renderer.render(self.template, model))
62  out.flush()
63 
64  if not dest_exists or not filecmp.cmp(out.name, dest_file):
65  print("Writing %s" % dest_file)
66  shutil.copyfile(out.name, dest_file)
67 

Field Documentation

◆ dest_file_template

dest_file_template

Definition at line 45 of file transform.py.

Referenced by Transform.render().

◆ overwrite

overwrite

Definition at line 46 of file transform.py.

Referenced by Transform.render().

◆ template

template

Definition at line 43 of file transform.py.

Referenced by Transform.render().


The documentation for this class was generated from the following file: