From: NeilBrown Date: Thu, 19 Dec 2013 05:36:36 +0000 (+1100) Subject: profile: read rules from a directory as well as a file. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=640d0b060b5ebf162f6521784352e09401f86c39;p=plato.git profile: read rules from a directory as well as a file. This makes it easier to handle temp over-rides --- diff --git a/lib/profile.py b/lib/profile.py index bb396b6..6c40676 100644 --- a/lib/profile.py +++ b/lib/profile.py @@ -3,7 +3,7 @@ # earlier. # These are just variable assignments stored in a map. -import time +import time, os expect = [ 'tone', @@ -36,18 +36,31 @@ def load_profile(p, t): else: read_profile(p, "/data/profiles/"+t) -def get_profile(event, who): +def load_rules(file, p, event, who): try: - f = open("/data/rules") + f = open(file) l = f.readlines() except IOError: l = [] - p = {} for ln in l: ln = ln.strip() w = ln.split(':', 1) if len(w) == 2 and rule_matches(w[0].strip(), event, who): load_profile(p, w[1].strip()) + +def get_profile(event, who): + p = {} + load_rules("/data/rules", p, event, who) + try: + d = os.listdir('/data/rules.d') + d = filter(lambda x: x[0] != '.', d) + d.sort() + for b in d: + load_rules(os.path.join('/data/rules.d',b), p, event, who) + except IOError: + pass + except OSError: + pass return p def rule_matches(rule, event, who):