]> git.parisson.com Git - deefuzzer.git/commitdiff
Add JSON conf parsing, add any conf parameter as an attribute to the DeeFuzzer class...
authorGuillaume Pellerin <yomguy@parisson.com>
Fri, 23 May 2014 14:08:03 +0000 (16:08 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Fri, 23 May 2014 14:08:03 +0000 (16:08 +0200)
deefuzzer/core.py
example/deefuzzer.json [new file with mode: 0644]
example/deefuzzer.xml
example/deefuzzer.yaml

index 6099c07a02f86fa19419dceb4f5357c0a07f951c..3a3f6242d9c127ed4cbd7c0466ae37a84c56fbf3 100644 (file)
@@ -40,10 +40,13 @@ import os
 import shout
 import Queue
 import datetime
+import mimetypes
 from threading import Thread
 from deefuzzer.station import *
 from deefuzzer.tools import *
 
+mimetypes.add_type('application/x-yaml','.yaml')
+
 
 class DeeFuzzer(Thread):
     """a DeeFuzzer diffuser"""
@@ -55,15 +58,15 @@ class DeeFuzzer(Thread):
         Thread.__init__(self)
         self.conf_file = conf_file
         self.conf = self.get_conf_dict()
-        if 'log' in self.conf['deefuzzer'].keys():
-            self.logger = Logger(self.conf['deefuzzer']['log'])
-        else:
-            self.logger = Logger('.' + os.sep + 'deefuzzer.log')
-        if 'm3u' in self.conf['deefuzzer'].keys():
-            self.m3u = self.conf['deefuzzer']['m3u']
-        else:
-            self.m3u = '.' + os.sep + 'deefuzzer.m3u'
-
+        
+        for key in self.conf['deefuzzer'].keys():
+            if key == 'log':
+                self.logger = Logger(self.conf['deefuzzer']['log'])
+            if key == 'm3u':
+                self.m3u = self.conf['deefuzzer']['m3u']
+            else:
+                setattr(self, key, self.conf['deefuzzer'][key])
+                    
         if isinstance(self.conf['deefuzzer']['station'], dict):
             # Fix wrong type data from xmltodict when one station (*)
             self.nb_stations = 1
@@ -79,16 +82,19 @@ class DeeFuzzer(Thread):
         self.logger.write_info('Number of stations : ' + str(self.nb_stations))
 
     def get_conf_dict(self):
+        mime_type = mimetypes.guess_type(self.conf_file)[0]
         confile = open(self.conf_file,'r')
-        filename, ext = os.path.splitext(self.conf_file)
         data = confile.read()
         confile.close()
 
-        if 'xml' in ext:
+        if 'xml' in mime_type:
             return xmltodict(data,'utf-8')
-        elif 'yaml' in ext:
+        elif 'yaml' in mime_type:
             import yaml
             return yaml.load(data)
+        elif 'json' in mime_type:
+            import json
+            return json.loads(data)
 
     def set_m3u_playlist(self):
         m3u_dir = os.sep.join(self.m3u.split(os.sep)[:-1])
diff --git a/example/deefuzzer.json b/example/deefuzzer.json
new file mode 100644 (file)
index 0000000..333f112
--- /dev/null
@@ -0,0 +1,61 @@
+{
+    "deefuzzer": {
+        "log": "mystation.log", 
+        "m3u": "mystation.m3u", 
+        "station": {
+            "control": {
+                "mode": 0, 
+                "port": 16001
+            }, 
+            "infos": {
+                "description": "my_station", 
+                "genre": "music", 
+                "name": "my_station", 
+                "url": "http://parisson.com"
+            }, 
+            "jingles": {
+                "dir": "/path/to/jingles", 
+                "mode": 0, 
+                "shuffle": 1
+            }, 
+            "media": {
+                "bitrate": 96, 
+                "dir": "/path/to/mp3/", 
+                "format": "mp3", 
+                "m3u": "/path/to/m3u_file", 
+                "ogg_quality": 4, 
+                "samplerate": 48000, 
+                "shuffle": 0, 
+                "voices": "2"
+            }, 
+            "record": {
+                "dir": "/home/telecaster/archives/mp3", 
+                "mode": 0
+            }, 
+            "relay": {
+                "author": "Inconnu", 
+                "mode": 0, 
+                "url": "http://127.0.0.1:8000/telecaster_live.mp3"
+            }, 
+            "rss": {
+                "dir": "/var/www/rss", 
+                "enclosure": 0, 
+                "media_url": "http://localhost/rss/"
+            }, 
+            "server": {
+                "host": "127.0.0.1", 
+                "mountpoint": "monitor", 
+                "port": 8000, 
+                "public": 0, 
+                "sourcepassword": "source2parisson", 
+                "type": "icecast"
+            }, 
+            "twitter": {
+                "key": "76728330-OjKgbHtn4II86Ad7pNUGEzfNAkGTW5Wvw38qUmLE", 
+                "mode": 0, 
+                "secret": "4egZs1dSM37XVY8zXa016Yueku2fleXF2bx8k25V4", 
+                "tags": "bla bla"
+            }
+        }
+    }
+}
\ No newline at end of file
index 5bb453a472c5240cc85ae440c46ec1968679e5c1..9b46c572e9dfe412e65a20118dfff836c455a0b1 100644 (file)
@@ -1,6 +1,6 @@
 <deefuzzer>
-    <log>/path/to/log/mystation.log</log>
-    <m3u>/path/to/m3u/mystation.m3u</m3u>
+    <log>mystation.log</log>
+    <m3u>mystation.m3u</m3u>
 
     <station>
         <infos>
index b9177904bd171ee8c1a45b91769162ef4f69bebf..c2d5712abf5e1ad7b637afdd9fa13f941e0b95fd 100644 (file)
@@ -1,6 +1,6 @@
 deefuzzer:
-  log: /path/to/log/mystation.log
-  m3u: /path/to/m3u/mystation.m3u
+  log: mystation.log
+  m3u: mystation.m3u
 
   station:
     control: {mode: 0,