From e602869cd56f1940f3aee3783c908cc1b7c40832 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Mon, 5 Feb 2024 14:59:36 +0100 Subject: [PATCH] add deefuzzer conf read and write methods --- telecaster/models.py | 2 +- telecaster/tools/tools.py | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/telecaster/models.py b/telecaster/models.py index f25a877..a33e5dd 100644 --- a/telecaster/models.py +++ b/telecaster/models.py @@ -54,7 +54,7 @@ from south.modelsinspector import add_introspection_rules from teleforma.models import Conference -from deefuzzer.tools.utils import get_conf_dict, write_conf +from tools.tools import write_conf, get_conf_dict from tools import * diff --git a/telecaster/tools/tools.py b/telecaster/tools/tools.py index 487d9ff..6769843 100644 --- a/telecaster/tools/tools.py +++ b/telecaster/tools/tools.py @@ -162,3 +162,51 @@ def norm_string(string): pass +def get_conf_dict(file): + mime_type = mimetypes.guess_type(file)[0] + + # Do the type check first, so we don't load huge files that won't be used + if 'xml' in mime_type: + confile = open(file, 'r') + data = confile.read() + confile.close() + return xmltodict(data, 'utf-8') + elif 'yaml' in mime_type: + import yaml + + def custom_str_constructor(loader, node): + return loader.construct_scalar(node).encode('utf-8') + + yaml.add_constructor(u'tag:yaml.org,2002:str', custom_str_constructor) + confile = open(file, 'r') + data = confile.read() + confile.close() + return yaml.load(data) + elif 'json' in mime_type: + import json + + confile = open(file, 'r') + data = confile.read() + confile.close() + return json.loads(data) + + return False + + +def write_conf(conf_dict, path): + filename, ext = os.path.splitext(path) + f = open(path, 'w') + + if 'xml' in ext: + xml_data = dicttoxml(conf_dict) + f.write(xml_data) + + elif 'yaml' in ext or 'yml' in ext: + import yaml + yaml.dump(conf_dict, f) + + elif 'json' in ext: + import json + json.dump(conf_dict, f) + + f.close() -- 2.39.5