From f4ca76d84443d3d6986e964fefc09c2c87474667 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 10 Mar 2021 08:09:56 +0100 Subject: [PATCH] Improved template handling --- cpl.json | 12 +---- src/cpl_cli/publish/publisher.py | 49 ++++++++----------- src/cpl_cli/templates/__init__.py | 25 ++++++++++ src/cpl_cli/templates/build/__init__.py | 25 ++++++++++ src/cpl_cli/templates/build/init.py | 35 +++++++++++++ src/cpl_cli/templates/build/init.txt | 25 ---------- .../new/console/src/tests/__init__.py | 25 ++++++++++ src/cpl_cli/templates/publish/__init__.py | 25 ++++++++++ src/cpl_cli/templates/publish/setup.py | 31 ++++++++++++ src/cpl_cli/templates/publish/setup.txt | 21 -------- src/cpl_cli/templates/template.py | 14 ++++++ 11 files changed, 201 insertions(+), 86 deletions(-) create mode 100644 src/cpl_cli/templates/__init__.py create mode 100644 src/cpl_cli/templates/build/__init__.py create mode 100644 src/cpl_cli/templates/build/init.py delete mode 100644 src/cpl_cli/templates/build/init.txt create mode 100644 src/cpl_cli/templates/publish/__init__.py create mode 100644 src/cpl_cli/templates/publish/setup.py delete mode 100644 src/cpl_cli/templates/publish/setup.txt create mode 100644 src/cpl_cli/templates/template.py diff --git a/cpl.json b/cpl.json index 989f10af..b251bfca 100644 --- a/cpl.json +++ b/cpl.json @@ -39,16 +39,6 @@ "*/logs", "*/tests" ], - "PackageData": { - "cpl_cli": [ - "templates/*", - "templates/build/*", - "templates/new/*", - "templates/new/console/*", - "templates/new/console/src/*", - "templates/new/console/src/tests/*", - "templates/publish/*" - ] - } + "PackageData": {} } } \ No newline at end of file diff --git a/src/cpl_cli/publish/publisher.py b/src/cpl_cli/publish/publisher.py index 7382ba7c..0b0aa09c 100644 --- a/src/cpl_cli/publish/publisher.py +++ b/src/cpl_cli/publish/publisher.py @@ -11,6 +11,8 @@ from cpl.console.console import Console from cpl_cli.configuration.build_settings import BuildSettings from cpl_cli.configuration.project_settings import ProjectSettings from cpl_cli.publish.publisher_abc import PublisherABC +from cpl_cli.templates.build.init import Init +from cpl_cli.templates.publish.setup import Setup class Publisher(PublisherABC): @@ -113,7 +115,7 @@ class Publisher(PublisherABC): def _create_packages(self): for file in self._included_files: - if file.endswith('console_src_tests.__init__.py'): + if file.endswith('__init__.py'): template_content = '' module_file_lines: list[str] = [] @@ -144,23 +146,22 @@ class Publisher(PublisherABC): if len(module_py_lines) > 0: imports = '\n'.join(module_py_lines) - with open(os.path.join(self._runtime.runtime_directory, 'templates/build/init.txt'), 'r') as template: - template_content = stringTemplate(template.read()).substitute( - Name=self._project_settings.name, - Description=self._project_settings.description, - LongDescription=self._project_settings.long_description, - CopyrightDate=self._project_settings.copyright_date, - CopyrightName=self._project_settings.copyright_name, - LicenseName=self._project_settings.license_name, - LicenseDescription=self._project_settings.license_description, - Title=title if title is not None and title != '' else self._project_settings.name, - Author=self._project_settings.author, - Version=self._project_settings.version.to_str(), - Major=self._project_settings.version.major, - Minor=self._project_settings.version.minor, - Micro=self._project_settings.version.micro, - Imports=imports - ) + template_content = stringTemplate(Init.get_init_py()).substitute( + Name=self._project_settings.name, + Description=self._project_settings.description, + LongDescription=self._project_settings.long_description, + CopyrightDate=self._project_settings.copyright_date, + CopyrightName=self._project_settings.copyright_name, + LicenseName=self._project_settings.license_name, + LicenseDescription=self._project_settings.license_description, + Title=title if title is not None and title != '' else self._project_settings.name, + Author=self._project_settings.author, + Version=self._project_settings.version.to_str(), + Major=self._project_settings.version.major, + Minor=self._project_settings.version.minor, + Micro=self._project_settings.version.micro, + Imports=imports + ) with open(file, 'w+') as py_file: py_file.write(template_content) @@ -227,16 +228,6 @@ class Publisher(PublisherABC): if os.path.isfile(setup_file): os.remove(setup_file) - template_path = os.path.join(self._runtime.runtime_directory, 'templates/publish/setup.txt') - if not os.path.isfile(template_path): - Console.error(__name__, f'setup.py template not found in {template_path}') - return - - template_string = '' - with open(template_path, 'r') as template_file: - template_string = template_file.read() - template_file.close() - main = None try: main = importlib.import_module(self._build_settings.main) @@ -248,7 +239,7 @@ class Publisher(PublisherABC): return with open(setup_file, 'w+') as setup_py: - setup_string = stringTemplate(template_string).substitute( + setup_string = stringTemplate(Setup.get_setup_py()).substitute( Name=self._project_settings.name, Version=self._project_settings.version.to_str(), Packages=setuptools.find_packages(where=self._output_path, exclude=self._build_settings.excluded), diff --git a/src/cpl_cli/templates/__init__.py b/src/cpl_cli/templates/__init__.py new file mode 100644 index 00000000..3bdf0291 --- /dev/null +++ b/src/cpl_cli/templates/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_cpl sh-edraft Common Python library +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library + +:copyright: (c) 2020 - 2021 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_cli.templates' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de' +__version__ = '2021.4.1' + +from collections import namedtuple + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2021, minor=4, micro=1) diff --git a/src/cpl_cli/templates/build/__init__.py b/src/cpl_cli/templates/build/__init__.py new file mode 100644 index 00000000..da9f2c97 --- /dev/null +++ b/src/cpl_cli/templates/build/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_cpl sh-edraft Common Python library +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library + +:copyright: (c) 2020 - 2021 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_cli.templates.build' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de' +__version__ = '2021.4.1' + +from collections import namedtuple + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2021, minor=4, micro=1) diff --git a/src/cpl_cli/templates/build/init.py b/src/cpl_cli/templates/build/init.py new file mode 100644 index 00000000..44f43e57 --- /dev/null +++ b/src/cpl_cli/templates/build/init.py @@ -0,0 +1,35 @@ +from cpl_cli.templates.template import Template + + +class Init: + + @staticmethod + def get_init_py() -> str: + string = """# -*- coding: utf-8 -*- + + \"\"\" + $Name $Description + ~~~~~~~~~~~~~~~~~~~ + + $LongDescription + + :copyright: (c) $CopyrightDate $CopyrightName + :license: $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) + """ + + return Template.build_template_string(string) diff --git a/src/cpl_cli/templates/build/init.txt b/src/cpl_cli/templates/build/init.txt deleted file mode 100644 index 7e1ac713..00000000 --- a/src/cpl_cli/templates/build/init.txt +++ /dev/null @@ -1,25 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -$Name $Description -~~~~~~~~~~~~~~~~~~~ - -$LongDescription - -:copyright: (c) $CopyrightDate $CopyrightName -:license: $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/cpl_cli/templates/new/console/src/tests/__init__.py b/src/cpl_cli/templates/new/console/src/tests/__init__.py index e69de29b..0d1131c8 100644 --- a/src/cpl_cli/templates/new/console/src/tests/__init__.py +++ b/src/cpl_cli/templates/new/console/src/tests/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_cpl sh-edraft Common Python library +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library + +:copyright: (c) 2020 - 2021 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_cli.templates.new.console.src.tests' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de' +__version__ = '2021.4.1' + +from collections import namedtuple + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2021, minor=4, micro=1) diff --git a/src/cpl_cli/templates/publish/__init__.py b/src/cpl_cli/templates/publish/__init__.py new file mode 100644 index 00000000..260f196c --- /dev/null +++ b/src/cpl_cli/templates/publish/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +""" +sh_cpl sh-edraft Common Python library +~~~~~~~~~~~~~~~~~~~ + +sh-edraft Common Python library + +:copyright: (c) 2020 - 2021 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'cpl_cli.templates.publish' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de' +__version__ = '2021.4.1' + +from collections import namedtuple + +# imports: + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major=2021, minor=4, micro=1) diff --git a/src/cpl_cli/templates/publish/setup.py b/src/cpl_cli/templates/publish/setup.py new file mode 100644 index 00000000..f03b63af --- /dev/null +++ b/src/cpl_cli/templates/publish/setup.py @@ -0,0 +1,31 @@ +from cpl_cli.templates.template import Template + + +class Setup: + + @staticmethod + def get_setup_py() -> str: + string = """\"\"\" + This file is generated by CPL CLI + \"\"\" + + import setuptools + + setuptools.setup( + name='$Name', + version='$Version', + packages=$Packages, + url='$URL', + license='$LicenseName', + author='$Author', + author_email='$AuthorMail', + include_package_data=$IncludePackageData, + description='$Description', + python_requires='$PyRequires', + install_requires=$Dependencies, + entry_points=$EntryPoints, + package_data=$PackageData + ) + """ + + return Template.build_template_string(string) diff --git a/src/cpl_cli/templates/publish/setup.txt b/src/cpl_cli/templates/publish/setup.txt deleted file mode 100644 index 90113dff..00000000 --- a/src/cpl_cli/templates/publish/setup.txt +++ /dev/null @@ -1,21 +0,0 @@ -""" - This file is generated by CPL CLI -""" - -import setuptools - -setuptools.setup( - name='$Name', - version='$Version', - packages=$Packages, - url='$URL', - license='$LicenseName', - author='$Author', - author_email='$AuthorMail', - include_package_data=$IncludePackageData, - description='$Description', - python_requires='$PyRequires', - install_requires=$Dependencies, - entry_points=$EntryPoints, - package_data=$PackageData -) diff --git a/src/cpl_cli/templates/template.py b/src/cpl_cli/templates/template.py new file mode 100644 index 00000000..fb1ee429 --- /dev/null +++ b/src/cpl_cli/templates/template.py @@ -0,0 +1,14 @@ +class Template: + + @staticmethod + def build_template_string(string: str) -> str: + return_value = '' + for i in range(0, len(string.splitlines())): + line = string.splitlines()[i] + if i == len(string.splitlines())-1: + return_value += f'{line.strip()}' + break + + return_value += f'{line.strip()}\n' + + return return_value