def cp(argc): """cp: Copy files. Usage: cp SOURCE DESTINATION """ copyanything(expand_path(argc.env, argc.args['SOURCE']), expand_path(argc.env, argc.args['DESTINATION'])) return
def main(argc): """cp: Copy files. Usage: cp SOURCE DESTINATION """ shutil.copy2(expand_path(argc.env, argc.args['SOURCE']), expand_path(argc.env, argc.args['DESTINATION'])) return
def mv(argc): """mv: Move files. Usage: mv TARGET DESTINATION """ shutil.move(expand_path(argc.env, argc.args['TARGET']), expand_path(argc.env, argc.args['DESTINATION'])) return
def removeline(argc): """removeline: Remove lines with indices LINENUM from FILE. Usage: removeline (-f FILE) <int>LINENUM... Options: -f --file Specify the file to operate on. """ _file = expand_path(argc.env, argc.args['FILE']) # read lines and delete file lines = open(_file, "rb").readlines() os.remove(_file) # delete line for item in argc.args['LINENUM']: lines[int(item)] = None # write to file lines = [x for x in lines if x is not None] open(argc.args["FILE"], "w").writelines(lines) return
def ls(argc): """ ls: List files in a directory. Usage: ls [DIR] [-c | --count-files] [-d | --date] [-h | --hide-dotfiles] Options: -d --date Show file creation dates. -h --hide-dotfiles Ignore dotfiles. -c --count-files Return the number of files in a directory. Examples: ls $ """ file_filter = lambda x: True if argc.args['--hide-dotfiles']: file_filter = lambda x: not x.startswith(".") # date processing from numerical time date = lambda t: str(datetime.datetime.fromtimestamp(creation_date(t))) +\ " " if argc.args['--date'] else "" if not argc.args['DIR']: argc.args['DIR'] = "." if not os.path.isdir(expand_path(argc.env, argc.args['DIR'])): raise ErgonomicaError( "[ergo: ls]: [DirectoryError]: No such directory '{}'.".format( expand_path(argc.env, argc.args['DIR']))) files = [ date(x) + x for x in os.listdir(expand_path(argc.env, argc.args['DIR'])) if file_filter(x) ] if argc.args['--count-files']: return len(files) else: return files
def write(argc): """write: Write STDIN to file FILE. Usage: write [-a] <file>FILE [STRING...] """ mode = 'w' if argc.args['-a']: mode = 'a' open(expand_path(argc.env, argc.args['FILE']), mode).write('\n'.join(argc.args['STRING']) + "\n")
def main(argc): """ load: Load a file into ergonomica. Usage: load FILE """ sys.path[0:0] = expand_path(argc.env, ".") module = __import__(argc.args['FILE'], locals(), globals()) argc.ns[argc.args['FILE']] = module.main return
def cd(argc): """cd: Changes the directory. Usage: cd <directory>[DIR] """ if not argc.args['DIR']: argc.args['DIR'] = "~" try: os.chdir(expand_path(argc.env, argc.args['DIR'])) except OSError: raise ErgonomicaError( "[ergo: cd]: [DirectoryError]: No such directory '{}'.".format( expand_path(argc.env, argc.args['DIR']))) argc.env.directory = os.getcwd() return None
def read(argc): """ read: Read a file. Usage: read FILE """ try: return open(expand_path(argc.env, argc.args['FILE']), "r").read().split("\n") except IOError: print("[ergo: IOError]: No such readable file '%s'." % (argc.args['FILE']))
def ls(argc): """ ls: List files in a directory. Usage: ls [DIR] [-c | --count-files] [-d | --date] [-a | --all] Options: -d --date Show file creation dates. -a --all Do not ignore files and directories starting with a `.` character.. -c --count-files Return the number of files in a directory. Examples: ls $ """ # date processing from numerical time date = lambda t: str(datetime.datetime.fromtimestamp(creation_date(t))) +\ " " if argc.args['--date'] else "" if not argc.args['DIR']: argc.args['DIR'] = "." if not os.path.isdir(expand_path(argc.env, argc.args['DIR'])): raise ErgonomicaError( "[ergo: ls]: [DirectoryError]: No such directory '{}'.".format( expand_path(argc.env, argc.args['DIR']))) files = [ date(x) + x for x in os.listdir(expand_path(argc.env, argc.args['DIR'])) if argc.args['--all'] or (not (x.startswith(".") or x == "~")) ] if argc.args['--count-files']: return len(files) else: return files
def tree(argc): """ tree: Show the directory tree. Usage: tree [DIR] """ # first build the tree directory = expand_path(argc.env, (argc.args['DIR'] if argc.args['DIR'] else '.')) tree = build_tree(directory) return print_tree(tree)
def main(argc): """gen_docs: Generate the Ergonomica defaults documentation. Usage: gen_docs TARGET """ target = expand_path(argc.env, argc.args['TARGET']) out = "" # load into reST format for command in ns: out += make_title(command) out += ns[command].__doc__ + "\n" # dump to file open(target, "w").write(out)
def main(argc): """ ls: List files in a directory. Usage: ls [DIR...] [-d | --date] [-h | --hide-dotfiles] Options: -d : Show file creation dates. -u : Do not show dotfiles. """ # date processing from numerical time date = lambda t: str(datetime.datetime.fromtimestamp(creation_date(t))) +\ " " if argc.args['--date'] else "" if not argc.args['DIR']: argc.args['DIR'] = ["."] for arg in argc.args["DIR"]: return [date(x) + x for x in os.listdir(expand_path(argc.env, arg))]