#!/usr/bin/env python # Copyright 1999-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # Universal Select Tool # Uselect Input/Output Module # uio.py mephx.x@gmail.com import os import pwd import stat # Aligning space = ' ' right = '\t' # Globals bold = lime = red = reset = yellow = notice = '' error = warning = bullet = ok = highlight = '' verbose = False class FileSystem: """ FileSystem Class """ def __init__(self): """ FileSystem Contructor """ self.home = os.getenv('HOME') self.uid = pwd.getpwuid(os.getuid())[0] self.environment = self.home + '/.uselect/' if not os.path.exists(self.environment): os.mkdir(self.environment) self.environment += 'bin/' if not os.path.exists(self.environment): os.mkdir(self.environment) def read_file(self, path): """ Reads a File and returns lines[] """ file = open(path,'r') lines = file.readlines() file.close return lines def write_file(self, path ,lines): """ Writes "lines[]" in File "path" """ if os.path.exists(path): raise Exception("File in " + path + " already Exists!") return file = open(path,'w') for line in lines: file.writelines(line + '\n') file.close return lines def execute_cmd(self, cmd, args): """ Executes "cmd" and returns output """ if not os.path.exists(cmd): raise Exception('File "' + path + '" not found!') return for arg in args: cmd += ' ' + arg return os.popen(cmd).readlines() def delete_file(self, path): """ Deletes file in "path" """ if os.path.exists(path): os.unlink(path) def make_exec_file(self, path): """ Makes file in path u+rwx """ if not os.path.exists(path): raise Exception('File "' + path + '" not found!') return os.chmod(path, stat.S_IXUSR + stat.S_IRUSR + stat.S_IWUSR) def create_symlink(self, source, destination): self.delete_file(destination) os.symlink(source, destination) def list_dir(self, path): """ Lists path directory """ if not os.path.exists(path): raise Exception("File " + path + " not found!") return else: return os.listdir(path) def path_exists(self, path): return os.path.exists(path) def real_path(self, path): return os.path.realpath(path) class PrintSystem: """ PrintSystem Class """ def __init__(self): """ PrintSystem Constructor """ return def verbose(self): global verbose verbose = True return def use_colors(self, usecolors): global bold, lime, red, reset, yellow, error, warning, bullet,\ ok, highlight, notice if usecolors: # Text Colors bold = '\033[1m' lime = '\033[32m' red = '\033[31m' reset = '\033[0m' yellow = '\033[1;33m' blue = '\033[1;34m' # Bullets else: bold = lime = red = reset = yellow = '' error = bold + '(' + red + '!' + reset + bold + ')' + reset warning = bold + '(' + yellow + '!' + reset + bold + ')' + reset bullet = bold + '(' + lime + '*' + reset + bold + ')' + reset notice = bold + '(' + blue + '*' + reset + bold + ')' + reset ok = bold + '(' + lime + '>' + reset + bold + ')' + reset highlight = lime + bold # Glocal Print Functions def print_line(self, line, _verbose = False): """ Prints line with type""" global verbose if _verbose and not verbose: return print line return def print_table(self,list, _verbose = False): """ Prints Lists of the form list[[a,b]] """ global verbose if _verbose and not verbose: return for item in list: print(' ' + item[0] + reset + '\r' + 3 * right + item[1]) return def print_exception(self, exception, iswarning = False): """ Prints Exceptions in a standart way """ if iswarning: type = warning + ' Warning! ' else: type = error + ' Error! ' self.print_line('\n' + type + str(exception) + '\n') def format_action(self, action): table = [] if action.parameters != '': for line in action.usage: table.append([line,'']) table.append(['','']) count = 1 for option in action.options: table.append([option[1] + space + \ eval(option[0]),'']) count += 1 return table # Screen Functions def print_ui(self, module = None, modules = None, \ action = None, args = None): if module == None: # uselect Usage # uselect Options self.print_usage() self.print_line('') self.print_options() self.print_line('') self.print_modules(modules) self.print_line('') elif action == None: # Modules Usage self.print_usage(module) self.print_line('') self.print_module(module) self.print_line('') # Modules Actions self.print_actions(module) self.print_line('') elif args == None: # Actions Usage self.print_usage(module, action) self.print_line('') # Action self.print_action(module, action) self.print_line('') else: # This means Action Done for line in action.output: print(line) return def print_module(self, module): self.print_line(bold + lime + 'Module' + space + reset \ + bold +module.name + lime + ':' + reset) module.get_actions() self.print_line('Author:' + space + module.author + space \ + 'Version:' + space + module.version) def print_modules(self, modules): self.print_line(lime + bold + 'Modules:' + reset) list = [] for module in modules: list.append([bold + module.name, bullet + space + module.description]) self.print_table(list) def print_actions(self, module): self.print_line(highlight + 'Actions:' + reset) if len(module.actions) == 0: print ' Module ' + module.name + \ ' has no actions!' return list = [] for action in module.actions: self.print_table([[bold + action.name + reset, \ action.description]]) def print_action(self, module, action): self.print_table([[bold + action.description + reset, '']]) self.print_line('') self.print_table(self.format_action(action)) def print_version(self, version): self.print_line(bold + 'Universal Select Tool - ' \ + lime + 'uselect' + reset) self.print_line(bold + 'Version ' + reset + version + '\n') def print_usage(self, module = None, action = None): """ General Usage Printer """ if module != None: module_name = module.name else: module_name = '' if action != None: action_name = action.name options = action.parameters else: action_name = '' options = '' self.print_line(bold + lime + 'Usage:' + reset + ' uselect ' + module_name \ + space + action_name + space + options) def print_options(self): self.print_line(highlight + space + 'Options:' + reset) self.print_table([ \ [bold + '-v', bullet + space + 'Verbose Mode'], \ [bold + '-nc', bullet + space + 'No Colors'], \ # [bold + '-profile', bullet + space + 'Profile Mode'], \ [bold + '-version', bullet + space + 'Version Information']])