sh_cpl/src/cpl_cli/configuration/project_settings.py

137 lines
4.9 KiB
Python
Raw Normal View History

2021-03-14 13:22:10 +01:00
import os
import sys
2021-03-04 19:06:53 +01:00
import traceback
from typing import Optional
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
from cpl.console.console import Console
2021-03-12 16:06:30 +01:00
from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl_cli.configuration.version_settings import VersionSettings
2021-03-13 22:53:28 +01:00
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
2021-03-14 14:47:53 +01:00
from cpl_cli.error import Error
2021-03-04 19:06:53 +01:00
class ProjectSettings(ConfigurationModelABC):
def __init__(self):
ConfigurationModelABC.__init__(self)
2021-03-04 21:57:18 +01:00
self._name: Optional[str] = None
2021-03-12 16:06:30 +01:00
self._version: Optional[VersionSettings] = VersionSettings()
2021-03-04 21:57:18 +01:00
self._author: Optional[str] = None
2021-03-08 20:29:08 +01:00
self._author_email: Optional[str] = None
2021-03-04 21:57:18 +01:00
self._description: Optional[str] = None
self._long_description: Optional[str] = None
2021-03-08 20:29:08 +01:00
self._url: Optional[str] = None
2021-03-04 21:57:18 +01:00
self._copyright_date: Optional[str] = None
self._copyright_name: Optional[str] = None
self._license_name: Optional[str] = None
self._license_description: Optional[str] = None
2021-03-08 20:29:08 +01:00
self._dependencies: Optional[list[str]] = None
self._python_version: Optional[str] = None
2021-03-14 13:22:10 +01:00
self._python_path: Optional[str] = None
2021-03-17 08:55:23 +01:00
self._python_executable: Optional[str] = None
2021-03-14 12:05:57 +01:00
self._classifiers: Optional[list[str]] = None
2021-03-14 13:22:10 +01:00
2021-03-04 19:06:53 +01:00
@property
2021-03-08 20:29:08 +01:00
def name(self):
2021-03-04 21:57:18 +01:00
return self._name
@property
2021-03-12 16:06:30 +01:00
def version(self) -> VersionSettings:
2021-03-08 20:29:08 +01:00
return self._version
@property
def author(self) -> str:
2021-03-04 21:57:18 +01:00
return self._author
@property
2021-03-08 20:29:08 +01:00
def author_email(self) -> str:
return self._author_email
2021-03-14 13:22:10 +01:00
2021-03-08 20:29:08 +01:00
@property
def description(self) -> str:
2021-03-04 21:57:18 +01:00
return self._description
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def long_description(self) -> str:
2021-03-04 21:57:18 +01:00
return self._long_description
2021-03-14 13:22:10 +01:00
2021-03-08 20:29:08 +01:00
@property
def url(self) -> str:
return self._url
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def copyright_date(self) -> str:
2021-03-04 21:57:18 +01:00
return self._copyright_date
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def copyright_name(self) -> str:
2021-03-04 21:57:18 +01:00
return self._copyright_name
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def license_name(self) -> str:
2021-03-04 21:57:18 +01:00
return self._license_name
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def license_description(self) -> str:
2021-03-04 21:57:18 +01:00
return self._license_description
@property
2021-03-08 20:29:08 +01:00
def dependencies(self) -> list[str]:
return self._dependencies
2021-03-14 13:22:10 +01:00
2021-03-04 21:57:18 +01:00
@property
2021-03-08 20:29:08 +01:00
def python_version(self) -> str:
return self._python_version
2021-03-04 19:06:53 +01:00
2021-03-14 13:22:10 +01:00
@property
def python_path(self) -> str:
return self._python_path
2021-03-17 08:55:23 +01:00
@property
def python_executable(self) -> str:
return self._python_executable
2021-03-14 12:05:57 +01:00
@property
def classifiers(self) -> list[str]:
return self._classifiers
2021-03-04 19:06:53 +01:00
def from_dict(self, settings: dict):
try:
2021-03-13 22:53:28 +01:00
self._name = settings[ProjectSettingsNameEnum.name.value]
self._version.from_dict(settings[ProjectSettingsNameEnum.version.value])
self._author = settings[ProjectSettingsNameEnum.author.value]
self._author_email = settings[ProjectSettingsNameEnum.author_email.value]
self._description = settings[ProjectSettingsNameEnum.description.value]
self._long_description = settings[ProjectSettingsNameEnum.long_description.value]
self._url = settings[ProjectSettingsNameEnum.url.value]
self._copyright_date = settings[ProjectSettingsNameEnum.copyright_date.value]
self._copyright_name = settings[ProjectSettingsNameEnum.copyright_name.value]
self._license_name = settings[ProjectSettingsNameEnum.license_name.value]
self._license_description = settings[ProjectSettingsNameEnum.license_description.value]
self._dependencies = settings[ProjectSettingsNameEnum.dependencies.value]
self._python_version = settings[ProjectSettingsNameEnum.python_version.value]
2021-03-17 08:55:23 +01:00
self._python_path = settings[ProjectSettingsNameEnum.python_path.value]
2021-03-14 13:22:10 +01:00
2021-03-17 08:55:23 +01:00
if ProjectSettingsNameEnum.python_path.value in settings and \
sys.platform in settings[ProjectSettingsNameEnum.python_path.value]:
path = settings[ProjectSettingsNameEnum.python_path.value][sys.platform]
2021-03-16 22:09:42 +01:00
if not os.path.isfile(path) and not os.path.islink(path):
2021-03-16 21:37:12 +01:00
if path != '' and path is not None:
2021-03-14 15:11:36 +01:00
Error.warn(f'{ProjectSettingsNameEnum.python_path.value} not found')
2021-03-14 14:47:53 +01:00
path = sys.executable
2021-03-14 13:22:10 +01:00
else:
path = sys.executable
2021-03-17 08:55:23 +01:00
self._python_executable = path
2021-03-14 13:22:10 +01:00
2021-03-14 12:05:57 +01:00
self._classifiers = settings[ProjectSettingsNameEnum.classifiers.value]
2021-03-04 19:06:53 +01:00
except Exception as e:
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.red)
2021-03-04 19:06:53 +01:00
Console.write_line(
2021-03-08 20:29:08 +01:00
f'[ ERROR ] [ {__name__} ]: Reading error in {ProjectSettings.__name__} settings')
2021-03-04 19:06:53 +01:00
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
2021-03-12 16:06:30 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)