1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
=====
Usage
=====
Concept
=======
QFace requires one or more IDL files as input file and a generator to produce output files. The IDL files are named QFace interface documents.
.. figure:: qface_concept.jpg
To use qface you need to write your own generator. A generatopr is a small python script which reads the qface document and write code using a generator.
.. code-block:: python
# gen.py
from qface.generator import FileSystem, Generator
def generate(input, output):
# parse the interface files
system = FileSystem.parse(input)
# setup the generator
generator = Generator(search_path='templates')
# create a context object
ctx = {'output': output, 'system': system}
# apply the context on the template and write the output to file
generator.write('{{output}}/modules.csv', 'modules.csv', ctx)
# call the generation function
generate('sample.qface', 'out')
.. code-block:: sh
python3 gen.py
Code Generation Principle
=========================
The code generation is driven by a small script which iterates over the domain model and writes files using the Python Jinja template language. Refer to http://jinja.pocoo.org and particularly the template designer documentation at http://jinja.pocoo.org/docs/dev/templates/.
.. code-block:: python
from qface.generator import FileSystem, Generator
def generate(input, output):
system = FileSystem.parse(input)
generator = Generator(searchpath='templates')
ctx = {'output': output, 'system': system}
generator.write('{{output}}/modules.csv', 'modules.csv', ctx)
This script reads the input directory returns a system object form the domain model. This is used as the root object for the code generation inside the template language.
.. code-block:: jinja
{% for module in system.modules %}
{%- for interface in module.interfaces -%}
SERVICE, {{module}}.{{interface}}
{% endfor -%}
{%- for struct in module.structs -%}
STRUCT , {{module}}.{{struct}}
{% endfor -%}
{%- for enum in module.enums -%}
ENUM , {{module}}.{{enum}}
{% endfor -%}
{% endfor %}
The template iterates over the domain objects and generates text which is written into a file. Using the generator write method ``generator.write(path, template, context)`` the output file path can also be specified using the template syntax .
|