Beispiel #1
0
 def indef(*args, **kws):
     try:
         res = f(*args, **kws)
     except CmdException as cmde:
         if cmde.level == ERROR:
             _cmd.cmdprint("%s: %s",
                           keywords=(cmde.level, cmde),
                           color=(ERROR_COLOR, INFO_COLOR))
         elif cmde.level == WARN:
             _cmd.cmdprint("%s: %s",
                           keywords=(cmde.level, cmde),
                           color=(WARN_COLOR, INFO_COLOR))
     except Exception as e:
         try:
             _cmd.cmdprint("%s: %s %s %s",
                           keywords=("USBError", "Errno", e.errno,
                                     e.strerror.decode("gbk")),
                           color=(ERROR_COLOR, INFO_COLOR, INFO_COLOR,
                                  INFO_COLOR))
         except Exception:
             _cmd.cmdprint("%s: %s",
                           keywords=("Error", e),
                           color=(ERROR_COLOR, INFO_COLOR))
     else:
         if prompt is not None:
             _cmd.cmdprint("$%s\n" % prompt, color=SYS_COLOR)
Beispiel #2
0
 def do_cls(self, line):
     r"""Function: Clear Dos Shell.
 No parameter(s)."""
     if line:
         NOPARAM_ERROR(line)
     else:
         os.system("cls")
         _cmd.cmdprint(HEAD, color=HEAD_COLOR)
Beispiel #3
0
    def do_write(self, line):
        r"""Function: Write data to usb device.
    Parameter(s):| s, l, f | Default: s
    (If SPLIT_KEY is '-')
        s: Format:|>>>write hello-s | or |>>>write hello world! |
           Write 'hello'<string> to usb device. This param is default one when param is not given.

        l: Format:|>>>write [104, 101, 108, 108, 111] -l |
           Write 'hello'(ascii) to usb device by <list>.

        f: Format:|>>>write file_name.dat -f |
           Write data in file_name.dat file. Data must be binary."""
        method = {
            "s": lambda x: x,
            "l": lambda x: eval(x),
            "f": lambda x: self.__wr_file(x)
        }
        name = {"s": "string", "l": "list", "f": "bytes"}
        if self.usb is not None:
            plist = line.split(self.SPLIT_KEY)
            if len(plist) == 2:
                param_check([plist[1]], method.keys())
                print("Written data<%s>:\n%s" %
                      (name[plist[1]], method[plist[1]](plist[0])))
                self.usb.write(method[plist[1]](plist[0]))
            elif len(plist) == 1:
                if "[" in plist[0] and "]" in plist[0]:
                    _cmd.cmdprint(
                        "%s: %s",
                        keywords=
                        (WARN,
                         "Detected type like <list>, but written as <string>!"
                         ),
                        color=(WARN_COLOR, INFO_COLOR))
                    if input("Do you want to change the type to <list>?(Y/N)"
                             ) in "Yy":
                        print("Written data<list>:\n%s" %
                              method["l"](plist[0]))
                        self.usb.write(method["l"](plist[0]))
                    else:
                        print("Written data<string>:\n%s" % plist[0])
                        self.usb.write(plist[0])
                else:
                    print("Written data<string>:\n%s" % plist[0])
                    self.usb.write(plist[0])
            else:
                raise NOPARAM_ERROR(line)
        else:
            raise NODEV_WARN
Beispiel #4
0
def chk_input(prompt, check=None):
    if isinstance(check, FunctionType):
        res = _cmd.cmdinput(prompt)
        chk = check(res)
        while chk is not True:
            if isinstance(chk, str):
                _cmd.cmdprint("%s: %s",
                              keywords=("Input Error", chk),
                              color=(ERROR_COLOR, INFO_COLOR))
            res = _cmd.cmdinput(prompt)
            chk = check(res)
        return res
    elif check is None:
        return _cmd.cmdinput(prompt)
    else:
        raise TypeError("Invalid Type: %s check" % type(check))
Beispiel #5
0
def init_ids(**kws):
    file = r"usb_id.info"
    if kws.get("f"):
        if os.path.isfile(file):
            with open(file, "rt") as f:
                ids = [_.split("=")[1].strip("\n ") for _ in f.readlines()]
            vid = int(ids[0], 16) if ids[0] else None
            pid = int(ids[1], 16) if ids[1] else None
            file_name = ids[2] if ids[2] else "Data.dat"
            return vid, pid, file_name
        else:
            _cmd.cmdprint("%s: %s",
                          keywords=(WARN, "No such a file: %s!" % file),
                          color=(WARN_COLOR, INFO_COLOR))
            if input("Do you want to creat that file?(Y/N):") in "yY":
                vid = chk_input("VID(hex):", check)
                pid = chk_input("PID(hex):", check)
                with open(file, "wt") as f:
                    f.write("VID = %s\nPID = %s\nFILE_NAME = Data.dat" %
                            (vid, pid))
                vid = int(vid, 16) if vid else None
                pid = int(pid, 16) if pid else None
                return vid, pid
            return None
    elif kws.get("n") or kws.get("s"):
        vid = chk_input("VID(hex):", check)
        pid = chk_input("PID(hex):", check)
        if kws.get("s"):
            with open(file, "wt") as f:
                f.write("VID = %s\nPID = %s" % (vid, pid))
        vid = int(vid, 16) if vid else None
        pid = int(pid, 16) if pid else None
        return vid, pid
    elif kws.get("r"):
        return None
    else:
        raise CmdException("init_ids: Unknown syntax: %s" % kws)
Beispiel #6
0
 def default(self, line):
     _cmd.cmdprint("%s: %s",
                   keywords=("*** Unknown syntax", line),
                   color=(ERROR_COLOR, INFO_COLOR))
Beispiel #7
0
def cmdinfo(message):
    _cmd.cmdprint(message, color=INFO_COLOR)
Beispiel #8
0
 def indef(*args, **kws):
     try:
         return f(*args, **kws)
     except KeyboardInterrupt:
         _cmd.cmdprint("\nUser KeyboardInterrupt Quit!", color=WARN_COLOR)
Beispiel #9
0
                    self.__restart(True)
                elif plist[0] == "off":
                    self.__restart(False)
                else:
                    raise NOPARAM_ERROR(line)
            else:
                raise NOPARAM_ERROR(line)
        else:
            self.__restart(True)

    def do_exit(self, line):
        r"""Function: Exit this shell.
    No Parameter(s)."""
        if line:
            raise NOPARAM_ERROR(line)
        else:
            os._exit(0)


if __name__ == "__main__":
    debug = DebugShell()
    _cmd.cmdprint(HEAD, color=HEAD_COLOR)
    while True:
        debug.cmdloop()
        if chk_input(
                _cmd.cmdstr("%s(Y/N): ",
                            keywords="Quit Shell?",
                            color=SYS_COLOR), lambda x: True
                if x in "YyNn" else "Unknown Input: '%s'!" % x) in "Yy":
            break