From 9df6bd0513f742302416aaef700ae73e5ff2d616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 8 Oct 2021 16:12:48 +0200 Subject: [PATCH] add formatter class and derive package formatter from it. future: add class formatter --- config.py | 8 ++-- enums.py | 6 +-- formatter.py | 101 +++++++++++++++++++++++++++++++++++++++++ package_formatter.py | 104 +++---------------------------------------- replacements.py | 72 +++++++++++++++--------------- 5 files changed, 152 insertions(+), 139 deletions(-) create mode 100644 formatter.py diff --git a/config.py b/config.py index 1a97356..281db91 100644 --- a/config.py +++ b/config.py @@ -22,8 +22,8 @@ LICENSE = [ ] PACKAGE_INFO_TEXT = [ - "This LaTeX package is free software and distributed under the MIT License. You", - "may use it freely for your purposes. The latest version of the package can be", + "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.", @@ -32,10 +32,10 @@ PACKAGE_INFO_TEXT = [ ] PYTEX_INFO_TEXT = [ - "This package has been generated by PyTeX, available at", + "This {latex_file_type} has been generated by PyTeX, available at", " https://github.com/kesslermaximilian/PyTeX", "and built from source file '{source_file}'.", "It is STRONGLY DISCOURAGED to edit this source file directly, since local", "changes will not be versioned by Git and overwritten by the next build. Always", - "edit the source file and build the package again." + "edit the source file and build the {latex_file_type} again." ] diff --git a/enums.py b/enums.py index 19d11ec..eeea582 100644 --- a/enums.py +++ b/enums.py @@ -2,11 +2,11 @@ from enum import Enum class Attributes(Enum): - package_name_raw = 'package_name_raw' + name_raw = 'name_raw' author = 'author' author_acronym = 'author_acronym' - package_name = 'package_name' - package_prefix = 'package_prefix' + name_lowercase = 'name_lowercase' + prefix = 'prefix' file_name = 'file_name' date = 'date' year = 'year' diff --git a/formatter.py b/formatter.py new file mode 100644 index 0000000..85df89e --- /dev/null +++ b/formatter.py @@ -0,0 +1,101 @@ +import datetime +import re +from pathlib import Path +from typing import Dict +from datetime import * +from enums import Attributes, Args + + +class Formatter: + def __init__(self, name: str, author: str, extra_header: str, file_extension: str): + self.extra_header = extra_header + self.name_raw = name + self.author = author + 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" + + @staticmethod + def command_name2keyword(keyword: str): + return '__' + keyword.upper().strip().replace(' ', '_') + '__' + + def parse_replacement_args(self, match_groups, *user_args, **user_kwargs): + new_args = [] + for arg in user_args: + if type(arg) == Attributes: + new_args.append(getattr(self, arg.value)) + elif type(arg) == Args: + new_args.append(match_groups[arg.value].strip()) + elif type(arg) == str: + new_args.append(arg.strip()) + else: + new_args += 'ERROR' + new_args = tuple(new_args) + new_kwargs = {} + for kw in user_kwargs: + if type(user_kwargs[kw]) == Attributes: + new_kwargs[kw] = getattr(self, user_kwargs[kw].value) + elif type(user_kwargs[kw]) == Args: + new_kwargs[kw] = match_groups[user_kwargs[kw].value].strip() + elif type(user_kwargs[kw]) == str: + new_kwargs[kw] = user_kwargs[kw] + else: + new_kwargs[kw] = 'ERROR' + return new_args, new_kwargs + + def add_replacement(self, keyword: str, replacement: str, *args, **kwargs): + args, kwargs = self.parse_replacement_args([], *args, **kwargs) + self.replace_dict[self.command_name2keyword(keyword)] = replacement.format(*args, **kwargs) + + def add_arg_replacement(self, num_args: int, keyword: str, replacement: str, *args, **kwargs): + self.arg_replace_dict[self.command_name2keyword(keyword)] = { + 'num_args': num_args, + 'replacement': replacement, + 'format_args': args, + 'format_kwargs': kwargs + } + + def format_string(self, contents: str) -> str: + for key in self.replace_dict.keys(): + contents = contents.replace(key, self.replace_dict[key]) + return contents + + def format_string_with_arg(self, contents: str) -> str: + for command in self.arg_replace_dict.keys(): + search_regex = re.compile(r'{keyword}\({arguments}(? str: - for key in self.replace_dict.keys(): - contents = contents.replace(key, self.replace_dict[key]) - return contents - - def format_with_arg(self, contents: str) -> str: - for command in self.arg_replace_dict.keys(): - search_regex = re.compile(r'{keyword}\({arguments}(?