diff options
author | 2009-07-27 18:28:46 +0100 | |
---|---|---|
committer | 2009-07-27 18:28:46 +0100 | |
commit | 2ce51ba0fb303c4c1953559963255125805aefce (patch) | |
tree | fb4ed329765bdbd6ad87b5d4364e6b268babbcfa /umodule.py | |
parent | Changed modules to plain python (diff) | |
download | uselect-2ce51ba0fb303c4c1953559963255125805aefce.tar.gz uselect-2ce51ba0fb303c4c1953559963255125805aefce.tar.bz2 uselect-2ce51ba0fb303c4c1953559963255125805aefce.zip |
Lacked -a on last commit
Diffstat (limited to 'umodule.py')
-rw-r--r-- | umodule.py | 497 |
1 files changed, 303 insertions, 194 deletions
@@ -8,106 +8,144 @@ import re import os +from uio import PrintSystem +from uio import FileSystem +from uio import Counter + +filesystem = FileSystem() +printsystem = PrintSystem() + modules_dir = '/usr/share/uselect/modules/' class Action: + def __init__(self, name = None, lines = None, description = None, type = None): - def __init__(self, name, lines, _filesystem): - - self.filesystem = _filesystem + self.description = description self.name = name - self.lines = lines self.output = [] self.usage = [] self.options = [] - - for line in self.lines: - match = re.match('description "(.*)"', line.lstrip()) - if match: - self.description = match.group(1) - continue - match = re.match('type (.*)', line.lstrip()) - if match: - type = match.group(1) - continue - + self.type = type + if type == 'runnable': self.__class__ = Runnable + elif type == 'env': + self.__class__ = Env elif type == 'sym': if filesystem.uid != 'root': self.__class__ = Path else: self.__class__ = Sym - self.build() + else: + raise UserWarning('Action "' + name + '" has no type set!') + + self.setup() class Runnable(Action): def do_action(self, args): - path = '/tmp/' + self.filename - self.filesystem.write_file(path, self.code) - self.filesystem.make_exec_file(path) - for line in self.filesystem.execute_cmd(path,args): - self.output.append(line[:-1]) - self.filesystem.delete_file(path) - for line in self.output: - self.options.append(['bold', line]) - - def get_code(self): - i = 0 - for line in self.lines: - match = re.match('file (.*) {', line.lstrip()) - if match: - ident = 1 - for char in line: - if char == '\t': - ident += 1 - self.filename = match.group(1) - for line in self.lines[i+1:]: - line = line.rstrip() - line = line.replace('\t', '', ident) - match = re.match('} ' + self.filename, line) - if match: - self.code.append('') - break - self.code.append(line) - i += 1 + if len(args) != 0: + path = '/tmp/' + self.name + filesystem.write_file(path, self.code) + filesystem.make_exec_file(path) + for line in filesystem.execute_cmd(path,args).readlines(): + self.output.append(line[:-1]) + filesystem.delete_file(path) - def build(self): + def add_parameter(self, parameter): + """ + Adds Usage Parameters. + """ + self.parameters.append(parameter) + + def add_usage(self, usage): + """ + Adds Usage Code. + """ + usage = usage.split('\n') + args = [] + path = '/tmp/' + self.name + filesystem.write_file(path, usage) + filesystem.make_exec_file(path) + for line in filesystem.execute_cmd(path,args): + self.usage.append(line[:-1]) + filesystem.delete_file(path) + + def add_code(self, lines = None): + """ + Add Code. + """ + lines = lines.split('\n') + self.code = lines + + def setup(self): self.code = [] - self.get_code() - for line in self.lines: - match = re.match('parameters "(.*)"', line.lstrip()) - if match: - self.parameters = match.group(1) - continue - match = re.match('usage "(.*)"', line.lstrip()) - if match: - self.usage.append(match.group(1)) - continue + self.parameters = [] + self.usage = [] + + def build(self): + + return + class Link: - def __init__(self, name, source_dir, source_regexp, source_target, \ - destination, _filesystem): + def add_link(self, link = None): + """ + Add a sub-link. + """ + self.links.append(link) + link.parent = self + + def __init__(self, lines = None, alias = None, target = None, prefix = None, regexp = None, sufix = ''): - self.name = name - self.destination = destination + self.alias = alias + self.target = prefix + alias + self.prefix = prefix + self.sufix = sufix + self.regexp = regexp self.targets = [] self.status = [] - self.filesystem = _filesystem + self.links = [] + self.destination = self.prefix + self.alias + + + def get_links(self, counter = None): + """ + Builds a nested list with the Link's hierarchy. + """ + links = [] + target = [] + if self.links == []: + return + for link in self.links: + if len(link.targets) == 0: + raise UserWarning('Link "' + link.alias \ + + '" has no targets!') + for i in range(len(link.targets)): + target.append([counter.count, link, i]) + counter.count += 1 + sub_links = link.get_links(counter) + if sub_links != None: + target.append(sub_links) + links.append(target) + return links - for dir in self.filesystem.list_dir(source_dir): - match = re.match(source_regexp, dir) + def build(self): + for link in self.links: + link.build() + for dir in filesystem.list_dir(self.prefix): + match = re.match(self.regexp, dir) if match: - source = source_dir + match.group(0) + source_target + source = self.prefix + match.group(0) + self.sufix self.targets.append(source) - if self.filesystem.path_exists(destination): - if self.filesystem.real_path( \ - self.filesystem.environment + self.name) == \ + if filesystem.path_exists(self.destination): + if filesystem.real_path( \ + filesystem.environment + self.alias) == \ source: self.status.append('notice') - elif self.filesystem.real_path(destination) == source: + elif filesystem.real_path(self.destination) == source: self.status.append('ok') else: self.status.append('warning') @@ -118,157 +156,228 @@ class Link: class Sym(Action): + def build_options(self, links): + """ + Builds available options for PrintSystem. + """ + options = [] + for link in links: + if link == []: + options.append([0, 'No Targets Found!', 'error']) + elif isinstance(link, list) and not isinstance(link[0], int): + options.append(self.build_options(link)) + else: + count = link[0] + i = link[2] + link = link[1] + options.append([count, link.targets[i], link.status[i]]) + return options + - def get_links(self): - for line in self.lines: - match = re.match('sym (.*) (.*) (.*) (.*) (.*)', line.lstrip()) - if match: - name = match.group(1) - source_dir = match.group(3) - source_regexp = match.group(4) - if match.group(5) == '*': - source_target = '' + + def get_targets(self, links, option, family = []): + """ + Builds a nested list of targets. + """ + parent = None + for link in links: + if isinstance(link, list) and not isinstance(link[0], int): + if self.get_targets(link, option, family): + if parent != None: + family.append(parent) + return family else: - source_target = match.group(5) - destination = match.group(2) - self.links.append(Link(name, source_dir, source_regexp,\ - source_target, destination, self.filesystem)) - + family = [] + elif isinstance(link, list) and isinstance(link[0], int): + if option == link[0]: + family.append(link) + return True + else: + parent = link + def do_action(self, args): + """ + Implementation of do_action() for Sym. + """ + links = [] + + counter = Counter() + + for link in self.links: + target = [] + if len(link.targets) == 0: + raise UserWarning('Link "' + link.alias \ + + '" has no targets!') + for i in range(len(link.targets)): + target.append([counter.count, link,i]) + counter.count +=1 + sub_links = link.get_links(counter) + if sub_links != None: + target.append(sub_links) + links.append(target) + + self.options = self.build_options(links) + if len(args) != 0: - if args[0] == 'clear': - for link in self.links: - self.filesystem.delete_file(link.destination) - elif len(args) >= 1: - targets = [] - option = int(args[0]) -1 - for link in self.links: - for i in range(len(link.targets)): - targets.append([link.targets[i], \ - link.destination]) - if option >= 0 and option < len(targets): - self.filesystem.create_symlink(targets[option][0], \ - targets[option][1]) - self.output.append('Setting ' + targets[option][0] \ - + ' success!') - else: - raise UserWarning('Invalid Option "' \ - + args[0] + '"!') - else: - raise UserWarning('Invalid Option "' + args[0] \ - + '"!') + if len(args) >= 1: + for arg in args: + + targets = self.get_targets(links, int(arg)) + + print targets + + if targets != None: + for target in targets: + self.do_target(target) + else: + raise UserWarning('Invalid Option "' + args[0] \ + + '"!') else: - for link in self.links: - for i in range(len(link.targets)): - self.options.append([link.status[i],str(i+1) + ' - ' + \ - link.targets[i]]) - self.parameters = '<target>' + self.parameters = ['<target>','...','<target>'] self.usage.append('Available ' + self.name + ' targets:' ) - - def build(self): + + def do_target(self, target, clear = False): + """ + Activate/Deactivate a target. + """ + i = target[2] + link = target[1] + if clear: + filesystem.delete_file(link.destination) + self.output.append('Unsetting ' + link.destination\ + + ' success!') + else: + filesystem.create_symlink(link.targets[i], \ + link.destination) + self.output.append('Setting ' + link.targets[i]\ + + ' success!') + + def add_link(self, link = None): + """ + Add sub-link. + """ + self.links.append(link) + def setup(self): self.links = [] - self.get_links() + + def build(self): + self.parameters = ['<target>', '(...)', '<target>'] + for link in self.links: + link.build() + +class Var(): + def __init__(self, var, filesystem): + filesystem = filesystem + self.name = var + self.values = [] + + def add_values(self, values, separator = ':', active = True): + """ + Parses a line of values and adds the values. + """ + values = values.split(separator) + for value in values: + self.add_value(value, active) + + def add_value(self, value, active = False): + """ + Add a value. + """ + self.values.append([value, active]) + + def export(self): + filesystem.set_env(self) + + def to_string(self, separator = ':'): + """ + Builds a line of values. + """ + string = '' + for value in self.values: + string += value + separator + return string + + +class Env(Action): + + def do_action(self, args):a + for var in self.vars: + for value in var.values: + if not value[1]: + self.usage.append(var.name) + for value in var.values: + self.usage.append(' ' + value[0]) + + def get_var(self, name): + """ + Gets a var by name. Inits the var if not found. + """ + for var in self.vars: + if var.name == name: + return var + var = Var(name, filesystem) + self.vars.append(var) + return var + + def setup(self): + self.vars = [] + + def build(self): + env = filesystem.get_env() + for var in env: + _var = self.get_var(var[0]) + _var.add_values(var[1]) + self.parameters = ['<varname>', '(...)', '<target>'] + + class Path(Action, Sym): - def do_action(self, args): - if len(args) != 0: - if args[0] == 'clear': - home = self.filesystem.get_home() - for link in self.links: - self.filesystem.delete_file(home + '.uselect/bin/' \ - + link.name) - elif len(args) >= 1: - options = [] - choice = int(args[0]) - 1 - for link in self.links: - for i in range(len(link.targets)): - options.append([link.targets[i], \ - link.destination]) - if choice >= 0 and choice < len(options): - self.filesystem.create_symlink( \ - options[choice][0], - self.filesystem.environment + \ - link.name) - self.output.append('Setting ' + options[choice][0] \ - + ' success!') - else: - raise UserWarning('Invalid Option "' \ - + args[0] + '"!') - else: - raise UserWarning('Invalid Option "' + args[0] \ - + '"!') - else: - for link in self.links: - for i in range(len(link.targets)): - self.options.append([link.status[i],str(i+1) + ' - ' + \ - link.targets[i]]) - self.parameters = '<target>' - self.usage.append('Available ' + self.name + ' targets:' ) - return + def do_target(self, target, clear = False): + """ + Implementation of do_target for Path. + Activates/Deactivates targets. + """ + i = target[2] + link = target[1] + filesystem.create_symlink(link.targets[i], \ + filesystem.environment + link.alias) + self.output.append('Setting ' + link.targets[i] \ + + ' success!') class Module(): - def __init__(self, name, _filesystem): + def __init__(self, name = None , description = None , version = 'Undefined', author = 'Undefined'): global filesystem - filesystem = _filesystem self.name = name - self.version = 'Unknown' - self.author = 'Anonymous' - self.description = name + ' has no description! ' - self.path = modules_dir + self.name + '.uselect' - self.lines = filesystem.read_file(self.path) - self.parse_module(name) + filesystem = filesystem + self.description = description + self.version = version + self.author = author self.actions = [] + return + + + def add_action(self, action = None): + """ + Adds an action to the module. + """ + if action != None: + self.actions.append(action) + else: + raise UserWarning('You must not add an empty action!') - def parse_module(self, name): - for line in self.lines: - line = line.lstrip() - match = re.match('description "(.*)"', line) - if match: - self.description = match.group(1) - continue - match = re.match('version "(.*)"', line) - if match: - self.version = match.group(1) - continue - match = re.match('author "(.*)"', line) - if match: - self.author = match.group(1) - continue - match = re.match('} ' + name, line) - if match: - break - def get_action(self, name): + """ + Gets an action by name. + """ i = 0 - self.get_actions() for action in self.actions: if action.name == name: return action raise Exception('No such action "' + name + '"!') - def get_actions(self): - i = 0 - for line in self.lines: - match = re.match('(\w*) action (\w*)', line.lstrip()) - if match: - wideness = match.group(1) - if wideness == "system" and filesystem.uid != "root": - continue - name = match.group(2) - u = 0 - for line in self.lines[i:]: - match = re.match('} ' + name + '\w*', line) - if match: - lines = self.lines[i:i+u+1] - action = Action(name, lines, filesystem) - self.actions.append(action) - break - u += 1 - if name == '(\w*)': - break - i += 1 + |