Added docs to cpl.utils

This commit is contained in:
2021-05-17 18:07:08 +02:00
parent 8d4597705d
commit 850d44a105
7 changed files with 250 additions and 129 deletions

View File

@@ -2,35 +2,52 @@ import base64
class CredentialManager:
"""
Handles credentials
"""
r"""Handles credential encryption and decryption"""
@staticmethod
def encrypt(string: str) -> str:
"""
Encode with base64
:param string:
:return:
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:
"""
Decode with base64
:param string:
:return:
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):
"""
Builds string with credentials in it
:param string:
:param credentials:
:return:
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))

View File

@@ -1,38 +1,30 @@
import os
import subprocess
import sys
import shlex
from contextlib import suppress
from textwrap import dedent
from typing import Optional
class Pip:
"""
Executes pip commands
"""
r"""Executes pip commands"""
_executable = sys.executable
_env = os.environ
_is_venv = False
"""
Getter
"""
"""Getter"""
@classmethod
def get_executable(cls) -> str:
return cls._executable
"""
Setter
"""
"""Setter"""
@classmethod
def set_executable(cls, executable: str):
"""
Sets the executable
:param executable:
:return:
r"""Sets the executable
Parameter
---------
executable: :class:`str`
The python command
"""
if executable is not None and executable != sys.executable:
cls._executable = executable
@@ -48,23 +40,22 @@ class Pip:
@classmethod
def reset_executable(cls):
"""
Resets the executable to system standard
:return:
"""
r"""Resets the executable to system standard"""
cls._executable = sys.executable
cls._is_venv = False
"""
Public utils functions
"""
"""Public utils functions"""
@classmethod
def get_package(cls, package: str) -> Optional[str]:
"""
Gets given package py local pip list
:param package:
:return:
r"""Gets given package py local pip list
Parameter
---------
package: :class:`str`
Returns
-------
The package name as string
"""
result = None
with suppress(Exception):
@@ -94,9 +85,11 @@ class Pip:
@classmethod
def get_outdated(cls) -> bytes:
"""
Gets table of outdated packages
:return:
r"""Gets table of outdated packages
Returns
-------
Bytes string of the command result
"""
args = [cls._executable, "-m", "pip", "list", "--outdated"]
if cls._is_venv:
@@ -106,14 +99,20 @@ class Pip:
@classmethod
def install(cls, package: str, *args, source: str = None, stdout=None, stderr=None):
"""
Installs given package
:param package:
:param args:
:param source:
:param stdout:
:param stderr:
:return:
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:
@@ -131,12 +130,16 @@ class Pip:
@classmethod
def uninstall(cls, package: str, stdout=None, stderr=None):
"""
Uninstalls given package
:param package:
:param stdout:
:param stderr:
:return:
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:

View File

@@ -4,16 +4,20 @@ import random
class String:
"""
Useful functions for strings
"""
r"""Useful functions for strings"""
@staticmethod
def convert_to_camel_case(chars: str) -> str:
"""
Converts string to camel case
:param chars:
:return:
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 + ' '
@@ -25,10 +29,16 @@ class String:
@staticmethod
def convert_to_snake_case(chars: str) -> str:
"""
Converts string to snake case
:param chars:
:return:
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:
@@ -41,26 +51,41 @@ class String:
@staticmethod
def first_to_upper(chars: str) -> str:
"""
Converts first char to upper
:param chars:
:return:
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:
"""
Converts first char to lower
:param chars:
:return:
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:
"""
Creates random string by given chars and length
r"""Creates random string by given chars and length
Returns
-------
String of random chars
"""
return ''.join(random.choice(chars) for _ in range(length))