Exemplo n.º 1
0
def gen_exports(
    dir_path="./domain",
    gen_flag="# the __all__ is generated",
    export_from_package=True,
    export_modules=None,
    exludes=None,
):
    if not exludes:
        exludes = ["logger"]
    fill_package_if_not_exist(dir_path=dir_path)
    files = list_all_files(dir_path=dir_path)
    for file in files:
        exports = []
        lines = []
        # read and generate __all__
        with open(file) as fp:
            line = fp.readline()
            while line:
                if line.startswith(gen_flag):
                    break
                lines.append(line)
                export = _get_interface_name(line)
                if not export:
                    export = _get_var_name(line)
                if export and export[0].isalpha() and export not in exludes:
                    exports.append(export)
                line = fp.readline()
        print(f"{file}:{exports}")
        lines.append(gen_flag)
        lines.append(f"\n__all__ = {exports}")

        # the package module
        if export_from_package:
            basename = os.path.basename(file)
            if basename == "__init__.py":
                dir_path = os.path.dirname(file)
                modules = all_sub_modules(dir_path)
                if modules:
                    if export_modules:
                        modules = set(modules) & set(export_modules)
                    lines.append(
                        """

# __init__.py structure:
# common code of the package
# export interface in __all__ which contains __all__ of its sub modules"""
                    )
                    for mod in modules:
                        lines.append(all_sub_all(mod))

        # write with __all__
        with open(file, mode="w") as fp:
            fp.writelines(lines)
Exemplo n.º 2
0
def gen_exports(dir_path='./domain', gen_flag='# the __all__ is generated', export_from_package=True):
    fill_package_if_not_exist(dir_path=dir_path)
    files = list_all_files(dir_path=dir_path)
    for file in files:
        exports = []
        lines = []
        # read and generate __all__
        with open(file) as fp:
            line = fp.readline()
            while line:
                if line.startswith(gen_flag):
                    break
                lines.append(line)
                export = _get_interface_name(line)
                if export and export[0].isalpha():
                    exports.append(export)
                line = fp.readline()
        print(f'{file}:{exports}')
        lines.append(gen_flag)
        lines.append(f'\n__all__ = {exports}')

        # the package module
        if export_from_package:
            basename = os.path.basename(file)
            if basename == '__init__.py':
                dir_path = os.path.dirname(file)
                modules = all_sub_modules(dir_path)
                if modules:
                    lines.append('''

# __init__.py structure:
# common code of the package
# export interface in __all__ which contains __all__ of its sub modules''')
                    for mod in modules:
                        lines.append(all_sub_all(mod))

        # write with __all__
        with open(file, mode='w') as fp:
            fp.writelines(lines)
Exemplo n.º 3
0
def all_tpls(project: str, entity_type: str):
    """
    return list of templates(location,Template)

    :param project:
    :return:
    """
    tpl_dir = os.path.join(os.path.dirname(__file__))
    tpl_files = list_all_files(tpl_dir, ext="template", return_base_name=True)
    tpls = []
    for tpl in tpl_files:
        data = resource_string(__name__, tpl)
        file_location = os.path.splitext(os.path.basename(tpl))[0]
        # we assure that line endings are converted to '\n' for all OS
        data = data.decode(encoding="utf-8").replace(os.linesep, "\n")

        # change path for specific file
        # domain
        if file_location == "kdata_common.py":
            file_location = f"{project}/domain/quotes/__init__.py"
        elif file_location == "meta.py":
            file_location = f"{project}/domain/{entity_type}_meta.py"
        # recorder
        elif file_location == "kdata_recorder.py":
            file_location = f"{project}/recorders/{entity_type}_kdata_recorder.py"
        elif file_location == "meta_recorder.py":
            file_location = f"{project}/recorders/{entity_type}_meta_recorder.py"
        # fill script
        elif file_location == "fill_project.py":
            file_location = f"{project}/fill_project.py"
        # tests
        elif file_location == "test_pass.py":
            file_location = f"tests/test_pass.py"
        elif file_location == "pkg_init.py":
            file_location = f"{project}/__init__.py"

        tpls.append((file_location, string.Template(data)))
    return tpls
Exemplo n.º 4
0
def gen_exports(
    dir_path="./domain",
    gen_flag="# the __all__ is generated",
    export_from_package=True,
    export_modules=None,
    excludes=None,
    export_var=False,
):
    if not excludes:
        excludes = ["logger"]
    if os.path.isfile(dir_path):
        files = [dir_path]
    else:
        fill_package_if_not_exist(dir_path=dir_path)
        files = list_all_files(dir_path=dir_path)
    for file in files:
        exports = []
        lines = []
        # read and generate __all__
        with open(file) as fp:
            line = fp.readline()
            while line:
                if line.startswith(gen_flag):
                    break
                lines.append(line)
                export = _get_interface_name(line)
                if export_var and not export:
                    export = _get_var_name(line)
                if export and export[0].isalpha() and export not in excludes:
                    exports.append(export)
                line = fp.readline()
        print(f"{file}:{exports}")
        lines.append(gen_flag)
        lines.append("\n")
        exports_str = f"__all__ = {exports}"
        exports_str = exports_str.replace("'", '"')
        if len(exports_str) > 120:
            exports_wrap = [f'\n    "{item}",' for item in exports]
            exports_str = "__all__ = [" + "".join(exports_wrap) + "\n]"
            exports_str = exports_str.replace("'", '"')
        lines.append(exports_str)
        lines.append("\n")

        # the package module
        if export_from_package:
            basename = os.path.basename(file)
            if basename == "__init__.py":
                dir_path = os.path.dirname(file)
                modules = all_sub_modules(dir_path)
                if modules:
                    if export_modules:
                        modules = set(modules) & set(export_modules)
                    lines.append(
                        """
# __init__.py structure:
# common code of the package
# export interface in __all__ which contains __all__ of its sub modules"""
                    )
                    for mod in modules:
                        lines.append(all_sub_all(mod))
                    lines.append("\n")

        # write with __all__
        with open(file, mode="w") as fp:
            fp.writelines(lines)