45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
'''Miscellaneous functions'''
|
|
import random, string, sys, os
|
|
|
|
from os import environ as env
|
|
from datetime import datetime
|
|
from pathlib import path
|
|
from os.path import abspath, dirname, basename, isdir, isfile
|
|
|
|
from . import logging
|
|
|
|
|
|
def Boolean(v, return_value=False):
|
|
if type(v) not in [str, bool, int, type(None)]:
|
|
raise ValueError(f'Value is not a string, boolean, int, or nonetype: {value}')
|
|
|
|
'''make the value lowercase if it's a string'''
|
|
value = v.lower() if isinstance(v, str) else v
|
|
|
|
if value in [1, True, 'on', 'y', 'yes', 'true', 'enable']:
|
|
'''convert string to True'''
|
|
return True
|
|
|
|
elif value in [0, False, None, 'off', 'n', 'no', 'false', 'disable', '']:
|
|
'''convert string to False'''
|
|
return False
|
|
|
|
elif return_value:
|
|
'''just return the value'''
|
|
return v
|
|
|
|
else:
|
|
return True
|
|
|
|
|
|
def RandomGen(chars=20):
|
|
if not isinstance(chars, int):
|
|
raise TypeError(f'Character length must be an integer, not a {type(char)}')
|
|
|
|
return ''.join(random.choices(string.ascii_letters + string.digits, k=chars))
|
|
|
|
|
|
def FormatUtc(timestamp=None):
|
|
date = datetime.fromtimestamp(timestamp) if timestamp else datetime.utcnow()
|
|
return date.strftime('%a, %d %b %Y %H:%M:%S GMT')
|