Exemple #1
0
    def __init__(self,
                 *,
                 openapi: GeneratorData,
                 meta: MetaType,
                 custom_template_path: Optional[Path] = None) -> None:
        self.openapi: GeneratorData = openapi
        self.meta: MetaType = meta

        package_loader = PackageLoader(__package__)
        loader: BaseLoader
        if custom_template_path is not None:
            loader = ChoiceLoader([
                FileSystemLoader(str(custom_template_path)),
                package_loader,
            ])
        else:
            loader = package_loader
        self.env: Environment = Environment(loader=loader,
                                            trim_blocks=True,
                                            lstrip_blocks=True)

        self.project_name: str = self.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
        self.project_dir: Path = Path.cwd()
        if meta != MetaType.NONE:
            self.project_dir /= self.project_name

        self.package_name: str = self.package_name_override or self.project_name.replace(
            "-", "_")
        self.package_dir: Path = self.project_dir / self.package_name
        self.package_description: str = utils.remove_string_escapes(
            f"A client library for accessing {self.openapi.title}")
        self.version: str = self.package_version_override or openapi.version

        self.env.filters.update(TEMPLATE_FILTERS)
    def __init__(
        self,
        *,
        openapi: GeneratorData,
        meta: MetaType,
        config: Config,
        custom_template_path: Optional[Path] = None,
        file_encoding: str = "utf-8",
    ) -> None:
        self.openapi: GeneratorData = openapi
        self.meta: MetaType = meta
        self.file_encoding = file_encoding
        self.config = config

        package_loader = PackageLoader(__package__)
        loader: BaseLoader
        if custom_template_path is not None:
            loader = ChoiceLoader(
                [
                    FileSystemLoader(str(custom_template_path)),
                    package_loader,
                ]
            )
        else:
            loader = package_loader
        self.env: Environment = Environment(
            loader=loader, trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.loopcontrols"]
        )

        self.project_name: str = config.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
        self.project_dir: Path = Path.cwd()
        if meta != MetaType.NONE:
            self.project_dir /= self.project_name

        self.package_name: str = config.package_name_override or self.project_name.replace("-", "_")
        self.package_dir: Path = self.project_dir / self.package_name
        self.package_description: str = utils.remove_string_escapes(
            f"A client library for accessing {self.openapi.title}"
        )
        self.version: str = config.package_version_override or openapi.version

        self.env.filters.update(TEMPLATE_FILTERS)
        self.env.globals.update(
            utils=utils,
            python_identifier=lambda x: utils.PythonIdentifier(x, config.field_prefix),
            class_name=lambda x: utils.ClassName(x, config.field_prefix),
            package_name=self.package_name,
            package_dir=self.package_dir,
            package_description=self.package_description,
            package_version=self.version,
            project_name=self.project_name,
            project_dir=self.project_dir,
        )
        self.errors: List[GeneratorError] = []
Exemple #3
0
    def __init__(self, *, openapi: GeneratorData) -> None:
        self.openapi: GeneratorData = openapi
        self.env: Environment = Environment(loader=PackageLoader(__package__),
                                            trim_blocks=True,
                                            lstrip_blocks=True)

        self.project_name: str = self.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
        self.project_dir: Path = Path.cwd() / self.project_name

        self.package_name: str = self.package_name_override or self.project_name.replace(
            "-", "_")
        self.package_dir: Path = self.project_dir / self.package_name
        self.package_description: str = utils.remove_string_escapes(
            f"A client library for accessing {self.openapi.title}")
        self.version: str = openapi.version

        self.env.filters.update(self.TEMPLATE_FILTERS)
Exemple #4
0
def test_no_string_escapes():
    assert utils.remove_string_escapes('an "evil" string') == 'an \\"evil\\" string'