]> git.parisson.com Git - telecaster-client.git/commitdiff
add deefuzzer conf read and write methods
authorGuillaume Pellerin <guillaume.pellerin@free.fr>
Mon, 5 Feb 2024 13:59:36 +0000 (14:59 +0100)
committerGuillaume Pellerin <guillaume.pellerin@free.fr>
Mon, 5 Feb 2024 13:59:36 +0000 (14:59 +0100)
telecaster/models.py
telecaster/tools/tools.py

index f25a877986aab172f0fbb1f3a4d9458f6f28fb1a..a33e5dd5cae264cfe24db4bd6cf690c7fb6a1f4b 100644 (file)
@@ -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 *
index 487d9ff1289bd7f369267f79394658c1e70d877e..6769843791185a4cc493cbb06a1ab36063755364 100644 (file)
@@ -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()