From 4c05971e84cd1056e748bab07593454326c6a6d8 Mon Sep 17 00:00:00 2001 From: Sérgio Almeida Date: Tue, 28 Jul 2009 04:44:28 +0100 Subject: uprofile structure done improved filesystem/printsystem sharing --- uio.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++------------ umodule.py | 22 +++++++++---- uprofile.py | 27 +++++++++++++--- uselect.py | 7 ++-- 4 files changed, 126 insertions(+), 34 deletions(-) diff --git a/uio.py b/uio.py index 013ea3e..d99d958 100644 --- a/uio.py +++ b/uio.py @@ -10,6 +10,9 @@ import pwd import stat import subprocess + + + # Aligning space = ' ' right = '\t' @@ -108,13 +111,18 @@ class FileSystem: def real_path(self, path): return os.path.realpath(path) - - + class PrintSystem: """ PrintSystem Class """ + + def set_type(self, type): + if type == 'profile': + self.__class__ = ProfilePrintSystem - def __init__(self): + def __init__(self, profile = False): """ PrintSystem Constructor """ + if profile: + self.__class__ = ProfilePrintSystem return def verbose(self): @@ -236,36 +244,27 @@ class PrintSystem: print(line) return - def print_uprofile_ui(self, profile = None, profiles = None, args = None): - if profile == None: - self.print_profiles(profiles) - - def print_profiles(self, profiles): - self.print_line(bold + lime + 'Profiles:' + reset) - table = [] - for profile in profiles: - table.append([bold + profile.name, profile.description]) - - self.print_table(table) + def print_module(self, module): - self.print_line(bold + lime + 'Module' + space + reset \ - + bold +module.name + lime + ':' + reset) - self.print_line('Author:' + space + module.author + space \ - + 'Version:' + space + module.version) + self.print_line(highlight + space + 'Module' + space + reset \ + + bold + module.name + lime + ':' + reset) + self.print_line(space * 4 + bold + 'Author:' + reset + space + \ + module.author + space + bold + 'Version:' + reset + space \ + + module.version) def print_modules(self, modules): - self.print_line(lime + bold + 'Modules:' + reset) + self.print_line(highlight + space + '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) + self.print_line(highlight + space + 'Actions:' + reset) if len(module.actions) == 0: - print ' Module ' + module.name + \ + print space * 4 + bold + '"' + module.name + '"' + \ ' has no actions!' return list = [] @@ -308,3 +307,66 @@ class PrintSystem: # [bold + '-profile', bullet + space + 'Profile Mode'], \ [bold + '-version', bullet + space + 'Version Information']]) + +class ProfilePrintSystem(PrintSystem): + + def print_ui(self, profile = None, profiles = None, args = None, \ + action = None): + self.print_usage(profile = profile, action = action) + self.print_line('') + if profile == None: + self.print_options() + self.print_line('') + self.print_profiles(profiles) + self.print_line('') + elif profiles == None: + self.print_profile(profile) + self.print_line('') + self.print_actions(profile) + self.print_line('') + + def print_profiles(self, profiles): + self.print_line(highlight + space + 'Profiles:' + reset) + table = [] + for profile in profiles: + table.append([bold + profile.name, bullet + space + profile.description]) + self.print_table(table) + + def print_profile(self, profile): + self.print_line(highlight + space + 'Profile' + space + reset \ + + bold + profile.name + lime + ':' + reset) + self.print_line(space * 4 + bold + 'Author:' + reset + space + \ + profile.author + space + bold + 'Version:' + reset + space \ + + profile.version) + + + + 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 Profile Tool - ' \ + + lime + 'uprofile' + reset) + self.print_line(bold + 'Version ' + reset + version + '\n') + + def print_usage(self, profile = None, action = None): + """ General Usage Printer """ + options = '' + if profile != None: + profile_name = profile.name + else: + profile_name = '' + if action != None: + action_name = action.name + for parameter in action.parameters: + options += parameter + space + else: + action_name = '' + + self.print_line(bold + lime + 'Usage:' + reset + ' uprofile ' + profile_name \ + + space + action_name + space + options) + +filesystem = FileSystem() +printsystem = PrintSystem() diff --git a/umodule.py b/umodule.py index f4d318a..567e7fb 100644 --- a/umodule.py +++ b/umodule.py @@ -8,12 +8,10 @@ import re import os -from uio import PrintSystem -from uio import FileSystem from uio import Counter +from uio import filesystem +from uio import printsystem -filesystem = FileSystem() -printsystem = PrintSystem() modules_dir = '/usr/share/uselect/modules/' @@ -37,6 +35,8 @@ class Action: self.__class__ = Path else: self.__class__ = Sym + elif type == 'profile': + self.__class__ = ProfileAction else: raise UserWarning('Action "' + name + '" has no type set!') @@ -299,7 +299,14 @@ class Var(): string += value + separator return string + +class ProfileAction(Action): + def do_action(self, args): + print 'Done!' + def setup(self): + return + class Env(Action): def do_action(self, args): @@ -348,10 +355,11 @@ class Path(Action, Sym): class Module(): - def __init__(self, name = None , description = None , version = 'Undefined', author = 'Undefined'): - global filesystem + def __init__(self, name = None , description = None , \ + version = 'Undefined', author = 'Undefined', \ + _filesystem = None, _printsystem = None): + self.name = name - filesystem = filesystem self.description = description self.version = version self.author = author diff --git a/uprofile.py b/uprofile.py index ad96eed..3945ebf 100755 --- a/uprofile.py +++ b/uprofile.py @@ -13,13 +13,27 @@ import string import traceback from umodule import * -from uio import * +from uio import filesystem +from uio import printsystem + + +verbose = False +printsystem.set_type('profile') class Profile: def __init__(self, name): self.name = name + self.author = 'unnamed' + self.version = '0.1' self.description = 'Empty' + self.actions = [] + self.actions.append(Action(name = 'set', \ + description = 'Set this profile for this folder.', \ + type = 'profile')) + self.actions.append(Action(name = 'default', \ + description = 'Set this profile the default profile.', \ + type = 'profile')) return class UniversalProfileTool: @@ -28,6 +42,10 @@ class UniversalProfileTool: self.profiles = [] return + def get_profile(self, name): + profile = Profile(name) + return profile + def get_profiles(self): """ Returns the list of available uprofiles """ for profile in filesystem.list_dir('.uprofile/'): @@ -51,7 +69,8 @@ class UniversalProfileTool: if len(args) < 1: self.get_profiles() profiles = self.profiles - + elif len(args) == 1: + profile = self.get_profile(args[0]) if len(args) == 2: args = None else: @@ -65,14 +84,14 @@ def main(): try: list = uprofile.parse_argv(sys.argv[1:]) - printsystem.print_uprofile_ui(profile = list[0], \ + printsystem.print_ui(profile = list[0], \ profiles = list[1], args = list[2]) except UserWarning, warning: printsystem.print_exception(warning, True) except Exception, exception: printsystem.print_exception(exception) - if not verbose: + if verbose: traceback.print_exc() printsystem.print_line('') exit(1) diff --git a/uselect.py b/uselect.py index e266b8c..e81b470 100755 --- a/uselect.py +++ b/uselect.py @@ -12,9 +12,12 @@ import stat import string import traceback + +from uio import filesystem +from uio import printsystem + from umodule import * -from uio import * - + verbose = False version = '0.2' -- cgit v1.2.3-65-gdbad