Пример #1
0
def makegen(
    app: App,
    *,
    tasks: t.Optional[t.List[str]] = None,
    rootdir: t.Optional[str] = None,
    out: t.Optional[str] = None,
    relative: bool = True,
) -> None:
    import contextlib
    import os
    from egoist.components.tracker import get_tracker
    from egoist.commands.generate import generate

    app.commit(dry_run=True)

    if not bool(os.environ.get("VERBOSE", "")):
        logging.getLogger("prestring.output").setLevel(logging.WARNING)
    generate(app, tasks=tasks, rootdir=rootdir)

    root_path = get_root_path(app.settings, root=rootdir)
    deps = get_tracker().get_dependencies(root=root_path, relative=relative)

    with contextlib.ExitStack() as s:
        out_port: t.Optional[t.IO[str]] = None
        if out is not None:
            out_port = s.enter_context(open(out, "w"))
        print(emit(deps, filename=getattr(out_port, "name", None)),
              file=out_port)
Пример #2
0
def describe(app: App) -> None:
    import json
    import inspect
    from egoist.langhelpers import get_fullname_of_type, get_fullname_of_callable

    app.commit(dry_run=False)

    definitions: t.Dict[str, t.Dict[str, t.Union[str, t.List[str]]]] = {}
    delayed_include_mapping = app.delayed_include_mapping
    for kit, fns in app.registry.generators.items():
        for fn in fns:
            name = get_fullname_of_callable(fn)
            summary = (inspect.getdoc(fn) or "").strip().split("\n", 1)[0]

            definitions[name] = {"doc": summary, "generator": kit}

            if fn in delayed_include_mapping:
                definitions[name]["include_when"] = [
                    get_fullname_of_callable(dep) if callable(dep) else
                    (_app.module.__name__ +
                     dep if dep.startswith(".") else dep)
                    for _app, dep in delayed_include_mapping[fn]
                ]

    factories = {
        name: [get_fullname_of_type(x) for x in xs]  # type: ignore
        for name, xs in app.registry.factories.items()
    }

    empty_context = app.context_factory()
    current_directives = set(name
                             for name, attr in app.context.__dict__.items()
                             if callable(attr))
    append_directives = current_directives.difference(
        empty_context.__dict__.keys())
    append_directives = append_directives.difference(["run"])

    d = {
        "definitions": definitions,
        "components": factories,
        "directives": {
            name: get_fullname_of_type(getattr(app.context, name))
            for name in append_directives
        },
    }
    print(json.dumps(d, indent=2, ensure_ascii=False))
Пример #3
0
def generate(app: App,
             *,
             tasks: t.Optional[t.List[str]] = None,
             rootdir: t.Optional[str] = None,
             dry_run: bool = False) -> None:
    root_path = get_root_path(app.settings, root=rootdir)
    app.commit(dry_run=dry_run)
    app.context.queue.clear()  # xxx: clear

    action_list: t.List[t.Callable[..., t.Any]] = []
    included_after_commit_list: t.List[t.Union[str, t.Callable[...,
                                                               t.Any]]] = []

    for kit, fns in app.registry.generators.items():
        walk_or_module = app.maybe_dotted(kit)
        if callable(walk_or_module):
            walk = walk_or_module
        elif hasattr(walk_or_module, "walk"):
            walk = walk_or_module.walk  # type: ignore
        else:
            # TODO: genetle error message
            raise ConfigurationError("{kit!r} is not callable")

        if not tasks:
            sources = {fn.__name__: fn for fn in fns}
        else:
            sources = {fn.__name__: fn for fn in fns if fn.__name__ in tasks}
        action_list.append(partial(walk, sources, root=root_path))

        # for app.include_when()
        delayed_include_mapping = app.delayed_include_mapping
        for fn in sources.values():
            if fn in delayed_include_mapping:
                for _app, path in delayed_include_mapping[fn]:
                    _app.include(path)
                    included_after_commit_list.append(path)
        app.delayed_include_mapping.clear()  # xxx: clear

    if len(included_after_commit_list) > 0:
        app.shallow_commit()

    for action in action_list:
        action()
Пример #4
0
def hello(app: App) -> None:
    app.commit(dry_run=False)
    print("do something")
Пример #5
0
    def _register():
        print("!")

    app.registry.generators[name].append(fn)
    app.action(fn.__name__, _register)
    return fn


if __name__ == "__main__":
    app = App()
    app.include("__main__.define_foo")

    @app.define_foo("xxx")
    def yyy():
        print("yyy")

    @app.define_foo("xxx")
    def zzz():
        print("zzz")

    print("s")
    app.commit()
    yyy()
    zzz()
    print("e")

    from egoist.commands.describe import describe

    describe(app)