예제 #1
0
    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] = []
예제 #2
0
 def test_empty_is_prefixed(self):
     assert utils.ClassName(value="", prefix="something") == "Something"
예제 #3
0
 def test_keywords_are_postfixed(self):
     assert utils.ClassName(value="none", prefix="prefix") == "None_"
예제 #4
0
 def test_invalid_symbols_are_stripped(self):
     assert utils.ClassName(value="$abc", prefix="prefix") == "Abc"
예제 #5
0
 def test_numbers_are_prefixed(self):
     assert utils.ClassName(value="1", prefix="field") == "Field1"
예제 #6
0
 def test_valid_is_not_changed(self):
     assert utils.ClassName(value="ValidClass", prefix="field") == "ValidClass"