Added docs to cpl.utils

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

Binary file not shown.

Binary file not shown.

View File

@ -179,30 +179,52 @@
<dt id="cpl.utils.credential_manager.CredentialManager">
<em class="property"><span class="pre">class</span> </em><code class="sig-prename descclassname"><span class="pre">cpl.utils.credential_manager.</span></code><code class="sig-name descname"><span class="pre">CredentialManager</span></code><a class="headerlink" href="#cpl.utils.credential_manager.CredentialManager" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Handles credentials</p>
<p>Handles credential encryption and decryption</p>
<dl class="py method">
<dt id="cpl.utils.credential_manager.CredentialManager.build_string">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">build_string</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">credentials</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cpl.utils.credential_manager.CredentialManager.build_string" title="Permalink to this definition"></a></dt>
<dd><p>Builds string with credentials in it
:param string:
:param credentials:
:return:</p>
<dd><p>Builds string with credentials in it</p>
<blockquote>
<div><dl class="simple">
<dt>string: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String in which the variable is replaced by credentials</p>
</dd>
<dt>credentials: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to encode</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>Decoded string</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.credential_manager.CredentialManager.decrypt">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">decrypt</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.credential_manager.CredentialManager.decrypt" title="Permalink to this definition"></a></dt>
<dd><p>Decode with base64
:param string:
:return:</p>
<dd><p>Decode with base64</p>
<blockquote>
<div><dl class="simple">
<dt>string: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to decode</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>Decoded string</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.credential_manager.CredentialManager.encrypt">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">encrypt</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.credential_manager.CredentialManager.encrypt" title="Permalink to this definition"></a></dt>
<dd><p>Encode with base64
:param string:
:return:</p>
<dd><p>Encode with base64</p>
<blockquote>
<div><dl class="simple">
<dt>string: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to encode</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>Encoded string</p>
</div></blockquote>
</dd></dl>
</dd></dl>
@ -223,53 +245,76 @@
<dl class="py method">
<dt id="cpl.utils.pip.Pip.get_outdated">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">get_outdated</span></code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; <span class="pre">bytes</span><a class="headerlink" href="#cpl.utils.pip.Pip.get_outdated" title="Permalink to this definition"></a></dt>
<dd><p>Gets table of outdated packages
:return:</p>
<dd><p>Gets table of outdated packages</p>
<blockquote>
<div><p>Bytes string of the command result</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.pip.Pip.get_package">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">get_package</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span><a class="headerlink" href="#cpl.utils.pip.Pip.get_package" title="Permalink to this definition"></a></dt>
<dd><p>Gets given package py local pip list
:param package:
:return:</p>
<dd><p>Gets given package py local pip list</p>
<blockquote>
<div><p>package: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></p>
</div></blockquote>
<blockquote>
<div><p>The package name as string</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.pip.Pip.install">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">install</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">source</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span></span> <span class="o"><span class="pre">=</span></span> <span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cpl.utils.pip.Pip.install" title="Permalink to this definition"></a></dt>
<dd><p>Installs given package
:param package:
:param args:
:param source:
:param stdout:
:param stderr:
:return:</p>
<dd><p>Installs given package</p>
<blockquote>
<div><dl class="simple">
<dt>package: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>The name of the package</p>
</dd>
<dt>args: <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></dt><dd><p>Arguments for the command</p>
</dd>
<dt>source: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>Extra index URL</p>
</dd>
<dt>stdout: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>Stdout of subprocess.run</p>
</dd>
<dt>stderr: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>Stderr of subprocess.run</p>
</dd>
</dl>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.pip.Pip.reset_executable">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">reset_executable</span></code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cpl.utils.pip.Pip.reset_executable" title="Permalink to this definition"></a></dt>
<dd><p>Resets the executable to system standard
:return:</p>
<dd><p>Resets the executable to system standard</p>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.pip.Pip.set_executable">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">set_executable</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">executable</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cpl.utils.pip.Pip.set_executable" title="Permalink to this definition"></a></dt>
<dd><p>Sets the executable
:param executable:
:return:</p>
<dd><p>Sets the executable</p>
<blockquote>
<div><dl class="simple">
<dt>executable: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>The python command</p>
</dd>
</dl>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.pip.Pip.uninstall">
<em class="property"><span class="pre">classmethod</span> </em><code class="sig-name descname"><span class="pre">uninstall</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cpl.utils.pip.Pip.uninstall" title="Permalink to this definition"></a></dt>
<dd><p>Uninstalls given package
:param package:
:param stdout:
:param stderr:
:return:</p>
<dd><p>Uninstalls given package</p>
<blockquote>
<div><dl class="simple">
<dt>package: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>The name of the package</p>
</dd>
<dt>stdout: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>Stdout of subprocess.run</p>
</dd>
<dt>stderr: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>Stderr of subprocess.run</p>
</dd>
</dl>
</div></blockquote>
</dd></dl>
</dd></dl>
@ -285,39 +330,70 @@
<dl class="py method">
<dt id="cpl.utils.string.String.convert_to_camel_case">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">convert_to_camel_case</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.string.String.convert_to_camel_case" title="Permalink to this definition"></a></dt>
<dd><p>Converts string to camel case
:param chars:
:return:</p>
<dd><p>Converts string to camel case</p>
<blockquote>
<div><dl class="simple">
<dt>chars: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to convert</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>String converted to CamelCase</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.string.String.convert_to_snake_case">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">convert_to_snake_case</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.string.String.convert_to_snake_case" title="Permalink to this definition"></a></dt>
<dd><p>Converts string to snake case
:param chars:
:return:</p>
<dd><p>Converts string to snake case</p>
<blockquote>
<div><dl class="simple">
<dt>chars: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to convert</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>String converted to snake_case</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.string.String.first_to_lower">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">first_to_lower</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.string.String.first_to_lower" title="Permalink to this definition"></a></dt>
<dd><p>Converts first char to lower
:param chars:
:return:</p>
<dd><p>Converts first char to lower</p>
<blockquote>
<div><dl class="simple">
<dt>chars: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to convert</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>String with first char as lower</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.string.String.first_to_upper">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">first_to_upper</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.string.String.first_to_upper" title="Permalink to this definition"></a></dt>
<dd><p>Converts first char to upper
:param chars:
:return:</p>
<dd><p>Converts first char to upper</p>
<blockquote>
<div><dl class="simple">
<dt>chars: <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></dt><dd><p>String to convert</p>
</dd>
</dl>
</div></blockquote>
<blockquote>
<div><p>String with first char as upper</p>
</div></blockquote>
</dd></dl>
<dl class="py method">
<dt id="cpl.utils.string.String.random_string">
<em class="property"><span class="pre">static</span> </em><code class="sig-name descname"><span class="pre">random_string</span></code><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">int</span></span></em><span class="sig-paren">)</span> &#x2192; <span class="pre">str</span><a class="headerlink" href="#cpl.utils.string.String.random_string" title="Permalink to this definition"></a></dt>
<dd><p>Creates random string by given chars and length</p>
<blockquote>
<div><p>String of random chars</p>
</div></blockquote>
</dd></dl>
</dd></dl>

File diff suppressed because one or more lines are too long

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))