def create_from_file(setting_file_path: Path, output_file_path: Path): assert setting_file_path.is_file(), setting_file_path template_file = Path(__file__).parent / 'template' / 'template.py' assert template_file.is_file(), str(template_file) # Load Setting config_parser = ConfigParser() setting = Configer.load_setting(setting_file_path) params = [ config_parser.parse(k, v, parent_class_name=None) for k, v in setting.items() ] # Render config_string = generate(list(config_parser.dataclasses.values()), params, str(setting_file_path), hash_md5(setting_file_path)) with prestring_output.output(root=output_file_path.parent) as fs: with fs.open(str(output_file_path.name), 'w') as wf: print(config_string, file=wf) # Log if lock_file_path.is_file(): with open(lock_file_path, 'r') as f: current_contents = yaml.safe_load(f) if not isinstance(current_contents, dict): current_contents = {} else: current_contents = {} with open(lock_file_path, 'w') as f: current_contents[str(setting_file_path)] = { 'hash_value': hash_md5(setting_file_path), 'output': str(output_file_path) } yaml.safe_dump(current_contents, f)
def run(dst: str): w = get_walker(aggressive=True, here=__name__) with output(dst, use_console=True, verbose=True, opener=partial(Module, indent="\t")) as fs: for cls in w.walk(): with fs.open(f"{snakecase(cls.__name__)}.go", "w") as m: ctx = Context(package="gen", w=w, m=m) emit_package_clause(ctx) m.sep() emit_simple_type_definition(ctx, cls)
def run(dst: str) -> None: c = Config() logging.basicConfig(level=logging.DEBUG) # NOCHECK=1 FAKE=1 VERBOSE=1 with output(dst) as fs: project_dir = pathlib.Path(c.name) with fs.open(project_dir / ".gitignore", "w") as wf: print("*.py", file=wf) with fs.open(project_dir / "README.md", "w") as wf: print(f"# {c.name}", file=wf) print("", file=wf) print(c.description, file=wf) fs.open(project_dir / "CHANGES.rst", "w")
def run(dst: str): import sys m = sys.modules[__name__] w = get_walker(m, aggressive=True) with output(dst, use_console=True, verbose=True, opener=partial(Module, indent="\t")) as fs: for cls in w.walk(): if issubclass(cls, Context): continue with fs.open(f"{snakecase(cls.__name__)}.go", "w") as m: ctx = Context(package="gen", w=w, m=m) emit_package_clause(ctx) m.sep() emit_simple_type_definition(ctx, cls)
def run(dst: str, *, config: str) -> None: d = loading.loadfile(config) try: c = Config(**d) except pydantic.ValidationError as e: print(e.json(), file=sys.stderr) sys.exit(1) logging.basicConfig(level=logging.DEBUG) # NOCHECK=1 FAKE=1 VERBOSE=1 with output(dst) as fs: project_dir = pathlib.Path(c.name) with fs.open(project_dir / ".gitignore", "w") as wf: print("*.py", file=wf) with fs.open(project_dir / "README.md", "w") as wf: print(f"# {c.name}", file=wf) print("", file=wf) print(c.description, file=wf) fs.open(project_dir / "CHANGES.rst", "w")
m = m or self.m m.stmt("definitions:") return m.scope() def parameters(self, *, m=None): m = m or self.m m.stmt("parameters:") return m.scope() if __name__ == "__main__": season = Enum( name="season", description="四季", choices=[ Choice(label="春", name="spring"), Choice(label="夏", name="summer"), Choice(label="秋", name="autumn"), Choice(label="冬", name="winter"), ], ) with output("schemas", verbose=True, use_console=True) as fs: with fs.open("schema.yaml", "w", opener=partial(Module, indent=" ")) as m: dsl = DSL(m) with dsl.definitions(): dsl.enum.definition(season) m.sep() with dsl.parameters(): dsl.enum.parameter(season)
if m is None: continue is_reading = True next(lines) continue if not line.startswith(" "): is_reading = False r.append(buf) buf = [] continue buf.append(line) rx2 = re.compile(r"^\s+([a-zA-Z0-9_]+)\.") with output("output") as fs: with fs.open("Makefile", "w", opener=Module) as mk: mk.stmt("SHELL := $(shell which bash)") mk.append("default: ") mdefault_deps = mk.submodule() mk.stmt("") for i, buf in enumerate(r): code = "\n".join(buf) if "..." in code: continue mdefault_deps.append(f"{i:03d} ") mk.stmt(f"{i:03d}:") mk.stmt(
def output(self, *, dry_run: bool) -> output[StringIO]: if dry_run: return output(self.dst, use_console=True, verbose=True) return output(self.dst)
from prestring.output import output from prestring.python import Module import hello with output("", opener=Module, use_console=True, verbose=True) as fs: with fs.open(hello.__name__, "w") as m: hello.hello(m)
import typing as t from prestring import output from io import StringIO from prestring.python import Module with output.output("", use_console=True, verbose=True, opener=StringIO) as fs: with fs.open("foo.py", "w", opener=Module) as m: m.stmt("print({!r})", "foo") with fs.open("VERSION", "w", opener=StringIO) as io: io.write("0.0.0") with fs.open("xxx", "w") as x: # type: t.IO[str] x.write("xxx")
import typing as t from prestring import output from prestring.python import Module with output.output("", use_console=True, verbose=True) as fs: with fs.open("foo.py", "w", opener=Module) as m: # type: Module m.stmt("print({!r})", "foo") with fs.open("VERSION", "w") as io: # type: t.IO[str] io.write("0.0.0")
import logging from prestring.python import Module from prestring.output import output, cleanup_all # noqa logging.basicConfig(level=logging.DEBUG) with output(root="11minifs", cleanup=cleanup_all) as fs: with fs.open("projects/x.txt", "w") as wf: print("hello x", file=wf) print("bye x", file=wf) with fs.open("projects/y.txt", "w") as wf: print("hello y", file=wf) print("bye y", file=wf) with fs.open("projects/z.py", "w", opener=Module) as m: with m.def_("hello"): m.stmt("print('hello')")