OptionsParser & Logger is importable from dbuscron
[dbuscron] / dbuscron / parser.py
index 6dcd6cb..0d26c85 100644 (file)
@@ -2,18 +2,15 @@ from __future__ import with_statement
 import re
 from dbuscron.bus import DbusBus
 
-try:
-    from itertools import product
-except ImportError:
-    def product(*args):
-        if args:
-            head, tail = args[0], args[1:]
-            for h in head:
-                for t in product(*tail):
-                    yield (h,) + t
-    
-        else:
-            yield ()
+def product(*args):
+    if args:
+        head, tail = args[0], args[1:]
+        for h in head:
+            for t in product(*tail):
+                yield (h,) + t
+
+    else:
+        yield ()
 
 class CrontabParser(object):
     __fields_sep = re.compile(r'\s+')
@@ -55,26 +52,64 @@ class CrontabParser(object):
                         self.__environ[parts[0]] = parts[1]
                     continue
 
-                rule = [(None,), (None,), (None,), (None,), (None,), (None,), (None,), (None,)]
+                rule = [('s','S'), ('signal','method_call','method_return','error'), (None,), (None,), (None,), (None,), (None,), (None,)]
 
-                for p in range(1, 8):
+                for p in range(0, 8):
                     if parts[p] != '*':
                         rule[p] = parts[p].split(',')
 
                 command = parts[8]
-
-                if parts[0] == '*' or parts[0] == 'S,s' or parts[0] == 's,S':
-                    rule[0] = (self.__bus.system, self.__bus.session)
-                elif parts[0] == 's':
-                    rule[0] = (self.__bus.session,)
-                elif parts[0] == 'S':
-                    rule[0] = (self.__bus.system,)
-                    
                 for r in product(*rule):
+                    r = list(r)
+                    if r[0] == 'S':
+                        r[0] = self.__bus.system
+                    elif r[0] == 's':
+                        r[0] = self.__bus.session
+                    else:
+                        continue
+       
                     if r[7]:
                         r[7] = r[7].split(';')
+
                     ruled = dict()
                     for i, f in enumerate(self.__fields):
                         ruled[f] = r[i]
                     yield ruled, command
 
+class OptionsParser(dict):
+    def __init__(self, opts, args=None):
+        super(OptionsParser, self).__init__()
+
+        if args is None:
+            import sys
+            args = sys.argv[1:]
+
+        from getopt import getopt
+        go, _ = getopt(args, opts)
+
+        for o, v in go:
+            k = o.strip('-')
+            withval = k+':' in opts
+
+            if self.has_key(k):
+                if withval:
+                    if isinstance(self[k], list):
+                        self[k].append(v)
+                    else:
+                        self[k] = [ self[k], v ]
+
+                else:
+                    self[k] += 1
+
+            else:
+                self[k] = v if withval else 1
+
+    def __getitem__(self, k):
+        if not self.has_key(k):
+            return False
+        return super(OptionsParser, self).__getitem__(k)
+
+    def __getattr__(self, k):
+        return self[k]
+