diff --git a/gouttelette/cmd/refresh_modules.py b/gouttelette/cmd/refresh_modules.py index 46aac79..ad4f874 100644 --- a/gouttelette/cmd/refresh_modules.py +++ b/gouttelette/cmd/refresh_modules.py @@ -22,7 +22,6 @@ get_module_added_ins, get_module_from_config, python_type, - get_generator, camel_to_snake, ignore_description, ) @@ -568,6 +567,7 @@ def renderer(self, target_dir: str, next_version: str): content = jinja2_renderer( self.template_file, + target_dir=target_dir, arguments=indent(arguments, 4), documentation=documentation_to_string, name=self.name, @@ -947,6 +947,7 @@ def renderer(self, target_dir: str, next_version: str): content = jinja2_renderer( self.template_file, + target_dir=target_dir, arguments=indent(arguments, 4), documentation=documentation, list_index=self.list_index(), @@ -1233,7 +1234,9 @@ def generate_vmware_rest(args: Iterable): module_list = [] for json_file in ["vcenter.json", "content.json", "appliance.json"]: print("Generating modules from {}".format(json_file)) - api_spec_file = args.target_dir / "api_specifications" / "7.0.2" / json_file + api_spec_file = ( + args.target_dir / "gouttelette" / "api_specifications" / "7.0.2" / json_file + ) raw_content = api_spec_file.read_text() swagger_file = SwaggerFile(raw_content) resources = swagger_file.init_resources(swagger_file.paths.values()) @@ -1290,20 +1293,14 @@ def generate_vmware_rest(args: Iterable): def main(): - generator = get_generator() - if not generator: - raise Exception("gouttelette.yaml is missing generator value") - - parser = argparse.ArgumentParser( - description=f"Build the {generator['name']} modules." - ) + parser = argparse.ArgumentParser(description=f"Build the modules.") parser.add_argument( "--target-dir", dest="target_dir", type=pathlib.Path, - default=pathlib.Path(generator["default_path"]), - help=f"location of the target repository (default: {generator['default_path']})", + default=pathlib.Path("."), + help=f"location of the target repository (default: .)", ) parser.add_argument( "--next-version", @@ -1311,29 +1308,30 @@ def main(): default="TODO", help="the next major version", ) - if generator.get("name") == "amazon_cloud_code_generator": - parser.add_argument( - "--schema-dir", - type=pathlib.Path, - default=pathlib.Path("gouttelette/api_specifications/"), - help="location where to store the collected schemas (default: ./gouttelette/api_specifications/amazon_cloud)", - ) + parser.add_argument( + "--schema-dir", + type=pathlib.Path, + default=pathlib.Path("gouttelette/api_specifications/"), + help="location where to store the collected schemas (default: ./gouttelette/api_specifications/)", + ) args = parser.parse_args() + gouttelette_config = yaml.safe_load( + (args.target_dir / "gouttelette.yml").read_text() + ) + print(gouttelette_config["generator"]) func = { "vmware_rest_code_generator": generate_vmware_rest, "amazon_cloud_code_generator": generate_amazon_cloud, - }[generator["name"]] + }[gouttelette_config["generator"]] func(args) - - info = VersionInfo(generator["name"]) dev_md = args.target_dir / "dev.md" dev_md.write_text( ( "The modules are autogenerated by:\n" "https://github.com/ansible-collections/gouttelette\n" "" - f"version: {info.version_string()}\n" + f"version: TODO\n" ) ) dev_md = args.target_dir / "commit_message" @@ -1344,7 +1342,7 @@ def main(): "The modules are autogenerated by:\n" "https://github.com/ansible-collections/gouttelette\n" "" - f"version: {info.version_string()}\n" + f"version: TODO\n" ) ) diff --git a/gouttelette/utils.py b/gouttelette/utils.py index 993f4d0..d461a64 100644 --- a/gouttelette/utils.py +++ b/gouttelette/utils.py @@ -14,31 +14,18 @@ from functools import lru_cache -def jinja2_renderer(template_file: str, **kwargs: Dict[str, Any]) -> str: +def jinja2_renderer( + template_file: str, target_dir: Path, **kwargs: Dict[str, Any] +) -> str: - template_path = re.sub("(.*)_code_generator", r"\1", get_generator()["name"]) - templateLoader = jinja2.FileSystemLoader("gouttelette") - templateEnv = jinja2.Environment(loader=templateLoader) - template = templateEnv.get_template( - "templates/" + template_path + "/" + template_file + templateLoader = jinja2.FileSystemLoader( + str(target_dir / "gouttelette" / "templates") ) + templateEnv = jinja2.Environment(loader=templateLoader) + template = templateEnv.get_template(template_file) return template.render(kwargs) -def get_generator() -> Dict[str, Any]: - generator = {} - with open("gouttelette.yml", "r") as file: - try: - generator.update({"name": yaml.safe_load(file)["generator"]}) - if "amazon_cloud_code_generator" in generator["name"]: - generator.update({"default_path": "cloud"}) - elif "vmware_rest_code_generator" in generator["name"]: - generator.update({"default_path": "vmware_rest"}) - except yaml.YAMLError as exc: - print(exc) - return generator - - def format_documentation(documentation: Any) -> str: yaml.Dumper.ignore_aliases = lambda *args: True # type: ignore