#!/usr/bin/env python # Copyright 1999-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # Universal Select Tool # Base Module # umodule.py mephx.x@gmail.com import os import re import sys import stat import string import traceback from umodule import Module from uio import PrintSystem from uio import FileSystem verbose = False version = '0.2' modules_dir = '/usr/share/uselect/modules/' class UniversalSelectTool: printsystem = PrintSystem() filesystem = FileSystem() def __init__(self): return def get_modules(self): list = self.filesystem.list_dir(modules_dir) self.modules = [] for file in list: match = re.match('(.*).uselect', file) if match: self.modules.append(Module(match.group(1), self.filesystem)) def get_module(self, name): if not os.path.exists(modules_dir + name + '.uselect'): raise Exception('Module ' + name + ' not found!') return else: module = Module(name, self.filesystem) return module def parse_argv(self, argv): global profile, verbose, version module = None modules = None action = None args = None self.printsystem.use_colors(True) for arg in argv: if arg == '-v': verbose = True self.printsystem.verbose() argv = argv[1:] elif arg == '-nc': self.printsystem.use_colors(False) argv = argv[1:] elif arg == '-version': self.printsystem.print_version(version) argv = argv[1:] if len(argv) < 1: self.get_modules() modules = self.modules elif len(argv) == 1: module = self.get_module(argv[0]) elif len(argv) >= 2: module = self.get_module(argv[0]) action = module.get_action(argv[1]) action.do_action(argv[2:]) if len(argv) == 2: argv = None else: argv = argv[2:] return [module, modules, argv, action] def main(): uselect = UniversalSelectTool() try: list = uselect.parse_argv(sys.argv[1:]) uselect.printsystem.print_ui(module = list[0], \ modules = list[1], args = list[2], action = list[3]) except UserWarning, warning: uselect.printsystem.print_exception(warning, True) except Exception, exception: uselect.printsystem.print_exception(exception) if verbose: traceback.print_exc() printsystem.print_line('') exit(1) exit(0) if __name__ == '__main__': main()