Module cevast.utils

Utils is a collection providing various functions for Cevast needs.

Expand source code
"""Utils is a collection providing various functions for Cevast needs."""

__all__ = (
    'validate_PEM',
    'BASE64_to_PEM',
    'make_PEM_filename',
    'remove_empty_folders',
    'directory_with_prefix',
)
__version__ = '1.1'
__author__ = 'Radim Podola'

from .cert_utils import validate_PEM, BASE64_to_PEM, make_PEM_filename
from .os_utils import remove_empty_folders, directory_with_prefix

Sub-modules

cevast.utils.cert_utils

This module provide functions supporting work with certificates.

cevast.utils.enrichment_analyzer

This module contains an implementation of dataset enrichment analyser.

cevast.utils.logging

This module contains helper functions for project logging.

cevast.utils.os_utils

This module provide functions supporting work with OS.

Functions

def BASE64_to_PEM(cert: str) ‑> str

Convert a raw BASE64 encoded certificate to PEM format (wrapped by 64 characters)

Expand source code
def BASE64_to_PEM(cert: str) -> str:
    """Convert a raw BASE64 encoded certificate to PEM format (wrapped by 64 characters)"""
    return '-----BEGIN CERTIFICATE-----' + '\n' + "\n".join(textwrap.wrap(cert, width=64)) + '\n' + '-----END CERTIFICATE-----'
def directory_with_prefix(directory: str, prefix: str, filename_only: bool = False) ‑> str

Generator listing directory and returning paths with the specified prefix.

Expand source code
def directory_with_prefix(directory: str, prefix: str, filename_only: bool = False) -> str:
    """Generator listing directory and returning paths with the specified prefix."""
    # Check if the directory exists
    if os.path.exists(directory):
        # Check if there is any file matching the prefix
        for file in os.listdir(directory):
            if file.startswith(prefix):
                if filename_only:
                    yield file
                else:
                    yield os.path.join(directory, file)
def make_PEM_filename(cert_id: str) ‑> str

Create a filename for PEM certificate

Expand source code
def make_PEM_filename(cert_id: str) -> str:
    """Create a filename for PEM certificate"""
    return cert_id + '.pem'
def remove_empty_folders(path: str)

Recursively remove empty folders

Expand source code
def remove_empty_folders(path: str):
    """Recursively remove empty folders"""
    files = os.listdir(path)
    if files:
        # Remove empty subfolders
        for file in files:
            fullpath = os.path.join(path, file)
            if os.path.isdir(fullpath):
                remove_empty_folders(fullpath)
    else:
        os.rmdir(path)
def validate_PEM(cert: str) ‑> bool

Simply verify the PEM certificate format

Expand source code
def validate_PEM(cert: str) -> bool:
    """Simply verify the PEM certificate format"""
    return cert.startswith('-----BEGIN CERTIFICATE-----\n') and cert.endswith('\n-----END CERTIFICATE-----')