From 4d49e0a1cf971904f1dc3e388e7b099d61cfc86b Mon Sep 17 00:00:00 2001 From: achbed Date: Wed, 19 Nov 2014 22:52:32 -0600 Subject: [PATCH] Added stationdefaults parameter (provides a mechanism to give default values for all stations) New merge_defaults function in utils.py - combines two dict objects recursively Signed-off-by: achbed --- deefuzzer/core.py | 5 +++++ deefuzzer/tools/utils.py | 17 +++++++++++++++++ example/deefuzzer.json | 11 +++++++++++ example/deefuzzer.xml | 11 +++++++++++ example/deefuzzer.yaml | 8 ++++++++ example/deefuzzer_doc.xml | 22 ++++++++++++++++++++++ 6 files changed, 74 insertions(+) diff --git a/deefuzzer/core.py b/deefuzzer/core.py index ef0f1fc..49e636f 100644 --- a/deefuzzer/core.py +++ b/deefuzzer/core.py @@ -120,6 +120,11 @@ class DeeFuzzer(Thread): station = self.conf['deefuzzer']['station'] else: station = self.conf['deefuzzer']['station'][i] + + # Apply station defaults if they exist + if 'stationdefaults' in self.conf['deefuzzer']: + if isinstance(self.conf['deefuzzer']['stationdefaults'], dict): + station = merge_defaults(station, self.conf['deefuzzer']['stationdefaults']) self.stations.append(Station(station, q, self.logger, self.m3u)) if self.m3u: diff --git a/deefuzzer/tools/utils.py b/deefuzzer/tools/utils.py index df4955b..3d2143b 100644 --- a/deefuzzer/tools/utils.py +++ b/deefuzzer/tools/utils.py @@ -13,6 +13,8 @@ import os import re import string +from itertools import chain +from deefuzzer.tools import * def clean_word(word) : """ Return the word without excessive blank spaces, underscores and @@ -37,3 +39,18 @@ def get_file_info(media): def is_absolute_path(path): return os.sep == path[0] + +def merge_defaults(setting, default): + combined = {} + for key in set(chain(setting, default)): + if key in setting: + if key in default: + if isinstance(setting[key], dict) and isinstance(default[key], dict): + combined[key] = merge_defaults(setting[key], default[key]) + else: + combined[key] = setting[key] + else: + combined[key] = setting[key] + else: + combined[key] = default[key] + return combined diff --git a/example/deefuzzer.json b/example/deefuzzer.json index cc4078e..2234ded 100644 --- a/example/deefuzzer.json +++ b/example/deefuzzer.json @@ -2,6 +2,17 @@ "deefuzzer": { "log": "/path/to/station.log", "m3u": "/path/to/station.m3u", + "station": { + "control": { + "mode": 0, + "port": 16001 + }, + "jingles": { + "dir": "/path/to/jingles", + "mode": 0, + "shuffle": 1 + } + }, "station": { "control": { "mode": 0, diff --git a/example/deefuzzer.xml b/example/deefuzzer.xml index 593d2ed..9cadbb1 100644 --- a/example/deefuzzer.xml +++ b/example/deefuzzer.xml @@ -1,6 +1,17 @@ /path/to/station.log /path/to/station.m3u + + + 0 + 16001 + + + /path/to/jingles + 0 + 1 + + 0 diff --git a/example/deefuzzer.yaml b/example/deefuzzer.yaml index 8e33454..366cb7f 100644 --- a/example/deefuzzer.yaml +++ b/example/deefuzzer.yaml @@ -2,6 +2,14 @@ deefuzzer: log: /path/to/station.log m3u: /path/to/station.m3u + stationdefaults: + control: {mode: 0, + port: 16001} + + jingles: {dir: /path/to/jingles, + mode: 0, + shuffle: 1} + station: control: {mode: 0, port: 16001} diff --git a/example/deefuzzer_doc.xml b/example/deefuzzer_doc.xml index be45286..1beb53f 100644 --- a/example/deefuzzer_doc.xml +++ b/example/deefuzzer_doc.xml @@ -5,6 +5,28 @@ The file is preferably accessible behind an url, for example, http://mydomain.com/m3u/mystation.m3u --> /path/to/station.m3u + + + + + 0 + + 16001 + + + + /path/to/jingles + + 0 + + 1 + +