Source code for nunavut.generators

#
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Copyright (C) 2018-2019  UAVCAN Development Team  <uavcan.org>
# This software is distributed under the terms of the MIT License.
#
"""
Module containing types and utilities for building generator objects.
Generators abstract the code generation technology used to transform
pydsdl AST into source code.
"""

import abc
import pathlib
import typing

import nunavut
from nunavut._utilities import YesNoDefault


[docs]class AbstractGenerator(metaclass=abc.ABCMeta): """ Abstract base class for classes that generate source file output from a given pydsdl parser result. :param nunavut.Namespace namespace: The top-level namespace to generates types at and from. :param YesNoDefault generate_namespace_types: Set to YES to force generation files for namespaces and NO to suppress. DEFAULT will generate namespace files based on the language preference. """ def __init__( self, namespace: nunavut.Namespace, generate_namespace_types: YesNoDefault = YesNoDefault.DEFAULT, ): self._namespace = namespace if generate_namespace_types == YesNoDefault.YES: self._generate_namespace_types = True elif generate_namespace_types == YesNoDefault.NO: self._generate_namespace_types = False else: target_language = self._namespace.get_language_context().get_target_language() if target_language is not None and target_language.has_standard_namespace_files: self._generate_namespace_types = True else: self._generate_namespace_types = False @property def namespace(self) -> nunavut.Namespace: """ The root :class:`nunavut.Namespace` for this generator. """ return self._namespace @property def generate_namespace_types(self) -> bool: """ If true then the generator is set to emit files for :class:`nunavut.Namespace` in addition to the pydsdl datatypes. If false then only files for pydsdl datatypes will be generated. """ return self._generate_namespace_types
[docs] @abc.abstractmethod def get_templates(self) -> typing.Iterable[pathlib.Path]: """ Enumerate all templates found in the templates path. :return: A list of paths to all templates found by this Generator object. """ raise NotImplementedError()
[docs] @abc.abstractmethod def generate_all(self, is_dryrun: bool = False, allow_overwrite: bool = True) -> typing.Iterable[pathlib.Path]: """ Generates all output for a given :class:`nunavut.Namespace` and using the templates found by this object. :param bool is_dryrun: If True then no output files will actually be written but all other operations will be performed. :param bool allow_overwrite: If True then the generator will attempt to overwrite any existing files it encounters. If False then the generator will raise an error if the output file exists and the generation is not a dry-run. :return: 0 for success. Non-zero for errors. :raises: PermissionError if :attr:`allow_overwrite` is False and the file exists. """ raise NotImplementedError()
[docs]def create_generators( namespace: nunavut.Namespace, **kwargs: typing.Any ) -> typing.Tuple["AbstractGenerator", "AbstractGenerator"]: """ Create the two generators used by Nunavut; a code-generator and a support-library generator. :param nunavut.Namespace namespace: The namespace to generate code within. :param kwargs: A list of arguments that are forwarded to the generator constructors. :return: Tuple with the first item being the code-generator and the second the support-library generator. """ from nunavut.jinja import DSDLCodeGenerator, SupportGenerator return (DSDLCodeGenerator(namespace, **kwargs), SupportGenerator(namespace, **kwargs))