Compare commits
26 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 | |||
ba0b49dbfc | |||
![]() |
b1d429e9b0 | ||
![]() |
1da78575c2 |
19 changed files with 496 additions and 109 deletions
|
@ -1,6 +1,7 @@
|
|||
from PyTeX.default_formatters import ClassFormatter, PackageFormatter
|
||||
from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter
|
||||
|
||||
__all__ = [
|
||||
"ClassFormatter",
|
||||
"PackageFormatter"
|
||||
"PackageFormatter",
|
||||
"DictionaryFormatter"
|
||||
]
|
||||
|
|
|
@ -11,6 +11,7 @@ class Attributes(Enum):
|
|||
date = 'date'
|
||||
year = 'year'
|
||||
source_file_name = 'source_file_name'
|
||||
version = 'version'
|
||||
|
||||
|
||||
class Args(Enum):
|
||||
|
|
|
@ -5,6 +5,7 @@ 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
|
||||
|
||||
|
@ -26,6 +27,7 @@ def build(
|
|||
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:
|
||||
|
@ -34,7 +36,7 @@ def build(
|
|||
text = f.readlines()
|
||||
extra_header = [line.rstrip() for line in text]
|
||||
else:
|
||||
raise FileNotFoundError('Path to extra header content is invalid.')
|
||||
raise ExtraHeaderFileNotFoundError
|
||||
current_build_info = BuildInfo(
|
||||
include_timestamp=include_timestamp,
|
||||
include_pytex_version=include_pytex_version,
|
||||
|
@ -46,6 +48,9 @@ def build(
|
|||
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
|
||||
|
||||
|
@ -65,23 +70,36 @@ def build(
|
|||
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 = next(
|
||||
(info for info in last_build_info['tex_sources'] if info['source file'] == file.name), {})
|
||||
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 = None
|
||||
last_build_info_for_this_file = []
|
||||
sources_to_build.append(
|
||||
TexFileToFormat(
|
||||
src_path=file,
|
||||
build_dir=output_dir / file.parent.relative_to(input_dir),
|
||||
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,
|
||||
|
@ -108,8 +126,25 @@ def build(
|
|||
}
|
||||
|
||||
for source in sources_to_build:
|
||||
info = source.format()
|
||||
info_dict['tex_sources'].append(info)
|
||||
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:
|
||||
|
|
|
@ -2,6 +2,7 @@ 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
|
||||
|
||||
|
@ -104,8 +105,19 @@ def parse_and_build(arglist: [str]):
|
|||
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()
|
||||
build(**args)
|
||||
try:
|
||||
build(**args)
|
||||
except PyTexError as e:
|
||||
print(e)
|
||||
exit(1)
|
||||
|
|
|
@ -2,7 +2,7 @@ import git
|
|||
import datetime
|
||||
from typing import Optional, List
|
||||
|
||||
from PyTeX.build.git_hook import git_describe, get_latest_commit
|
||||
from PyTeX.build.git_hook import get_latest_commit
|
||||
from PyTeX.config.header_parts import *
|
||||
|
||||
|
||||
|
@ -79,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,
|
||||
|
@ -96,7 +96,8 @@ class BuildInfo:
|
|||
or include_pytex_info_text
|
||||
or include_timestamp
|
||||
or include_pytex_version
|
||||
or include_git_version):
|
||||
or include_git_version
|
||||
or extra_header):
|
||||
self._header = None
|
||||
return
|
||||
else:
|
||||
|
@ -113,7 +114,8 @@ class BuildInfo:
|
|||
self._header += PYTEX_VERSION
|
||||
if include_git_version:
|
||||
self._header += SOURCE_CODE_VERSION
|
||||
self._header += ['']
|
||||
if len(self._header) > 0:
|
||||
self._header += ['']
|
||||
if extra_header:
|
||||
self._header += extra_header + ['']
|
||||
|
||||
|
|
|
@ -2,8 +2,10 @@ 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
|
||||
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
|
||||
|
||||
|
@ -12,18 +14,22 @@ class TexFileToFormat:
|
|||
def __init__(
|
||||
self,
|
||||
src_path: Path,
|
||||
build_dir: Path,
|
||||
build_root: Path,
|
||||
src_root: Path,
|
||||
latex_name: str,
|
||||
current_build_info: BuildInfo,
|
||||
last_build_info: Optional[dict],
|
||||
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_path = build_dir
|
||||
self.tex_name = latex_name # Still an identifier on how to name the package when being formatted
|
||||
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 = 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.overwrite_existing_files: overwrite_existing_files
|
||||
self.build_all = build_all
|
||||
|
@ -48,13 +54,10 @@ class TexFileToFormat:
|
|||
self.recent = False
|
||||
self.pytex_recent = False
|
||||
|
||||
def format(self) -> dict:
|
||||
def format(self) -> List[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')
|
||||
)
|
||||
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:
|
||||
|
@ -64,41 +67,86 @@ class TexFileToFormat:
|
|||
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
|
||||
return self.last_build_info_all
|
||||
|
||||
def __format_header(self):
|
||||
new_header = []
|
||||
for line in self.current_build_info.header:
|
||||
new_header.append(line.format(
|
||||
source_file=self.src_path.name,
|
||||
latex_file_type='package' if '.pysty' in self.src_path.name else 'class'
|
||||
))
|
||||
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) -> dict:
|
||||
if '.pysty' in self.src_path.name:
|
||||
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)
|
||||
elif '.pycls' in self.src_path.name:
|
||||
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)
|
||||
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 Exception('Programming error. Please contact the developer.')
|
||||
pytex_msg('Writing file {}'.format(formatter.file_name))
|
||||
raise ProgrammingError
|
||||
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
|
||||
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
|
||||
|
|
|
@ -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, 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, 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
|
|
@ -5,29 +5,49 @@ 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, header: Optional[List[str]], file_extension: str):
|
||||
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
|
||||
|
@ -93,7 +113,12 @@ 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()
|
||||
|
@ -108,4 +133,7 @@ class TexFormatter:
|
|||
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)]
|
||||
|
|
|
@ -3,44 +3,12 @@ import PyTeX.base
|
|||
import PyTeX.config
|
||||
|
||||
|
||||
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str):
|
||||
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(),
|
||||
)
|
||||
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())
|
||||
|
@ -50,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