1 from __future__ import with_statement
3 from dbuscron.bus import DbusBus
7 head, tail = args[0], args[1:]
9 for t in product(*tail):
15 class CrontabParserError(SyntaxError):
16 def __init__(self, message, lineno, expected=None):
18 if isinstance(expected, (tuple, list)):
19 exp = ' (expected %s or %s)' % (', '.join(expected[:-1]), expected[-1])
23 msg = '%s%s at line %d' % (message, exp, lineno)
25 SyntaxError.__init__(self, msg)
27 class CrontabParser(object):
28 __fields_sep = re.compile(r'\s+')
29 __envvar_sep = re.compile(r'\s*=\s*')
32 'type_' : ('signal','method_call','method_return','error'),
34 'interface_' : re.compile(r'^[a-zA-Z][a-zA-Z0-9_.]+$'),
35 'path_' : re.compile(r'^/[a-zA-Z0-9_/]+$'),
36 'member_' : re.compile(r'^[a-zA-Z][a-zA-Z0-9_]+$'),
37 'destination_' : None,
51 def __init__(self, fname):
52 self.__bus = DbusBus()
53 self.__filename = fname
54 self.__environ = dict()
61 # bus type sender interface path member destination args command
63 with open(self.__filename) as f:
68 if not line or line.startswith('#'):
71 parts = self.__fields_sep.split(line, 8)
73 parts = self.__envvar_sep(line, 1)
75 self.__environ[parts[0]] = parts[1]
78 raise CrontabParserError('Unexpected number of records', lineno)
80 rule = [('s','S'), self.__fields_chk['type_'], (None,), (None,), (None,), (None,), (None,), (None,)]
84 rule[p] = parts[p].split(',')
88 for r in product(*rule):
91 r[0] = self.__bus.system
93 r[0] = self.__bus.session
95 raise CrontabParserError('Unexpected bus value', lineno, expected=('S', 's', '*'))
98 r[7] = r[7].split(';')
101 for i, f in enumerate(self.__fields):
102 if self.__fields_chk[f]:
103 if isinstance(self.__fields_chk[f], tuple):
104 if r[i] not in self.__fields_chk[f]:
105 raise CrontabParserError('Unexpected %s value' % (f.strip('_')), lineno,
106 expected=self.__fields_chk[f])
108 if not self.__fields_chk[f].match(r[i]):
109 raise CrontabParserError('Incorrect %s value' % (f.strip('_')), lineno)
114 class OptionsParser(dict):
115 def __init__(self, opts, args=None):
116 super(OptionsParser, self).__init__()
122 from getopt import getopt
123 go, _ = getopt(args, opts)
127 withval = k+':' in opts
131 if isinstance(self[k], list):
134 self[k] = [ self[k], v ]
140 self[k] = v if withval else 1
142 def __getitem__(self, k):
143 if not self.has_key(k):
145 return super(OptionsParser, self).__getitem__(k)
147 def __getattr__(self, k):