Improved cpl g templating & added custom templating #137
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
cpl-cli sh-edraft Common Python library CLI
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Common Python library Command Line Interface
|
||||
|
||||
:copyright: (c) 2020 - 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'cpl_cli._templates.generate'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
|
||||
__version__ = '2022.12.0'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2022', minor='12', micro='0')
|
@@ -1,44 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ABCTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class $Name(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,35 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ClassTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
class $Name:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,60 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ConfigModelTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
import traceback
|
||||
|
||||
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl_core.console import Console
|
||||
|
||||
|
||||
class $Name(ConfigurationModelABC):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
self._atr = ''
|
||||
|
||||
@property
|
||||
def atr(self) -> str:
|
||||
return self._atr
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._atr = settings['atr']
|
||||
except Exception as e:
|
||||
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings')
|
||||
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,43 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class EnumTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class $Name(Enum):
|
||||
|
||||
atr = 0
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,35 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class InitTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'__init__.py'
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
# imports
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,46 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class PipeTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
from cpl_core.pipes.pipe_abc import PipeABC
|
||||
|
||||
|
||||
class $Name(PipeABC):
|
||||
|
||||
def __init__(self): pass
|
||||
|
||||
def transform(self, value: any, *args):
|
||||
return value
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,41 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ServiceTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
class $Name:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,47 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class TestCaseTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
import unittest
|
||||
|
||||
|
||||
class $Name(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_equal(self):
|
||||
pass
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,47 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ThreadTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
import threading
|
||||
|
||||
|
||||
class $Name(threading.Thread):
|
||||
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self) -> None:
|
||||
pass
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
@@ -1,47 +0,0 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
from cpl_core.utils.string import String
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
|
||||
|
||||
class ValidatorTemplate(TemplateFileABC):
|
||||
|
||||
def __init__(self, name: str, schematic: str, schematic_upper: str, path: str):
|
||||
TemplateFileABC.__init__(self)
|
||||
|
||||
self._name = f'{String.convert_to_snake_case(name)}_{schematic}.py'
|
||||
if schematic in name.lower():
|
||||
self._name = f'{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
self._class_name = f'{String.first_to_upper(name)}{schematic_upper}'
|
||||
if schematic in name.lower():
|
||||
self._class_name = f'{String.first_to_upper(name)}'
|
||||
|
||||
self._path = path
|
||||
self._value = textwrap.dedent("""\
|
||||
from cpl_core.configuration.validator_abc import ValidatorABC
|
||||
|
||||
|
||||
class $Name(ValidatorABC):
|
||||
|
||||
def __init__(self):
|
||||
ValidatorABC.__init__(self)
|
||||
|
||||
def validate(self) -> bool:
|
||||
return True
|
||||
""")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def path(self) -> str:
|
||||
return self._path
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return Template(self._value).substitute(
|
||||
Name=self._class_name
|
||||
)
|
Reference in New Issue
Block a user