From 4c3867e0f4e1b2c3bc55688868beb3b7ccad5451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 22 Oct 2021 14:45:32 +0200 Subject: [PATCH] refactor files into better module structure --- base/__init__.py | 2 - build/__init__.py | 5 ++ build/{build => }/build.py | 96 +-------------------- build/build/__init__.py | 5 -- build/build/config.py | 10 --- build/utils/__init__.py | 9 ++ build/{build => utils}/build_information.py | 16 ---- build/utils/pytex_file.py | 93 ++++++++++++++++++++ build/utils/pytex_msg.py | 2 + config/__init__.py | 13 +++ config/constants.py | 4 + base/config.py => config/header_parts.py | 9 +- default_formatters/class_formatter.py | 2 +- default_formatters/package_formatter.py | 2 +- macros/default_macros.py | 5 +- 15 files changed, 140 insertions(+), 133 deletions(-) rename build/{build => }/build.py (50%) delete mode 100644 build/build/__init__.py delete mode 100644 build/build/config.py rename build/{build => utils}/build_information.py (80%) create mode 100644 build/utils/pytex_file.py create mode 100644 build/utils/pytex_msg.py create mode 100644 config/__init__.py create mode 100644 config/constants.py rename base/config.py => config/header_parts.py (90%) diff --git a/base/__init__.py b/base/__init__.py index 299428e..d956802 100644 --- a/base/__init__.py +++ b/base/__init__.py @@ -1,9 +1,7 @@ -from .config import LICENSE, DEFAULT_AUTHOR, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT from .enums import Attributes, Args __all__ = [ 'LICENSE', - 'DEFAULT_AUTHOR', 'PACKAGE_INFO_TEXT', 'PYTEX_INFO_TEXT', 'Args', diff --git a/build/__init__.py b/build/__init__.py index e69de29..3b3e07b 100644 --- a/build/__init__.py +++ b/build/__init__.py @@ -0,0 +1,5 @@ +from .build import build + +__all__ = [ + 'build' +] diff --git a/build/build/build.py b/build/build.py similarity index 50% rename from build/build/build.py rename to build/build.py index 4544a56..99e3f99 100644 --- a/build/build/build.py +++ b/build/build.py @@ -4,101 +4,9 @@ from typing import Optional import git -import PyTeX +from PyTeX.config.constants import BUILD_INFO_FILENAME -from .build_information import BuildInfo - -from PyTeX.build.git_hook.recent import is_recent -from PyTeX.build.git_hook.git_version import get_latest_commit - -BUILD_INFO_FILENAME = 'build_info.json' - - -def pytex_msg(msg: str): - print('[PyTeX] ' + msg) - - -class TexFileToFormat: - def __init__( - self, - src_path: Path, - build_dir: Path, - latex_name: str, - current_build_info: BuildInfo, - last_build_info: Optional[dict], - allow_dirty: bool = False, - overwrite_existing_files: bool = False, - build_all: bool = False): - self.src_path = src_path - self.build_path = build_dir - self.tex_name = latex_name # Still an identifier on how to name the package when being formatted - self.current_build_info = current_build_info - self.last_build_info = last_build_info - self.allow_dirty = allow_dirty - self.overwrite_existing_files: overwrite_existing_files - self.build_all = build_all - - self.dirty = not is_recent(self.src_path, self.current_build_info.package_repo, compare=None) - self.pytex_dirty: bool = self.current_build_info.pytex_repo.is_dirty( - working_tree=True, - untracked_files=True - ) - if self.last_build_info: - self.recent: bool = is_recent( - file=self.src_path, - repo=self.current_build_info.package_repo, - compare=self.current_build_info.package_repo.commit(self.last_build_info['source commit hash']) - ) - self.pytex_recent: bool = get_latest_commit( - self.current_build_info.pytex_repo - ).hexsha == self.last_build_info['pytex commit hash'] - else: - self.recent = False - self.pytex_recent = False - - def format(self) -> dict: - if self.dirty or self.pytex_dirty: - if not self.allow_dirty: - raise Exception( - '{file} is dirty, but writing dirty files not allowed.'.format( - file=self.src_path.name if self.dirty else 'Submodule PyTeX') - ) - # TODO: add this to the header...? - return self.__format() # Dirty files are always built, since we have no information about them - elif self.build_all: - return self.__format() # Build file since we build all of them - elif not self.pytex_recent or not self.recent: - return self.__format() # Build file since either pytex or package repo is not recent - elif self.last_build_info and self.last_build_info['dirty']: - return self.__format() # Build file since we do not know in what state it is - else: - return self.last_build_info - - def __format(self) -> dict: - if '.pysty' in self.src_path.name: - formatter = PyTeX.PackageFormatter( - package_name=self.src_path.with_suffix('').name, - extra_header=self.current_build_info.header) - elif '.pycls' in self.src_path.name: - formatter = PyTeX.ClassFormatter( - class_name=self.src_path.with_suffix('').name, - extra_header=self.current_build_info.header) - else: - exit(1) - pytex_msg('Writing file {}'.format(formatter.file_name)) - formatter.make_default_macros() - formatter.format_file(self.src_path, self.build_path) - info = { - 'name': formatter.file_name, - 'source file': self.src_path.name, - 'build time': self.current_build_info.build_time, - 'source version': self.current_build_info.packages_version, - 'source commit hash': self.current_build_info.packages_hash, - 'pytex version': self.current_build_info.pytex_version, - 'pytex commit hash': self.current_build_info.pytex_hash, - 'dirty': self.dirty - } - return info +from .utils import BuildInfo, pytex_msg, TexFileToFormat def build( diff --git a/build/build/__init__.py b/build/build/__init__.py deleted file mode 100644 index 3b3e07b..0000000 --- a/build/build/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .build import build - -__all__ = [ - 'build' -] diff --git a/build/build/config.py b/build/build/config.py deleted file mode 100644 index a6d8ad0..0000000 --- a/build/build/config.py +++ /dev/null @@ -1,10 +0,0 @@ -BUILD_DETAILS = [ - "Build details:", - " Build time: {build_time}", - " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", - " LatexPackages version: {packages_version} (commit {packages_commit_hash})" -] - -FILENAME_TYPE_PREPEND_AUTHOR = 'prepend-author' -FILENAME_TYPE_RAW_NAME = 'raw' -DATE_FORMAT = '%Y/%m/%d %H:%M' diff --git a/build/utils/__init__.py b/build/utils/__init__.py index e69de29..a29fb42 100644 --- a/build/utils/__init__.py +++ b/build/utils/__init__.py @@ -0,0 +1,9 @@ +from .build_information import BuildInfo +from .pytex_file import TexFileToFormat +from .pytex_msg import pytex_msg + +__all__ = [ + 'BuildInfo', + 'TexFileToFormat', + 'pytex_msg' +] diff --git a/build/build/build_information.py b/build/utils/build_information.py similarity index 80% rename from build/build/build_information.py rename to build/utils/build_information.py index 65b9012..1f9bafc 100644 --- a/build/build/build_information.py +++ b/build/utils/build_information.py @@ -4,22 +4,6 @@ from typing import Optional from PyTeX.build.git_hook import git_describe, get_latest_commit -from .config import BUILD_DETAILS - - -def build_information(): - repo = git.Repo() - repo_description = git_describe(get_latest_commit(repo)) - pytex_repo = repo.submodule('PyTeX').module() - pytex_repo_description = git_describe(get_latest_commit(pytex_repo)) - return list(map(lambda line: line.format( - build_time=datetime.datetime.now().strftime('%Y/%m/%d %H:%M'), - pytex_version=pytex_repo_description, - pytex_commit_hash=get_latest_commit(pytex_repo).hexsha[0:7], - packages_version=repo_description, - packages_commit_hash=get_latest_commit(repo).hexsha[0:7] - ), BUILD_DETAILS)), repo_description - class BuildInfo: def __init__( diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py new file mode 100644 index 0000000..972bf8e --- /dev/null +++ b/build/utils/pytex_file.py @@ -0,0 +1,93 @@ +from pathlib import Path +from typing import Optional + +from PyTeX.build.git_hook import is_recent, get_latest_commit +from PyTeX import PackageFormatter, ClassFormatter +from .pytex_msg import pytex_msg + +from .build_information import BuildInfo + + +class TexFileToFormat: + def __init__( + self, + src_path: Path, + build_dir: Path, + latex_name: str, + current_build_info: BuildInfo, + last_build_info: Optional[dict], + allow_dirty: bool = False, + overwrite_existing_files: bool = False, + build_all: bool = False): + self.src_path = src_path + self.build_path = build_dir + self.tex_name = latex_name # Still an identifier on how to name the package when being formatted + self.current_build_info = current_build_info + self.last_build_info = last_build_info + self.allow_dirty = allow_dirty + self.overwrite_existing_files: overwrite_existing_files + self.build_all = build_all + + self.dirty = not is_recent(self.src_path, self.current_build_info.package_repo, compare=None) + self.pytex_dirty: bool = self.current_build_info.pytex_repo.is_dirty( + working_tree=True, + untracked_files=True + ) + if self.last_build_info: + self.recent: bool = is_recent( + file=self.src_path, + repo=self.current_build_info.package_repo, + compare=self.current_build_info.package_repo.commit(self.last_build_info['source commit hash']) + ) + self.pytex_recent: bool = get_latest_commit( + self.current_build_info.pytex_repo + ).hexsha == self.last_build_info['pytex commit hash'] + else: + self.recent = False + self.pytex_recent = False + + def format(self) -> dict: + if self.dirty or self.pytex_dirty: + if not self.allow_dirty: + raise Exception( + '{file} is dirty, but writing dirty files not allowed.'.format( + file=self.src_path.name if self.dirty else 'Submodule PyTeX') + ) + # TODO: add this to the header...? + return self.__format() # Dirty files are always built, since we have no information about them + elif self.build_all: + return self.__format() # Build file since we build all of them + elif not self.pytex_recent or not self.recent: + return self.__format() # Build file since either pytex or package repo is not recent + elif self.last_build_info and self.last_build_info['dirty']: + return self.__format() # Build file since we do not know in what state it is + else: + return self.last_build_info + + def __format(self) -> dict: + if '.pysty' in self.src_path.name: + formatter = PackageFormatter( + package_name=self.src_path.with_suffix('').name, + author=self.current_build_info.author, + extra_header=self.current_build_info.header) + elif '.pycls' in self.src_path.name: + formatter = ClassFormatter( + class_name=self.src_path.with_suffix('').name, + author=self.current_build_info.author, + extra_header=self.current_build_info.header) + else: + exit(1) + pytex_msg('Writing file {}'.format(formatter.file_name)) + formatter.make_default_macros() + formatter.format_file(self.src_path, self.build_path) + info = { + 'name': formatter.file_name, + 'source file': self.src_path.name, + 'build time': self.current_build_info.build_time, + 'source version': self.current_build_info.packages_version, + 'source commit hash': self.current_build_info.packages_hash, + 'pytex version': self.current_build_info.pytex_version, + 'pytex commit hash': self.current_build_info.pytex_hash, + 'dirty': self.dirty + } + return info diff --git a/build/utils/pytex_msg.py b/build/utils/pytex_msg.py new file mode 100644 index 0000000..4905001 --- /dev/null +++ b/build/utils/pytex_msg.py @@ -0,0 +1,2 @@ +def pytex_msg(msg: str): + print('[PyTeX] ' + msg) diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..0f7abce --- /dev/null +++ b/config/__init__.py @@ -0,0 +1,13 @@ +from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME +from .header_parts import LICENSE, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT, BUILD_DETAILS + +__all__ = [ + 'FILENAME_TYPE_PREPEND_AUTHOR', + 'FILENAME_TYPE_RAW_NAME', + 'DATE_FORMAT', + 'BUILD_INFO_FILENAME', + 'LICENSE', + 'PACKAGE_INFO_TEXT', + 'PYTEX_INFO_TEXT', + 'BUILD_DETAILS' +] diff --git a/config/constants.py b/config/constants.py new file mode 100644 index 0000000..a2d2e38 --- /dev/null +++ b/config/constants.py @@ -0,0 +1,4 @@ +FILENAME_TYPE_PREPEND_AUTHOR = 'prepend-author' +FILENAME_TYPE_RAW_NAME = 'raw' +DATE_FORMAT = '%Y/%m/%d %H:%M' +BUILD_INFO_FILENAME = 'build_info.json' diff --git a/base/config.py b/config/header_parts.py similarity index 90% rename from base/config.py rename to config/header_parts.py index 281db91..2c2dc8f 100644 --- a/base/config.py +++ b/config/header_parts.py @@ -1,5 +1,3 @@ -DEFAULT_AUTHOR = 'Maximilian Keßler' - LICENSE = [ 'Copyright © {year} {copyright_holders}', '', @@ -39,3 +37,10 @@ PYTEX_INFO_TEXT = [ "changes will not be versioned by Git and overwritten by the next build. Always", "edit the source file and build the {latex_file_type} again." ] + +BUILD_DETAILS = [ + "Build details:", + " Build time: {build_time}", + " PyTeX version: {pytex_version} (commit {pytex_commit_hash})", + " LatexPackages version: {packages_version} (commit {packages_commit_hash})" +] diff --git a/default_formatters/class_formatter.py b/default_formatters/class_formatter.py index 9a1b77b..c93a8a4 100644 --- a/default_formatters/class_formatter.py +++ b/default_formatters/class_formatter.py @@ -4,7 +4,7 @@ import PyTeX.macros class ClassFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, class_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []): + def __init__(self, class_name: str, author: str, extra_header: [str] = []): PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls') def make_default_macros(self): diff --git a/default_formatters/package_formatter.py b/default_formatters/package_formatter.py index 873ef40..774e4ab 100644 --- a/default_formatters/package_formatter.py +++ b/default_formatters/package_formatter.py @@ -4,7 +4,7 @@ import PyTeX.macros class PackageFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, package_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []): + def __init__(self, package_name: str, author: str, extra_header: [str] = []): PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty') def make_default_macros(self): diff --git a/macros/default_macros.py b/macros/default_macros.py index 8dc89ba..d0074df 100644 --- a/macros/default_macros.py +++ b/macros/default_macros.py @@ -1,12 +1,13 @@ import PyTeX.formatter import PyTeX.base +import PyTeX.config def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str): header = '%' * 80 + '\n' \ + '\n'.join(map(lambda line: '% ' + line, - PyTeX.base.LICENSE + [''] + PyTeX.base.PACKAGE_INFO_TEXT + [ - ''] + PyTeX.base.PYTEX_INFO_TEXT + PyTeX.config.LICENSE + [''] + PyTeX.config.PACKAGE_INFO_TEXT + [ + ''] + PyTeX.config.PYTEX_INFO_TEXT + [''] + formatter.extra_header) ) \ + '\n' + '%' * 80 + '\n\n' \