configurationengine/source/scripts/cone_subaction.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
--- a/configurationengine/source/scripts/cone_subaction.py	Fri Mar 12 08:30:17 2010 +0200
+++ b/configurationengine/source/scripts/cone_subaction.py	Tue Aug 10 14:29:28 2010 +0300
@@ -19,8 +19,46 @@
 import fnmatch 
 import re
 import logging
+import ConfigParser
 
 
+def get_cfg_files(paths, filename):
+    """ Find out the cone subscripts that are present in the"""
+    files = []
+    for path in paths:
+        cfgfile = os.path.join(path, filename)
+        if os.path.exists(cfgfile):
+            files.append(cfgfile)
+    return files
+
+def get_config(cfgfiles):
+    config = ConfigParser.ConfigParser()
+    config.read(cfgfiles)
+    return config
+
+def get_actions(cfgfiles):
+    """ Find out the cone subscripts that are present in the"""
+    subacts = ActionContainer()
+    config = get_config(cfgfiles)
+    actions_section = 'actions'
+    if not config.has_section(actions_section):
+        raise Exception('The cone.ini does not have any %s section.' % actions_section)
+    for (name, section) in config.items(actions_section):
+        subacts += get_subactions_from_configs(cfgfiles, section)     
+    return subacts
+
+def get_subactions_from_configs(cfgfiles, section):
+    """ Find out the cone subscripts that are present in the"""
+    subacts = ActionContainer()
+    config = get_config(cfgfiles)
+    paths = [os.path.dirname(cfgfile) for cfgfile in cfgfiles]
+    if not config.has_section(section):
+        raise Exception('The cone.ini does not have any %s section.' % section)
+    for (commandname, path) in config.items(section):
+        subacts.append(ConeAction(commandname, path, type=section, paths=paths))
+    
+    return subacts
+
 def get_subactions(path,pattern):
     """ Find out the cone subscripts that are present in the ROOT_PATH """
     subacts = ActionContainer()
@@ -42,24 +80,70 @@
     elif level == 5 : return logging.DEBUG
     else : return logging.NOTSET
 
-class ActionContainer(object):
-    def __init__(self):
-        self._actions = {}
+class ActionContainer(list):
+    pass
 
-    def __len__(self):
-        return len(self._actions)
-
+#    def __init__(self):
+#        self._actions = {}
+#
+#    def __len__(self):
+#        return len(self._actions)
+#
     def __getitem__(self, key):
-        return self._actions[key]
+        for item in self:
+            if item.name == key:
+                return item
+        raise KeyError('Key %s not found in %s'  % (key, self))
+#
+#    def __setitem__( self, key, value):
+#        self._actions[key] = value
+#
+#    def __delitem__( self, key):
+#        del self._actions[key]
+#
+#    def __iter__( self):
+#        return self._actions.__iter__()
 
-    def __setitem__( self, key, value):
-        self._actions[key] = value
+class ConeAction(object):
+    def __init__(self, name, path, **kwargs):
+        self.name = name
+        self.path = path
+        self.type = kwargs.get('type', '')
+        self.paths = kwargs.get('paths', [])
+        self._module = None
+
+    @property
+    def module_name(self):
+        return os.path.basename(self.path)
 
-    def __delitem__( self, key):
-        del self._actions[key]
+    @property
+    def module_path(self):
+        return os.path.dirname(self.path)
 
-    def __iter__( self):
-        return self._actions.__iter__()
+    @property
+    def module(self):
+        if not self._module:
+            paths = [os.path.join(pth, self.module_path) for pth in self.paths]
+            paths.append(self.module_path)
+            sys.path += paths
+            try:
+                self._module  = __import__(self.module_name)
+            finally:
+                del sys.path[-len(paths):]
+        return self._module
+    
+    def short_help(self):
+        if hasattr(self.module, 'short_help'):
+            return self.module.short_help
+        elif hasattr(self.module, 'main'):
+            helpstr = self.module.main.__doc__ or '<None>'
+            return helpstr.replace('\n', '').strip(' ')[:50]
+        else:
+            return 'Not a valid module!!'
+        
+    def run(self):
+        self.module.main()
+
 
 class SubAction(object):
     def __init__(self, scriptname, pattern):