class Completion(): def __init__(self, raw_input): #init framework core dependencies #self.env = env.env() #self.loader = loader.loader() #self.vfs = vfs.vfs() #self.lmodules = self.loader.modules self.api = ApiManager() self.env = self.api.env() self.loader = self.api.loader() self.vfs = self.api.vfs() self.lmodules = self.loader.get_modules() self.OS = self.api.OS() self.console = raw_input def get_completion_scope(self, arg, begidx): cur_arg = None prev_arg = None opt = [] for a in arg: opt.append(a.arg) if begidx <= a.end: if begidx >= a.start: cur_arg = a.arg elif cur_arg == None: cur_arg = "" if self.is_cmd_arg(a.arg): if cur_arg == None: opt = [] opt.append(a.arg) else: break return opt, cur_arg #print "\ncurrent argument:", cur_arg, "of completion scope:", opt def complete_node(self): #print "complete node" rpath = "" supplied = "" out = {"type": "path", "matches": [], "length": 1, "supplied": "", "matched": 0} path = self.cur_str if path == "" or path[0] != "/": if self.vfs.getcwd().path == "" and self.vfs.getcwd().name == "": rpath = "/" else: rpath = self.vfs.getcwd().path + "/" + self.vfs.getcwd().name + "/" if path == "/": path = "//" idx = path.rfind("/") if idx == -1: supplied = path else: supplied = path[idx+1:] rpath += path[:idx] try: rpath = rpath.replace("\ ", " ") node = self.vfs.getnode(rpath) except OSError, e: out["matches"].append("") supplied = supplied.replace("\ ", " ") out["supplied"] = supplied if node: if node.empty_child(): if self.cur_str == "/": out["matches"].append("") else: out["matches"].append("/") out["matched"] += 1 else: #list = node.next() list = node.next #for i in range(node.next.size()): # print node.next[i].name #completion on a path if supplied == "": for i in list: if not i.empty_child(): if len(i.name + "/") > out["length"]: out["length"] = len(i.name + "/") out["matches"].append(i.name + "/") else: if len(i.name) > out["length"]: out["length"] = len(i.name) out["matches"].append(i.name) out["matched"] += 1 else: for i in list: if i.name.startswith(supplied) == True: if not i.empty_child(): if len(i.name + "/") > out["length"]: out["length"] = len(i.name + "/") out["matches"].append(i.name + "/") else: if len(i.name) > out["length"]: out["length"] = len(i.name) out["matches"].append(i.name) out["matched"] += 1 return out
class Line_to_arguments(): def __init__(self): self.api = ApiManager() self.loader = self.api.loader() self.vfs = self.api.vfs() def generate(self, line): self.args, bopt = utils.split_line(line) cmds = [] cmd = [] generated_arguments = [] #generated_args for a in self.args: if a in [";", "<", ">", "&", "|", "&&"]: pass else: cmd.append(a) cmds.append(cmd) for cmd in cmds: gen_arg = self.argument("input") gen_arg.thisown = 0 if (self.string_to_arguments(cmd, gen_arg) != -1): exc = {} exc[cmd[0]] = gen_arg generated_arguments.append(exc) else: pass return generated_arguments def node_to_argument(self, key, value, gen_arg): res = 0 if value == None: node = self.vfs.getnode(value) else: value = value.replace("\ ", " ") node = self.vfs.getnode(value) if node != None: gen_arg.add_node(key, node) else: print "Value error: node <", value, "> doesn't exist" res = -1 return res def path_to_argument(self, key, value, gen_arg): res = 0 if value == None: abs_path = os.getcwd() else: value = value.replace("\ ", " ") abs_path = utils.get_absolute_path(value) if os.path.exists(abs_path): gen_arg.add_path(key, str(abs_path)) else: print "Value error: local path <", value, "> doesn't exist" res = -1 return res def bool_to_argument(self, key, value, gen_arg): if value == None: gen_arg.add_bool(key, int(0)) else: gen_arg.add_bool(key, int(1)) return 0 def string_to_argument(self, key, value, gen_arg): if value == None: gen_arg.add_string(key, str("")) else: gen_arg.add_string(key, str(value)) return 0 def int_to_argument(self, key, value, gen_arg): res = 0 if value == None: gen_arg.add_int(key, int(-1)) else: if utils.is_hex(value): gen_arg.add_int(key, int(value, 16)) elif utils.is_int(value): gen_arg.add_int(key, int(value)) else: print "Value error: value <", value, "> cannot be converted to int" res = -1 return res def get_func_generator(self, cdl): func_name = cdl.type + "_to_argument" func = None if hasattr(self, func_name): func = getattr(self, func_name) else: print "Type error: type <", cdl.type, "> associated to argument <", cdl.name, "> doesn't exist" return func def several_argument(self, cdl, args, gen_arg): res = 0 i = 0 needs_no_key = utils.needs_no_key(cdl) priority = utils.keyPriority(cdl) arg_with_no_key = utils.get_arg_with_no_key(args) while i != (len(cdl)) and res == 0: func = self.get_func_generator(cdl[i]) if func != None: key = "--" + cdl[i].name if key not in args: if cdl[i].optional == False: if needs_no_key != None and arg_with_no_key != -1: value = args[arg_with_no_key] res = func(cdl[i].name, value, gen_arg) arg_with_no_key = -1 else: print "Argument error: the argument <", cdl[i].name, "> is required by command: <", args[0], ">" res = -1 else: if arg_with_no_key != -1 and len(priority[0]) == 0 and len(priority[1]) == 1: if cdl[i].name == priority[1][0].name: value = args[arg_with_no_key] res = func(cdl[i].name, value, gen_arg) else: res = func(cdl[i].name, None, gen_arg) else: res = func(cdl[i].name, None, gen_arg) else: key_idx = args.index(key) if cdl[i].type == "bool": res = func(cdl[i].name, "", gen_arg) else: if (key_idx) == len(args) - 1: print "Key error: the argument <", key, "> needs a value" res = -1 else: value = args[key_idx + 1] res = func(cdl[i].name, value, gen_arg) else: res = -1 i += 1 return res def string_to_arguments(self, args, gen_arg): try: if not self.loader.is_modules(args[0]) : print "dff:" + args[0] + ": command not found" except IndexError: return -1 conf = self.loader.get_conf(args[0]) if conf != None: cdl = conf.descr_l if cdl == None: return gen_arg res = self.several_argument(cdl, args, gen_arg) if res == -1: return -1 else: return gen_arg else: return -1
class Completion(): def __init__(self, raw_input): #init framework core dependencies #self.env = env.env() #self.loader = loader.loader() #self.vfs = vfs.vfs() #self.lmodules = self.loader.modules self.api = ApiManager() self.env = self.api.env() self.loader = self.api.loader() self.vfs = self.api.vfs() self.lmodules = self.loader.get_modules() self.OS = self.api.OS() self.console = raw_input def get_completion_scope(self, arg, begidx): cur_arg = None prev_arg = None opt = [] for a in arg: opt.append(a.arg) if begidx <= a.end: if begidx >= a.start: cur_arg = a.arg elif cur_arg == None: cur_arg = "" if self.is_cmd_arg(a.arg): if cur_arg == None: opt = [] opt.append(a.arg) else: break return opt, cur_arg #print "\ncurrent argument:", cur_arg, "of completion scope:", opt def complete_node(self): #print "complete node" rpath = "" supplied = "" out = { "type": "path", "matches": [], "length": 1, "supplied": "", "matched": 0 } path = self.cur_str if path == "" or path[0] != "/": if self.vfs.getcwd().path == "" and self.vfs.getcwd().name == "": rpath = "/" else: rpath = self.vfs.getcwd().path + "/" + self.vfs.getcwd( ).name + "/" if path == "/": path = "//" idx = path.rfind("/") if idx == -1: supplied = path else: supplied = path[idx + 1:] rpath += path[:idx] try: rpath = rpath.replace("\ ", " ") node = self.vfs.getnode(rpath) except OSError, e: out["matches"].append("") supplied = supplied.replace("\ ", " ") out["supplied"] = supplied if node: if node.empty_child(): if self.cur_str == "/": out["matches"].append("") else: out["matches"].append("/") out["matched"] += 1 else: #list = node.next() list = node.next #for i in range(node.next.size()): # print node.next[i].name #completion on a path if supplied == "": for i in list: if not i.empty_child(): if len(i.name + "/") > out["length"]: out["length"] = len(i.name + "/") out["matches"].append(i.name + "/") else: if len(i.name) > out["length"]: out["length"] = len(i.name) out["matches"].append(i.name) out["matched"] += 1 else: for i in list: if i.name.startswith(supplied) == True: if not i.empty_child(): if len(i.name + "/") > out["length"]: out["length"] = len(i.name + "/") out["matches"].append(i.name + "/") else: if len(i.name) > out["length"]: out["length"] = len(i.name) out["matches"].append(i.name) out["matched"] += 1 return out
class Line_to_arguments(): def __init__(self): # self.env = env.env() # self.loader = loader.loader() # self.vfs = vfs.vfs() self.api = ApiManager() self.env = self.api.env() self.argument = self.api.argument self.loader = self.api.loader() self.vfs = self.api.vfs() def generate(self, line): self.args, bopt = utils.split_line(line) cmds = [] cmd = [] generated_arguments = [] #generated_args for a in self.args: if a in ["|", "&", "&&", ">>", "<<", ">", "<"]: # cmds.append(cmd) # cmd = [] pass else: cmd.append(a) cmds.append(cmd) for cmd in cmds: gen_arg = self.argument("input") gen_arg.thisown = 0 if (self.string_to_arguments(cmd, gen_arg) != -1): exc = {} exc[cmd[0]] = gen_arg generated_arguments.append(exc) else: pass return generated_arguments def node_to_argument(self, key, value, gen_arg): res = 0 if value == None: value = self.vfs.getcwd().path + "/" + self.vfs.getcwd().name node = self.vfs.getnode(value) else: value = value.replace("\ ", " ") node = self.vfs.getnode(value) if node != None: gen_arg.add_node(key, node) else: print "Value error: node <", value, "> doesn't exist" res = -1 return res def path_to_argument(self, key, value, gen_arg): res = 0 if value == None: abs_path = os.getcwd() else: value = value.replace("\ ", " ") abs_path = utils.get_absolute_path(value) #XXX print ? #print abs_path if os.path.exists(abs_path): # path = self.api.Path(str(abs_path)) # path.thisown = False # gen_arg.add_path(key, path) gen_arg.add_path(key, str(abs_path)) else: print "Value error: local path <", value, "> doesn't exist" res = -1 return res def bool_to_argument(self, key, value, gen_arg): if value == None: gen_arg.add_bool(key, int(0)) else: gen_arg.add_bool(key, int(1)) return 0 def string_to_argument(self, key, value, gen_arg): if value == None: gen_arg.add_string(key, str("")) else: gen_arg.add_string(key, str(value)) return 0 def int_to_argument(self, key, value, gen_arg): res = 0 if value == None: gen_arg.add_int(key, int(-1)) else: if utils.is_hex(value): gen_arg.add_int(key, int(value, 16)) elif utils.is_int(value): gen_arg.add_int(key, int(value)) else: print "Value error: value <", value, "> cannot be converted to int" res = -1 return res def get_func_generator(self, cdl): func_name = cdl.type + "_to_argument" func = None if hasattr(self, func_name): func = getattr(self, func_name) else: print "Type error: type <", cdl.type, "> associated to argument <", cdl.name, "> doesn't exist" return func def several_argument(self, cdl, args, gen_arg): res = 0 i = 0 needs_no_key = utils.needs_no_key(cdl) arg_with_no_key = utils.get_arg_with_no_key(args) #print args while i != (len(cdl)) and res == 0: func = self.get_func_generator(cdl[i]) if func != None: key = "--" + cdl[i].name if key not in args: if cdl[i].optional == False: if needs_no_key != None and arg_with_no_key != -1: value = args[arg_with_no_key] res = func(cdl[i].name, value, gen_arg) arg_with_no_key = -1 else: print "Argument error: the argument <", cdl[i].name, "> is required by command: <", args[0], ">" res = -1 else: #if arg_with_no_key != -1: # value = args[arg_with_no_key] # res = func(cdl[i].name, value, gen_arg) #else:# cdl[i].type == "node" or cdl[i].type == "path": res = func(cdl[i].name, None, gen_arg) else: key_idx = args.index(key) if cdl[i].type == "bool": res = func(cdl[i].name, "", gen_arg) else: if (key_idx) == len(args) - 1: print "Key error: the argument <", key, "> needs a value" res = -1 else: value = args[key_idx + 1] res = func(cdl[i].name, value, gen_arg) else: res = -1 i += 1 return res def string_to_arguments(self, args, gen_arg): try: if not self.loader.is_modules(args[0]) : print "dff:" + args[0] + ": command not found" except IndexError: return -1 #mod = self.loader.get_modules(args[0]) conf = self.loader.get_conf(args[0]) # conf = mod.conf if conf != None: cdl = conf.descr_l if cdl == None: return gen_arg res = self.several_argument(cdl, args, gen_arg) if res == -1: return -1 else: return gen_arg else: return -1