No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

66 líneas
1.9 KiB

  1. import os
  2. import sys
  3. from typing import Iterable
  4. from jinja2 import Environment, FileSystemLoader, Template
  5. import config as cfg
  6. from . import app_root_dir, resource_dir, template_dir
  7. _usage = "Usage: generate.py <aws|gcp|azure>"
  8. def load_tmpl(tmpl: str) -> Template:
  9. env = Environment(loader=FileSystemLoader(template_dir()))
  10. env.filters["up_or_title"] = up_or_title
  11. return env.get_template(tmpl)
  12. def up_or_title(pvd: str, s: str) -> str:
  13. return s.upper() if s in cfg.UPPER_WORDS[pvd] else s.title()
  14. def gen_classes(pvd: str, typ: str, paths: Iterable[str]) -> str:
  15. """Generate all service node classes based on resources paths with class templates."""
  16. tmpl = load_tmpl(cfg.TMPL_MODULE)
  17. def _gen_class_meta(path: str) -> dict:
  18. base = os.path.splitext(path)[0]
  19. name = "".join([up_or_title(pvd, s) for s in base.split("-")])
  20. return {"name": name, "icon": path}
  21. metas = map(_gen_class_meta, paths)
  22. aliases = cfg.ALIASES[pvd][typ] if typ in cfg.ALIASES[pvd] else {}
  23. return tmpl.render(pvd=pvd, typ=typ, metas=metas, aliases=aliases)
  24. def make_module(pvd: str, typ: str, classes: str) -> None:
  25. """Create a module file"""
  26. mod_path = os.path.join(app_root_dir(pvd), f"{typ}.py")
  27. with open(mod_path, "w+") as f:
  28. f.write(classes)
  29. def generate(pvd: str) -> None:
  30. """Generates a service node classes."""
  31. for root, _, files in os.walk(resource_dir(pvd)):
  32. # Extract the names and paths from resources.
  33. files.sort()
  34. pngs = filter(lambda f: f.endswith(".png"), files)
  35. paths = filter(lambda f: "rounded" not in f, pngs)
  36. # Skip the top-root directory.
  37. typ = os.path.basename(root)
  38. if typ == pvd:
  39. continue
  40. classes = gen_classes(pvd, typ, paths)
  41. make_module(pvd, typ, classes)
  42. if __name__ == "__main__":
  43. pvd = sys.argv[1]
  44. if pvd not in cfg.PROVIDERS:
  45. sys.exit()
  46. generate(pvd)