OptionsParser & Logger is importable from dbuscron
[dbuscron] / dbuscron / parser.py
1 from __future__ import with_statement
2 import re
3 from dbuscron.bus import DbusBus
4
5 def product(*args):
6     if args:
7         head, tail = args[0], args[1:]
8         for h in head:
9             for t in product(*tail):
10                 yield (h,) + t
11
12     else:
13         yield ()
14
15 class CrontabParser(object):
16     __fields_sep = re.compile(r'\s+')
17     __envvar_sep = re.compile(r'\s*=\s*')
18     __fields = [
19             'bus_',
20             'type_',
21             'sender_',
22             'interface_',
23             'path_',
24             'member_',
25             'destination_',
26             'args_',
27             #'command'
28             ]
29
30     def __init__(self, fname):
31         self.__bus = DbusBus()
32         self.__filename = fname
33         self.__environ = dict()
34
35     @property
36     def environ(self):
37         return self.__environ
38
39     def __iter__(self):
40         # bus type sender interface path member destination args command
41         with open(self.__filename) as f:
42             for line in f:
43                 line = line.strip()
44
45                 if not line or line.startswith('#'):
46                     continue
47
48                 parts = self.__fields_sep.split(line, 8)
49                 if len(parts) < 9:
50                     parts = self.__envvar_sep(line, 1)
51                     if len(parts) == 2:
52                         self.__environ[parts[0]] = parts[1]
53                     continue
54
55                 rule = [('s','S'), ('signal','method_call','method_return','error'), (None,), (None,), (None,), (None,), (None,), (None,)]
56
57                 for p in range(0, 8):
58                     if parts[p] != '*':
59                         rule[p] = parts[p].split(',')
60
61                 command = parts[8]
62  
63                 for r in product(*rule):
64                     r = list(r)
65                     if r[0] == 'S':
66                         r[0] = self.__bus.system
67                     elif r[0] == 's':
68                         r[0] = self.__bus.session
69                     else:
70                         continue
71         
72                     if r[7]:
73                         r[7] = r[7].split(';')
74
75                     ruled = dict()
76                     for i, f in enumerate(self.__fields):
77                         ruled[f] = r[i]
78                     yield ruled, command
79
80 class OptionsParser(dict):
81     def __init__(self, opts, args=None):
82         super(OptionsParser, self).__init__()
83
84         if args is None:
85             import sys
86             args = sys.argv[1:]
87
88         from getopt import getopt
89         go, _ = getopt(args, opts)
90
91         for o, v in go:
92             k = o.strip('-')
93             withval = k+':' in opts
94
95             if self.has_key(k):
96                 if withval:
97                     if isinstance(self[k], list):
98                         self[k].append(v)
99                     else:
100                         self[k] = [ self[k], v ]
101
102                 else:
103                     self[k] += 1
104
105             else:
106                 self[k] = v if withval else 1
107
108     def __getitem__(self, k):
109         if not self.has_key(k):
110             return False
111         return super(OptionsParser, self).__getitem__(k)
112
113     def __getattr__(self, k):
114         return self[k]
115