]> git.neil.brown.name Git - plato.git/commitdiff
profile: multiple conditions in a rule.
authorNeilBrown <neilb@suse.de>
Thu, 19 Dec 2013 22:46:33 +0000 (09:46 +1100)
committerNeilBrown <neilb@suse.de>
Thu, 19 Dec 2013 23:05:13 +0000 (10:05 +1100)
A rule can have multiple conditions.  All must succeed.

lib/profile.py

index c8a1708c71c321ac714b0f1fea188f7cd5635835..42780fe024fc8900b83173117854bea72aa63095 100644 (file)
@@ -66,26 +66,40 @@ def get_profile(event, who):
     return p
 
 def rule_matches(rule, event, who, when):
-    if rule == '':
-        return True
-    if rule == event:
-        return True
-    if rule[0] == '<' and rule[1:].isdigit():
-        return time.time() >= when and time.time() < when + int(rule[1:])*60
-    for w in who.split(' '):
-        if w[-8:] == rule[-8:]:
-            return True
-    dt = rule.split(' ')
-    tm = time.localtime()
-    for d in dt:
-        a = day_match(d, tm)
-        if a == None:
-            a = time_match(d, tm)
+    """A rule can contain several words. If none fail,
+       the rule succeeds (so an empty rule always succeeds)
+       words can match:
+         - The event:  ring sms alarm
+         - who:   last 8 digits must match, "who" can be a list
+         - <N:   rule file was changed less than N minutes ago
+         - day-of-week:  mo,we,fr-su
+         - time-of-day: 10.00-11.30,23.30-01.30
+    """
+    for r in rule.split(' '):
+        if r == '':
+            continue
+        if r == event:
+            continue
+        if len(r) > 1 and r[0] == '<' and r[1:].isdigit():
+            if time.time() >= when and time.time() < when + int(r[1:])*60:
+                continue
+            return False
+
+        for w in who.split(' '):
+            if w[-8:] == r[-8:]:
+                continue
+
+        tm = time.localtime()
+        a = day_match(r, tm)
+        if a == False:
+            return False
         if a == True:
-            return True
-        #if a == None:
-        #    raise ValueError
-    return False
+            continue
+        a = time_match(r, tm)
+        if a == False:
+            return False
+
+    return True
 
 
 # python is broken: tm_wday==0 means monday!!!