def scan_accelerators(self, modules=None): if modules == None: self._accel_group = AccelGroup() modules = self.modules() recurse_mods = [] for mod in modules: if type(mod) == types.ModuleType: recurse_mods.append(mod) else: accel = mod.accelerator() if accel != None: self._accel_group.add(accel, self.accelerator_activated, mod) for mod in recurse_mods: self.scan_accelerators(mod.commands())
class Commands(Singleton): class Continuated: def __init__(self, generator): self.generator = generator self.retval = None def autocomplete_func(self): if retval == result.Result.PROMPT: return retval.autocomplete else: return {} class State: def __init__(self): self.clear() def clear(self): self.stack = [] def top(self): return self.stack[0] def run(self, ret): ct = self.top() if ret: ct.retval = ct.generator.send(ret) else: ct.retval = ct.generator.next() return ct.retval def push(self, gen): self.stack.insert(0, Commands.Continuated(gen)) def pop(self): if not self.stack: return try: self.stack[0].generator.close() except GeneratorExit: pass del self.stack[0] def __len__(self): return len(self.stack) def __nonzero__(self): return len(self) != 0 def __init_once__(self): self._modules = None self._dirs = [] self._monitors = [] self._accel_group = None self._timeouts = {} self._stack = [] def set_dirs(self, dirs): self._dirs = dirs def stop(self): for mon in self._monitors: mon.cancel() self._monitors = [] self._modules = None for k in self._timeouts: glib.source_remove(self._timeouts[k]) self._timeouts = {} def accelerator_activated(self, accel, mod, state, entry): self.run(state, mod.execute('', [], entry, 0, accel.arguments)) def scan_accelerators(self, modules=None): if modules == None: self._accel_group = AccelGroup() modules = self.modules() recurse_mods = [] for mod in modules: if type(mod) == types.ModuleType: recurse_mods.append(mod) else: accel = mod.accelerator() if accel != None: self._accel_group.add(accel, self.accelerator_activated, mod) for mod in recurse_mods: self.scan_accelerators(mod.commands()) def accelerator_group(self): if not self._accel_group: self.scan_accelerators() return self._accel_group def modules(self): self.ensure() return list(self._modules) def add_monitor(self, d): gfile = gio.File(d) monitor = None try: monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None) except gio.Error, e: # Could not create monitor, this happens on systems where file monitoring is # not supported, but we don't really care pass if monitor: monitor.connect('changed', self.on_monitor_changed) self._monitors.append(monitor)