From 96d3ccd8d2375c12cab257ef7719c40e1ba1184c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 21 Nov 2020 12:51:05 +0100 Subject: [PATCH] Improved and finished publish library --- docs/init_template.txt | 24 --- publish_templates/*_template.txt | 25 +++ src/sh_edraft/__init__.py | 13 +- src/sh_edraft/common/__init__.py | 0 src/sh_edraft/common/interface/__init__.py | 0 src/sh_edraft/common/interface/ipublisher.py | 24 --- src/sh_edraft/common/model/__init__.py | 0 src/sh_edraft/configuration/__init__.py | 25 +++ src/sh_edraft/configuration/model/__init__.py | 26 +++ .../model/configuration_model_base.py | 12 ++ src/sh_edraft/discord/__init__.py | 25 +++ src/sh_edraft/logging/__init__.py | 25 +++ src/sh_edraft/mailing/__init__.py | 25 +++ src/sh_edraft/messenger/__init__.py | 25 +++ src/sh_edraft/publish/__init__.py | 26 +++ src/sh_edraft/publish/base/__init__.py | 26 +++ src/sh_edraft/publish/base/publisher_base.py | 30 +++ src/sh_edraft/publish/model/__init__.py | 27 +++ src/sh_edraft/publish/model/template.py | 118 +++++++++- src/sh_edraft/publish/model/template_enum.py | 16 ++ src/sh_edraft/publish/publisher.py | 201 ++++++++++++++++-- src/sh_edraft/service/__init__.py | 25 +++ src/sh_edraft/source_code/__init__.py | 25 +++ src/sh_edraft/source_code/model/__init__.py | 27 +++ src/sh_edraft/source_code/model/version.py | 44 ++++ .../source_code/model/version_enum.py | 8 + src/test.py | 42 +++- 27 files changed, 787 insertions(+), 77 deletions(-) delete mode 100644 docs/init_template.txt create mode 100644 publish_templates/*_template.txt delete mode 100644 src/sh_edraft/common/__init__.py delete mode 100644 src/sh_edraft/common/interface/__init__.py delete mode 100644 src/sh_edraft/common/interface/ipublisher.py delete mode 100644 src/sh_edraft/common/model/__init__.py create mode 100644 src/sh_edraft/configuration/model/__init__.py create mode 100644 src/sh_edraft/configuration/model/configuration_model_base.py create mode 100644 src/sh_edraft/publish/base/__init__.py create mode 100644 src/sh_edraft/publish/base/publisher_base.py create mode 100644 src/sh_edraft/publish/model/template_enum.py create mode 100644 src/sh_edraft/source_code/__init__.py create mode 100644 src/sh_edraft/source_code/model/__init__.py create mode 100644 src/sh_edraft/source_code/model/version.py create mode 100644 src/sh_edraft/source_code/model/version_enum.py diff --git a/docs/init_template.txt b/docs/init_template.txt deleted file mode 100644 index 920c0c2f..00000000 --- a/docs/init_template.txt +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -{} {} -~~~~~~~~~~~~~~~~~~~ - -{} - -:copyright: (c) {} {} -:license: {} - -""" - -__title__ = '{}' -__author__ = '{}' -__license__ = '{}' -__copyright__ = '{}' -__version__ = '{}' - -from collections import namedtuple - -VersionInfo = namedtuple('VersionInfo', 'major minor micro') - -version_info = VersionInfo(major={}, minor={}, micro={}) diff --git a/publish_templates/*_template.txt b/publish_templates/*_template.txt new file mode 100644 index 00000000..3b24f44f --- /dev/null +++ b/publish_templates/*_template.txt @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +$Name $Description +~~~~~~~~~~~~~~~~~~~ + +$LongDescription + +:copyright: (c) $CopyrightDate $CopyrightName +:license: $LicenseName$LicenseDescription + +""" + +__title__ = '$Title' +__author__ = '$Author' +__license__ = '$LicenseName' +__copyright__ = 'Copyright (c) $CopyrightDate $CopyrightName' +__version__ = '$Version' + +from collections import namedtuple + +$Imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=$Major, minor=$Minor, micro=$Micro) diff --git a/src/sh_edraft/__init__.py b/src/sh_edraft/__init__.py index 96602fbe..58fb4bac 100644 --- a/src/sh_edraft/__init__.py +++ b/src/sh_edraft/__init__.py @@ -1,24 +1,25 @@ # -*- coding: utf-8 -*- """ -sh_edraft python common lib +sh_edraft common python library ~~~~~~~~~~~~~~~~~~~ -Common python functions and classes for sh-edraft.de ecosystem +Library to share common classes and models used at sh-edraft.de -:copyright: (c) 2020 edraft +:copyright: (c) 2020 sh-edraft.de :license: MIT, see LICENSE for more details. """ -__title__ = 'sh_edraft.de' +__title__ = 'sh_edraft' __author__ = 'Sven Heidemann' __license__ = 'MIT' -__copyright__ = 'Copyright 2020 sh-edraft.de' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' __version__ = '2020.12.0.1' from collections import namedtuple -VersionInfo = namedtuple('VersionInfo', 'major minor micro') + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/common/__init__.py b/src/sh_edraft/common/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/sh_edraft/common/interface/__init__.py b/src/sh_edraft/common/interface/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/sh_edraft/common/interface/ipublisher.py b/src/sh_edraft/common/interface/ipublisher.py deleted file mode 100644 index a1df83e8..00000000 --- a/src/sh_edraft/common/interface/ipublisher.py +++ /dev/null @@ -1,24 +0,0 @@ -from abc import ABC, abstractmethod -from typing import List - -from sh_edraft.publish.model.template import Template - - -class IPublisher(ABC): - - @abstractmethod - def __init__(self, local_path: str): - pass - - @property - @abstractmethod - def local_path(self) -> str: - pass - - @abstractmethod - def create(self, templates: List[Template]): - pass - - @abstractmethod - def publish(self): - pass diff --git a/src/sh_edraft/common/model/__init__.py b/src/sh_edraft/common/model/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/sh_edraft/configuration/__init__.py b/src/sh_edraft/configuration/__init__.py index e69de29b..f4d4c98a 100644 --- a/src/sh_edraft/configuration/__init__.py +++ b/src/sh_edraft/configuration/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.configuration +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.configuration' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/configuration/model/__init__.py b/src/sh_edraft/configuration/model/__init__.py new file mode 100644 index 00000000..824ece23 --- /dev/null +++ b/src/sh_edraft/configuration/model/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.configuration.model +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.configuration.model' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: +from sh_edraft.configuration.model.configuration_model_base import ConfigurationModelBase + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/configuration/model/configuration_model_base.py b/src/sh_edraft/configuration/model/configuration_model_base.py new file mode 100644 index 00000000..fe295727 --- /dev/null +++ b/src/sh_edraft/configuration/model/configuration_model_base.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod + + +class ConfigurationModelBase(ABC): + + @abstractmethod + def from_dict(self, settings: dict): + pass + + @abstractmethod + def to_dict(self) -> dict: + pass diff --git a/src/sh_edraft/discord/__init__.py b/src/sh_edraft/discord/__init__.py index e69de29b..e8957e78 100644 --- a/src/sh_edraft/discord/__init__.py +++ b/src/sh_edraft/discord/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.discord +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.discord' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/logging/__init__.py b/src/sh_edraft/logging/__init__.py index e69de29b..7ac6e90c 100644 --- a/src/sh_edraft/logging/__init__.py +++ b/src/sh_edraft/logging/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.logging +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.logging' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/mailing/__init__.py b/src/sh_edraft/mailing/__init__.py index e69de29b..45e5a87e 100644 --- a/src/sh_edraft/mailing/__init__.py +++ b/src/sh_edraft/mailing/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.mailing +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.mailing' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/messenger/__init__.py b/src/sh_edraft/messenger/__init__.py index e69de29b..2bab0145 100644 --- a/src/sh_edraft/messenger/__init__.py +++ b/src/sh_edraft/messenger/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.messenger +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.messenger' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/publish/__init__.py b/src/sh_edraft/publish/__init__.py index e69de29b..e754a8ac 100644 --- a/src/sh_edraft/publish/__init__.py +++ b/src/sh_edraft/publish/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.publish +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.publish' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: +from sh_edraft.publish.publisher import Publisher + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/publish/base/__init__.py b/src/sh_edraft/publish/base/__init__.py new file mode 100644 index 00000000..0c156e0d --- /dev/null +++ b/src/sh_edraft/publish/base/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.publish.base +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.publish.base' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: +from sh_edraft.publish.base.publisher_base import PublisherBase + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/publish/base/publisher_base.py b/src/sh_edraft/publish/base/publisher_base.py new file mode 100644 index 00000000..17274fdf --- /dev/null +++ b/src/sh_edraft/publish/base/publisher_base.py @@ -0,0 +1,30 @@ +from abc import ABC, abstractmethod + +from sh_edraft.publish.model.template import Template + + +class PublisherBase(ABC): + + @abstractmethod + def __init__(self, source_path: str, dist_path: str, settings: list[Template]): + self._source_path = source_path + self._dist_path = dist_path + self._settings = settings + + @property + @abstractmethod + def source_path(self) -> str: + pass + + @property + @abstractmethod + def dist_path(self): + pass + + @abstractmethod + def create(self): + pass + + @abstractmethod + def publish(self): + pass diff --git a/src/sh_edraft/publish/model/__init__.py b/src/sh_edraft/publish/model/__init__.py index e69de29b..caad810e 100644 --- a/src/sh_edraft/publish/model/__init__.py +++ b/src/sh_edraft/publish/model/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.publish.model +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.publish.model' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: +from sh_edraft.publish.model.template import Template +from sh_edraft.publish.model.template_enum import TemplateEnum + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/publish/model/template.py b/src/sh_edraft/publish/model/template.py index 7e1b1f57..d917374b 100644 --- a/src/sh_edraft/publish/model/template.py +++ b/src/sh_edraft/publish/model/template.py @@ -1,16 +1,122 @@ from typing import Optional +from sh_edraft.source_code.model.version import Version +from sh_edraft.configuration.model import ConfigurationModelBase +from sh_edraft.publish.model.template_enum import TemplateEnum -class Template: - def __init__(self, name: Optional[str] = None, path: Optional[str] = None): +class Template(ConfigurationModelBase): + + def __init__( + self, + template_path: Optional[str] = None, + name: Optional[str] = None, + description: Optional[str] = None, + long_description: Optional[str] = None, + copyright_date: Optional[str] = None, + copyright_name: Optional[str] = None, + license_name: Optional[str] = None, + license_description: Optional[str] = None, + title: Optional[str] = None, + author: Optional[str] = None, + version: Optional[dict] = None + ): + self._template_path: Optional[str] = template_path self._name: Optional[str] = name - self._path: Optional[str] = path + self._description: Optional[str] = description + self._long_description: Optional[str] = long_description + self._copyright_date: Optional[str] = copyright_date + self._copyright_name: Optional[str] = copyright_name + self._license_name: Optional[str] = license_name + self._license_description: Optional[str] = license_description + self._title: Optional[str] = title + self._author: Optional[str] = author + + self._version: Optional[Version] = Version() + self._version.from_dict(version) + + self._file_content: Optional[str] = None @property - def name(self): + def template_path(self) -> Optional[str]: + return self._template_path + + @property + def name(self) -> Optional[str]: return self._name @property - def path(self): - return self._path + def description(self) -> Optional[str]: + return self._description + + @property + def long_description(self) -> Optional[str]: + return self._long_description + + @property + def copyright_date(self) -> Optional[str]: + return self._copyright_date + + @property + def copyright_name(self) -> Optional[str]: + return self._copyright_name + + @property + def license_name(self) -> Optional[str]: + return self._license_name + + @property + def license_description(self) -> Optional[str]: + return self._license_description + + @property + def title(self) -> Optional[str]: + return self._title + + @property + def author(self) -> Optional[str]: + return self._author + + @property + def version(self) -> Optional[Version]: + return self._version + + @property + def file_content(self) -> Optional[str]: + return self._file_content + + @file_content.setter + def file_content(self, file_content: Optional[str]): + self._file_content = file_content + + def from_dict(self, settings: dict): + self._template_path = settings[TemplateEnum.TemplatePath.value] + self._name = settings[TemplateEnum.Name.value] + self._description = settings[TemplateEnum.Description.value] + self._long_description = settings[TemplateEnum.LongDescription.value] + self._copyright_date = settings[TemplateEnum.CopyrightDate.value] + self._copyright_name = settings[TemplateEnum.CopyrightName.value] + self._license_name = settings[TemplateEnum.LicenseName.value] + self._license_description = settings[TemplateEnum.LicenseDescription.value] + self._title = settings[TemplateEnum.Title.value] + self._author = settings[TemplateEnum.Author.value] + self._version.from_dict(settings[TemplateEnum.Version.value]) + + def to_dict(self) -> dict: + version: Optional[dict] = None + if self._version is not None: + version = self._version.to_dict() + + return { + TemplateEnum.TemplatePath.value: self._template_path, + TemplateEnum.Name.value: self._name, + TemplateEnum.Description.value: self._description, + TemplateEnum.LongDescription.value: self._long_description, + TemplateEnum.CopyrightDate.value: self._copyright_date, + TemplateEnum.CopyrightName.value: self._copyright_name, + TemplateEnum.LicenseName.value: self._license_name, + TemplateEnum.LicenseDescription.value: self._license_description, + TemplateEnum.Title.value: self._title, + TemplateEnum.Author.value: self._author, + TemplateEnum.Version.value: version + } diff --git a/src/sh_edraft/publish/model/template_enum.py b/src/sh_edraft/publish/model/template_enum.py new file mode 100644 index 00000000..f628c4ce --- /dev/null +++ b/src/sh_edraft/publish/model/template_enum.py @@ -0,0 +1,16 @@ +from enum import Enum + + +class TemplateEnum(Enum): + + TemplatePath = 'TemplatePath' + Name = 'Name' + Description = 'Description' + LongDescription = 'LongDescription' + CopyrightDate = 'CopyrightDate' + CopyrightName = 'CopyrightName' + LicenseName = 'LicenseName' + LicenseDescription = 'LicenseDescription' + Title = 'Title' + Author = 'Author' + Version = 'Version' diff --git a/src/sh_edraft/publish/publisher.py b/src/sh_edraft/publish/publisher.py index ab1fd5a5..75c90ce4 100644 --- a/src/sh_edraft/publish/publisher.py +++ b/src/sh_edraft/publish/publisher.py @@ -1,22 +1,199 @@ -from typing import List +import os +import shutil +from string import Template as stringTemplate -from sh_edraft.common.interface.ipublisher import IPublisher +from sh_edraft.publish.base.publisher_base import PublisherBase from sh_edraft.publish.model.template import Template -class Publisher(IPublisher): +class Publisher(PublisherBase): - def __init__(self, local_path: str): - super().__init__(local_path) - self._local_path = local_path - self._templates: List[Template] = [] + def __init__(self, source_path: str, dist_path: str, settings: list[Template]): + super().__init__(source_path, dist_path, settings) + + self._included_files: list[str] = [] + self._excluded_files: list[str] = [] + + self._template_ending = '_template.txt' @property - def local_path(self) -> str: - return self._local_path + def source_path(self) -> str: + return self._source_path - def create(self, templates: List[Template]): - self._templates = templates + @property + def dist_path(self): + return self._dist_path + + def _get_template_output(self, t: Template, name: str, imports: str) -> str: + try: + if t.file_content == '': + raise Exception(f'Template is empty: {t.template_path}') + + return stringTemplate(t.file_content).substitute( + Name=name, + Description=t.description, + LongDescription=t.long_description, + CopyrightDate=t.copyright_date, + CopyrightName=t.copyright_name, + LicenseName=t.license_name, + LicenseDescription=t.license_description, + Title=t.title if t.title is not None and t.title != '' else name, + Author=t.author, + Version=t.version.to_str(), + Major=t.version.major, + Minor=t.version.minor, + Micro=t.version.micro, + Imports=imports + ) + except Exception as e: + print(1, e) + # todo: better logging + + def _read_source_path(self): + for r, d, f in os.walk(self._source_path): + for file in f: + if file.endswith('.py') or file in self._included_files: + self._included_files.append(os.path.join(r, file)) + + def _read_templates(self): + for t in self._settings: + output_template: str = '' + if not os.path.isfile(t.template_path): + raise Exception(f'Template not found: {t.template_path}') + + with open(t.template_path) as template: + t.file_content = template.read() + template.close() + if t.file_content == '': + raise Exception(f'Template is empty: {t.template_path}') + + def _create_dist_path(self): + if os.path.isdir(self._dist_path): + try: + shutil.rmtree(self._dist_path) + print(f'Deleted {self._dist_path}') + # todo: better logging + except Exception as e: + print(e) + # todo: log error + + if not os.path.isdir(self._dist_path): + try: + os.makedirs(self._dist_path) + except Exception as e: + print(e) + # todo: log error + + def _get_template_name_from_dirs(self, file: str) -> str: + dirs = os.path.dirname(file).split('/') + for d in dirs: + if d.__contains__('.'): + dirs.remove(d) + + if len(dirs) == 0: + return os.path.basename(file) + else: + return '.'.join(dirs) + + def _write_templates(self): + for template in self._settings: + for file in self._included_files: + if os.path.basename(file) == '__init__.py' and file not in self._excluded_files: + template_name = template.name + if template.name == '*' or template.name == '': + template_name = self._get_template_name_from_dirs(file) + else: + name = self._get_template_name_from_dirs(file) + + if name.__contains__('.'): + if template.name != name.split('.')[len(name.split('.')) - 1]: + break + + else: + if template.name != name: + break + + try: + module_file_lines: list[str] = [] + module_py_lines: list[str] = [] + imports = '' + with open(file, 'r') as py_file: + module_file_lines = py_file.readlines() + py_file.close() + + if len(module_file_lines) == 0: + with open(file, 'w+') as py_file: + py_file.write(self._get_template_output(template, template_name, '# imports:')) + py_file.close() + print(f'Written to {file}') + else: + is_started = False + for line in module_file_lines: + if line.__contains__('# imports'): + is_started = True + + if (line.__contains__('from') or line.__contains__('import')) and is_started: + module_py_lines.append(line.replace('\n', '')) + + if len(module_py_lines) > 0: + imports = '\n'.join(module_py_lines) + + with open(file, 'w+') as py_file: + py_file.write(self._get_template_output(template, template_name, imports)) + py_file.close() + print(f'Written to {file}') + + except Exception as e: + print(e) + # todo: better logging + + def _copy_all_included_files(self): + dist_path = self._dist_path + if self._dist_path.endswith('/'): + dist_path = dist_path[:len(dist_path) - 1] + + for file in self._included_files: + if file not in self._excluded_files: + output_file = '' + if file.startswith('..'): + output_file = file.replace('..', '', 1) + + elif file.startswith('.'): + output_file = file.replace('.', '', 1) + + output_file = f'{dist_path}{output_file}' + output_path = os.path.dirname(output_file) + + try: + if not os.path.isdir(output_path): + os.makedirs(output_path) + except Exception as e: + print(e) + # todo: better logging + + try: + shutil.copy(file, output_file) + except Exception as e: + print(e) + # todo: better logging + + print(f'Copied {file} to {output_path}') + # todo: better logging + + def include(self, path: str): + self._included_files.append(path) + + def exclude(self, path: str): + self._excluded_files.append(path) + + def create(self): + if not self._dist_path.endswith('/'): + self._dist_path += '/' + + self._read_source_path() + self._read_templates() + self._create_dist_path() def publish(self): - print(self._local_path, [(t.name, t.path) for t in self._templates]) + self._write_templates() + self._copy_all_included_files() diff --git a/src/sh_edraft/service/__init__.py b/src/sh_edraft/service/__init__.py index e69de29b..070b67c3 100644 --- a/src/sh_edraft/service/__init__.py +++ b/src/sh_edraft/service/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.service +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.service' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/source_code/__init__.py b/src/sh_edraft/source_code/__init__.py new file mode 100644 index 00000000..43c2d77d --- /dev/null +++ b/src/sh_edraft/source_code/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.source_code +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.source_code' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + + + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/source_code/model/__init__.py b/src/sh_edraft/source_code/model/__init__.py new file mode 100644 index 00000000..a37844ed --- /dev/null +++ b/src/sh_edraft/source_code/model/__init__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +""" +sh_edraft.source_code.model +~~~~~~~~~~~~~~~~~~~ + + + +:copyright: (c) 2020 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'sh_edraft.source_code.model' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 sh-edraft.de' +__version__ = '2020.12.0.1' + +from collections import namedtuple + +# imports: +from sh_edraft.source_code.model.version import Version +from sh_edraft.source_code.model.version_enum import VersionEnum + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2020, minor=12, micro=0.1) diff --git a/src/sh_edraft/source_code/model/version.py b/src/sh_edraft/source_code/model/version.py new file mode 100644 index 00000000..c9ccfeb0 --- /dev/null +++ b/src/sh_edraft/source_code/model/version.py @@ -0,0 +1,44 @@ +from typing import Optional + +from sh_edraft.source_code.model.version_enum import VersionEnum +from sh_edraft.configuration.model.configuration_model_base import ConfigurationModelBase + + +class Version(ConfigurationModelBase): + + def __init__( + self, + major: int = None, + minor: int = None, + micro: float = None + ): + self._major: Optional[int] = major + self._minor: Optional[int] = minor + self._micro: Optional[float] = micro + + @property + def major(self) -> int: + return self._major + + @property + def minor(self) -> int: + return self._minor + + @property + def micro(self) -> float: + return self._micro + + def to_str(self) -> str: + return f'{self._major}.{self._minor}.{self._micro}' + + def from_dict(self, settings: dict): + self._major = int(settings[VersionEnum.Major.value]) + self._minor = int(settings[VersionEnum.Minor.value]) + self._micro = float(settings[VersionEnum.Micro.value]) + + def to_dict(self) -> dict: + return { + VersionEnum.Major.value: self._major, + VersionEnum.Minor.value: self._minor, + VersionEnum.Micro.value: self._micro + } diff --git a/src/sh_edraft/source_code/model/version_enum.py b/src/sh_edraft/source_code/model/version_enum.py new file mode 100644 index 00000000..446b6e16 --- /dev/null +++ b/src/sh_edraft/source_code/model/version_enum.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class VersionEnum(Enum): + + Major = 'major' + Minor = 'minor' + Micro = 'micro' diff --git a/src/test.py b/src/test.py index aed60e3b..6bc8e8d0 100644 --- a/src/test.py +++ b/src/test.py @@ -1,10 +1,42 @@ -from sh_edraft.publish.model.template import Template -from sh_edraft.publish.publisher import Publisher +from sh_edraft.source_code.model import Version +from sh_edraft.publish import Publisher +from sh_edraft.publish.model import Template if __name__ == '__main__': - publisher = Publisher('./') templates = [ - Template('*', '../docs/init_template.txt') + Template( + '../publish_templates/*_template.txt', + '*', + '', + '', + '2020', + 'sh-edraft.de', + 'MIT', + ', see LICENSE for more details.', + '', + 'Sven Heidemann', + Version(2020, 12, 0.1).to_dict() + ), + Template( + '../publish_templates/*_template.txt', + 'sh_edraft', + 'common python library', + 'Library to share common classes and models used at sh-edraft.de', + '2020', + 'sh-edraft.de', + 'MIT', + ', see LICENSE for more details.', + '', + 'Sven Heidemann', + Version(2020, 12, 0.1).to_dict() + ) ] - publisher.create(templates) + + publisher = Publisher('./', '../dist', templates) + + publisher.exclude('./test.py') + publisher.include('../LICENSE') + publisher.include('../README.md') + + publisher.create() publisher.publish()