Refactored code
This commit is contained in:
30
src_old/sh_edraft/publish/base/publisher_base.py
Normal file
30
src_old/sh_edraft/publish/base/publisher_base.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class PublisherBase(ServiceBase):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def source_path(self) -> str: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def dist_path(self) -> str: pass
|
||||
|
||||
@abstractmethod
|
||||
def include(self, path: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def exclude(self, path: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def build(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def publish(self): pass
|
90
src_old/sh_edraft/publish/model/publish_settings_model.py
Normal file
90
src_old/sh_edraft/publish/model/publish_settings_model.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import traceback
|
||||
from typing import Optional
|
||||
|
||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.publish.model.template import Template
|
||||
from sh_edraft.publish.model.publish_settings_name import PublishSettingsName
|
||||
from sh_edraft.console.console import Console
|
||||
from sh_edraft.console.model.foreground_color import ForegroundColor
|
||||
|
||||
|
||||
class PublishSettings(ConfigurationModelBase):
|
||||
|
||||
def __init__(self):
|
||||
ConfigurationModelBase.__init__(self)
|
||||
|
||||
self._source_path: Optional[str] = None
|
||||
self._dist_path: Optional[str] = None
|
||||
self._templates: list[Template] = []
|
||||
|
||||
self._included_files: list[str] = []
|
||||
self._excluded_files: list[str] = []
|
||||
|
||||
self._template_ending: Optional[str] = None
|
||||
|
||||
@property
|
||||
def source_path(self) -> str:
|
||||
return self._source_path
|
||||
|
||||
@source_path.setter
|
||||
def source_path(self, source_path: str):
|
||||
self._source_path = source_path
|
||||
|
||||
@property
|
||||
def dist_path(self) -> str:
|
||||
return self._dist_path
|
||||
|
||||
@dist_path.setter
|
||||
def dist_path(self, dist_path: str):
|
||||
self._dist_path = dist_path
|
||||
|
||||
@property
|
||||
def templates(self) -> list[Template]:
|
||||
return self._templates
|
||||
|
||||
@templates.setter
|
||||
def templates(self, templates: list[Template]):
|
||||
self._templates = templates
|
||||
|
||||
@property
|
||||
def included_files(self) -> list[str]:
|
||||
return self._included_files
|
||||
|
||||
@included_files.setter
|
||||
def included_files(self, included_files: list[str]):
|
||||
self._included_files = included_files
|
||||
|
||||
@property
|
||||
def excluded_files(self) -> list[str]:
|
||||
return self._excluded_files
|
||||
|
||||
@excluded_files.setter
|
||||
def excluded_files(self, excluded_files: list[str]):
|
||||
self._excluded_files = excluded_files
|
||||
|
||||
@property
|
||||
def template_ending(self) -> str:
|
||||
return self._template_ending
|
||||
|
||||
@template_ending.setter
|
||||
def template_ending(self, template_ending: str):
|
||||
self._template_ending = template_ending
|
||||
|
||||
def from_dict(self, settings: dict):
|
||||
try:
|
||||
self._source_path = settings[PublishSettingsName.source_path.value]
|
||||
self._dist_path = settings[PublishSettingsName.dist_path.value]
|
||||
for template in settings[PublishSettingsName.templates.value]:
|
||||
temp = Template()
|
||||
temp.from_dict(template)
|
||||
self._templates.append(temp)
|
||||
|
||||
self._included_files = settings[PublishSettingsName.included_files.value]
|
||||
self._excluded_files = settings[PublishSettingsName.excluded_files.value]
|
||||
self._template_ending = settings[PublishSettingsName.template_ending.value]
|
||||
except Exception as e:
|
||||
Console.set_foreground_color(ForegroundColor.red)
|
||||
Console.write_line(
|
||||
f'[ ERROR ] [ {__name__} ]: Reading error in {PublishSettingsName.publish.value} settings')
|
||||
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
|
||||
Console.set_foreground_color(ForegroundColor.default)
|
12
src_old/sh_edraft/publish/model/publish_settings_name.py
Normal file
12
src_old/sh_edraft/publish/model/publish_settings_name.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class PublishSettingsName(Enum):
|
||||
|
||||
publish = 'Publish'
|
||||
source_path = 'SourcePath'
|
||||
dist_path = 'DistPath'
|
||||
templates = 'Templates'
|
||||
included_files = 'IncludedFiles'
|
||||
excluded_files = 'ExcludedFiles'
|
||||
template_ending = 'TemplateEnding'
|
122
src_old/sh_edraft/publish/model/template.py
Normal file
122
src_old/sh_edraft/publish/model/template.py
Normal file
@@ -0,0 +1,122 @@
|
||||
from typing import Optional
|
||||
|
||||
from sh_edraft.coding.model.version import Version
|
||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.publish.model.template_enum import TemplateEnum
|
||||
|
||||
|
||||
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[Version] = Version()
|
||||
):
|
||||
ConfigurationModelBase.__init__(self)
|
||||
self._template_path: Optional[str] = template_path
|
||||
self._name: Optional[str] = name
|
||||
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._file_content: Optional[str] = None
|
||||
|
||||
@property
|
||||
def template_path(self) -> Optional[str]:
|
||||
return self._template_path
|
||||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
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
|
||||
}
|
16
src_old/sh_edraft/publish/model/template_enum.py
Normal file
16
src_old/sh_edraft/publish/model/template_enum.py
Normal file
@@ -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'
|
279
src_old/sh_edraft/publish/publisher.py
Normal file
279
src_old/sh_edraft/publish/publisher.py
Normal file
@@ -0,0 +1,279 @@
|
||||
import os
|
||||
import shutil
|
||||
from string import Template as stringTemplate
|
||||
|
||||
from setuptools import sandbox
|
||||
|
||||
from sh_edraft.logging.base.logger_base import LoggerBase
|
||||
from sh_edraft.publish.base.publisher_base import PublisherBase
|
||||
from sh_edraft.publish.model.publish_settings_model import PublishSettings
|
||||
from sh_edraft.publish.model.template import Template
|
||||
|
||||
|
||||
class Publisher(PublisherBase):
|
||||
|
||||
def __init__(self, logger: LoggerBase, publish_settings: PublishSettings):
|
||||
PublisherBase.__init__(self)
|
||||
|
||||
self._logger: LoggerBase = logger
|
||||
self._publish_settings: PublishSettings = publish_settings
|
||||
|
||||
self._included_files: list[str] = []
|
||||
|
||||
@property
|
||||
def source_path(self) -> str:
|
||||
return self._publish_settings.source_path
|
||||
|
||||
@property
|
||||
def dist_path(self):
|
||||
return self._publish_settings.dist_path
|
||||
|
||||
def _get_template_output(self, t: Template, name: str, imports: str) -> str:
|
||||
self._logger.trace(__name__, f'Started {__name__}._get_template_output')
|
||||
try:
|
||||
if t.file_content == '':
|
||||
raise Exception(f'Template is empty: {t.template_path}')
|
||||
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._get_template_output')
|
||||
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:
|
||||
self._logger.fatal(__name__, f'Cannot read Template: {t.template_path}', e)
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._get_template_output')
|
||||
|
||||
def _read_source_path(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}._read_source_path')
|
||||
included_files = self._publish_settings.included_files
|
||||
for included in included_files:
|
||||
if os.path.isdir(included):
|
||||
self._publish_settings.included_files.remove(included)
|
||||
|
||||
for r, d, f in os.walk(included):
|
||||
for file in f:
|
||||
rel_path = os.path.relpath(r)
|
||||
if not rel_path.startswith('.'):
|
||||
rel_path = f'./{rel_path}'
|
||||
|
||||
file_path = os.path.join(self._publish_settings.source_path, r, file)
|
||||
if os.path.isfile(file_path):
|
||||
self._included_files.append(file_path)
|
||||
elif os.path.isfile(os.path.join(rel_path, file)):
|
||||
self._included_files.append(os.path.join(rel_path, file))
|
||||
else:
|
||||
self._logger.fatal(__name__, f'File not found: {file}')
|
||||
|
||||
elif os.path.isfile(included):
|
||||
self._included_files.append(included)
|
||||
else:
|
||||
self._logger.fatal(__name__, f'File not found: {included}')
|
||||
|
||||
for r, d, f in os.walk(self._publish_settings.source_path):
|
||||
for file in f:
|
||||
is_file_excluded = False
|
||||
if os.path.join(r, file) in self._publish_settings.excluded_files:
|
||||
is_file_excluded = True
|
||||
else:
|
||||
for excluded in self._publish_settings.excluded_files:
|
||||
if os.path.join(r, file).__contains__(excluded):
|
||||
is_file_excluded = True
|
||||
|
||||
if not is_file_excluded and file.endswith('.py') or file in self._publish_settings.included_files:
|
||||
self._included_files.append(os.path.join(r, file))
|
||||
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._read_source_path')
|
||||
|
||||
def _read_templates(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}._read_templates')
|
||||
for t in self._publish_settings.templates:
|
||||
output_template: str = ''
|
||||
if not os.path.isfile(t.template_path):
|
||||
self._logger.fatal(__name__, 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 == '':
|
||||
self._logger.fatal(__name__, f'Template is empty: {t.template_path}')
|
||||
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._read_templates')
|
||||
|
||||
def _create_dist_path(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}._create_dist_path')
|
||||
if os.path.isdir(self._publish_settings.dist_path):
|
||||
try:
|
||||
shutil.rmtree(self._publish_settings.dist_path)
|
||||
self._logger.info(__name__, f'Deleted {self._publish_settings.dist_path}')
|
||||
except Exception as e:
|
||||
self._logger.fatal(__name__, f'Cannot delete old dist directory', e)
|
||||
|
||||
if not os.path.isdir(self._publish_settings.dist_path):
|
||||
try:
|
||||
os.makedirs(self._publish_settings.dist_path)
|
||||
self._logger.debug(__name__, f'Created directories: {self._publish_settings.dist_path}')
|
||||
self._logger.info(__name__, f'Created dist directory')
|
||||
except Exception as e:
|
||||
self._logger.fatal(__name__, f'Cannot create dist directory', e)
|
||||
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._create_dist_path')
|
||||
|
||||
@staticmethod
|
||||
def _get_template_name_from_dirs(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):
|
||||
self._logger.trace(__name__, f'Started {__name__}._write_templates')
|
||||
for template in self._publish_settings.templates:
|
||||
for file in self._included_files:
|
||||
if os.path.basename(file) == '__init__.py' and file not in self._publish_settings.excluded_files:
|
||||
template_name = template.name
|
||||
if template.name == 'all' 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]:
|
||||
continue
|
||||
|
||||
else:
|
||||
if template.name != name:
|
||||
continue
|
||||
|
||||
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()
|
||||
self._logger.debug(__name__, 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()
|
||||
self._logger.debug(__name__, f'Written to {file}')
|
||||
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot write to file: {file}', e)
|
||||
|
||||
self._logger.info(__name__, f'Written to all included modules')
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._write_templates')
|
||||
|
||||
def _copy_all_included_files(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}._copy_all_included_files')
|
||||
dist_path = self._publish_settings.dist_path
|
||||
if self._publish_settings.dist_path.endswith('/'):
|
||||
dist_path = dist_path[:len(dist_path) - 1]
|
||||
|
||||
for file in self._included_files:
|
||||
is_file_excluded = False
|
||||
if file in self._publish_settings.excluded_files:
|
||||
is_file_excluded = True
|
||||
else:
|
||||
for excluded in self._publish_settings.excluded_files:
|
||||
if file.__contains__(excluded):
|
||||
is_file_excluded = True
|
||||
|
||||
if not is_file_excluded:
|
||||
output_file = ''
|
||||
|
||||
if file.startswith('..'):
|
||||
output_file = file.replace('..', '')
|
||||
elif file.startswith('.'):
|
||||
output_file = file.replace('.', '', 1)
|
||||
|
||||
if output_file.__contains__('..'):
|
||||
output_file = os.path.join(dist_path, os.path.basename(file))
|
||||
else:
|
||||
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, exist_ok=True)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot create directories: {output_path}', e)
|
||||
|
||||
try:
|
||||
shutil.copy(file, output_file)
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f'Cannot copy file: {file} to {output_path}', e)
|
||||
|
||||
self._logger.debug(__name__, f'Copied {file} to {output_path}')
|
||||
|
||||
self._logger.info(__name__, f'Copied all included files')
|
||||
self._logger.trace(__name__, f'Stopped {__name__}._copy_all_included_files')
|
||||
|
||||
def include(self, path: str):
|
||||
self._logger.trace(__name__, f'Started {__name__}.include')
|
||||
self._publish_settings.included_files.append(path)
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.include')
|
||||
|
||||
def exclude(self, path: str):
|
||||
self._logger.trace(__name__, f'Started {__name__}.exclude')
|
||||
self._publish_settings.excluded_files.append(path)
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.exclude')
|
||||
|
||||
def create(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}.create')
|
||||
if not self._publish_settings.dist_path.endswith('/'):
|
||||
self._publish_settings.dist_path += '/'
|
||||
|
||||
self._read_source_path()
|
||||
self._read_templates()
|
||||
self._create_dist_path()
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.create')
|
||||
|
||||
def build(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}.build')
|
||||
self._write_templates()
|
||||
self._copy_all_included_files()
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.build')
|
||||
|
||||
def publish(self):
|
||||
self._logger.trace(__name__, f'Started {__name__}.publish')
|
||||
setup_py = os.path.join(self._publish_settings.dist_path, 'setup.py')
|
||||
if not os.path.isfile(setup_py):
|
||||
self._logger.fatal(__name__, f'setup.py not found in {self._publish_settings.dist_path}')
|
||||
|
||||
sandbox.run_setup(os.path.abspath(setup_py), ['sdist', 'bdist_wheel'])
|
||||
self._logger.trace(__name__, f'Stopped {__name__}.publish')
|
Reference in New Issue
Block a user