class CommandsManager(object): def __init__(self, Ini): self.commands = SortedDict() for k, v in Ini.items(): if k.startswith('program:'): kwargs = {} kwargs['name'] = name = k[8:] kwargs['command'] = v.command kwargs['cwd'] = v.get('directory', None) kwargs['logfile'] = v.get('logfile', name+'.log') kwargs['logfile_maxbytes'] = v.get('logfile_maxbytes', 50*1024*1024) kwargs['logfile_backups'] = v.get('logfile_backups', 10) kwargs['startretries'] = v.get('startretries', 3) kwargs['starting_time'] = v.get('starting_time', 1) self.commands[name] = Command(**kwargs) def start(self, command=None): if not command: for k, command in self.commands.items(): command.do_start() else: cmd = self.commands.get(command, '') if not cmd: msg = "Program %s is not found" % command else: msg = cmd.do_start() return msg def stop(self, command): cmd = self.commands.get(command, '') if not cmd: msg = "Program %s is not found" % command else: msg = cmd.do_stop() return msg def shutdown(self): for k, command in self.commands.items(): command.do_stop() msg = 'shutdown successful' return msg def status(self): s = [] for k, command in self.commands.items(): s.append(command.do_status()) return '\n'.join(s) def check(self): for k, p in self.commands.items(): if not p.stop in (STOPPED, FATAL) and not p.is_ok(): p.do_start()
def test_keys_are_sorted(keys): items = [(key, key + 1) for key in keys] sorted_items = sorted(items) sorted_keys = sorted(keys) d = SortedDict(items) assert sorted(d) == sorted_keys assert sorted(d.keys()) == sorted_keys assert d.items() == sorted(items) assert d.values() == sorted(v for k, v in items)
def test_keys_values_items(self): a = SortedDict({'a': 1, 'b': 2}) self.assertListEqual(a.keys(), ['a', 'b']) self.assertListEqual(a.values(), [1, 2]) self.assertListEqual(a.items(), [('a', 1), ('b', 2)])