Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from cryptography.fernet import Fernet
from conff import utils
from conff.utils import Munch2, update_recursive, yaml_safe_load, filter_value, odict
class Parser:
# default params
default_params = {
'etype': 'fernet',
# list of simpleeval library parameters
'simpleeval': {
# by default operators = simpleeval.DEFAULT_OPERATORS,
'operators': {},
'options': {
'max_power': simpleeval.MAX_POWER,
'max_string_length': simpleeval.MAX_STRING_LENGTH,
'disallow_prefixes': simpleeval.DISALLOW_PREFIXES
}
}
}
def __init__(self, names=None, fns=None, params=None):
"""
:param params: A dictionary containing some parameters that will modify
how the builtin functions run. For example, the type of encryption to
use and the encrpyption key to use or simpleeval library parameters
"""
self.errors = []
self.logger = self.prepare_logger()
self.params = self.prepare_params(params=params)
self.fns = self.prepare_functions(fns=fns)
self.names = self.prepare_names(names=names)
def load(fs_path: str, fs_root: str = '', errors: list = None, params: dict = None, opts: dict = None):
errors = errors if type(errors) == list else []
fs_file_path = os.path.join(fs_root, fs_path)
fs_root = fs_root if fs_root is None else os.path.dirname(fs_file_path)
params = params if params else {}
opts = opts if opts else {
'max_power': simpleeval.MAX_POWER,
'max_string_length': simpleeval.MAX_STRING_LENGTH,
'disallow_prefixes': simpleeval.DISALLOW_PREFIXES
}
try:
with open(fs_file_path) as stream:
# load_yaml initial structure
data = yaml_safe_load(stream)
data['_'] = data['_'] if data.get('_') else {}
data_internal = {'fs_path': fs_path, 'fs_root': fs_root}
data_internal = {**{'etype': 'fernet'}, **data_internal, **params}
data['_'] = {**data['_'], **data_internal}
names = {'R': data}
fns = {}
data = parse(data, names=names, fns=fns)
# data cleanup to ensure no data_internal
all(map(data['_'].pop, data_internal))
# remove when empty