From: Guillaume Pellerin Date: Fri, 23 May 2014 14:08:03 +0000 (+0200) Subject: Add JSON conf parsing, add any conf parameter as an attribute to the DeeFuzzer class... X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=881838f25312067d460c5b716c6bb07f29a79301;p=deefuzzer.git Add JSON conf parsing, add any conf parameter as an attribute to the DeeFuzzer class (fix #2) --- diff --git a/deefuzzer/core.py b/deefuzzer/core.py index 6099c07..3a3f624 100644 --- a/deefuzzer/core.py +++ b/deefuzzer/core.py @@ -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 index 0000000..333f112 --- /dev/null +++ b/example/deefuzzer.json @@ -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 diff --git a/example/deefuzzer.xml b/example/deefuzzer.xml index 5bb453a..9b46c57 100644 --- a/example/deefuzzer.xml +++ b/example/deefuzzer.xml @@ -1,6 +1,6 @@ - /path/to/log/mystation.log - /path/to/m3u/mystation.m3u + mystation.log + mystation.m3u diff --git a/example/deefuzzer.yaml b/example/deefuzzer.yaml index b917790..c2d5712 100644 --- a/example/deefuzzer.yaml +++ b/example/deefuzzer.yaml @@ -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,