[pkg-fso-commits] [SCM] framworkd debian packageing branch, master, updated. milestone2-89-geb27523

Guillaume Chereau (none) charlie at nikopol.
Sat Aug 23 14:06:23 UTC 2008


The following commit has been merged in the master branch:
commit c45f168c54d7c03db8b69f1dd41d88d2268207c6
Author: Guillaume Chereau <charlie at nikopol.(none)>
Date:   Fri Aug 22 12:34:39 2008 +0800

    [oeventsd] Added trigger for time change events
    * Added the Time(hour, minute) trigger in the rule file
    * Added the Debug(msg) action

diff --git a/etc/freesmartphone/oevents/rules.yaml b/etc/freesmartphone/oevents/rules.yaml
index 73711ce..52e25c1 100644
--- a/etc/freesmartphone/oevents/rules.yaml
+++ b/etc/freesmartphone/oevents/rules.yaml
@@ -17,6 +17,8 @@
 # - StartVibration
 # - StopVibration
 # - RingTone(cmd)           : cmd can be 'start' or 'stop'
+# - Time(hour, min)         : create a trigger activated at the given time
+# - Debug(msg)              : Action that prints a debug message (only for debuging)
 
 -
     trigger: CallStatus()
@@ -26,3 +28,7 @@
     trigger: CallStatus()
     filters: Not(HasAttr(status, "incoming"))
     actions: RingTone(stop)
+-
+    trigger: Time(12,29)
+    actions: Debug("A Test")
+
diff --git a/framework/subsystems/oeventsd/action.py b/framework/subsystems/oeventsd/action.py
index 579e787..8d8b8e5 100644
--- a/framework/subsystems/oeventsd/action.py
+++ b/framework/subsystems/oeventsd/action.py
@@ -14,15 +14,30 @@ logger = logging.getLogger('oeventsd')
 
 import dbus
 
+#============================================================================#
 class Action(object):
+#============================================================================#
     """An action is a functor object that is called by a rule"""
     def __init__(self):
         pass
     def __call__(self, **kargs):
-        logger.info('%s called', self)
-
+        logger.info("%s called", self)
+    def __repr__(self):
+        return "unamed action"
+    
+#============================================================================#
+class DebugAction(Action):
+#============================================================================#
+    def __init__(self, msg):
+        self.msg = msg
+    def __call__(self, **kargs):
+        logger.info("DebugAction : %s", self.msg)
+    def __repr__(self):
+        return "Debug(\"%s\")" % self.msg
 
+#============================================================================#
 class DBusAction(Action):
+#============================================================================#
     """A special action that will call a DBus method"""
     def __init__(self, bus, service, obj, interface, method, *args):
         super(DBusAction, self).__init__()
@@ -60,7 +75,9 @@ class DBusAction(Action):
     def __repr__(self):
         return "%s(%s)" % (self.method, self.args)
 
+#============================================================================#
 class AudioAction(DBusAction):
+#============================================================================#
     def __init__(self, file = None, action = 'play'):
         bus = dbus.SystemBus()
         service = 'org.freesmartphone.odeviced'
@@ -69,7 +86,9 @@ class AudioAction(DBusAction):
         method = 'PlaySound' if action == 'play' else 'StopSound'
         super(AudioAction, self).__init__(bus, service, obj, interface, method, file)
 
+#============================================================================#
 class VibratorAction(DBusAction):
+#============================================================================#
     def __init__(self, target = 'neo1973_vibrator', action = 'start'):
         bus = dbus.SystemBus()
         service = 'org.freesmartphone.odeviced'
diff --git a/framework/subsystems/oeventsd/filter.py b/framework/subsystems/oeventsd/filter.py
index 1a88235..5aac0cf 100644
--- a/framework/subsystems/oeventsd/filter.py
+++ b/framework/subsystems/oeventsd/filter.py
@@ -8,15 +8,35 @@ The freesmartphone Events Module - Python Implementation
 (C) 2008 Openmoko, Inc.
 GPLv2 or later
 """
-
+#============================================================================#
 class Filter(object):
+#============================================================================#
+    """Base class for every filter
+       
+       A filter is used after a rule has been triggered to decide if the actions
+       will be called or not. When a rule is triggered, the trigger generate a dict
+       of values, that can be later used by the filter.
+       
+       All the filters need to implement the __call__ method, taking an arbitrary 
+       number of keywords argument (**kargs) representing the event generated dict
+       of values. The method returns True if the filter accept the event, False otherwise.
+    """
     def __call__(self, **kargs):
         raise NotImplementedError
 
-    def __invert__(self):   # The ~ operator
+    def __invert__(self):
+        """Return the inverted filter of this filter
+        
+           The __invert__ method is called by the `~` operator.
+        """
         return InvertFilter(self)
 
+#============================================================================#
 class AttributeFilter(Filter):
+#============================================================================#
+    """This filter is True if all the keywords argument are present in the
+       call and have the given value
+    """
     def __init__(self, **kargs):
         self.kargs = kargs
     def __call__(self, **kargs):
@@ -25,7 +45,10 @@ class AttributeFilter(Filter):
     def __repr__(self):
         return "and".join( "%s == %s" % (key, value) for (key, value) in self.kargs.items() )
 
+#============================================================================#
 class InvertFilter(Filter):
+#============================================================================#
+    """This filer returns the negation of the argument filter"""
     def __init__(self, filter):
         super(InvertFilter, self).__init__()
         self.filter = filter
diff --git a/framework/subsystems/oeventsd/oevents.py b/framework/subsystems/oeventsd/oevents.py
index 6e129e3..437198f 100644
--- a/framework/subsystems/oeventsd/oevents.py
+++ b/framework/subsystems/oeventsd/oevents.py
@@ -41,14 +41,18 @@ class EventsManager(dbus.service.Object):
        because the events can be defined into a configuration file.
     """
     def __init__(self, bus):
+        # Those attributes are needed by the framework system
         self.path = '/org/freesmartphone/Events'
         self.interface = 'org.freesmartphone.Events'
         self.bus = bus
+
         super(EventsManager, self).__init__(bus, self.path)
+        # The set of rules is empty
         self.rules = []
         logger.info( "%s %s initialized. Serving %s at %s", self.__class__.__name__, __version__, self.interface, self.path )
 
     def add_rule(self, rule):
+        """Add a new rule, and acticate it"""
         self.rules.append(rule)
         rule.init()
 
diff --git a/framework/subsystems/oeventsd/parser.py b/framework/subsystems/oeventsd/parser.py
index e45b8f0..e848d14 100644
--- a/framework/subsystems/oeventsd/parser.py
+++ b/framework/subsystems/oeventsd/parser.py
@@ -15,13 +15,15 @@ logger = logging.getLogger('oeventsd')
 import yaml
 import re
 
-from trigger import Trigger, CallStatusTrigger
+from trigger import Trigger, CallStatusTrigger, TimeTrigger
 from filter import Filter, AttributeFilter
-from action import Action, AudioAction, VibratorAction
+from action import Action, AudioAction, VibratorAction, DebugAction
 from ring_tone_action import RingToneAction
 from rule import Rule
 
+#============================================================================#
 class FunctionMetaClass(type):
+#============================================================================#
     """The meta class for Function class"""
     def __init__(cls, name, bases, dict):
         super(FunctionMetaClass, cls).__init__(name, bases, dict)
@@ -29,7 +31,9 @@ class FunctionMetaClass(type):
             logger.debug("register function %s", dict['name'])
             Function.functions[dict['name']] = cls
 
+#============================================================================#
 class Function(object):
+#============================================================================#
     __metaclass__ = FunctionMetaClass
     functions = {}
 
@@ -109,15 +113,27 @@ class HasAttr(Function):
     def __call__(self, name, value):
         kargs = {name:value}
         return AttributeFilter(**kargs)
+        
+class Debug(Function):
+    name = 'Debug'
+    def __call__(self, msg):
+        return DebugAction(msg)
+        
+class Time(Function):
+    name = 'Time'
+    def __call__(self, hour, minute):
+        return TimeTrigger(hour, minute)
 
 def as_rule(r):
-    assert isinstance(r, dict)
+    assert isinstance(r, dict), type(r)
     trigger = r['trigger']
     filters = r.get('filters', [])
     actions = r['actions']
     return Rule(trigger, filters, actions)
 
+#============================================================================#
 class Parser(object):
+#============================================================================#
     def parse_rules(self, src):
         rules = yaml.load(src)
         ret = []
@@ -125,7 +141,7 @@ class Parser(object):
             try:
                 ret.append(as_rule(r))
             except Exception, e:
-                logger.Error("can't parse rule : %s", e)
+                logger.error("can't parse rule %s : %s", r, e)
         return ret
 
 if __name__ == '__main__':
diff --git a/framework/subsystems/oeventsd/rule.py b/framework/subsystems/oeventsd/rule.py
index 0b528a5..00798ea 100644
--- a/framework/subsystems/oeventsd/rule.py
+++ b/framework/subsystems/oeventsd/rule.py
@@ -17,7 +17,9 @@ from action import Action
 
 # TODO : add a way to deactivate a rule
 
+#============================================================================#
 class Rule(object):
+#============================================================================#
     """Event Rule object
 
        A Rule consist of :
@@ -34,18 +36,20 @@ class Rule(object):
     def __init__(self, trigger, filters, actions):
         """Create a new rule
 
-           We need to call the init method of the rule before
-           it will actually be actvated
+           We need to add the rule into the EventManager (with the `add_rule`
+           method) before it will actually be activated. 
         """
+        # We accept list OR single value as argument
         if not isinstance(filters, list):
             filters = [filters]
         if not isinstance(actions, list):
             actions = [actions]
 
-        assert all(isinstance(x, Filter) for x in filters)
-        assert all(isinstance(x, Action) for x in actions)
+        assert all(isinstance(x, Filter) for x in filters), "Bad filter argument"
+        assert all(isinstance(x, Action) for x in actions), "Bad action parameter"
 
         self.trigger = trigger
+        # The trigger will call this rule when triggered
         trigger.connect(self)
         self.filters = filters
         self.actions = actions
@@ -56,6 +60,7 @@ class Rule(object):
         return "on %s if %s then %s" % (self.trigger, self.filters, self.actions)
 
     def on_signal(self, **kargs):
+        """Thos method is called by a trigger when it is activated"""
         # First we check that ALL the filters match the signal
         if any(not filter(**kargs) for filter in self.filters):
             return
@@ -64,7 +69,11 @@ class Rule(object):
             c(**kargs)
 
     def init(self):
-        """After we call this method, the rule is active"""
+        """After we call this method, the rule is active
+        
+           This method should only be called by the EventManager when we add
+           the rule 
+        """
         logger.info("init rule : %s", self)
         self.trigger.init()
 
diff --git a/framework/subsystems/oeventsd/trigger.py b/framework/subsystems/oeventsd/trigger.py
index 65920aa..27e2284 100644
--- a/framework/subsystems/oeventsd/trigger.py
+++ b/framework/subsystems/oeventsd/trigger.py
@@ -14,7 +14,9 @@ logger = logging.getLogger('oeventsd')
 
 import dbus
 
+#============================================================================#
 class Trigger(object):
+#============================================================================#
     """A trigger is the initial event that will activate a rule.
 
        When a trigger is activated, it call the rule __call__ method,
@@ -49,9 +51,21 @@ class Trigger(object):
         """
         pass
 
+#============================================================================#
 class DBusTrigger(Trigger):
+#============================================================================#
     """A special trigger that wait for a given DBus signal to trigger its rules"""
     def __init__(self, bus, service, obj, interface, signal):
+        """Create the DBus trigger
+        
+        arguments:
+        - bus       the DBus bus name
+        - service   the DBus name of the service
+        - obj       the DBus path of the object
+        - interface the Dbus interface of the signal
+        - signal    the DBus name of the signal
+        
+        """
         super(DBusTrigger, self).__init__()
         # some arguments checking
         assert isinstance(service, str)
@@ -76,7 +90,9 @@ class DBusTrigger(Trigger):
     def on_signal(self, *args):
         self(args=args)
 
+#============================================================================#
 class CallStatusTrigger(DBusTrigger):
+#============================================================================#
     """Just a sugar trigger for a GSM call status change"""
     def __init__(self):
         bus = dbus.SystemBus()
@@ -93,4 +109,26 @@ class CallStatusTrigger(DBusTrigger):
 
     def __repr__(self):
         return "CallStatus"
+        
+#============================================================================#
+class TimeTrigger(DBusTrigger):
+#============================================================================#
+    def __init__(self, hour, minute):
+        self.hour = hour
+        self.minute = minute
+        bus = dbus.SystemBus()
+        super(TimeTrigger, self).__init__(
+            bus,
+            'org.freesmartphone.otimed',
+            '/org/freesmartphone/Time',
+            'org.freesmartphone.Time',
+            'Minute'
+        )
+    def on_signal(self, year, mon, day, hour, min, sec, wday, yday, isdst):
+        if self.hour == hour and self.minute == min:
+            logger.debug("%s triggered", self)
+            self()
+
+    def __repr__(self):
+        return "Time(%d:%d)" % (self.hour, self.minute)
 

-- 
framworkd debian packageing



More information about the pkg-fso-commits mailing list