handle header correctly, i.e. do not only expand on macro
This commit is contained in:
parent
12a03e7989
commit
126d420b7a
8 changed files with 117 additions and 45 deletions
|
@ -21,19 +21,27 @@ def build(
|
||||||
include_license: bool = False, # header
|
include_license: bool = False, # header
|
||||||
include_git_version: bool = False, # header
|
include_git_version: bool = False, # header
|
||||||
include_pytex_info_text: bool = False, # header
|
include_pytex_info_text: bool = False, # header
|
||||||
use_git: bool = False, # versioning (not implemented yet)
|
extra_header: Optional[Path] = None,
|
||||||
allow_dirty: bool = False, # versioning
|
allow_dirty: bool = False, # versioning
|
||||||
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
|
||||||
):
|
):
|
||||||
pytex_msg('Getting git repository information...')
|
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 FileNotFoundError('Path to extra header content is invalid.')
|
||||||
current_build_info = BuildInfo(
|
current_build_info = BuildInfo(
|
||||||
include_timestamp=include_timestamp,
|
include_timestamp=include_timestamp,
|
||||||
include_pytex_version=include_pytex_version,
|
include_pytex_version=include_pytex_version,
|
||||||
include_license=include_license,
|
include_license=include_license,
|
||||||
include_git_version=include_git_version,
|
include_git_version=include_git_version,
|
||||||
include_pytex_info_text=include_pytex_info_text,
|
include_pytex_info_text=include_pytex_info_text,
|
||||||
|
extra_header=extra_header,
|
||||||
author=author,
|
author=author,
|
||||||
pytex_repo=git.Repo(__file__, search_parent_directories=True),
|
pytex_repo=git.Repo(__file__, search_parent_directories=True),
|
||||||
packages_repo=git.Repo(src_dir, search_parent_directories=True)
|
packages_repo=git.Repo(src_dir, search_parent_directories=True)
|
||||||
|
|
|
@ -53,7 +53,7 @@ def parse_and_build(arglist: [str]):
|
||||||
help='Insert git version information into build. This assumes your input'
|
help='Insert git version information into build. This assumes your input'
|
||||||
'files are located in a git repository. Default: false',
|
'files are located in a git repository. Default: false',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='use_git'
|
dest='include_git_version'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-d', '--allow-dirty',
|
'-d', '--allow-dirty',
|
||||||
|
@ -92,6 +92,18 @@ def parse_and_build(arglist: [str]):
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='overwrite_existing_files'
|
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'
|
||||||
|
)
|
||||||
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:
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import git
|
import git
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Optional
|
from typing import Optional, List
|
||||||
|
|
||||||
from PyTeX.build.git_hook import git_describe, get_latest_commit
|
from PyTeX.build.git_hook import git_describe, get_latest_commit
|
||||||
|
from PyTeX.config.header_parts import *
|
||||||
|
|
||||||
|
|
||||||
class BuildInfo:
|
class BuildInfo:
|
||||||
|
@ -13,6 +14,7 @@ class BuildInfo:
|
||||||
include_license: bool = False,
|
include_license: bool = False,
|
||||||
include_git_version: bool = False,
|
include_git_version: bool = False,
|
||||||
include_pytex_info_text: bool = False,
|
include_pytex_info_text: bool = False,
|
||||||
|
extra_header: Optional[List[str]] = None,
|
||||||
author: Optional[str] = None,
|
author: Optional[str] = None,
|
||||||
pytex_repo: Optional[git.Repo] = None,
|
pytex_repo: Optional[git.Repo] = None,
|
||||||
packages_repo: Optional[git.Repo] = None):
|
packages_repo: Optional[git.Repo] = None):
|
||||||
|
@ -33,11 +35,12 @@ class BuildInfo:
|
||||||
self.get_repo_version()
|
self.get_repo_version()
|
||||||
|
|
||||||
self.create_header(
|
self.create_header(
|
||||||
include_timestamp=include_timestamp,
|
|
||||||
include_pytex_version=include_pytex_version,
|
|
||||||
include_license=include_license,
|
include_license=include_license,
|
||||||
|
include_pytex_info_text=include_pytex_info_text,
|
||||||
|
include_timestamp=include_timestamp,
|
||||||
include_git_version=include_git_version,
|
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
|
@property
|
||||||
|
@ -86,5 +89,47 @@ class BuildInfo:
|
||||||
include_pytex_version: bool = False,
|
include_pytex_version: bool = False,
|
||||||
include_license: bool = False,
|
include_license: bool = False,
|
||||||
include_git_version: bool = False,
|
include_git_version: bool = False,
|
||||||
include_pytex_info_text: bool = False):
|
include_pytex_info_text: bool = False,
|
||||||
self._header = [] # TODO
|
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):
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional, List
|
||||||
|
|
||||||
from PyTeX.build.git_hook import is_recent, get_latest_commit
|
from PyTeX.build.git_hook import is_recent, get_latest_commit
|
||||||
from PyTeX import PackageFormatter, ClassFormatter
|
from PyTeX import PackageFormatter, ClassFormatter
|
||||||
|
@ -27,6 +27,8 @@ class TexFileToFormat:
|
||||||
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
|
||||||
|
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.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(
|
self.pytex_dirty: bool = self.current_build_info.pytex_repo.is_dirty(
|
||||||
|
@ -64,19 +66,28 @@ class TexFileToFormat:
|
||||||
else:
|
else:
|
||||||
return self.last_build_info
|
return self.last_build_info
|
||||||
|
|
||||||
|
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'
|
||||||
|
))
|
||||||
|
self._header = new_header
|
||||||
|
|
||||||
def __format(self) -> dict:
|
def __format(self) -> dict:
|
||||||
if '.pysty' in self.src_path.name:
|
if '.pysty' in self.src_path.name:
|
||||||
formatter = PackageFormatter(
|
formatter = PackageFormatter(
|
||||||
package_name=self.src_path.with_suffix('').name,
|
package_name=self.src_path.with_suffix('').name,
|
||||||
author=self.current_build_info.author,
|
author=self.current_build_info.author,
|
||||||
extra_header=self.current_build_info.header)
|
extra_header=self._header)
|
||||||
elif '.pycls' in self.src_path.name:
|
elif '.pycls' in self.src_path.name:
|
||||||
formatter = ClassFormatter(
|
formatter = ClassFormatter(
|
||||||
class_name=self.src_path.with_suffix('').name,
|
class_name=self.src_path.with_suffix('').name,
|
||||||
author=self.current_build_info.author,
|
author=self.current_build_info.author,
|
||||||
extra_header=self.current_build_info.header)
|
extra_header=self._header)
|
||||||
else:
|
else:
|
||||||
exit(1)
|
raise Exception('Programming error. Please contact the developer.')
|
||||||
pytex_msg('Writing file {}'.format(formatter.file_name))
|
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)
|
formatter.format_file(self.src_path, self.build_path)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME
|
from .constants import FILENAME_TYPE_PREPEND_AUTHOR, FILENAME_TYPE_RAW_NAME, DATE_FORMAT, BUILD_INFO_FILENAME
|
||||||
from .header_parts import LICENSE, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT, BUILD_DETAILS
|
from .header_parts import LICENSE, PYTEX_INFO_TEXT, BUILD_DETAILS
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'FILENAME_TYPE_PREPEND_AUTHOR',
|
'FILENAME_TYPE_PREPEND_AUTHOR',
|
||||||
|
@ -7,7 +7,6 @@ __all__ = [
|
||||||
'DATE_FORMAT',
|
'DATE_FORMAT',
|
||||||
'BUILD_INFO_FILENAME',
|
'BUILD_INFO_FILENAME',
|
||||||
'LICENSE',
|
'LICENSE',
|
||||||
'PACKAGE_INFO_TEXT',
|
|
||||||
'PYTEX_INFO_TEXT',
|
'PYTEX_INFO_TEXT',
|
||||||
'BUILD_DETAILS'
|
'BUILD_DETAILS'
|
||||||
]
|
]
|
||||||
|
|
|
@ -19,16 +19,6 @@ LICENSE = [
|
||||||
'SOFTWARE.'
|
'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 = [
|
PYTEX_INFO_TEXT = [
|
||||||
"This {latex_file_type} has been generated by PyTeX, available at",
|
"This {latex_file_type} has been generated by PyTeX, available at",
|
||||||
" https://github.com/kesslermaximilian/PyTeX",
|
" https://github.com/kesslermaximilian/PyTeX",
|
||||||
|
@ -39,8 +29,17 @@ PYTEX_INFO_TEXT = [
|
||||||
]
|
]
|
||||||
|
|
||||||
BUILD_DETAILS = [
|
BUILD_DETAILS = [
|
||||||
"Build details:",
|
"Build details:"
|
||||||
" Build time: {build_time}",
|
]
|
||||||
" PyTeX version: {pytex_version} (commit {pytex_commit_hash})",
|
|
||||||
" LatexPackages version: {packages_version} (commit {packages_commit_hash})"
|
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,15 +1,15 @@
|
||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict
|
from typing import Dict, Optional, List
|
||||||
from datetime import *
|
from datetime import *
|
||||||
|
|
||||||
from PyTeX.base import Attributes, Args
|
from PyTeX.base import Attributes, Args
|
||||||
|
|
||||||
|
|
||||||
class TexFormatter:
|
class TexFormatter:
|
||||||
def __init__(self, name: str, author: str, extra_header: [str], file_extension: str):
|
def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str):
|
||||||
self.extra_header = extra_header
|
self.header = header
|
||||||
self.name_raw = name
|
self.name_raw = name
|
||||||
self.author = author
|
self.author = author
|
||||||
author_parts = self.author.lower().replace('ß', 'ss').split(' ')
|
author_parts = self.author.lower().replace('ß', 'ss').split(' ')
|
||||||
|
@ -28,6 +28,10 @@ class TexFormatter:
|
||||||
def __command_name2keyword(keyword: str):
|
def __command_name2keyword(keyword: str):
|
||||||
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
|
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def filename(self):
|
||||||
|
return self.file_name
|
||||||
|
|
||||||
def __parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
|
def __parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
|
||||||
new_args = []
|
new_args = []
|
||||||
for arg in user_args:
|
for arg in user_args:
|
||||||
|
@ -93,6 +97,11 @@ class TexFormatter:
|
||||||
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()
|
||||||
|
if self.header:
|
||||||
|
newlines = '%' * 80 + '\n' \
|
||||||
|
+ '\n'.join(map(lambda line: '% ' + line, self.header)) \
|
||||||
|
+ '\n' + '%' * 80 + '\n\n'
|
||||||
|
else:
|
||||||
newlines = []
|
newlines = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
newlines += self.__format_string_with_arg(self.__format_string(line))
|
newlines += self.__format_string_with_arg(self.__format_string(line))
|
||||||
|
|
|
@ -4,14 +4,7 @@ import PyTeX.config
|
||||||
|
|
||||||
|
|
||||||
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str):
|
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str):
|
||||||
header = '%' * 80 + '\n' \
|
header = '\\NeedsTeXFormat{{LaTeX2e}}\n' \
|
||||||
+ '\n'.join(map(lambda line: '% ' + line,
|
|
||||||
PyTeX.config.LICENSE + [''] + PyTeX.config.PACKAGE_INFO_TEXT + [
|
|
||||||
''] + PyTeX.config.PYTEX_INFO_TEXT
|
|
||||||
+ [''] + formatter.extra_header)
|
|
||||||
) \
|
|
||||||
+ '\n' + '%' * 80 + '\n\n' \
|
|
||||||
+ '\\NeedsTeXFormat{{LaTeX2e}}\n' \
|
|
||||||
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
|
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
|
||||||
formatter.add_arg_replacement(
|
formatter.add_arg_replacement(
|
||||||
1, 'header',
|
1, 'header',
|
||||||
|
@ -19,11 +12,7 @@ def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type
|
||||||
name_lowercase=PyTeX.base.Attributes.name_lowercase,
|
name_lowercase=PyTeX.base.Attributes.name_lowercase,
|
||||||
date=PyTeX.base.Attributes.date,
|
date=PyTeX.base.Attributes.date,
|
||||||
description=PyTeX.base.Args.one,
|
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(),
|
Type=latex_file_type.capitalize(),
|
||||||
latex_file_type=latex_file_type
|
|
||||||
)
|
)
|
||||||
formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.name_lowercase)
|
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_replacement('{Type} prefix'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.prefix)
|
||||||
|
|
Loading…
Reference in a new issue