import os, re, os.path import ccdb.cmd from ccdb.ccdb_pyllapi import DMySQLDataProvider class ConsoleContext: """ Class to manage console commands This class uses console_utitilities from utils directories """ _utils = {} _verbose = True; _connectionString = "ccdb_user@localhost"; _prov=None _current_path="/" def __init__(self): print "Console context created" import sys print sys.path[0] self.verbose = ccdb.cmd.verbose print ccdb.cmd.verbose print ccdb.cmd.__path__ self._prov = DMySQLDataProvider() @property def verbose(self): """Sets or gets verbose behaviour for this class""" return self._verbose @verbose.setter def verbose(self, isTrue): self._verbose = isTrue; @property def utils(self): return self._utils @property def connection_string(self): return self._connectionString @connection_string.setter def connection_string(self, str): self._connectionString = str @property def provider(self): return self._prov @property def current_path(self): return self._current_path @current_path.setter def current_path(self, newPath): self._current_path = newPath def register_utilities(self, path = None): if(path==None): path = os.path.join(ccdb.cmd.__path__[0],"utils") modules = [] modules = self.search_utils(path) for module in modules: try: registerFunc = getattr(module, "create_util_instance") util = registerFunc() if(util): self._utils[util.command]=util; util.context = self except AttributeError as ex: if (ccdb.cmd.verbose == True): print ex pass if(self.verbose): print "Utils registred in directory %s are:"%(path,) print "%-10s %-15s %s:"%("(command)", "(name)", "(description)") print "\n".join(["%-10s %-15s %s" % (command, util.name, util.shortDescr) for command, util in self._utils.items()]) def register_util(self, utility): #print "Utility registred ", utility.__class__, " \nwhat it do: ", utility.description pass def search_utils(self, path): import imp """Load plugin from directory and return list of modules""" files = os.listdir( path ) test = re.compile(".py$", re.IGNORECASE) files = filter(test.search, files) filenameToModuleName = lambda f: os.path.splitext(f)[0] moduleNames = sorted(map(filenameToModuleName, files)) #f, filename, desc = imp.find_module('plugin') #plugin = imp.load_module('plugin', f, filename, desc) modules = [] #print moduleNames for m in moduleNames: # skip any files starting with '__', such as __init__.py if m.startswith('__'): continue try: f, filename, desc = imp.find_module(m, [path]) modules.append( imp.load_module(m, f, filename, desc)) except ImportError: continue return modules def process(self, args, startIndex=1): #check if there is enough argumens... if(len(args)<(startIndex+1)): self.print_general_usage(); return lastCommand="" workli=args[startIndex:] for token in workli: if(token.startswith('-')): if(token == "-v" or token == "--verbose"): print "set to verbose mode: On" ccdb.cmd.verbose = True else: #it is not a single char token #so lets save it and on next loop #seeing value we will know what to do #with it lastCommand=token #skeep for next token continue else: #it is not a -... or -- command #lets see if last argument was -...something... if(lastCommand == ""): #looks like we found a real command command = token; commandArgs = workli[workli.index(token)+1:] return self.process_command(command, commandArgs) else: #ou... last if(lastCommand == "-s" or lastCommand == "--server"): self._connectionString = token else: #something unknown! self.print_general_usage(); return #lets clear last command lastCommand="" print "\n".join(args) def print_general_usage(self): print "To use utilites print command and arguments" print " help" print "or" print " help ls" print " ls /" def process_command(self, command, commandArgs): if(self.verbose): print "command: ", command print "commandArgs: " print commandArgs #lets do the job... #try to find function... util = None try: util = self._utils[command] except KeyError: print "command ", command," is unknown! please use help to see avalable commands" if util: if not self._prov.IsConnected(): if(ccdb.cmd.verbose): print "Connection string is: ", self.connection_string result = self._prov.Connect(self.connection_string) if not result: print "CCDB provider unable to connect. Aborting command" return util.process(commandArgs) else: return False;