Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
load templates from gouttelette/templates in the target directory
Browse files Browse the repository at this point in the history
  • Loading branch information
goneri committed Dec 22, 2022
1 parent 5b6082c commit 6b6ca92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
44 changes: 21 additions & 23 deletions gouttelette/cmd/refresh_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
get_module_added_ins,
get_module_from_config,
python_type,
get_generator,
camel_to_snake,
ignore_description,
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -1290,50 +1293,45 @@ 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",
type=str,
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"
Expand All @@ -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"
)
)

Expand Down
27 changes: 7 additions & 20 deletions gouttelette/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6b6ca92

Please sign in to comment.