2023-07-25 15:21:38 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
from datetime import datetime
|
2023-07-26 15:28:44 +02:00
|
|
|
import hashlib
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Prints dictionary in formatted json string.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
def debug_dict(
|
|
|
|
jsontxt = ""
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Prints any list, dict, string, float or integer as a json
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
debug_json = json.dumps(jsontxt, indent=4)
|
|
|
|
print (debug_json)
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
print(f"debug_dict: Failed to dump. Error: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"debug_dict: Unexpected error: {e}")
|
|
|
|
|
|
|
|
####
|
|
|
|
# Assign to a key in a dict a given value.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
def set_dict_key_value(
|
|
|
|
input_dict: dict = {},
|
|
|
|
input_key: str = "",
|
|
|
|
input_value
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Assign to a key in a dict a given value
|
|
|
|
"""
|
|
|
|
key = input_key.strip()
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
if len(key) > 0:
|
|
|
|
input_dict[key] = input_value
|
|
|
|
|
|
|
|
####
|
|
|
|
# Return MD5 hash string.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
def generate_md5(
|
|
|
|
input_string: str = ""
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Generates an MD5 hash for the given input string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
input_string (str): The string for which the MD5 hash will be generated.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The MD5 hash of the input string as a hexadecimal string.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
md5_hash = hashlib.md5(input_string.encode()).hexdigest()
|
|
|
|
except:
|
|
|
|
md5_hash = ""
|
|
|
|
|
|
|
|
return md5_hash
|
|
|
|
|
|
|
|
####
|
|
|
|
# Returns or print current time in date format or utimestamp.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def now(
|
2023-07-27 15:53:10 +02:00
|
|
|
print_flag: int = 0,
|
|
|
|
utimestamp: int = 0
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Returns time in yyyy/mm/dd HH:MM:SS format by default. Use 1 as an argument
|
|
|
|
to get epoch time (utimestamp)
|
|
|
|
"""
|
|
|
|
today = datetime.today()
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
if utimestamp:
|
2023-07-27 15:53:10 +02:00
|
|
|
time = datetime.timestamp(today)
|
2023-07-25 15:21:38 +02:00
|
|
|
else:
|
2023-07-27 15:53:10 +02:00
|
|
|
time = today.strftime('%Y/%m/%d %H:%M:%S')
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
if print_flag:
|
2023-07-27 15:53:10 +02:00
|
|
|
print(time)
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
return time
|
|
|
|
|
|
|
|
####
|
|
|
|
# Translate macros in string from a dict.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
|
|
|
def translate_macros(
|
2023-07-27 15:53:10 +02:00
|
|
|
macro_dic: dict = {},
|
|
|
|
data: str = ""
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Expects a macro dictionary key:value (macro_name:macro_value)
|
|
|
|
and a string to replace macro.
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
It will replace the macro_name for the macro_value in any string.
|
|
|
|
"""
|
|
|
|
for macro_name, macro_value in macro_dic.items():
|
|
|
|
data = data.replace(macro_name, macro_value)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Parse configuration file line by line based on separator and return dict.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def parse_configuration(
|
2023-07-27 15:53:10 +02:00
|
|
|
file: str = "/etc/pandora/pandora_server.conf",
|
|
|
|
separator: str = " ",
|
|
|
|
default_values: dict = {}
|
|
|
|
) -> dict:
|
2023-07-25 15:21:38 +02:00
|
|
|
"""
|
|
|
|
Parse configuration. Reads configuration file and stores its data as dict.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
- file (str): configuration file path. Defaults to "/etc/pandora/pandora_server.conf". \n
|
|
|
|
- separator (str, optional): Separator for option and value. Defaults to " ".
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
- dict: containing all keys and values from file.
|
|
|
|
"""
|
|
|
|
config = {}
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
try:
|
|
|
|
with open (file, "r") as conf:
|
|
|
|
lines = conf.read().splitlines()
|
|
|
|
for line in lines:
|
2023-07-27 15:53:10 +02:00
|
|
|
if line.strip().startswith("#") or len(line.strip()) < 1 :
|
|
|
|
continue
|
2023-07-25 15:21:38 +02:00
|
|
|
else:
|
2023-07-27 15:53:10 +02:00
|
|
|
option, value = line.strip().split(separator, maxsplit=1)
|
2023-07-25 15:21:38 +02:00
|
|
|
config[option.strip()] = value.strip()
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print (f"{type(e).__name__}: {e}")
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
for option, value in default_values.items():
|
|
|
|
if option.strip() not in config:
|
|
|
|
config[option.strip()] = value.strip()
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
return config
|
|
|
|
|
|
|
|
####
|
|
|
|
# Parse csv file line by line and return list.
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
def parse_csv_file(
|
2023-07-27 15:53:10 +02:00
|
|
|
file: str = "",
|
|
|
|
separator: str = ';',
|
|
|
|
count_parameters: int = 0,
|
|
|
|
debug: bool = False
|
2023-07-25 15:21:38 +02:00
|
|
|
) -> list:
|
|
|
|
"""
|
2023-07-27 15:53:10 +02:00
|
|
|
Parse csv configuration. Reads configuration file and stores its data in a list.
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
- file (str): configuration csv file path. \n
|
|
|
|
- separator (str, optional): Separator for option and value. Defaults to ";".
|
|
|
|
- coun_parameters (int): min number of parameters each line shold have. Default None
|
2023-07-27 15:53:10 +02:00
|
|
|
- debug (bool): print errors on lines
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
- List: containing a list for of values for each csv line.
|
|
|
|
"""
|
|
|
|
csv_arr = []
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
try:
|
2023-07-27 15:53:10 +02:00
|
|
|
with open (file, "r") as csv:
|
|
|
|
lines = csv.read().splitlines()
|
2023-07-25 15:21:38 +02:00
|
|
|
for line in lines:
|
2023-07-27 15:53:10 +02:00
|
|
|
if line.strip().startswith("#") or len(line.strip()) < 1 :
|
2023-07-25 15:21:38 +02:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
value = line.strip().split(separator)
|
2023-07-27 15:53:10 +02:00
|
|
|
if len(value) >= count_parameters:
|
2023-07-25 15:21:38 +02:00
|
|
|
csv_arr.append(value)
|
|
|
|
elif debug==True:
|
2023-07-27 15:53:10 +02:00
|
|
|
print(f'Csv line: {line} does not match minimun parameter defined: {count_parameters}',file=sys.stderr)
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print (f"{type(e).__name__}: {e}")
|
2023-07-26 15:28:44 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
return csv_arr
|
2023-07-26 15:28:44 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Parse given variable to integer.
|
2023-07-26 15:28:44 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
def parse_int(
|
|
|
|
var=""
|
|
|
|
) -> int:
|
|
|
|
"""
|
|
|
|
Parse given variable to integer.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
var (any): The variable to be parsed as an integer.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The parsed integer value. If parsing fails, returns 0.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return int(var)
|
|
|
|
except:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
####
|
|
|
|
# Parse given variable to float.
|
2023-07-26 15:28:44 +02:00
|
|
|
#########################################################################################
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
def parse_float(
|
|
|
|
var=""
|
|
|
|
) -> float:
|
2023-07-26 15:28:44 +02:00
|
|
|
"""
|
2023-07-27 15:53:10 +02:00
|
|
|
Parse given variable to float.
|
2023-07-26 15:28:44 +02:00
|
|
|
|
|
|
|
Args:
|
2023-07-27 15:53:10 +02:00
|
|
|
var (any): The variable to be parsed as an float.
|
2023-07-26 15:28:44 +02:00
|
|
|
|
|
|
|
Returns:
|
2023-07-27 15:53:10 +02:00
|
|
|
float: The parsed float value. If parsing fails, returns 0.
|
2023-07-26 15:28:44 +02:00
|
|
|
"""
|
2023-07-27 15:53:10 +02:00
|
|
|
try:
|
|
|
|
return float(var)
|
|
|
|
except:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
####
|
|
|
|
# Parse given variable to string.
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def parse_str(
|
|
|
|
var=""
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Parse given variable to string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
var (any): The variable to be parsed as an string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The parsed string value. If parsing fails, returns "".
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return str(var)
|
|
|
|
except:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
####
|
|
|
|
# Parse given variable to bool.
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def parse_bool(
|
|
|
|
var=""
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Parse given variable to bool.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
var (any): The variable to be parsed as an bool.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: The parsed bool value. If parsing fails, returns False.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return bool(var)
|
|
|
|
except:
|
|
|
|
return False
|