Updated docs

This commit is contained in:
2023-02-20 15:55:20 +01:00
parent 48d0daabf5
commit 9e28dce5ce
632 changed files with 10917 additions and 6775 deletions

View File

@@ -1,21 +1,21 @@
# -*- coding: utf-8 -*-
"""
cpl-core sh-edraft Common Python library
cpl-core CPL core
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library
CPL core package
:copyright: (c) 2020 - 2023 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_core.utils'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2023 sh-edraft.de'
__version__ = '2022.12.1'
__title__ = "cpl_core.utils"
__author__ = "Sven Heidemann"
__license__ = "MIT"
__copyright__ = "Copyright (c) 2020 - 2023 sh-edraft.de"
__version__ = "2023.2.0"
from collections import namedtuple
@@ -25,5 +25,5 @@ from .credential_manager import CredentialManager
from .string import String
from .pip import Pip
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='1')
VersionInfo = namedtuple("VersionInfo", "major minor micro")
version_info = VersionInfo(major="2023", minor="2", micro="0")

View File

@@ -8,46 +8,39 @@ class CredentialManager:
def encrypt(string: str) -> str:
r"""Encode with base64
Parameter
---------
Parameter:
string: :class:`str`
String to encode
Returns
-------
Returns:
Encoded string
"""
return base64.b64encode(string.encode('utf-8')).decode('utf-8')
return base64.b64encode(string.encode("utf-8")).decode("utf-8")
@staticmethod
def decrypt(string: str) -> str:
r"""Decode with base64
Parameter
---------
Parameter:
string: :class:`str`
String to decode
Returns
-------
Returns:
Decoded string
"""
return base64.b64decode(string).decode('utf-8')
return base64.b64decode(string).decode("utf-8")
@staticmethod
def build_string(string: str, credentials: str):
r"""Builds string with credentials in it
Parameter
---------
Parameter:
string: :class:`str`
String in which the variable is replaced by credentials
credentials: :class:`str`
String to encode
Returns
-------
Returns:
Decoded string
"""
return string.replace('$credentials', CredentialManager.decrypt(credentials))
return string.replace("$credentials", CredentialManager.decrypt(credentials))

View File

@@ -24,8 +24,7 @@ class Pip:
def set_executable(cls, executable: str):
r"""Sets the executable
Parameter
---------
Parameter:
executable: :class:`str`
The python command
"""
@@ -38,11 +37,11 @@ class Pip:
path = os.path.dirname(os.path.dirname(cls._executable))
cls._env = os.environ
if sys.platform == 'win32':
cls._env['PATH'] = f'{path}\\bin' + os.pathsep + os.environ.get('PATH', '')
if sys.platform == "win32":
cls._env["PATH"] = f"{path}\\bin" + os.pathsep + os.environ.get("PATH", "")
else:
cls._env['PATH'] = f'{path}/bin' + os.pathsep + os.environ.get('PATH', '')
cls._env['VIRTUAL_ENV'] = path
cls._env["PATH"] = f"{path}/bin" + os.pathsep + os.environ.get("PATH", "")
cls._env["VIRTUAL_ENV"] = path
@classmethod
def reset_executable(cls):
@@ -55,26 +54,21 @@ class Pip:
def get_package(cls, package: str) -> Optional[str]:
r"""Gets given package py local pip list
Parameter
---------
Parameter:
package: :class:`str`
Returns
-------
Returns:
The package name as string
"""
result = None
with suppress(Exception):
args = [cls._executable, "-m", "pip", "freeze", "--all"]
result = subprocess.check_output(
args,
stderr=subprocess.DEVNULL, env=cls._env
)
result = subprocess.check_output(args, stderr=subprocess.DEVNULL, env=cls._env)
if result is None:
return None
for p in str(result.decode()).split('\n'):
for p in str(result.decode()).split("\n"):
if p.startswith(package):
return p
@@ -84,8 +78,7 @@ class Pip:
def get_outdated(cls) -> bytes:
r"""Gets table of outdated packages
Returns
-------
Returns:
Bytes string of the command result
"""
args = [cls._executable, "-m", "pip", "list", "--outdated"]
@@ -96,8 +89,7 @@ class Pip:
def install(cls, package: str, *args, source: str = None, stdout=None, stderr=None):
r"""Installs given package
Parameter
---------
Parameter:
package: :class:`str`
The name of the package
args: :class:`list`
@@ -117,7 +109,7 @@ class Pip:
pip_args.append(package)
if source is not None:
pip_args.append(f'--extra-index-url')
pip_args.append(f"--extra-index-url")
pip_args.append(source)
subprocess.run(pip_args, stdout=stdout, stderr=stderr, env=cls._env)
@@ -126,8 +118,7 @@ class Pip:
def uninstall(cls, package: str, stdout=None, stderr=None):
r"""Uninstalls given package
Parameter
---------
Parameter:
package: :class:`str`
The name of the package
stdout: :class:`str`
@@ -137,7 +128,4 @@ class Pip:
"""
args = [cls._executable, "-m", "pip", "uninstall", "--yes", package]
subprocess.run(
args,
stdout=stdout, stderr=stderr, env=cls._env
)
subprocess.run(args, stdout=stdout, stderr=stderr, env=cls._env)

View File

@@ -10,20 +10,18 @@ class String:
def convert_to_camel_case(chars: str) -> str:
r"""Converts string to camel case
Parameter
---------
Parameter:
chars: :class:`str`
String to convert
Returns
-------
Returns:
String converted to CamelCase
"""
converted_name = chars
char_set = string.punctuation + ' '
char_set = string.punctuation + " "
for char in char_set:
if char in converted_name:
converted_name = ''.join(word.title() for word in converted_name.split(char))
converted_name = "".join(word.title() for word in converted_name.split(char))
return converted_name
@@ -31,61 +29,54 @@ class String:
def convert_to_snake_case(chars: str) -> str:
r"""Converts string to snake case
Parameter
---------
Parameter:
chars: :class:`str`
String to convert
Returns
-------
Returns:
String converted to snake_case
"""
# convert to train-case to CamelCase
if '-' in chars:
chars = ''.join(word.title() for word in chars.split('-'))
if "-" in chars:
chars = "".join(word.title() for word in chars.split("-"))
pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
pattern2 = re.compile(r'([a-z0-9])([A-Z])')
file_name = re.sub(pattern1, r'\1_\2', chars)
return re.sub(pattern2, r'\1_\2', file_name).lower()
pattern1 = re.compile(r"(.)([A-Z][a-z]+)")
pattern2 = re.compile(r"([a-z0-9])([A-Z])")
file_name = re.sub(pattern1, r"\1_\2", chars)
return re.sub(pattern2, r"\1_\2", file_name).lower()
@staticmethod
def first_to_upper(chars: str) -> str:
r"""Converts first char to upper
Parameter
---------
Parameter:
chars: :class:`str`
String to convert
Returns
-------
Returns:
String with first char as upper
"""
return f'{chars[0].upper()}{chars[1:]}'
return f"{chars[0].upper()}{chars[1:]}"
@staticmethod
def first_to_lower(chars: str) -> str:
r"""Converts first char to lower
Parameter
---------
Parameter:
chars: :class:`str`
String to convert
Returns
-------
Returns:
String with first char as lower
"""
return f'{chars[0].lower()}{chars[1:]}'
return f"{chars[0].lower()}{chars[1:]}"
@staticmethod
def random_string(chars: str, length: int) -> str:
r"""Creates random string by given chars and length
Returns
-------
Returns:
String of random chars
"""
return ''.join(random.choice(chars) for _ in range(length))
return "".join(random.choice(chars) for _ in range(length))