def system(self, cmd): if self.background: try: proc = Popen(cmd, shell=True, executable=self.shell[0], close_fds=True, preexec_fn=os.setsid, stdout=PIPE, stderr=PIPE) message.puts("Spawn: {0} ({1})".format(cmd.strip(), proc.pid)) ProcessNotify(proc, cmd).start() except Exception as e: message.exception(e) else: try: widget.end_curses() os.system("clear") proc = Popen(cmd, shell=True, executable=self.shell[0], close_fds=True, preexec_fn=os.setsid) proc.wait() if not self.quick: util.wait_restore() message.puts("Spawn: {0} ({1})".format(cmd.strip(), proc.pid)) except KeyboardInterrupt as e: message.error("KeyboardInterrupt: {0}".format(cmd)) except Exception as e: message.exception(e) finally: widget.start_curses()
def execute(self, name, action): if name in look.looks: look.Look.mylook = name look.init_colors() widget.refresh_all_widgets() else: message.error("`{0}' looks doesn't exist".format(name))
def notify_error(self, err): for line in err.splitlines(): if not line: continue try: line = line.decode() except UnicodeError: line = "????? - Invalid encoding" message.error("{0} - ({1})".format(line, self.name))
def execute(self, cmd, action): if action == self.actions[0]: widgets.help.show_command(cmd) return (cmd, -1) else: from pyful.command import commands try: commands[cmd]() except KeyError: message.error("Undefined command `{0}'".format(cmd))
def python(self, cmd): cmd = self.parsemacro(cmd) try: widget.end_curses() os.system("clear") exec(cmd) if not self.quick: util.wait_restore() message.puts("Eval: {0}".format(cmd)) except KeyboardInterrupt as e: message.error("KeyboardInterrupt: {0}".format(cmd)) except Exception as e: message.exception(e) finally: widget.start_curses()
def show(self, name, pos=0): if name not in self.items: return message.error("Undefined menu `{0}'".format(name)) self.title = name self.current = self.items[name] self.refresh() entries = [Entry("{0} ({1})".format(item[0], item[1])) for item in self.current] super(self.__class__, self).show(entries, pos)
def kill_thread(): try: thread = Filectrl.threads[0] except IndexError: return message.error("Thread doesn't exist.") title = thread.title ret = message.confirm("Kill {0}: ".format(title), ["OK", "Cancel"]) if ret == "OK": thread.kill()
def execute(self, path, action): filer = widgets.filer if filer.dir.ismark(): if not os.path.exists(path): return message.error("{0} - {1}".format(os.strerror(errno.ENOENT), path)) elif not os.path.isdir(path): return message.error("{0} - {1}".format(os.strerror(errno.ENOTDIR), path)) for f in filer.dir.get_mark_files(): dst = os.path.join(path, os.path.basename(f)) filectrl.symlink(os.path.abspath(f), dst) filer.workspace.all_reload() elif self.src is None: if not path: return self.src = path return (filer.workspace.nextdir.path, -1) else: filectrl.symlink(self.src, path) filer.workspace.all_reload()
def loadfile(self, path): try: path = os.path.expanduser(path) with open(path, "r") as f: obj = json.load(f) except Exception as e: if e[0] != errno.ENOENT: message.exception(e) message.error("InformationLoadError: {0}".format(path)) return self.workspaces[:] = [] try: self.cursor = obj.get("FocusWorkspace", 0) data = obj.get("WorkspacesData", []) for i, wmap in enumerate(data): title = wmap.get("title", str(i+1)) ws = Workspace(title) ws.layout = wmap.get("layout", "Tile") ws.cursor = wmap.get("focus", 0) if "dirs" not in wmap: ws.create_dir(os.getenv("HOME")) ws.create_dir(os.getenv("HOME")) self.workspaces.append(ws) continue for dmap in wmap["dirs"]: path = dmap.get("path", os.getenv("HOME")) d = Directory(path, 1, 1, 1, 0) d.sort_kind = dmap.get("sort", "Name[^]") d.cursor = dmap.get("cursor", 0) d.scrolltop = dmap.get("scrolltop", 0) ws.dirs.append(d) self.workspaces.append(ws) except Exception as e: message.exception(e) self.default_init() if not 0 <= self.cursor < len(self.workspaces): self.cursor = 0 self.workspace.refresh()
def show_command(self, name): from pyful.command import commands if not name: return if not name in commands: return message.error("Undefined command `{0}'".format(name)) doc = commands[name].__doc__ if not doc: return message.error("`{0}' hasn't documentation".format(name)) entries = [] entries.append(Entry("Name:", attr=curses.A_BOLD)) entries.append(Entry(self.indent+name)) entries.append(Entry("")) keys = self.find_keybind(commands[name]) if keys: entries.append(Entry("Keybinds:", attr=curses.A_BOLD)) entries.extend(keys) entries.append(Entry("")) entries.extend(self.parse_docstring(doc)) self.show(entries)
def replace(pattern, repstr): filer = widgets.filer files = filer.dir.get_mark_files() renamed = [pattern.sub(r""+repstr, f) for f in files] msg = [] matched = [] for i in range(0, len(files)): if files[i] != renamed[i]: msg.append("{0} -> {1}".format(files[i], renamed[i])) matched.append((files[i], renamed[i])) if not matched: return message.error("No pattern matched for mark files: {0} ".format(pattern.pattern)) if "Start" != message.confirm("Replace:", ["Start", "Cancel"], msg): return ret = "" for member in matched: src, dst = member if os.path.exists(os.path.join(filer.dir.path, dst)): if ret == "No(all)": continue if ret != "Yes(all)": ret = message.confirm( "{0}; Override? {{{1} -> {2}}}".format(os.strerror(errno.EEXIST), src, dst), ["Yes", "No", "Yes(all)", "No(all)", "Cancel"]) if ret == "Yes" or ret == "Yes(all)": pass elif ret == "No" or ret == "No(all)": continue elif ret is None or ret == "Cancel": break try: os.renames(src, dst) message.puts("Renamed: {0} -> {1}".format(src, dst)) except Exception as e: message.exception(e) break filer.dir.mark_clear()
def execute(self, st, action): if not self.path: if os.path.exists(st): self.path = st self.timesec = time.localtime(os.stat(self.path).st_mtime) return ("", 0) else: return message.error("{0} doesn't exist.".format(st)) elif len(self.sttime) == 0: if st == "": self.sttime.append(self.timesec[0]) else: self.sttime.append(int(st)) return ("", 0) elif len(self.sttime) == 1: if st == "": self.sttime.append(self.timesec[1]) elif 0 < int(st) <= 12: self.sttime.append(int(st)) else: self.sttime.append(self.timesec[1]) return ("", 0) elif len(self.sttime) == 2: if st == "": self.sttime.append(self.timesec[2]) elif 0 < int(st) <= 31: self.sttime.append(int(st)) else: self.sttime.append(self.timesec[2]) return ("", 0) elif len(self.sttime) == 3: if st == "": self.sttime.append(self.timesec[3]) elif 0 <= int(st) <= 23: self.sttime.append(int(st)) else: self.sttime.append(self.timesec[3]) return ("", 0) elif len(self.sttime) == 4: if st == "": self.sttime.append(self.timesec[4]) elif 0 <= int(st) <= 59: self.sttime.append(int(st)) else: self.sttime.append(self.timesec[4]) return ("", 0) elif len(self.sttime) == 5: if st == "": self.sttime.append(self.timesec[5]) elif 0 <= int(st) <= 61: self.sttime.append(int(st)) else: self.sttime.append(self.timesec[6]) self.sttime += [-1, -1, -1] try: atime = mtime = time.mktime(tuple(self.sttime)) os.utime(self.path, (atime, mtime)) except Exception as e: message.exception(e) widgets.filer.workspace.all_reload()
def execute(self, path, action): filer = widgets.filer if os.path.exists(path): filer.dir.open_listfile(path) else: message.error("No such list file: " + path)
def zipeach(src, dst, wrap=""): if not isinstance(src, list): return message.error("source must present `list'") Filectrl().zipeach(src, dst, wrap)
def tareach(src, dst, tarmode="gzip", wrap=""): if not isinstance(src, list): return message.error("source must present `list'") Filectrl().tareach(src, dst, tarmode, wrap)