Compare commits
29 commits
Author | SHA1 | Date | |
---|---|---|---|
60488127e5 | |||
2cad289b32 | |||
f5a5ad6328 | |||
bfd0f6e01b | |||
f8a6f83de9 | |||
785eb6923f | |||
4e7ea6fd14 | |||
026e3a6cb3 | |||
1df567da82 | |||
4a0e095899 | |||
d1806d970f | |||
75a2b137ec | |||
c8deda5af8 | |||
23766e8dcd | |||
494c43e52b | |||
5e077e77d8 | |||
99c984049c | |||
9bf3daa352 | |||
e55a6728a9 | |||
4c5efa4087 | |||
315e3647ab | |||
357144e9f0 | |||
c98fd28245 | |||
126d420b7a | |||
12a03e7989 | |||
4c3867e0f4 | |||
ba0b49dbfc | |||
![]() |
b1d429e9b0 | ||
![]() |
1da78575c2 |
29 changed files with 896 additions and 329 deletions
__init__.py
base
build
config
default_formatters
errors
formatter
macros
utils
|
@ -1,6 +1,7 @@
|
|||
from PyTeX.default_formatters import ClassFormatter, PackageFormatter
|
||||
from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter
|
||||
|
||||
__all__ = [
|
||||
"ClassFormatter",
|
||||
"PackageFormatter"
|
||||
"PackageFormatter",
|
||||
"DictionaryFormatter"
|
||||
]
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -11,6 +11,7 @@ class Attributes(Enum):
|
|||
date = 'date'
|
||||
year = 'year'
|
||||
source_file_name = 'source_file_name'
|
||||
version = 'version'
|
||||
|
||||
|
||||
class Args(Enum):
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
from .build import build
|
||||
from .build_parser import parse_and_build
|
||||
|
||||
__all__ = [
|
||||
'build',
|
||||
'parse_and_build'
|
||||
]
|
152
build/build.py
Normal file
152
build/build.py
Normal file
|
@ -0,0 +1,152 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import git
|
||||
|
||||
from PyTeX.config.constants import BUILD_INFO_FILENAME
|
||||
from PyTeX.errors import *
|
||||
|
||||
from .utils import BuildInfo, pytex_msg, TexFileToFormat
|
||||
|
||||
|
||||
def build(
|
||||
src_dir: Optional[Path] = None,
|
||||
build_dir: Optional[Path] = None,
|
||||
input_file: Optional[Path] = None,
|
||||
author: Optional[str] = None,
|
||||
latex_name: str = 'prepend-author', # name handling
|
||||
recursive: bool = False, # input control
|
||||
include_timestamp: bool = False, # header
|
||||
include_pytex_version: bool = False, # header
|
||||
include_license: bool = False, # header
|
||||
include_git_version: bool = False, # header
|
||||
include_pytex_info_text: bool = False, # header
|
||||
extra_header: Optional[Path] = None,
|
||||
allow_dirty: bool = False, # versioning
|
||||
overwrite_existing_files: bool = False, # output control
|
||||
build_all: bool = False, # output control / versioning
|
||||
write_build_information: bool = True, # meta
|
||||
clean_old_files: bool = False
|
||||
):
|
||||
pytex_msg('Getting git repository information...')
|
||||
if extra_header:
|
||||
if extra_header.exists():
|
||||
with open(extra_header, 'r') as f:
|
||||
text = f.readlines()
|
||||
extra_header = [line.rstrip() for line in text]
|
||||
else:
|
||||
raise ExtraHeaderFileNotFoundError
|
||||
current_build_info = BuildInfo(
|
||||
include_timestamp=include_timestamp,
|
||||
include_pytex_version=include_pytex_version,
|
||||
include_license=include_license,
|
||||
include_git_version=include_git_version,
|
||||
include_pytex_info_text=include_pytex_info_text,
|
||||
extra_header=extra_header,
|
||||
author=author,
|
||||
pytex_repo=git.Repo(__file__, search_parent_directories=True),
|
||||
packages_repo=git.Repo(src_dir, search_parent_directories=True)
|
||||
)
|
||||
print(r'[PyTeX] This is PyTex version {pytex_version}'.format(
|
||||
pytex_version=current_build_info.pytex_version
|
||||
))
|
||||
input_dir = src_dir if src_dir else input_file.parent
|
||||
output_dir = build_dir if build_dir else input_file.parent
|
||||
|
||||
last_build_info_file = output_dir / BUILD_INFO_FILENAME
|
||||
if last_build_info_file.exists():
|
||||
with open(output_dir / 'build_info.json', 'r') as f:
|
||||
last_build_info = json.load(f)
|
||||
else:
|
||||
last_build_info = None
|
||||
|
||||
files = []
|
||||
if input_file:
|
||||
files.append(input_file)
|
||||
if src_dir:
|
||||
if recursive:
|
||||
for file in src_dir.rglob('*.pysty'):
|
||||
files.append(file)
|
||||
for file in src_dir.rglob('*.pycls'):
|
||||
files.append(file)
|
||||
for file in src_dir.rglob('*.pydict'):
|
||||
files.append(file)
|
||||
for file in src_dir.rglob('*.pysty3'):
|
||||
files.append(file)
|
||||
for file in src_dir.rglob('*.pycls3'):
|
||||
files.append(file)
|
||||
else:
|
||||
for file in src_dir.glob('*.pysty'):
|
||||
files.append(file)
|
||||
for file in src_dir.glob('*.pycls'):
|
||||
files.append(file)
|
||||
for file in src_dir.glob('*.pydict'):
|
||||
files.append(file)
|
||||
for file in src_dir.glob('*.pysty3'):
|
||||
files.append(file)
|
||||
for file in src_dir.glob('*.pycls3'):
|
||||
files.append(file)
|
||||
|
||||
sources_to_build = []
|
||||
for file in files:
|
||||
if last_build_info:
|
||||
last_build_info_for_this_file =\
|
||||
list(filter(lambda i: i['source file'] == str(file.relative_to(src_dir)), last_build_info['tex_sources']))
|
||||
else:
|
||||
last_build_info_for_this_file = []
|
||||
sources_to_build.append(
|
||||
TexFileToFormat(
|
||||
src_path=file,
|
||||
build_root=output_dir,
|
||||
src_root=src_dir,
|
||||
latex_name=latex_name,
|
||||
current_build_info=current_build_info,
|
||||
last_build_info=last_build_info_for_this_file,
|
||||
allow_dirty=allow_dirty,
|
||||
overwrite_existing_files=overwrite_existing_files,
|
||||
build_all=build_all
|
||||
))
|
||||
|
||||
info_dict = {
|
||||
'build_time': '',
|
||||
'source files': {
|
||||
'version': current_build_info.packages_version,
|
||||
'commit': current_build_info.packages_hash,
|
||||
'dirty': current_build_info.package_repo.is_dirty(untracked_files=True)
|
||||
},
|
||||
'pytex': {
|
||||
'version': current_build_info.pytex_version,
|
||||
'commit': current_build_info.pytex_hash,
|
||||
'dirty': current_build_info.pytex_repo.is_dirty(untracked_files=True)
|
||||
},
|
||||
'tex_sources': [
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
for source in sources_to_build:
|
||||
infos = source.format()
|
||||
for info in infos:
|
||||
info_dict['tex_sources'].append(info)
|
||||
|
||||
built_files = [info['name'] for info in info_dict['tex_sources']]
|
||||
if last_build_info:
|
||||
lastly_built_files = [info['name'] for info in last_build_info['tex_sources']]
|
||||
else:
|
||||
lastly_built_files = []
|
||||
if clean_old_files:
|
||||
for file in output_dir.rglob('*'):
|
||||
if str(file.relative_to(output_dir)) in lastly_built_files:
|
||||
if str(file.relative_to(output_dir)) not in built_files:
|
||||
print(f'[PyTeX] Removing old built file {str(file.relative_to(output_dir))}')
|
||||
file.unlink()
|
||||
elif not str(file.relative_to(output_dir)) in built_files:
|
||||
if not file.is_dir() and not str(file.relative_to(output_dir)) == 'build_info.json':
|
||||
# PyTeX does not at all know something about this file
|
||||
raise UnknownFileInBuildDirectory(file.relative_to(output_dir))
|
||||
|
||||
if write_build_information:
|
||||
with open(output_dir / 'build_info.json', 'w') as f:
|
||||
json.dump(info_dict, f, indent=4)
|
||||
pytex_msg('Build done')
|
|
@ -1,5 +0,0 @@
|
|||
from .build import build
|
||||
|
||||
__all__ = [
|
||||
'build'
|
||||
]
|
|
@ -1,201 +0,0 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import git
|
||||
|
||||
import PyTeX
|
||||
|
||||
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
|
||||
|
||||
|
||||
def build(
|
||||
src_dir: Optional[Path] = None,
|
||||
build_dir: Optional[Path] = None,
|
||||
input_file: Optional[Path] = None,
|
||||
author: Optional[str] = None,
|
||||
latex_name: str = 'prepend-author', # name handling
|
||||
recursive: bool = False, # input control
|
||||
include_timestamp: bool = False, # header
|
||||
include_pytex_version: bool = False, # header
|
||||
include_license: bool = False, # header
|
||||
include_git_version: bool = False, # header
|
||||
include_pytex_info_text: bool = False, # header
|
||||
use_git: bool = False, # versioning (not implemented yet)
|
||||
allow_dirty: bool = False, # versioning
|
||||
overwrite_existing_files: bool = False, # output control
|
||||
build_all: bool = False, # output control / versioning
|
||||
write_build_information: bool = True, # meta
|
||||
):
|
||||
pytex_msg('Getting git repository information...')
|
||||
current_build_info = BuildInfo(
|
||||
include_timestamp=include_timestamp,
|
||||
include_pytex_version=include_pytex_version,
|
||||
include_license=include_license,
|
||||
include_git_version=include_git_version,
|
||||
include_pytex_info_text=include_pytex_info_text,
|
||||
author=author,
|
||||
pytex_repo=git.Repo(__file__, search_parent_directories=True),
|
||||
packages_repo=git.Repo(src_dir, search_parent_directories=True)
|
||||
)
|
||||
input_dir = src_dir if src_dir else input_file.parent
|
||||
output_dir = build_dir if build_dir else input_file.parent
|
||||
|
||||
last_build_info_file = output_dir / BUILD_INFO_FILENAME
|
||||
if last_build_info_file.exists():
|
||||
with open(output_dir / 'build_info.json', 'r') as f:
|
||||
last_build_info = json.load(f)
|
||||
else:
|
||||
last_build_info = None
|
||||
|
||||
files = []
|
||||
if input_file:
|
||||
files.append(input_file)
|
||||
if src_dir:
|
||||
if recursive:
|
||||
for file in src_dir.rglob('*.pysty'):
|
||||
files.append(file)
|
||||
for file in src_dir.rglob('*.pycls'):
|
||||
files.append(file)
|
||||
else:
|
||||
for file in src_dir.glob('*.pysty'):
|
||||
files.append(file)
|
||||
for file in src_dir.glob('*.pycls'):
|
||||
files.append(file)
|
||||
|
||||
sources_to_build = []
|
||||
for file in files:
|
||||
if last_build_info:
|
||||
last_build_info_for_this_file = next(
|
||||
(info for info in last_build_info['tex_sources'] if info['source file'] == file.name), {})
|
||||
else:
|
||||
last_build_info_for_this_file = None
|
||||
sources_to_build.append(
|
||||
TexFileToFormat(
|
||||
src_path=file,
|
||||
build_dir=output_dir / file.parent.relative_to(input_dir),
|
||||
latex_name=latex_name,
|
||||
current_build_info=current_build_info,
|
||||
last_build_info=last_build_info_for_this_file,
|
||||
allow_dirty=allow_dirty,
|
||||
overwrite_existing_files=overwrite_existing_files,
|
||||
build_all=build_all
|
||||
))
|
||||
|
||||
info_dict = {
|
||||
'build_time': '',
|
||||
'source files': {
|
||||
'version': current_build_info.packages_version,
|
||||
'commit': current_build_info.packages_hash,
|
||||
'dirty': current_build_info.package_repo.is_dirty(untracked_files=True)
|
||||
},
|
||||
'pytex': {
|
||||
'version': current_build_info.pytex_version,
|
||||
'commit': current_build_info.pytex_hash,
|
||||
'dirty': current_build_info.pytex_repo.is_dirty(untracked_files=True)
|
||||
},
|
||||
'tex_sources': [
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
for source in sources_to_build:
|
||||
info = source.format()
|
||||
info_dict['tex_sources'].append(info)
|
||||
|
||||
if write_build_information:
|
||||
with open(output_dir / 'build_info.json', 'w') as f:
|
||||
json.dump(info_dict, f, indent=4)
|
||||
pytex_msg('Build done')
|
|
@ -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'
|
123
build/build_parser.py
Normal file
123
build/build_parser.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
import argparse
|
||||
import pathlib
|
||||
|
||||
from PyTeX.config import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME
|
||||
from PyTeX.errors import PyTexError
|
||||
|
||||
from .build import build
|
||||
|
||||
|
||||
def parse_and_build(arglist: [str]):
|
||||
parser = argparse.ArgumentParser(description='Incrementally build LatexPackages with PyTeX')
|
||||
input_group = parser.add_mutually_exclusive_group(required=True)
|
||||
input_group.add_argument(
|
||||
'-s', '--source-dir',
|
||||
metavar='SRC_DIR',
|
||||
help='Relative or absolute path to source directory of .pysty or .pycls files',
|
||||
type=pathlib.Path,
|
||||
nargs='?',
|
||||
default='./src',
|
||||
dest='src_dir'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-b', '--build-dir',
|
||||
metavar='BUILD_DIR',
|
||||
help='Relativ or absolute path to output directory for processed packages and classes',
|
||||
type=pathlib.Path,
|
||||
nargs='?',
|
||||
default='./build',
|
||||
dest='build_dir'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-r', '--recursive',
|
||||
help='Recursively search subdirectories for files. Default: false',
|
||||
action='store_true',
|
||||
dest='recursive'
|
||||
)
|
||||
input_group.add_argument(
|
||||
'-i', '--input-file',
|
||||
metavar='FILE',
|
||||
help='Filename to be built. Can be in valid .pysty or .pycls format',
|
||||
type=pathlib.Path,
|
||||
dest='input_file'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-n', '--name',
|
||||
help='Name of the package / class to be formatted.',
|
||||
type=str,
|
||||
choices=[FILENAME_TYPE_RAW_NAME, FILENAME_TYPE_PREPEND_AUTHOR],
|
||||
default=FILENAME_TYPE_PREPEND_AUTHOR,
|
||||
dest='latex_name'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-g', '--git-version',
|
||||
help='Insert git version information into build. This assumes your input'
|
||||
'files are located in a git repository. Default: false',
|
||||
action='store_true',
|
||||
dest='include_git_version'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-d', '--allow-dirty',
|
||||
help='If git flag is set, allow building of a dirty repo. Default: false',
|
||||
action='store_true',
|
||||
dest='allow_dirty'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-p',
|
||||
'--pytex-version',
|
||||
help='Write PyTeX version information into built LaTeX files',
|
||||
action='store_true',
|
||||
dest='include_pytex_version'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-t', '--build-time',
|
||||
help='Insert build time into built LaTeX files',
|
||||
action='store_true',
|
||||
dest='include_timestamp'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-l', '--license',
|
||||
help='Insert MIT license into package header',
|
||||
action='store_true',
|
||||
dest='include_license'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-a', '--author',
|
||||
help='Set author of packages',
|
||||
type=str,
|
||||
dest='author'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-f', '--force',
|
||||
help='Overwrite unknown existing files without confirmation',
|
||||
action='store_true',
|
||||
dest='overwrite_existing_files'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--pytex-info-text',
|
||||
help='Include a PyTeX info text into headers',
|
||||
action='store_true',
|
||||
dest='include_pytex_info_text'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-e', '--extra-header',
|
||||
help='Path to file containing extra text for header of each package',
|
||||
type=pathlib.Path,
|
||||
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))
|
||||
for arg in args.keys():
|
||||
if type(args[arg]) == pathlib.PosixPath:
|
||||
args[arg] = args[arg].resolve()
|
||||
try:
|
||||
build(**args)
|
||||
except PyTexError as e:
|
||||
print(e)
|
||||
exit(1)
|
|
@ -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'
|
||||
]
|
|
@ -1,24 +1,9 @@
|
|||
import git
|
||||
import datetime
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
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
|
||||
from PyTeX.build.git_hook import get_latest_commit
|
||||
from PyTeX.config.header_parts import *
|
||||
|
||||
|
||||
class BuildInfo:
|
||||
|
@ -29,6 +14,7 @@ class BuildInfo:
|
|||
include_license: bool = False,
|
||||
include_git_version: bool = False,
|
||||
include_pytex_info_text: bool = False,
|
||||
extra_header: Optional[List[str]] = None,
|
||||
author: Optional[str] = None,
|
||||
pytex_repo: Optional[git.Repo] = None,
|
||||
packages_repo: Optional[git.Repo] = None):
|
||||
|
@ -49,11 +35,12 @@ class BuildInfo:
|
|||
self.get_repo_version()
|
||||
|
||||
self.create_header(
|
||||
include_timestamp=include_timestamp,
|
||||
include_pytex_version=include_pytex_version,
|
||||
include_license=include_license,
|
||||
include_pytex_info_text=include_pytex_info_text,
|
||||
include_timestamp=include_timestamp,
|
||||
include_git_version=include_git_version,
|
||||
include_pytex_info_text=include_pytex_info_text
|
||||
include_pytex_version=include_pytex_version,
|
||||
extra_header=extra_header
|
||||
)
|
||||
|
||||
@property
|
||||
|
@ -92,9 +79,9 @@ class BuildInfo:
|
|||
|
||||
def get_repo_version(self):
|
||||
if self._packages_repo_commit:
|
||||
self._packages_repo_version = git_describe(self._packages_repo_commit)
|
||||
self._packages_repo_version = self._packages_repo.git.describe()
|
||||
if self._pytex_repo_commit:
|
||||
self._pytex_repo_version = git_describe(self._pytex_repo_commit)
|
||||
self._pytex_repo_version = self._pytex_repo.git.describe()
|
||||
|
||||
def create_header(
|
||||
self,
|
||||
|
@ -102,5 +89,49 @@ class BuildInfo:
|
|||
include_pytex_version: bool = False,
|
||||
include_license: bool = False,
|
||||
include_git_version: bool = False,
|
||||
include_pytex_info_text: bool = False):
|
||||
self._header = [] # TODO
|
||||
include_pytex_info_text: bool = False,
|
||||
extra_header: Optional[List[str]] = None
|
||||
):
|
||||
if not (include_license
|
||||
or include_pytex_info_text
|
||||
or include_timestamp
|
||||
or include_pytex_version
|
||||
or include_git_version
|
||||
or extra_header):
|
||||
self._header = None
|
||||
return
|
||||
else:
|
||||
self._header = []
|
||||
if include_license:
|
||||
self._header += LICENSE + ['']
|
||||
if include_pytex_info_text:
|
||||
self._header += PYTEX_INFO_TEXT + ['']
|
||||
if include_timestamp or include_pytex_version or include_git_version:
|
||||
self._header += BUILD_DETAILS
|
||||
if include_timestamp:
|
||||
self._header += BUILD_TIME
|
||||
if include_pytex_version:
|
||||
self._header += PYTEX_VERSION
|
||||
if include_git_version:
|
||||
self._header += SOURCE_CODE_VERSION
|
||||
if len(self._header) > 0:
|
||||
self._header += ['']
|
||||
if extra_header:
|
||||
self._header += extra_header + ['']
|
||||
|
||||
if self._header[-1] == '':
|
||||
self._header.pop()
|
||||
|
||||
formatted_header = []
|
||||
for line in self._header:
|
||||
formatted_header.append(line.format(
|
||||
year=datetime.datetime.now().strftime('%Y'),
|
||||
copyright_holders=self.author,
|
||||
source_file='{source_file}',
|
||||
latex_file_type='{latex_file_type}',
|
||||
pytex_version=self.pytex_version,
|
||||
pytex_commit_hash=self.pytex_hash[:7],
|
||||
packages_version=self.packages_version,
|
||||
packages_commit_hash=self.packages_hash[:7]
|
||||
))
|
||||
self._header = formatted_header
|
152
build/utils/pytex_file.py
Normal file
152
build/utils/pytex_file.py
Normal file
|
@ -0,0 +1,152 @@
|
|||
from pathlib import Path
|
||||
from typing import Optional, List
|
||||
|
||||
from PyTeX.build.git_hook import is_recent, get_latest_commit
|
||||
from PyTeX import PackageFormatter, ClassFormatter, DictionaryFormatter
|
||||
from PyTeX.errors import *
|
||||
from .pytex_msg import pytex_msg
|
||||
from PyTeX.utils import md5
|
||||
|
||||
from .build_information import BuildInfo
|
||||
|
||||
|
||||
class TexFileToFormat:
|
||||
def __init__(
|
||||
self,
|
||||
src_path: Path,
|
||||
build_root: Path,
|
||||
src_root: Path,
|
||||
latex_name: str,
|
||||
current_build_info: BuildInfo,
|
||||
last_build_info: Optional[List[dict]],
|
||||
allow_dirty: bool = False,
|
||||
overwrite_existing_files: bool = False,
|
||||
build_all: bool = False):
|
||||
self.src_path = src_path
|
||||
self.build_root = build_root
|
||||
self.src_root = src_root
|
||||
self.build_path = build_root / src_path.parent.relative_to(src_root)
|
||||
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.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.overwrite_existing_files: overwrite_existing_files
|
||||
self.build_all = build_all
|
||||
self._header: Optional[List[str]] = None
|
||||
self.__format_header()
|
||||
|
||||
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) -> List[dict]:
|
||||
if self.dirty or self.pytex_dirty:
|
||||
if not self.allow_dirty:
|
||||
raise SubmoduleDirtyForbiddenError
|
||||
# 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_all
|
||||
|
||||
def __format_header(self):
|
||||
new_header = []
|
||||
if self.current_build_info.header:
|
||||
for line in self.current_build_info.header:
|
||||
if '.pysty' in self.src_path.name:
|
||||
latex_file_type = 'package'
|
||||
elif '.pycls' in self.src_path.name:
|
||||
latex_file_type = 'class'
|
||||
elif '.pydict' in self.src_path.name:
|
||||
latex_file_type = 'dictionary'
|
||||
else:
|
||||
raise ProgrammingError
|
||||
new_header.append(line.format(
|
||||
source_file=self.src_path.name,
|
||||
latex_file_type=latex_file_type
|
||||
))
|
||||
self._header = new_header
|
||||
|
||||
def __format(self) -> List[dict]:
|
||||
if self.src_path.name.endswith('.pysty'):
|
||||
formatter = PackageFormatter(
|
||||
package_name=self.src_path.with_suffix('').name,
|
||||
author=self.current_build_info.author,
|
||||
extra_header=self._header,
|
||||
tex_version='LaTeX2e',
|
||||
version=self.current_build_info.packages_version,
|
||||
latex_name=self.latex_name)
|
||||
elif self.src_path.name.endswith('.pycls'):
|
||||
formatter = ClassFormatter(
|
||||
class_name=self.src_path.with_suffix('').name,
|
||||
author=self.current_build_info.author,
|
||||
extra_header=self._header,
|
||||
tex_version='LaTeX2e',
|
||||
version=self.current_build_info.packages_version,
|
||||
latex_name=self.latex_name)
|
||||
elif self.src_path.name.endswith('.pysty3'):
|
||||
formatter = PackageFormatter(
|
||||
package_name=self.src_path.with_suffix('').name,
|
||||
author=self.current_build_info.author,
|
||||
extra_header=self._header,
|
||||
tex_version='LaTeX3',
|
||||
version=self.current_build_info.packages_version,
|
||||
latex_name=self.latex_name)
|
||||
elif self.src_path.name.endswith('.pycls3'):
|
||||
formatter = ClassFormatter(
|
||||
class_name=self.src_path.with_suffix('').name,
|
||||
author=self.current_build_info.author,
|
||||
extra_header=self._header,
|
||||
tex_version='LaTeX3',
|
||||
version=self.current_build_info.packages_version,
|
||||
latex_name=self.latex_name)
|
||||
elif self.src_path.name.endswith('.pydict'):
|
||||
formatter = DictionaryFormatter(
|
||||
kind=self.src_path.with_suffix('').name,
|
||||
author=self.current_build_info.author,
|
||||
header=self._header
|
||||
)
|
||||
else:
|
||||
raise ProgrammingError
|
||||
formatter.make_default_macros()
|
||||
written_files = formatter.format_file(
|
||||
input_path=self.src_path,
|
||||
output_dir=self.build_path,
|
||||
relative_name=str(self.src_path.relative_to(self.src_root)),
|
||||
last_build_info=self.last_build_info_all)
|
||||
build_infos = []
|
||||
for written_file in written_files:
|
||||
info = {
|
||||
'name': str(self.src_path.parent.relative_to(self.src_root)) + "/" + written_file,
|
||||
'source file': str(self.src_path.relative_to(self.src_root)),
|
||||
'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,
|
||||
'md5sum': md5(self.build_root / self.src_path.parent.relative_to(self.src_root) / written_file),
|
||||
'dirty': self.dirty
|
||||
}
|
||||
build_infos.append(info)
|
||||
pytex_msg('Written file {}'.format(written_file))
|
||||
return build_infos
|
2
build/utils/pytex_msg.py
Normal file
2
build/utils/pytex_msg.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
def pytex_msg(msg: str):
|
||||
print('[PyTeX] ' + msg)
|
12
config/__init__.py
Normal file
12
config/__init__.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME
|
||||
from .header_parts import LICENSE, PYTEX_INFO_TEXT, BUILD_DETAILS
|
||||
|
||||
__all__ = [
|
||||
'FILENAME_TYPE_PREPEND_AUTHOR',
|
||||
'FILENAME_TYPE_RAW_NAME',
|
||||
'DATE_FORMAT',
|
||||
'BUILD_INFO_FILENAME',
|
||||
'LICENSE',
|
||||
'PYTEX_INFO_TEXT',
|
||||
'BUILD_DETAILS'
|
||||
]
|
4
config/constants.py
Normal file
4
config/constants.py
Normal file
|
@ -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'
|
|
@ -1,5 +1,3 @@
|
|||
DEFAULT_AUTHOR = 'Maximilian Keßler'
|
||||
|
||||
LICENSE = [
|
||||
'Copyright © {year} {copyright_holders}',
|
||||
'',
|
||||
|
@ -21,16 +19,6 @@ LICENSE = [
|
|||
'SOFTWARE.'
|
||||
]
|
||||
|
||||
PACKAGE_INFO_TEXT = [
|
||||
"This LaTeX {latex_file_type} is free software and distributed under the MIT License. You",
|
||||
"may use it freely for your purposes. The latest version of the {latex_file_type} can be",
|
||||
"obtained via GitHub under",
|
||||
" https://github.com/kesslermaximilian/LatexPackages",
|
||||
"For further information see the url above.",
|
||||
"Reportings of bugs, suggestions and improvements are welcome, see the README",
|
||||
"at the Git repository for further information."
|
||||
]
|
||||
|
||||
PYTEX_INFO_TEXT = [
|
||||
"This {latex_file_type} has been generated by PyTeX, available at",
|
||||
" https://github.com/kesslermaximilian/PyTeX",
|
||||
|
@ -39,3 +27,19 @@ 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: {build_time}"
|
||||
]
|
||||
|
||||
PYTEX_VERSION = [
|
||||
" PyTeX version: {pytex_version} (commit {pytex_commit_hash})"
|
||||
]
|
||||
|
||||
SOURCE_CODE_VERSION = [
|
||||
" Source code version: {packages_version} (commit {packages_commit_hash})"
|
||||
]
|
|
@ -1,7 +1,9 @@
|
|||
from .class_formatter import ClassFormatter
|
||||
from .package_formatter import PackageFormatter
|
||||
from .dictionary_formatter import DictionaryFormatter
|
||||
|
||||
__all__ = [
|
||||
'PackageFormatter',
|
||||
'ClassFormatter'
|
||||
'ClassFormatter',
|
||||
'DictionaryFormatter'
|
||||
]
|
||||
|
|
|
@ -4,8 +4,19 @@ import PyTeX.macros
|
|||
|
||||
|
||||
class ClassFormatter(PyTeX.formatter.TexFormatter):
|
||||
def __init__(self, class_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []):
|
||||
PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls')
|
||||
def __init__(self, class_name: str, author: str, extra_header: [str] = [], tex_version: str = 'LaTeX2e',
|
||||
version: str = '0.0.0', latex_name: str = 'prepend-author'):
|
||||
PyTeX.formatter.TexFormatter.__init__(
|
||||
self,
|
||||
name=class_name,
|
||||
author=author,
|
||||
header=extra_header,
|
||||
file_extension='.cls',
|
||||
tex_version=tex_version,
|
||||
version=version,
|
||||
latex_name=latex_name
|
||||
)
|
||||
self.tex_version = tex_version
|
||||
|
||||
def make_default_macros(self):
|
||||
PyTeX.macros.make_default_macros(self, 'class')
|
||||
PyTeX.macros.make_default_macros(self, 'class', tex_version=self.tex_version)
|
||||
|
|
88
default_formatters/dictionary_formatter.py
Normal file
88
default_formatters/dictionary_formatter.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional, List
|
||||
from datetime import *
|
||||
import csv
|
||||
|
||||
from PyTeX.formatter import Formatter
|
||||
from PyTeX.utils import ensure_file_integrity
|
||||
|
||||
|
||||
class DictionaryFormatter(Formatter):
|
||||
def __init__(self, kind: str, author: str, header: Optional[List[str]]):
|
||||
self.header = header
|
||||
self.kind = kind.lower()
|
||||
self.author = author
|
||||
author_parts = self.author.lower().replace('ß', 'ss').split(' ')
|
||||
self.author_acronym = author_parts[0][0] + author_parts[-1]
|
||||
self.dictname = r'translator-{kind}-dictionary'.format(
|
||||
kind=self.kind
|
||||
)
|
||||
self.name_lowercase = self.dictname + '-{language}'
|
||||
self.file_name = self.name_lowercase + '.dict'
|
||||
self.date = datetime.now().strftime('%Y/%m/%d')
|
||||
self.year = int(datetime.now().strftime('%Y'))
|
||||
self.replace_dict: Dict = {}
|
||||
self.arg_replace_dict: Dict = {}
|
||||
self.source_file_name = "not specified"
|
||||
super().__init__()
|
||||
|
||||
def expected_file_name(self):
|
||||
return self.file_name
|
||||
|
||||
def format_file(
|
||||
self,
|
||||
input_path: Path,
|
||||
output_dir: Path = None,
|
||||
relative_name: Optional[str] = None,
|
||||
last_build_info: Optional[List[Dict]] = None
|
||||
) -> List[str]:
|
||||
self.source_file_name = str(input_path.name)
|
||||
written_files = []
|
||||
|
||||
if self.header:
|
||||
lines = '%' * 80 + '\n' \
|
||||
+ '\n'.join(map(lambda line: '% ' + line, self.header)) \
|
||||
+ '\n' + '%' * 80 + '\n\n'
|
||||
else:
|
||||
lines = []
|
||||
if output_dir is None:
|
||||
output_dir = input_path.parent
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(input_path, newline='') as csvfile:
|
||||
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
|
||||
langs = next(spamreader)
|
||||
translations = {}
|
||||
for lang in langs[1:]:
|
||||
translations[lang] = {}
|
||||
for line in spamreader:
|
||||
for n in range(1, len(line)):
|
||||
translations[langs[n]][line[0]] = line[n]
|
||||
|
||||
for lang in langs[1:]:
|
||||
lang_lines = lines
|
||||
lang_lines += r'\ProvidesDictionary{{{dictname}}}{{{lang}}}'.format(
|
||||
dictname=self.dictname,
|
||||
lang=lang
|
||||
)
|
||||
lang_lines += '\n'
|
||||
lang_lines += '\n'
|
||||
for key in translations[lang].keys():
|
||||
if translations[lang][key].strip() != '':
|
||||
lang_lines += r'\providetranslation{{{key}}}{{{translation}}}'.format(
|
||||
key=key.strip(),
|
||||
translation=translations[lang][key].strip()
|
||||
)
|
||||
lang_lines += '\n'
|
||||
ensure_file_integrity(
|
||||
output_dir / self.file_name.format(language=lang),
|
||||
str(Path(relative_name).parent / self.file_name.format(language=lang)),
|
||||
last_build_info
|
||||
)
|
||||
(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):
|
||||
pass
|
|
@ -4,8 +4,19 @@ import PyTeX.macros
|
|||
|
||||
|
||||
class PackageFormatter(PyTeX.formatter.TexFormatter):
|
||||
def __init__(self, package_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []):
|
||||
PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty')
|
||||
def __init__(self, package_name: str, author: str, extra_header: [str] = [], tex_version: str = 'LaTeX2e',
|
||||
version: str = '0.0.0', latex_name: str = 'prepend-author'):
|
||||
PyTeX.formatter.TexFormatter.__init__(
|
||||
self,
|
||||
name=package_name,
|
||||
author=author,
|
||||
header=extra_header,
|
||||
file_extension='.sty',
|
||||
tex_version=tex_version,
|
||||
version=version,
|
||||
latex_name=latex_name
|
||||
)
|
||||
self.tex_version = tex_version
|
||||
|
||||
def make_default_macros(self):
|
||||
PyTeX.macros.make_default_macros(self, 'package')
|
||||
PyTeX.macros.make_default_macros(self, 'package', tex_version=self.tex_version)
|
||||
|
|
14
errors/__init__.py
Normal file
14
errors/__init__.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from .errors import PyTexError, SubmoduleDirtyForbiddenError, ProgrammingError, ExtraHeaderFileNotFoundError, \
|
||||
UnknownTexVersionError, ModifiedFileInBuildDirectoryError, UnknownFileInBuildDirectoryNoOverwriteError, \
|
||||
UnknownFileInBuildDirectory
|
||||
|
||||
__all__ = [
|
||||
'PyTexError',
|
||||
'SubmoduleDirtyForbiddenError',
|
||||
'ProgrammingError',
|
||||
'ExtraHeaderFileNotFoundError',
|
||||
'UnknownTexVersionError',
|
||||
'ModifiedFileInBuildDirectoryError',
|
||||
'UnknownFileInBuildDirectoryNoOverwriteError',
|
||||
'UnknownFileInBuildDirectory'
|
||||
]
|
65
errors/errors.py
Normal file
65
errors/errors.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
class PyTexError(Exception):
|
||||
def __init__(self, message, *args, **kwargs):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return r'{prefix} ERROR: {message}'.format(
|
||||
prefix='[PyTeX]',
|
||||
message=self.message
|
||||
)
|
||||
|
||||
|
||||
class SubmoduleDirtyForbiddenError(PyTexError):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
"Submodule PyTeX is dirty, but writing dirty files is not allowed. "
|
||||
"Call PyTeX with '--allow-dirty' option, or commit the submodule changes.")
|
||||
|
||||
|
||||
class ExtraHeaderFileNotFoundError(PyTexError):
|
||||
def __init__(self):
|
||||
super().__init__('Path to extra header content is invalid.')
|
||||
|
||||
|
||||
class ProgrammingError(PyTexError):
|
||||
def __init__(self):
|
||||
super().__init__("A FATAL programming error has occurred. Please contact the developer.")
|
||||
|
||||
|
||||
class UnknownTexVersionError(PyTexError):
|
||||
def __init__(self, tex_version: str):
|
||||
super().__init__(
|
||||
f"Unknown TeX version {tex_version}given. Only 'LaTeX2e' and 'LaTeX3' "
|
||||
f"are currently supported"
|
||||
)
|
||||
|
||||
|
||||
class ModifiedFileInBuildDirectoryError(PyTexError):
|
||||
def __init__(self, filename: str):
|
||||
super().__init__(
|
||||
f"File '{filename}' in the build directory has been modified since the last build. "
|
||||
f"Refusing to overwrite a modified file, since you could lose your manual changes. "
|
||||
f"If you are sure you do not need this anymore, delete it manually and build again. "
|
||||
f"Note that for exactly this reason, it is strongly discouraged to edit built files directly."
|
||||
)
|
||||
|
||||
|
||||
class UnknownFileInBuildDirectoryNoOverwriteError(PyTexError):
|
||||
def __init__(self, filename: str):
|
||||
super().__init__(
|
||||
f"Unknown file {filename} in build directory found. "
|
||||
f"PyTeX has no knowledge whether this file has been built by PyTeX. "
|
||||
f"Refusing to overwrite this file, since you could lose your data. "
|
||||
f"If you are sure, this can be got rid of, delete the file manually, "
|
||||
f"and run the build again."
|
||||
)
|
||||
|
||||
|
||||
class UnknownFileInBuildDirectory(PyTexError):
|
||||
def __init__(self, filename):
|
||||
super().__init__(
|
||||
f"Detected unknown file {filename} in build directory."
|
||||
f"PyTeX has no knowledge about this, you should probably"
|
||||
f"remove it."
|
||||
)
|
|
@ -1,5 +1,6 @@
|
|||
from .tex_formatter import TexFormatter
|
||||
from .tex_formatter import TexFormatter, Formatter
|
||||
|
||||
__all__ = [
|
||||
'TexFormatter'
|
||||
'TexFormatter',
|
||||
'Formatter'
|
||||
]
|
||||
|
|
17
formatter/formatter.py
Normal file
17
formatter/formatter.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
from pathlib import Path
|
||||
from typing import List, Dict, Optional
|
||||
|
||||
|
||||
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, last_build_info: Optional[List[Dict]] = None) -> List[str]:
|
||||
pass
|
||||
|
||||
def expected_file_name(self) -> str:
|
||||
pass
|
|
@ -1,33 +1,57 @@
|
|||
import datetime
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
from typing import Dict, Optional, List
|
||||
from datetime import *
|
||||
|
||||
from PyTeX.base import Attributes, Args
|
||||
from PyTeX.errors import *
|
||||
from PyTeX.utils import ensure_file_integrity
|
||||
|
||||
from .formatter import Formatter
|
||||
|
||||
|
||||
class TexFormatter:
|
||||
def __init__(self, name: str, author: str, extra_header: [str], file_extension: str):
|
||||
self.extra_header = extra_header
|
||||
class TexFormatter(Formatter):
|
||||
def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str,
|
||||
tex_version: str, version: str, latex_name: str):
|
||||
|
||||
self.version = version[1:] if version.startswith('v') else version
|
||||
self.header = header
|
||||
self.name_raw = name
|
||||
self.author = author
|
||||
self.latex_name = latex_name
|
||||
author_parts = self.author.lower().replace('ß', 'ss').split(' ')
|
||||
self.author_acronym = author_parts[0][0] + author_parts[-1]
|
||||
self.name_lowercase = r'{prefix}-{name}'.format(prefix=self.author_acronym,
|
||||
name=self.name_raw.lower().strip().replace(' ', '-'))
|
||||
self.prefix = self.name_lowercase.replace('-', '@') + '@'
|
||||
self.file_name = self.name_lowercase + file_extension
|
||||
self.date = datetime.now().strftime('%Y/%m/%d')
|
||||
self.year = int(datetime.now().strftime('%Y'))
|
||||
self.replace_dict: Dict = {}
|
||||
self.arg_replace_dict: Dict = {}
|
||||
self.source_file_name = "not specified"
|
||||
if self.latex_name == 'prepend-author':
|
||||
self.name_lowercase = r'{prefix}-{name}'.format(prefix=self.author_acronym,
|
||||
name=self.name_raw.lower().strip().replace(' ', '-'))
|
||||
else:
|
||||
self.name_lowercase = self.name_raw.lower().strip().replace(' ', '-')
|
||||
self.file_name = self.name_lowercase + file_extension
|
||||
if tex_version == 'LaTeX2e':
|
||||
self.prefix = self.name_lowercase.replace('-', '@') + '@'
|
||||
elif tex_version == 'LaTeX3':
|
||||
self.prefix = '__' + self.name_lowercase.replace('-', '_') + '_'
|
||||
else:
|
||||
raise UnknownTexVersionError(tex_version)
|
||||
super().__init__()
|
||||
|
||||
@staticmethod
|
||||
def __command_name2keyword(keyword: str):
|
||||
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
|
||||
|
||||
def expected_file_name(self):
|
||||
return self.file_name
|
||||
|
||||
@property
|
||||
def filename(self):
|
||||
return self.file_name
|
||||
|
||||
def __parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
|
||||
new_args = []
|
||||
for arg in user_args:
|
||||
|
@ -89,14 +113,27 @@ class TexFormatter:
|
|||
'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,
|
||||
relative_name: Optional[str] = None,
|
||||
last_build_info: Optional[List[Dict]] = None) -> List[str]:
|
||||
self.source_file_name = str(input_path.name)
|
||||
input_file = input_path.open()
|
||||
lines = input_file.readlines()
|
||||
newlines = []
|
||||
if self.header:
|
||||
newlines = '%' * 80 + '\n' \
|
||||
+ '\n'.join(map(lambda line: '% ' + line, self.header)) \
|
||||
+ '\n' + '%' * 80 + '\n\n'
|
||||
else:
|
||||
newlines = []
|
||||
for line in lines:
|
||||
newlines += self.__format_string_with_arg(self.__format_string(line))
|
||||
if output_dir is None:
|
||||
output_dir = input_path.parent
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
ensure_file_integrity(output_dir / self.file_name, str(Path(relative_name).parent / self.file_name), last_build_info)
|
||||
|
||||
(output_dir / self.file_name).write_text(''.join(newlines))
|
||||
return [str(self.file_name)]
|
||||
|
|
|
@ -1,56 +1,14 @@
|
|||
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
|
||||
+ [''] + formatter.extra_header)
|
||||
) \
|
||||
+ '\n' + '%' * 80 + '\n\n' \
|
||||
+ '\\NeedsTeXFormat{{LaTeX2e}}\n' \
|
||||
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
|
||||
formatter.add_arg_replacement(
|
||||
1, 'header',
|
||||
header,
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase,
|
||||
date=PyTeX.base.Attributes.date,
|
||||
description=PyTeX.base.Args.one,
|
||||
year=PyTeX.base.Attributes.year,
|
||||
copyright_holders=PyTeX.base.Attributes.author,
|
||||
source_file=PyTeX.base.Attributes.source_file_name,
|
||||
Type=latex_file_type.capitalize(),
|
||||
latex_file_type=latex_file_type
|
||||
)
|
||||
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str, tex_version: str = 'LaTeX2e'):
|
||||
formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.name_lowercase)
|
||||
formatter.add_replacement('{Type} prefix'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.prefix)
|
||||
formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}',
|
||||
PyTeX.base.Attributes.prefix, PyTeX.base.Args.one)
|
||||
formatter.add_replacement('file name', '{name}', name=PyTeX.base.Attributes.file_name)
|
||||
formatter.add_replacement('date', '{}', PyTeX.base.Attributes.date)
|
||||
formatter.add_replacement('author', '{}', PyTeX.base.Attributes.author)
|
||||
formatter.add_arg_replacement(2, 'new if', r'\newif\if{prefix}{condition}\{prefix}{condition}{value}',
|
||||
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
|
||||
value=PyTeX.base.Args.two)
|
||||
formatter.add_arg_replacement(2, 'set if', r'\{prefix}{condition}{value}',
|
||||
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
|
||||
value=PyTeX.base.Args.two)
|
||||
formatter.add_arg_replacement(1, 'if', r'\if{prefix}{condition}', prefix=PyTeX.base.Attributes.prefix,
|
||||
condition=PyTeX.base.Args.one)
|
||||
formatter.add_replacement('language options x',
|
||||
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
|
||||
r'\DeclareOptionX{{german}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOptionX{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOptionX{{english}}{{\{prefix}englishtrue}}',
|
||||
prefix=PyTeX.base.Attributes.prefix)
|
||||
formatter.add_replacement('language options',
|
||||
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
|
||||
r'\DeclareOption{{german}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOption{{english}}{{\{prefix}englishtrue}}',
|
||||
prefix=PyTeX.base.Attributes.prefix)
|
||||
formatter.add_replacement('author acronym', '{}', PyTeX.base.Attributes.author_acronym)
|
||||
formatter.add_arg_replacement(1, 'info', r'\{Type}Info{{{name}}}{{{info}}}',
|
||||
name=PyTeX.base.Attributes.name_lowercase,
|
||||
info=PyTeX.base.Args.one, Type=latex_file_type.capitalize())
|
||||
|
@ -60,11 +18,57 @@ def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type
|
|||
formatter.add_arg_replacement(1, 'error', r'\{Type}Error{{{name}}}{{{error}}}',
|
||||
name=PyTeX.base.Attributes.name_lowercase, error=PyTeX.base.Args.one,
|
||||
Type=latex_file_type.capitalize())
|
||||
formatter.add_replacement('end options x',
|
||||
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}"
|
||||
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n',
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
|
||||
formatter.add_replacement('end options',
|
||||
r"\DeclareOption*{{\{Type}Warning{{{name_lowercase}}}"
|
||||
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptions\relax' + '\n',
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
|
||||
formatter.add_replacement('file name', '{name}', name=PyTeX.base.Attributes.file_name)
|
||||
formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}',
|
||||
PyTeX.base.Attributes.prefix, PyTeX.base.Args.one)
|
||||
|
||||
if tex_version == 'LaTeX3':
|
||||
header = r'\ProvidesExpl{Type}{{{name_lowercase}}}{{{date}}}{{{version}}}{{{description}}}' + '\n\n'
|
||||
formatter.add_arg_replacement(
|
||||
1, 'header',
|
||||
header,
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase,
|
||||
date=PyTeX.base.Attributes.date,
|
||||
description=PyTeX.base.Args.one,
|
||||
Type=latex_file_type.capitalize(),
|
||||
version=PyTeX.base.Attributes.version
|
||||
)
|
||||
elif tex_version == 'LaTeX2e':
|
||||
header = '\\NeedsTeXFormat{{LaTeX2e}}\n' \
|
||||
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
|
||||
formatter.add_arg_replacement(
|
||||
1, 'header',
|
||||
header,
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase,
|
||||
date=PyTeX.base.Attributes.date,
|
||||
description=PyTeX.base.Args.one,
|
||||
Type=latex_file_type.capitalize(),
|
||||
)
|
||||
formatter.add_arg_replacement(2, 'new if', r'\newif\if{prefix}{condition}\{prefix}{condition}{value}',
|
||||
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
|
||||
value=PyTeX.base.Args.two)
|
||||
formatter.add_arg_replacement(2, 'set if', r'\{prefix}{condition}{value}',
|
||||
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
|
||||
value=PyTeX.base.Args.two)
|
||||
formatter.add_arg_replacement(1, 'if', r'\if{prefix}{condition}', prefix=PyTeX.base.Attributes.prefix,
|
||||
condition=PyTeX.base.Args.one)
|
||||
formatter.add_replacement('language options x',
|
||||
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
|
||||
r'\DeclareOptionX{{german}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOptionX{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOptionX{{english}}{{\{prefix}englishtrue}}',
|
||||
prefix=PyTeX.base.Attributes.prefix)
|
||||
formatter.add_replacement('language options',
|
||||
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
|
||||
r'\DeclareOption{{german}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
|
||||
r'\DeclareOption{{english}}{{\{prefix}englishtrue}}',
|
||||
prefix=PyTeX.base.Attributes.prefix)
|
||||
formatter.add_replacement('end options x',
|
||||
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}"
|
||||
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n',
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
|
||||
formatter.add_replacement('end options',
|
||||
r"\DeclareOption*{{\{Type}Warning{{{name_lowercase}}}"
|
||||
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptions\relax' + '\n',
|
||||
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
|
||||
|
|
7
utils/__init__.py
Normal file
7
utils/__init__.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from. checksum import md5
|
||||
from .file_integrity import ensure_file_integrity
|
||||
|
||||
__all__ = [
|
||||
'md5',
|
||||
'ensure_file_integrity'
|
||||
]
|
12
utils/checksum.py
Normal file
12
utils/checksum.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
# https://stackoverflow.com/a/3431838/16371376
|
||||
|
||||
|
||||
def md5(file: Path):
|
||||
hash_md5 = hashlib.md5()
|
||||
with open(file, "rb") as f:
|
||||
for block in iter(lambda: f.read(4096), b""):
|
||||
hash_md5.update(block)
|
||||
return hash_md5.hexdigest()
|
18
utils/file_integrity.py
Normal file
18
utils/file_integrity.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from pathlib import Path
|
||||
from PyTeX.errors import *
|
||||
from .checksum import md5
|
||||
from typing import Optional, List, Dict
|
||||
|
||||
|
||||
def ensure_file_integrity(file: Path, output_file_name: str, build_info: Optional[List[Dict]] = None):
|
||||
if file.exists():
|
||||
if not build_info:
|
||||
raise UnknownFileInBuildDirectoryNoOverwriteError(str(file))
|
||||
found = False
|
||||
for info in build_info:
|
||||
if info['name'] == output_file_name:
|
||||
if not md5(file) == info['md5sum']:
|
||||
raise ModifiedFileInBuildDirectoryError(str(file))
|
||||
found = True
|
||||
if not found:
|
||||
raise UnknownFileInBuildDirectoryNoOverwriteError(str(file))
|
Loading…
Add table
Add a link
Reference in a new issue