Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_gen_interface():
system = loadSystem()
gen = Generator(search_path='tests/templates')
template = """
{%- for interface in module.interfaces -%}
{{interface}}
{%- endfor -%}
"""
module = system.lookup('com.pelagicore.ivi.tuner')
text = gen.apply(template, {"module": module})
assert text == 'Tuner'
def generate(tplconfig, moduleConfig, annotations, src, dst):
log.debug('run {0} {1}'.format(src, dst))
FileSystem.strict = True
Generator.strict = True
system = FileSystem.parse(src)
for annotations_file in annotations:
log.debug('{0}'.format(annotations_file))
if not os.path.isabs(annotations_file):
annotations_file = Path.getcwd() / str(annotations_file)
if not Path(annotations_file).exists():
print('no such annotation file: {0}'.format(annotations_file))
exit(1)
FileSystem.merge_annotations(system, Path(annotations_file))
generator = Generator(search_path=[tplconfig, here])
generator.env.keep_trailing_newline = True
generator.register_filter('return_type', return_type)
generator.register_filter('parameter_type_default', parameter_type_default)
generator.register_filter('parameter_type', parameter_type)
generator.register_filter('getter_name', getter_name)
generator.register_filter('setter_name', setter_name)
generator.register_filter('test_type_value', test_type_value)
generator.register_filter('default_type_value', default_type_value)
generator.register_filter('default_value', default_value)
generator.register_filter('model_type', model_type)
generator.register_filter('flag_type', flag_type)
generator.register_filter('range_low', range_low)
generator.register_filter('range_high', range_high)
generator.register_filter('strip_QT', strip_QT)
generator.register_filter('domain_values', domain_values)
error = False
try:
self._write(file_path, template, context, preserve, force)
except TemplateSyntaxError as exc:
message = '{0}:{1}: error: {2}'.format(exc.filename, exc.lineno, exc.message)
click.secho(message, fg='red', err=True)
error = True
except TemplateNotFound as exc:
message = '{0}: error: Template not found'.format(exc.name)
click.secho(message, fg='red', err=True)
error = True
except TemplateError as exc:
# Just return with an error, the generic template_error_handler takes care of printing it
error = True
if error and Generator.strict:
sys.exit(1)
def paramterType(symbol):
if symbol.type.is_void or symbol.type.is_primitive:
return '{0} {1}'.format(symbol.type, symbol)
else:
return 'const {0} &{1}'.format(symbol.type, symbol)
def returnType(symbol):
if symbol.type.is_void or symbol.type.is_primitive:
return symbol.type
else:
return symbol.type
generator = Generator()
generator.register_filter('returnType', returnType)
generator.register_filter('parameterType', paramterType)
for package in system.packages:
for service in package.services:
ctx = {'service': service, 'package': package}
generator.write('out/{{service|lower}}.h', 'service.tpl.h', ctx)
generator.write('out/{{service|lower}}.cpp', 'service.tpl.cpp', ctx)
ctx = {'package': package}
generator.write('out/{{package}}.pro', 'services.pro', ctx)
click.secho('create: {0}'.format(path), fg='blue')
path.open('w', encoding='utf-8').write(data)
def _has_different_content(self, data, path):
if not path.exists():
return True
dataHash = hashlib.new('md5', data.encode('utf-8')).digest()
pathHash = path.read_hash('md5')
return dataHash != pathHash
def register_filter(self, name, callback):
"""Register your custom template filter"""
self.env.filters[name] = callback
class RuleGenerator(Generator):
"""Generates documents based on a rule YAML document"""
def __init__(self, search_path: str, destination:Path, context:dict={}, features:set=set()):
super().__init__(search_path, context)
self.context.update({
'dst': destination,
'project': Path(destination).name,
'features': features,
})
self.destination = '{{dst}}'
self.features = features
def process_rules(self, path: Path, system: System):
"""writes the templates read from the rules document"""
self.context.update({
'system': system,
})
def paramterType(symbol):
if symbol.type.is_void or symbol.type.is_primitive:
return '{0} {1}'.format(symbol.type, symbol)
else:
return 'const {0} &{1}'.format(symbol.type, symbol)
def returnType(symbol):
if symbol.type.is_void or symbol.type.is_primitive:
return symbol.type
else:
return symbol.type
generator = Generator()
generator.register_filter('returnType', returnType)
generator.register_filter('parameterType', paramterType)
generator.register_filter('hex', hex)
for package in system.packages:
for service in package.services:
ctx = {'service': service, 'package': package}
generator.write('out/{{service|lower}}.h', 'ivi_service.h', ctx)
generator.write('out/{{service|lower}}.cpp', 'ivi_service.cpp', ctx)
ctx = {'package': package}
generator.write('out/{{package}}.pro', 'ivi_project.pro', ctx)
def generate(input, output):
system = FileSystem.parse_dir(input)
generator = Generator(searchpath='./templates')
generator.register_filter('className', className)
generator.register_filter('returnType', returnType)
generator.register_filter('parameterType', paramterType)
ctx = {'output': output}
for module in system.modules:
logger.debug('process %s' % module)
moduleName = module.nameParts[-1].capitalize()
ctx.update({'module': module, 'moduleName': moduleName})
moduleOutput = generator.apply('{{output}}/ivi{{moduleName|lower}}', ctx)
ctx.update({'path': moduleOutput})
generator.write('{{path}}/ivi{{moduleName|lower}}.pro', 'project.pro', ctx)
for interface in module.interfaces:
ctx.update({'interface': interface})
generator.write('{{path}}/{{interface|className|lower}}.h', 'interface.h', ctx)
generator.write('{{path}}/{{interface|className|lower}}_p.h', 'interface_p.h', ctx)
generator.write('{{path}}/{{interface|className|lower}}.cpp', 'interface.cpp', ctx)
def run(src, dst):
log.debug('run {0} {1}'.format(src, dst))
system = FileSystem.parse(src)
generator = Generator(search_path=here / 'templates')
generator.register_filter('returnType', Filters.returnType)
generator.register_filter('parameterType', Filters.parameterType)
generator.register_filter('defaultValue', Filters.defaultValue)
generator.register_filter('parameters', Filters.parameters)
generator.register_filter('parse_doc', parse_doc)
ctx = {'dst': dst}
for module in system.modules:
log.debug('generate code for module %s', module)
ctx.update({'module': module})
dst = generator.apply('{{dst}}/{{module|lower|replace(".", "-")}}', ctx)
generator.destination = dst
generator.write('.qmake.conf', 'qmake.conf', ctx)
generator.write('{{module|lower|replace(".", "-")}}.pro', 'plugin.pro', ctx, preserve=True)
generator.write('CMakeLists.txt', 'CMakeLists.txt', ctx)
generator.write('plugin.cpp', 'plugin.cpp', ctx, preserve=True)
generator.write('plugin.h', 'plugin.h', ctx, preserve=True)
def generate(input, output):
system = FileSystem.parse_dir(input)
generator = Generator(searchpath='templates')
ctx = {'output': output, 'system': system}
generator.write('{{output}}/modules.csv', 'modules.csv', ctx)