Renamed project
This commit is contained in:
28
src/cpl_core/utils/__init__.py
Normal file
28
src/cpl_core/utils/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_cpl-core 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_core.utils'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de'
|
||||
__version__ = '2021.10.6'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .credential_manager import CredentialManager
|
||||
from .string import String
|
||||
from .pip import Pip
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2021', minor='10', micro='6')
|
53
src/cpl_core/utils/credential_manager.py
Normal file
53
src/cpl_core/utils/credential_manager.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import base64
|
||||
|
||||
|
||||
class CredentialManager:
|
||||
r"""Handles credential encryption and decryption"""
|
||||
|
||||
@staticmethod
|
||||
def encrypt(string: str) -> str:
|
||||
r"""Encode with base64
|
||||
|
||||
Parameter
|
||||
---------
|
||||
string: :class:`str`
|
||||
String to encode
|
||||
|
||||
Returns
|
||||
-------
|
||||
Encoded string
|
||||
"""
|
||||
return base64.b64encode(string.encode('utf-8')).decode('utf-8')
|
||||
|
||||
@staticmethod
|
||||
def decrypt(string: str) -> str:
|
||||
r"""Decode with base64
|
||||
|
||||
Parameter
|
||||
---------
|
||||
string: :class:`str`
|
||||
String to decode
|
||||
|
||||
Returns
|
||||
-------
|
||||
Decoded string
|
||||
"""
|
||||
return base64.b64decode(string).decode('utf-8')
|
||||
|
||||
@staticmethod
|
||||
def build_string(string: str, credentials: str):
|
||||
r"""Builds string with credentials in it
|
||||
|
||||
Parameter
|
||||
---------
|
||||
string: :class:`str`
|
||||
String in which the variable is replaced by credentials
|
||||
credentials: :class:`str`
|
||||
String to encode
|
||||
|
||||
Returns
|
||||
-------
|
||||
Decoded string
|
||||
"""
|
||||
return string.replace('$credentials', CredentialManager.decrypt(credentials))
|
||||
|
151
src/cpl_core/utils/pip.py
Normal file
151
src/cpl_core/utils/pip.py
Normal file
@@ -0,0 +1,151 @@
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from contextlib import suppress
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Pip:
|
||||
r"""Executes pip commands"""
|
||||
_executable = sys.executable
|
||||
_env = os.environ
|
||||
_is_venv = False
|
||||
|
||||
"""Getter"""
|
||||
@classmethod
|
||||
def get_executable(cls) -> str:
|
||||
return cls._executable
|
||||
|
||||
"""Setter"""
|
||||
@classmethod
|
||||
def set_executable(cls, executable: str):
|
||||
r"""Sets the executable
|
||||
|
||||
Parameter
|
||||
---------
|
||||
executable: :class:`str`
|
||||
The python command
|
||||
"""
|
||||
if executable is not None and executable != sys.executable:
|
||||
cls._executable = executable
|
||||
if os.path.islink(cls._executable):
|
||||
cls._is_venv = True
|
||||
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', '')
|
||||
else:
|
||||
cls._env['PATH'] = f'{path}/bin' + os.pathsep + os.environ.get('PATH', '')
|
||||
cls._env['VIRTUAL_ENV'] = path
|
||||
|
||||
@classmethod
|
||||
def reset_executable(cls):
|
||||
r"""Resets the executable to system standard"""
|
||||
cls._executable = sys.executable
|
||||
cls._is_venv = False
|
||||
|
||||
"""Public utils functions"""
|
||||
@classmethod
|
||||
def get_package(cls, package: str) -> Optional[str]:
|
||||
r"""Gets given package py local pip list
|
||||
|
||||
Parameter
|
||||
---------
|
||||
package: :class:`str`
|
||||
|
||||
Returns
|
||||
-------
|
||||
The package name as string
|
||||
"""
|
||||
result = None
|
||||
with suppress(Exception):
|
||||
args = [cls._executable, "-m", "pip", "show", package]
|
||||
if cls._is_venv:
|
||||
args = ["pip", "show", package]
|
||||
|
||||
result = subprocess.check_output(
|
||||
args,
|
||||
stderr=subprocess.DEVNULL, env=cls._env
|
||||
)
|
||||
|
||||
if result is None:
|
||||
return None
|
||||
|
||||
new_package: list[str] = str(result, 'utf-8').lower().split('\n')
|
||||
new_version = ''
|
||||
|
||||
for atr in new_package:
|
||||
if 'version' in atr:
|
||||
new_version = atr.split(': ')[1]
|
||||
|
||||
if new_version != '':
|
||||
return f'{package}=={new_version}'
|
||||
|
||||
return package
|
||||
|
||||
@classmethod
|
||||
def get_outdated(cls) -> bytes:
|
||||
r"""Gets table of outdated packages
|
||||
|
||||
Returns
|
||||
-------
|
||||
Bytes string of the command result
|
||||
"""
|
||||
args = [cls._executable, "-m", "pip", "list", "--outdated"]
|
||||
if cls._is_venv:
|
||||
args = ["pip", "list", "--outdated"]
|
||||
|
||||
return subprocess.check_output(args, env=cls._env)
|
||||
|
||||
@classmethod
|
||||
def install(cls, package: str, *args, source: str = None, stdout=None, stderr=None):
|
||||
r"""Installs given package
|
||||
|
||||
Parameter
|
||||
---------
|
||||
package: :class:`str`
|
||||
The name of the package
|
||||
args: :class:`list`
|
||||
Arguments for the command
|
||||
source: :class:`str`
|
||||
Extra index URL
|
||||
stdout: :class:`str`
|
||||
Stdout of subprocess.run
|
||||
stderr: :class:`str`
|
||||
Stderr of subprocess.run
|
||||
"""
|
||||
pip_args = [cls._executable, "-m", "pip", "install"]
|
||||
if cls._is_venv:
|
||||
pip_args = ["pip", "install"]
|
||||
|
||||
for arg in args:
|
||||
pip_args.append(arg)
|
||||
|
||||
if source is not None:
|
||||
pip_args.append(f'--extra-index-url')
|
||||
pip_args.append(source)
|
||||
|
||||
pip_args.append(package)
|
||||
subprocess.run(pip_args, stdout=stdout, stderr=stderr, env=cls._env)
|
||||
|
||||
@classmethod
|
||||
def uninstall(cls, package: str, stdout=None, stderr=None):
|
||||
r"""Uninstalls given package
|
||||
|
||||
Parameter
|
||||
---------
|
||||
package: :class:`str`
|
||||
The name of the package
|
||||
stdout: :class:`str`
|
||||
Stdout of subprocess.run
|
||||
stderr: :class:`str`
|
||||
Stderr of subprocess.run
|
||||
"""
|
||||
args = [cls._executable, "-m", "pip", "uninstall", "--yes", package]
|
||||
if cls._is_venv:
|
||||
args = ["pip", "uninstall", "--yes", package]
|
||||
|
||||
subprocess.run(
|
||||
args,
|
||||
stdout=stdout, stderr=stderr, env=cls._env
|
||||
)
|
91
src/cpl_core/utils/string.py
Normal file
91
src/cpl_core/utils/string.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import re
|
||||
import string
|
||||
import random
|
||||
|
||||
|
||||
class String:
|
||||
r"""Useful functions for strings"""
|
||||
|
||||
@staticmethod
|
||||
def convert_to_camel_case(chars: str) -> str:
|
||||
r"""Converts string to camel case
|
||||
|
||||
Parameter
|
||||
---------
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns
|
||||
-------
|
||||
String converted to CamelCase
|
||||
"""
|
||||
converted_name = chars
|
||||
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))
|
||||
|
||||
return converted_name
|
||||
|
||||
@staticmethod
|
||||
def convert_to_snake_case(chars: str) -> str:
|
||||
r"""Converts string to snake case
|
||||
|
||||
Parameter
|
||||
---------
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns
|
||||
-------
|
||||
String converted to snake_case
|
||||
"""
|
||||
# convert to train-case to CamelCase
|
||||
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()
|
||||
|
||||
@staticmethod
|
||||
def first_to_upper(chars: str) -> str:
|
||||
r"""Converts first char to upper
|
||||
|
||||
Parameter
|
||||
---------
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns
|
||||
-------
|
||||
String with first char as upper
|
||||
"""
|
||||
return f'{chars[0].upper()}{chars[1:]}'
|
||||
|
||||
@staticmethod
|
||||
def first_to_lower(chars: str) -> str:
|
||||
r"""Converts first char to lower
|
||||
|
||||
Parameter
|
||||
---------
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns
|
||||
-------
|
||||
String with first char as lower
|
||||
"""
|
||||
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
|
||||
-------
|
||||
String of random chars
|
||||
"""
|
||||
|
||||
return ''.join(random.choice(chars) for _ in range(length))
|
Reference in New Issue
Block a user