def test_001(self): HEADING("assign key=value") v = Variables() n = len(v) v["gregor"] = "gregor" assert (len(v) == n + 1) assert "gregor" in v v.close()
def test_004(self): HEADING("directory and key subtract ") d = {"a": "1", "b": "2"} v = Variables() v + d print(v) assert "a" in v and "b" in v v - d.keys() assert "a" not in v and "b" not in v print(v) v.close()
def postcmd(self, stop, line): StopWatch.stop("command") try: variable = Variables() if "timer" not in variable: variable["timer"] = "off" if variable["timer"].lower() in ['on', 'true']: print("Timer: {:.4f}s ({})".format(StopWatch.get("command"), line.strip())) variable.close() except Exception as e: Error.traceback(error=e) return stop
def test_003(self): HEADING("directory add ") d = {"a": "1", "b": "2"} v = Variables() v + d print(v) assert "a" in v and "b" in v del v["a"] del v["b"] v + d assert "a" in v and "b" in v v - d assert "a" not in v and "b" not in v print(v) v.close()
def replace_vars(self, line): # self.update_time() variable = Variables() newline = line if len(variable) is not None: for name in variable.data: value = str(variable[name]) newline = newline.replace("$" + name, value) newline = newline.replace("var." + name, value) for v in os.environ: name = v.replace('os.', '') if name in newline: value = os.environ[name] newline = newline.replace("os." + v, value) default = Default() if default is not None: for v in default.data: name = "default." + v.replace(",", ".") value = default.data[v] if name in newline: newline = newline.replace(name, value) # replace if global is missing global_default = default["global"] if global_default is not None: for v in global_default: name = "default." + v value = global_default[v] if name in newline: newline = newline.replace(name, value) default.close() variable.close() newline = path_expand(newline) return line, newline
def onecmd(self, line): """Interpret the argument as though it had been typed in response to the prompt. This may be overridden, but should not normally need to be; see the precmd() and postcmd() methods for useful execution hooks. The return value is a flag indicating whether interpretation of commands by the interpreter should stop. """ oldline, line = self.replace_vars(line) # ----------------------------- # print comment lines, but do not execute them # ----------------------------- if line.startswith('#') \ or line.startswith('//') \ or line.startswith('/*'): print(line) return "" if line.startswith('!'): os.system(line[1:]) return "" # if line is None: # return "" # if line.startswith("!"): # line.replace("!", "! ") # line = self.var_replacer(line) # if line != "hist" and line: # self._hist += [line.strip()] # if line.startswith("!") or line.startswith("shell"): # self.do_shell_exec(line[1:]) # return "" cmd, arg, line = self.parseline(line) if line.startswith("$") or line.startswith('var.'): line = line.replace("$", "", 1) line = line.replace("var.", "", 1) print("FIND>", line, "<", sep='') variable = Variables() print(variable[line]) variable.close() return "" # ----------------------------- # handle empty line # ----------------------------- if not line: return self.emptyline() # ----------------------------- # handle file execution # ----------------------------- # # this does not yet work # # if os.path.isfile(line): # print ("... execute", line) # self.do_exec(line) # return "" if cmd != '': try: func = getattr(self, 'do_' + cmd) return func(arg) except AttributeError as e: print("ERROR: command error while executing '", cmd, "'", sep='') cmd = None line = oldline # Error.traceback(error=e, debug=True, trace=True) return ""
def test_002(self): HEADING("delete") v = Variables() del v["gregor"] assert "gregor" not in v v.close()