|
| 1 | +import os |
| 2 | +import logging |
| 3 | +import shutil |
| 4 | +from importlib import resources |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import attrs |
| 8 | +import chevron |
| 9 | +import click |
| 10 | + |
| 11 | +from . import templates |
| 12 | + |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + 'generate_soup_app' |
| 16 | +] |
| 17 | +TEMPLATES_PATH = resources.files(templates) |
| 18 | +LOG = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +@attrs.define(auto_attribs=True) |
| 22 | +class Ans1Generator: |
| 23 | + asn1_input_files_dir: str |
| 24 | + app_name: str |
| 25 | + pdu: str |
| 26 | + op_dir: str |
| 27 | + template: str |
| 28 | + prefix: str = '' |
| 29 | + package_name: str = '' |
| 30 | + generate_init_file: bool = False |
| 31 | + _op_file: str = None |
| 32 | + _module_name: str = None |
| 33 | + _asn1_files: list = None |
| 34 | + |
| 35 | + def __attrs_post_init__(self): |
| 36 | + prefix = f'{self.prefix}_' if self.prefix else '' |
| 37 | + self.template = self.template if self.template.endswith('.mustache') else f'{self.template}.mustache' |
| 38 | + self.template = os.path.join(str(TEMPLATES_PATH), self.template) |
| 39 | + self._module_name = f'{prefix}{self.app_name}' |
| 40 | + self._op_file = os.path.join(self.op_dir, f'{self._module_name}.py') |
| 41 | + self._asn1_files = [ |
| 42 | + os.path.join(self.asn1_input_files_dir, file) |
| 43 | + for file in os.listdir(self.asn1_input_files_dir) |
| 44 | + if file.endswith('.asn1') |
| 45 | + ] |
| 46 | + shutil.rmtree(self.op_dir) |
| 47 | + Path(self.op_dir).mkdir(parents=True, exist_ok=True) |
| 48 | + |
| 49 | + def generate(self, extra_context=None): |
| 50 | + context = { |
| 51 | + 'app_name': self.app_name, |
| 52 | + 'pdu_name': self.pdu, |
| 53 | + 'package_name': self.package_name, |
| 54 | + 'spec': { |
| 55 | + 'name': self.app_name, |
| 56 | + 'capitalised_name': self.app_name.capitalize() |
| 57 | + }, |
| 58 | + } |
| 59 | + context.update(extra_context or {}) |
| 60 | + |
| 61 | + generated_files = [Ans1Generator._generate(self.template, context, self._op_file)] |
| 62 | + if self.generate_init_file: |
| 63 | + generated_files.append(self._generate_init_file()) |
| 64 | + |
| 65 | + self._copy_asn1_files() |
| 66 | + |
| 67 | + return generated_files |
| 68 | + |
| 69 | + def _generate_init_file(self): |
| 70 | + init_file = os.path.join(self.op_dir, '__init__.py') |
| 71 | + context = { |
| 72 | + 'modules': [ |
| 73 | + {'name': self._module_name} |
| 74 | + ] |
| 75 | + } |
| 76 | + init_template = os.path.join(str(TEMPLATES_PATH), 'init.mustache') |
| 77 | + Ans1Generator._generate(init_template, context, init_file) |
| 78 | + return init_file |
| 79 | + |
| 80 | + def _copy_asn1_files(self): |
| 81 | + op_spec_dir = os.path.join(self.op_dir, 'spec') |
| 82 | + Path(op_spec_dir).mkdir(parents=True, exist_ok=True) |
| 83 | + for spec_file in self._asn1_files: |
| 84 | + shutil.copy2(spec_file, os.path.join(op_spec_dir, os.path.basename(spec_file))) |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def _generate(template, context, op_file): |
| 88 | + with open(op_file, 'a', encoding='utf-8') as op, open(template, 'r', encoding='utf-8') as inp: |
| 89 | + code_as_string = chevron.render(inp.read(), context, partials_path=str(TEMPLATES_PATH)) |
| 90 | + op.write(code_as_string) |
| 91 | + print(f'Generated: {op_file}') |
| 92 | + return op_file |
| 93 | + |
| 94 | + |
| 95 | +@click.command() |
| 96 | +@click.option('--asn1-files-dir', type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True)) |
| 97 | +@click.option('--app-name', type=click.STRING) |
| 98 | +@click.option('--pdu-name', type=click.STRING) |
| 99 | +@click.option('--prefix', type=click.STRING, default='') |
| 100 | +@click.option('--op-dir', type=click.Path(exists=True, writable=True)) |
| 101 | +@click.option('--package-name', type=click.STRING) |
| 102 | +@click.option('--init-file/--no-init-file', show_default=True, default=True) |
| 103 | +def generate_soup_app(asn1_files_dir, app_name, pdu_name, prefix, op_dir, package_name, init_file): |
| 104 | + generator = Ans1Generator( |
| 105 | + asn1_files_dir, |
| 106 | + app_name, |
| 107 | + pdu_name, |
| 108 | + op_dir, |
| 109 | + template='ans1_soup_app.mustache', |
| 110 | + prefix=prefix, |
| 111 | + package_name=package_name, |
| 112 | + generate_init_file=init_file |
| 113 | + ) |
| 114 | + generator.generate() |
0 commit comments