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

Guillaume Chereau (none) charlie at nikopol.
Tue Sep 2 10:34:59 UTC 2008


The following commit has been merged in the master branch:
commit beb827c2fd9a1c069035085c43df64c8090c4a6a
Author: Guillaume Chereau <charlie at nikopol.(none)>
Date:   Mon Sep 1 19:30:11 2008 +0800

    [oeventsd] Use metaclass to create rules function from Action and Trigger class
    Every Action or Trigger class that has a 'function_name' attribute will automatically be accessible from the rule file using that
    function name. So that we don't need to create the function in parser.py.

diff --git a/framework/subsystems/oeventsd/action.py b/framework/subsystems/oeventsd/action.py
index 00f0ac6..b346265 100644
--- a/framework/subsystems/oeventsd/action.py
+++ b/framework/subsystems/oeventsd/action.py
@@ -15,11 +15,27 @@ logger = logging.getLogger('oeventsd')
 import dbus
 
 #============================================================================#
+class ActionMetaClass(type):
+#============================================================================#
+    """The meta class for Action class"""
+    def __init__(cls, name, bases, dict):
+        # If an action has a class attribute : 'function_name',
+        # Then we create a new function of that name that create this action
+        super(ActionMetaClass, cls).__init__(name, bases, dict)
+        if 'function_name' in dict:
+            def func(*args):
+                return cls(*args)
+            from parser import Function
+            Function.register(dict['function_name'], func)
+
+#============================================================================#
 class Action(object):
 #============================================================================#
     """
     An action is a functor object that is called by a rule
     """
+    __metaclass__ = ActionMetaClass
+    
     def __init__(self):
         pass
     def __call__(self, **kargs):
@@ -33,6 +49,8 @@ class DebugAction(Action):
     """
     A special action for debugging purposes
     """
+    function_name = 'Debug'
+    
     def __init__(self, msg):
         self.msg = msg
     def __call__(self, **kargs):
@@ -95,6 +113,20 @@ class AudioAction(DBusAction):
         interface = 'org.freesmartphone.Device.Audio'
         method = 'PlaySound' if action == 'play' else 'StopSound'
         super(AudioAction, self).__init__(bus, service, obj, interface, method, scenario)
+        
+#============================================================================#
+class PlaySound(AudioAction):
+#============================================================================#
+    function_name = 'PlaySound'
+    def __init__(self, file):
+        super(PlaySound, self).__init__(file, 'play')
+        
+#============================================================================#
+class StopSound(AudioAction):
+#============================================================================#
+    function_name = 'StopSound'
+    def __init__(self, file):
+        super(StopSound, self).__init__(file, 'stop')
 
 #============================================================================#
 class AudioScenarioAction(DBusAction):
@@ -102,6 +134,8 @@ class AudioScenarioAction(DBusAction):
     """
     A dbus action on the freesmartphone audio device
     """
+    function_name = 'SetScenario'
+    
     def __init__(self, scenario = None, action = 'set' ):
         bus = dbus.SystemBus()
         service = 'org.freesmartphone.odeviced'
@@ -119,6 +153,8 @@ class LedAction(DBusAction):
     """
     A dbus action on an Openmoko Neo LED device
     """
+    function_name = 'SetLed'
+    
     # FIXME device specific, needs to go away from here
     def __init__(self, device, action):
         bus = dbus.SystemBus()
@@ -146,4 +182,14 @@ class VibratorAction(DBusAction):
             super(VibratorAction, self).__init__(bus, service, obj, interface, 'SetBlinking', 300, 700)
         else:
             super(VibratorAction, self).__init__(bus, service, obj, interface, 'SetBrightness', 0)
+            
+class StartVibrationAction(VibratorAction):
+    function_name = 'StartVibration'
+    def __init__(self):
+        super(StartVibrationAction, self).__init__(action='start')
+        
+class StopVibrationAction(VibratorAction):
+    function_name = 'StopVibration'
+    def __init__(self):
+        super(StartVibrationAction, self).__init__(action='stop')
 
diff --git a/framework/subsystems/oeventsd/oevents.py b/framework/subsystems/oeventsd/oevents.py
index 609e4ed..f95809b 100644
--- a/framework/subsystems/oeventsd/oevents.py
+++ b/framework/subsystems/oeventsd/oevents.py
@@ -23,6 +23,8 @@ from action import Action, AudioAction
 from parser import Parser
 from rule import Rule
 
+import ring_tone_action
+
 import dbus
 import os
 
diff --git a/framework/subsystems/oeventsd/parser.py b/framework/subsystems/oeventsd/parser.py
index a412eaf..8bbb753 100644
--- a/framework/subsystems/oeventsd/parser.py
+++ b/framework/subsystems/oeventsd/parser.py
@@ -15,10 +15,8 @@ logger = logging.getLogger('oeventsd')
 import yaml
 import re
 
-from trigger import Trigger, CallStatusTrigger, PowerStatusTrigger, TimeTrigger
 from filter import Filter, AttributeFilter
-from action import Action, AudioAction, AudioScenarioAction, LedAction, VibratorAction, DebugAction
-from ring_tone_action import RingToneAction
+import action
 from rule import Rule
 
 #============================================================================#
@@ -28,14 +26,18 @@ class FunctionMetaClass(type):
     def __init__(cls, name, bases, dict):
         super(FunctionMetaClass, cls).__init__(name, bases, dict)
         if 'name' in dict:
-            logger.debug("register function %s", dict['name'])
-            Function.functions[dict['name']] = cls
+            cls.register(dict['name'], cls())
 
 #============================================================================#
 class Function(object):
 #============================================================================#
     __metaclass__ = FunctionMetaClass
     functions = {}
+    
+    @classmethod
+    def register(cls, name, func):
+        logger.debug("register function %s", name)
+        cls.functions[name] = func
 
     def __call__(self, *args):
         raise NotImplementedError
@@ -67,58 +69,14 @@ def function_constructor(loader, node):
     name = match.group(1)
     params = split_params(match.group(2))
     params = [yaml.load(p) for p in params]
+    if not name in Function.functions:
+        raise Exception("Function %s not registered" % name)
     func = Function.functions[name]
-    return func()(*params)
+    return func(*params)
 
 yaml.add_constructor(u'!Function', function_constructor)
 yaml.add_implicit_resolver(u'!Function', pattern)
 
-# FIXME compute these from the actual triggers and actions
-
-class CallStatus(Function):
-    name = 'CallStatus'
-    def __call__(self):
-        return CallStatusTrigger()
-
-class PowerStatus(Function):
-    name = 'PowerStatus'
-    def __call__(self):
-        return PowerStatusTrigger()
-
-class PlaySound(Function):
-    name = 'PlaySound'
-    def __call__(self, file):
-        return AudioAction(file, 'play')
-
-class StopSound(Function):
-    name = 'StopSound'
-    def __call__(self, file):
-        return AudioAction(file, 'stop')
-
-class SetScenario(Function):
-    name = 'SetScenario'
-    def __call__(self, scenario):
-        return AudioScenarioAction(scenario)
-
-class RingTone(Function):
-    name = 'RingTone'
-    def __call__(self, cmd):
-        return RingToneAction(cmd)
-
-class SetLed(Function):
-    name = 'SetLed'
-    def __call__(self, led, cmd):
-        return LedAction(led, cmd)
-
-class StartVibration(Function):
-    name = 'StartVibration'
-    def __call__(self):
-        return VibratorAction(action='start')
-
-class StopVibration(Function):
-    name = 'StopVibration'
-    def __call__(self):
-        return VibratorAction(action='stop')
 
 class Not(Function):
     name = 'Not'
@@ -131,16 +89,6 @@ class HasAttr(Function):
         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), type(r)
     trigger = r['trigger']
diff --git a/framework/subsystems/oeventsd/ring_tone_action.py b/framework/subsystems/oeventsd/ring_tone_action.py
index ddd9a3e..89145f7 100644
--- a/framework/subsystems/oeventsd/ring_tone_action.py
+++ b/framework/subsystems/oeventsd/ring_tone_action.py
@@ -27,6 +27,8 @@ logger = logging.getLogger('oeventsd')
 #=========================================================================#
 class RingToneAction(Action):
 #=========================================================================#
+    function_name = 'RingTone'
+    
     def __init__( self, cmd = 'play' ):
         self.cmd = cmd
 
diff --git a/framework/subsystems/oeventsd/trigger.py b/framework/subsystems/oeventsd/trigger.py
index 26b0a21..1b6f8e5 100644
--- a/framework/subsystems/oeventsd/trigger.py
+++ b/framework/subsystems/oeventsd/trigger.py
@@ -15,6 +15,20 @@ logger = logging.getLogger('oeventsd')
 import dbus
 
 #============================================================================#
+class TriggerMetaClass(type):
+#============================================================================#
+    """The meta class for Trigger class"""
+    def __init__(cls, name, bases, dict):
+        super(TriggerMetaClass, cls).__init__(name, bases, dict)
+        # If a trigger has a class attribute : 'function_name',
+        # Then we create a new function of that name that create this trigger
+        if 'function_name' in dict:
+            def func(*args):
+                return cls(*args)
+            from parser import Function
+            Function.register(dict['function_name'], func)
+
+#============================================================================#
 class Trigger(object):
 #============================================================================#
     """A trigger is the initial event that will activate a rule.
@@ -23,6 +37,8 @@ class Trigger(object):
        giving a set of keywork arguments (the signal attributes to the method)
        Then the rule can decide to start or not its actions.
     """
+    __metaclass__ = TriggerMetaClass
+    
     def __init__(self):
         """Create a new trigger
 
@@ -94,6 +110,9 @@ class DBusTrigger(Trigger):
 class CallStatusTrigger(DBusTrigger):
 #============================================================================#
     """Just a sugar trigger for a GSM call status change"""
+    
+    function_name = 'CallStatus'
+    
     def __init__(self):
         bus = dbus.SystemBus()
         super(CallStatusTrigger, self).__init__(
@@ -114,6 +133,9 @@ class CallStatusTrigger(DBusTrigger):
 class PowerStatusTrigger(DBusTrigger):
 #============================================================================#
     """Just a sugar trigger for a Power management status change"""
+    
+    function_name = 'PowerStatus'
+    
     def __init__(self):
         bus = dbus.SystemBus()
         super(PowerStatusTrigger, self).__init__(
@@ -133,6 +155,8 @@ class PowerStatusTrigger(DBusTrigger):
 #============================================================================#
 class TimeTrigger(DBusTrigger):
 #============================================================================#
+    function_name = 'Time'
+
     def __init__(self, hour, minute):
         self.hour = hour
         self.minute = minute

-- 
framworkd debian packageing



More information about the pkg-fso-commits mailing list