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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#!/usr/bin/env python3
# Copyright (c) Pelagicore AB 2016
import click
import logging
import logging.config
import yaml
from qface.generator import FileSystem, Generator
import os
from path import Path
here = os.path.dirname(__file__)
def paramterType(symbol):
module_name = symbol.module.module_name
if symbol.type.is_enum:
return 'Qml{0}Module::{1} {2}'.format(module_name, symbol.type, symbol)
if symbol.type.is_void or symbol.type.is_primitive:
if symbol.type.name == 'string':
return 'const QString &{0}'.format(symbol)
if symbol.type.name == 'var':
return 'const QVariant &{0}'.format(symbol)
if symbol.type.name == 'real':
return 'float {0}'.format(symbol)
return '{0} {1}'.format(symbol.type, symbol)
elif symbol.type.is_list:
return 'const QList<{0}> &{1}'.format(symbol.type.nested, symbol)
elif symbol.type.is_model:
return '{0}Model *{1}'.format(symbol.type.nested, symbol)
else:
return 'const {0} &{1}'.format(symbol.type, symbol)
def returnType(symbol):
module_name = symbol.module.module_name
if symbol.type.is_enum:
return 'Qml{0}Module::{1}'.format(module_name, symbol.type)
if symbol.type.is_void or symbol.type.is_primitive:
if symbol.type.name == 'string':
return 'QString'
if symbol.type.name == 'var':
return 'QVariant'
if symbol.type.name == 'real':
return 'float'
return symbol.type
elif symbol.type.is_list:
return 'QList<{0}>'.format(symbol.type.nested)
elif symbol.type.is_model:
return '{0}Model*'.format(symbol.type.nested)
else:
return symbol.type
def run_generation(input, output):
system = FileSystem.parse(input)
generator = Generator(searchpath=os.path.join(here, 'templates'))
generator.register_filter('returnType', returnType)
generator.register_filter('parameterType', paramterType)
ctx = {'output': output}
for module in system.modules:
ctx.update({'module': module})
moduleOutput = generator.apply('{{output}}/{{module|lower}}', ctx)
ctx.update({'path': moduleOutput})
generator.write('{{path}}/qmldir', 'qmldir', ctx)
generator.write('{{path}}/plugin.cpp', 'plugin.cpp', ctx)
generator.write('{{path}}/plugin.h', 'plugin.h', ctx)
generator.write('{{path}}/{{module|lower}}.pri', 'project.pri', ctx)
generator.write('{{path}}/{{module|lower}}.pro', 'project.pro', ctx)
generator.write('{{path}}/qml{{module.module_name|lower}}module.h', 'module.h', ctx)
generator.write('{{path}}/qml{{module.module_name|lower}}module.cpp', 'module.cpp', ctx)
for interface in module.interfaces:
ctx.update({'interface': interface})
generator.write('{{path}}/qmlabstract{{interface|lower}}.h', 'interface.h', ctx)
generator.write('{{path}}/qmlabstract{{interface|lower}}.cpp', 'interface.cpp', ctx)
for struct in module.structs:
ctx.update({'struct': struct})
generator.write('{{path}}/{{struct|lower}}.h', 'struct.h', ctx)
generator.write('{{path}}/{{struct|lower}}.cpp', 'struct.cpp', ctx)
generator.write('{{path}}/{{struct|lower}}model.h', 'structmodel.h', ctx)
generator.write('{{path}}/{{struct|lower}}model.cpp', 'structmodel.cpp', ctx)
@click.command()
@click.argument('input', nargs=-1, type=click.Path(exists=True))
@click.argument('output', nargs=1, type=click.Path(exists=True))
def generate(input, output):
"""Takes several files or directories as input and generates the code
in the given output directory."""
run_generation(input, output)
if __name__ == '__main__':
generate()
|