def test_bind_collection(self): class TestBoundData(BoundData): pass class TestChildData(BoundData): def template(self): return Node('', children=[PropertyNode('value', None)]) TestBoundData.bind_collection('items', item_class=TestChildData, selector=lambda x: x.name != 'test') TestChildData.bind_property('value', 'value') n = Node('name', children=[ Node('1', children=[PropertyNode('value', 1)]), Node('2', children=[PropertyNode('value', 2)]), Node('test', children=[PropertyNode('value', 3)]), Node('3', children=[PropertyNode('value', 3)]), ]) d = TestBoundData(n) self.assertEqual(d.items[0].value, 1) self.assertEqual(len(d.items), 3) c = TestChildData() c.value = 4 d.items.append(c) self.assertEqual(len(d.items), 4) self.assertEqual(d.items[-1].value, 4)
def create_destination(): return OptionData( Node( 'option', Node('argument', PropertyNode('value', 'ACCEPT')), PropertyNode('negative', False), PropertyNode('name', 'j'), ))
def template(self): return Node('line', children=[ Node('token', children=[PropertyNode('value', 'nameserver')]), Node('token', children=[PropertyNode('value', '8.8.8.8')]), ])
def template(self): return Node('line', children=[ Node('token', children=[PropertyNode('value', '127.0.0.1')]), Node('token', children=[PropertyNode('value', 'eth0')]), ])
def create(template_id): t = OptionData.templates[template_id] return OptionData( Node( 'option', *([Node('argument', PropertyNode('value', x)) for x in t[1]] + [PropertyNode('negative', False)] + [PropertyNode('name', t[0])])))
def template(self, name, *args): children = [PropertyNode('1', name)] index = 2 for arg in args: children += [PropertyNode(str(index), arg)] index += 1 return Node('line', PropertyNode('name', 'acl'), Node('arguments', *children))
def template(self): return Node( 'append', Node( 'option', Node('argument', PropertyNode('value', 'ACCEPT')), PropertyNode('negative', False), PropertyNode('name', 'j'), ))
def test_bind_property(self): class TestBoundData (BoundData): pass TestBoundData.bind_property('prop', 'dataprop', getter=lambda x: 'd' + x, setter=lambda x: x[1:]) n = Node('name', children=[ PropertyNode('prop', 'value') ]) d = TestBoundData(n) self.assertEqual(d.dataprop, 'dvalue') d.dataprop = 'dnew' self.assertEqual(d.dataprop, 'dnew') self.assertEqual(n.get('prop').value, 'new')
def template(self): return Node( 'zone', PropertyNode('type', 'master'), PropertyNode('file', 'db.example.com'), parameter='"example.com"', )
def template(self): return Node( 'unnamed', PropertyNode('configs', {}), PropertyNode('password', ''), PropertyNode('permissions', []), )
def test_bind_property(self): class TestBoundData(BoundData): pass TestBoundData.bind_property('prop', 'dataprop', getter=lambda x: 'd' + x, setter=lambda x: x[1:]) n = Node('name', children=[PropertyNode('prop', 'value')]) d = TestBoundData(n) self.assertEqual(d.dataprop, 'dvalue') d.dataprop = 'dnew' self.assertEqual(d.dataprop, 'dnew') self.assertEqual(n.get('prop').value, 'new')
def template(self, **kwargs): return Node('normal_task', children=[ PropertyNode('minute', '0'), PropertyNode('hour', '0'), PropertyNode('day_of_month', '1'), PropertyNode('month', '1'), PropertyNode('day_of_week', '1'), PropertyNode('command', 'false') ])
def template(self): return Node('line', children=[ Node('token', children=[PropertyNode('value', 'none')]), Node('token', children=[PropertyNode('value', 'none')]), Node('token', children=[PropertyNode('value', 'auto')]), Node('token', children=[PropertyNode('value', 'none')]), Node('token', children=[PropertyNode('value', '0')]), Node('token', children=[PropertyNode('value', '0')]), ])
class CrontabParserTest(BaseParserTest): parser = CrontabParser() source = '\n'.join([ '#comment line', '* * * * * date', '@reboot ls -al', '1 * 0 1 2 date -s', 'NAME = TEST', ]) parsed = RootNode(None, children=[ Node('normal_task', comment='comment line', children=[ PropertyNode('minute', '*'), PropertyNode('hour', '*'), PropertyNode('day_of_month', '*'), PropertyNode('month', '*'), PropertyNode('day_of_week', '*'), PropertyNode('command', 'date'), ]), Node('special_task', children=[ PropertyNode('special', '@reboot'), PropertyNode('command', 'ls -al'), ]), Node('normal_task', children=[ PropertyNode('minute', '1'), PropertyNode('hour', '*'), PropertyNode('day_of_month', '0'), PropertyNode('month', '1'), PropertyNode('day_of_week', '2'), PropertyNode('command', 'date -s'), ]), Node('env_setting', children=[ PropertyNode('name', 'NAME'), PropertyNode('value', 'TEST'), ]), ])
def template(self): return Node('custom')
def template(self, **kwargs): return Node('special_task', children=[ PropertyNode('special', '@reboot'), PropertyNode('command', 'false') ])
def template(self): return Node( 'CUSTOM', PropertyNode('default', '-'), )
def template(self): return Node('program:new', PropertyNode('command', 'false'), )
def template(self, **kwargs): return Node('env_setting', children=[ PropertyNode('name', 'ENV_NAME'), PropertyNode('value', 'ENV_VALUE') ])
def parse(self, content): root = RootNode() lines = [l.strip() for l in content.splitlines() if l] comment = None for line in lines: if line.startswith('#'): comment = '\n'.join([comment, line]) if comment else line[1:] continue elif line.startswith('@'): special, command = line.split(' ', 1) node = Node('special_task', comment=comment) node.append(PropertyNode('special', special)) node.append(PropertyNode('command', command)) else: split_line = line.split(' ', 5) if len(split_line) <= 3 and '=' in line: name, value = [n.strip() for n in line.split('=')] if not name: continue node = Node('env_setting', comment=comment) node.append(PropertyNode('name', name)) node.append(PropertyNode('value', value)) elif len(split_line) == 6: node = Node('normal_task', comment=comment) node.append(PropertyNode('minute', split_line[0])) node.append(PropertyNode('hour', split_line[1])) node.append(PropertyNode('day_of_month', split_line[2])) node.append(PropertyNode('month', split_line[3])) node.append(PropertyNode('day_of_week', split_line[4])) node.append(PropertyNode('command', split_line[5])) else: continue root.append(node) comment = None root.comment = comment return root
def template(self): return Node( 'program:new', PropertyNode('command', '127.0.0.1'), )
def template(self): return Node( 'line', PropertyNode('name', 'http_access'), Node('arguments', PropertyNode('1', '')) )
def template(self): return Node( 'line', PropertyNode('name', 'https_port'), Node('arguments', PropertyNode('1', '3128')) )
def remove_include(self, node): return Node('include', children=[PropertyNode('files', node.files)])
def template(self): return Node('localhost', PropertyNode('options', ''))
def template(self): return Node('/', Node('clients'))
def template(self): return Node( 'line', Node('token', PropertyNode('value', 'mibs')), Node('token', PropertyNode('value', 'IF-MIB')), )
def template(self): return Node( 'line', *[ Node('token', children=[PropertyNode('value', '')]) for x in GroupData.fields ])
def template(self): return Node( 'line', Node('token', PropertyNode('value', 'rwcommunity')), Node('token', PropertyNode('value', 'public 192.168.0.0/24')), )
def template(self): return Node( 'line', Node('token', PropertyNode('value', 'informsink')), Node('token', PropertyNode('value', 'localhost public')), )
def template(self): return Node( 'share', *[PropertyNode(x, y) for x, y in zip(ShareData.fields, ShareData.defaults)] )