adapt to multiple output files per input file in build info

This commit is contained in:
Maximilian Keßler 2022-01-12 22:30:10 +01:00
parent 1df567da82
commit 026e3a6cb3
7 changed files with 74 additions and 31 deletions

View file

@ -27,6 +27,7 @@ def build(
overwrite_existing_files: bool = False, # output control overwrite_existing_files: bool = False, # output control
build_all: bool = False, # output control / versioning build_all: bool = False, # output control / versioning
write_build_information: bool = True, # meta write_build_information: bool = True, # meta
clean_old_files: bool = False
): ):
pytex_msg('Getting git repository information...') pytex_msg('Getting git repository information...')
if extra_header: if extra_header:
@ -90,10 +91,10 @@ def build(
sources_to_build = [] sources_to_build = []
for file in files: for file in files:
if last_build_info: if last_build_info:
last_build_info_for_this_file = next( last_build_info_for_this_file =\
(info for info in last_build_info['tex_sources'] if info['source file'] == file.name), {}) list(filter(lambda i: i['source file'] == file.name, last_build_info['tex_sources']))
else: else:
last_build_info_for_this_file = None last_build_info_for_this_file = []
sources_to_build.append( sources_to_build.append(
TexFileToFormat( TexFileToFormat(
src_path=file, src_path=file,
@ -124,7 +125,8 @@ def build(
} }
for source in sources_to_build: for source in sources_to_build:
info = source.format() infos = source.format()
for info in infos:
info_dict['tex_sources'].append(info) info_dict['tex_sources'].append(info)
if write_build_information: if write_build_information:

View file

@ -105,6 +105,13 @@ def parse_and_build(arglist: [str]):
type=pathlib.Path, type=pathlib.Path,
dest='extra_header' dest='extra_header'
) )
parser.add_argument(
'-c', '--clean-old-files',
help='Cleans old files present in build order that are not present in the sources anymore. '
'Setting this option guarantees that the build directory will be equivalent as if a '
'clean new build has been made (Build metadata might differ).',
action='store_true'
)
args = vars(parser.parse_args(arglist)) args = vars(parser.parse_args(arglist))
for arg in args.keys(): for arg in args.keys():
if type(args[arg]) == pathlib.PosixPath: if type(args[arg]) == pathlib.PosixPath:

View file

@ -16,7 +16,7 @@ class TexFileToFormat:
build_dir: Path, build_dir: Path,
latex_name: str, latex_name: str,
current_build_info: BuildInfo, current_build_info: BuildInfo,
last_build_info: Optional[dict], last_build_info: Optional[List[dict]],
allow_dirty: bool = False, allow_dirty: bool = False,
overwrite_existing_files: bool = False, overwrite_existing_files: bool = False,
build_all: bool = False): build_all: bool = False):
@ -24,7 +24,8 @@ class TexFileToFormat:
self.build_path = build_dir self.build_path = build_dir
self.latex_name = latex_name # Still an identifier on how to name the package when being formatted self.latex_name = latex_name # Still an identifier on how to name the package when being formatted
self.current_build_info = current_build_info self.current_build_info = current_build_info
self.last_build_info = last_build_info self.last_build_info_all = last_build_info
self.last_build_info = self.last_build_info_all[0] if self.last_build_info_all else None
self.allow_dirty = allow_dirty self.allow_dirty = allow_dirty
self.overwrite_existing_files: overwrite_existing_files self.overwrite_existing_files: overwrite_existing_files
self.build_all = build_all self.build_all = build_all
@ -49,7 +50,7 @@ class TexFileToFormat:
self.recent = False self.recent = False
self.pytex_recent = False self.pytex_recent = False
def format(self) -> dict: def format(self) -> List[dict]:
if self.dirty or self.pytex_dirty: if self.dirty or self.pytex_dirty:
if not self.allow_dirty: if not self.allow_dirty:
raise SubmoduleDirtyForbiddenError raise SubmoduleDirtyForbiddenError
@ -62,7 +63,7 @@ class TexFileToFormat:
elif self.last_build_info and self.last_build_info['dirty']: 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 return self.__format() # Build file since we do not know in what state it is
else: else:
return self.last_build_info return [self.last_build_info]
def __format_header(self): def __format_header(self):
new_header = [] new_header = []
@ -82,7 +83,7 @@ class TexFileToFormat:
)) ))
self._header = new_header self._header = new_header
def __format(self) -> dict: def __format(self) -> List[dict]:
if self.src_path.name.endswith('.pysty'): if self.src_path.name.endswith('.pysty'):
formatter = PackageFormatter( formatter = PackageFormatter(
package_name=self.src_path.with_suffix('').name, package_name=self.src_path.with_suffix('').name,
@ -123,11 +124,12 @@ class TexFileToFormat:
) )
else: else:
raise ProgrammingError raise ProgrammingError
pytex_msg('Writing file {}'.format(formatter.file_name))
formatter.make_default_macros() formatter.make_default_macros()
formatter.format_file(self.src_path, self.build_path) written_files = formatter.format_file(self.src_path, self.build_path)
build_infos = []
for written_file in written_files:
info = { info = {
'name': formatter.file_name, 'name': written_file,
'source file': self.src_path.name, 'source file': self.src_path.name,
'build time': self.current_build_info.build_time, 'build time': self.current_build_info.build_time,
'source version': self.current_build_info.packages_version, 'source version': self.current_build_info.packages_version,
@ -136,4 +138,6 @@ class TexFileToFormat:
'pytex commit hash': self.current_build_info.pytex_hash, 'pytex commit hash': self.current_build_info.pytex_hash,
'dirty': self.dirty 'dirty': self.dirty
} }
return info build_infos.append(info)
pytex_msg('Written file {}'.format(written_file))
return build_infos

View file

@ -4,8 +4,10 @@ from typing import Dict, Optional, List
from datetime import * from datetime import *
import csv import csv
from PyTeX.formatter import Formatter
class DictionaryFormatter:
class DictionaryFormatter(Formatter):
def __init__(self, kind: str, author: str, header: Optional[List[str]]): def __init__(self, kind: str, author: str, header: Optional[List[str]]):
self.header = header self.header = header
self.kind = kind.lower() self.kind = kind.lower()
@ -22,13 +24,14 @@ class DictionaryFormatter:
self.replace_dict: Dict = {} self.replace_dict: Dict = {}
self.arg_replace_dict: Dict = {} self.arg_replace_dict: Dict = {}
self.source_file_name = "not specified" self.source_file_name = "not specified"
super().__init__()
@property def expected_file_name(self):
def filename(self):
return self.file_name return self.file_name
def format_file(self, input_path: Path, output_dir: Path = None): def format_file(self, input_path: Path, output_dir: Path = None) -> List[str]:
self.source_file_name = str(input_path.name) self.source_file_name = str(input_path.name)
written_files = []
if self.header: if self.header:
lines = '%' * 80 + '\n' \ lines = '%' * 80 + '\n' \
@ -67,6 +70,8 @@ class DictionaryFormatter:
lang_lines += '\n' lang_lines += '\n'
(output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines)) (output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines))
written_files.append(self.file_name.format(language=lang))
return written_files
def make_default_macros(self): def make_default_macros(self):
pass pass

View file

@ -1,5 +1,6 @@
from .tex_formatter import TexFormatter from .tex_formatter import TexFormatter, Formatter
__all__ = [ __all__ = [
'TexFormatter' 'TexFormatter',
'Formatter'
] ]

17
formatter/formatter.py Normal file
View file

@ -0,0 +1,17 @@
from pathlib import Path
from typing import List
class Formatter:
def __init__(self, *args, **kwargs):
""" Implementation unknown, this is an Interface"""
pass
def make_default_macros(self) -> None:
pass
def format_file(self, input_path: Path, output_dir: Path) -> List[str]:
pass
def expected_file_name(self) -> str:
pass

View file

@ -7,8 +7,10 @@ from datetime import *
from PyTeX.base import Attributes, Args from PyTeX.base import Attributes, Args
from PyTeX.errors import UnknownTexVersionError from PyTeX.errors import UnknownTexVersionError
from .formatter import Formatter
class TexFormatter:
class TexFormatter(Formatter):
def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str, def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str,
tex_version: str, version: str, latex_name: str): tex_version: str, version: str, latex_name: str):
@ -36,11 +38,15 @@ class TexFormatter:
self.prefix = '__' + self.name_lowercase.replace('-', '_') + '_' self.prefix = '__' + self.name_lowercase.replace('-', '_') + '_'
else: else:
raise UnknownTexVersionError(tex_version) raise UnknownTexVersionError(tex_version)
super().__init__()
@staticmethod @staticmethod
def __command_name2keyword(keyword: str): def __command_name2keyword(keyword: str):
return '__' + keyword.upper().strip().replace(' ', '_') + '__' return '__' + keyword.upper().strip().replace(' ', '_') + '__'
def expected_file_name(self):
return self.file_name
@property @property
def filename(self): def filename(self):
return self.file_name return self.file_name
@ -106,7 +112,7 @@ class TexFormatter:
'format_kwargs': kwargs 'format_kwargs': kwargs
} }
def format_file(self, input_path: Path, output_dir: Path = None): def format_file(self, input_path: Path, output_dir: Path = None) -> List[str]:
self.source_file_name = str(input_path.name) self.source_file_name = str(input_path.name)
input_file = input_path.open() input_file = input_path.open()
lines = input_file.readlines() lines = input_file.readlines()
@ -122,3 +128,4 @@ class TexFormatter:
output_dir = input_path.parent output_dir = input_path.parent
output_dir.mkdir(parents=True, exist_ok=True) output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / self.file_name).write_text(''.join(newlines)) (output_dir / self.file_name).write_text(''.join(newlines))
return [str(self.file_name)]