def getgitdirlist_ap(parser_initial=None): """ Get a list of git projects Also allow for other arguments to be input into argparse by specifying a prefilled parser And allow for the outputted arguments to be output as an argument """ if parser_initial is None: parser = argparse.ArgumentParser() else: parser = parser_initial # add filelist elements parser = add_fileinputs(parser, desc="rootdir") parser = add_fileinputs(parser, desc="singledir") args = parser.parse_args() rootdirs = process_fileinputs(args, desc="rootdir") singledirs = process_fileinputs(args, desc="singledir") for rootdir in rootdirs: rootdir = rootdir.replace('~', os.path.expanduser('~')) dirs = os.listdir(rootdir) for folder in dirs: singledirs.append(os.path.join(rootdir, folder)) singledirs = sorted(singledirs) singledirs = [ singledir for singledir in singledirs if not singledir.startswith('#') ] singledirs = [singledir for singledir in singledirs if singledir != ''] singledirs = [ singledir.replace('~', os.path.expanduser('~')) for singledir in singledirs ] if parser_initial is None: return (singledirs) else: return (args, singledirs)
def greplist_ap(): import argparse #Argparse:{{{ parser=argparse.ArgumentParser() parser.add_argument("searchterm",type=str,help="default: ") parser = add_fileinputs(parser) args=parser.parse_args() # End argparse:}}} filelist = process_fileinputs(args) greplist(filelist, args.searchterm)
def pathmv_argparse(filelist = None): parser = argparse.ArgumentParser() # Actual files that are being moved: parser.add_argument('files', nargs='*') parser = add_fileinputs(parser) args = parser.parse_args() # Get files to do search and replace on: if filelist is None: filelist = process_fileinputs(args) pathmv_main(args.files, filelist)
def getallcode_ap(): #Argparse:{{{ import argparse parser=argparse.ArgumentParser() parser = add_fileinputs(parser) args=parser.parse_args() #End argparse:}}} filelist = process_fileinputs(args) # remove comments and blank lines in parselist filelist = [filename for filename in filelist if not filename.startswith('#') and not filename == ''] # replace ~ in parselist filelist = [filename.replace('~', os.path.expanduser('~')) for filename in filelist] files = getallcode(filelist) print('\n'.join(files))
def replacecommonsections_ap(): """ Run the function on the command line Get filelist inputs with the usual arguments """ #Argparse:{{{ import argparse parser = argparse.ArgumentParser() parser.add_argument( "folder", help="string path to directory containing the common sections") parser = add_fileinputs(parser) args = parser.parse_args() #End argparse:}}} filelist = process_fileinputs(args) replacecommonsections(filelist, args.folder)
def infrep_argparse(filelist = None): """ Always need to specify inputterm outputterm Which files do search and replace on: If put filelist as an argument at the start of the function, use those. Otherwise, need to give an argument specifying filenames to argparse. 5 options - see below Can specify inputmethod/outputmethod: Note I can't use inputmethod== 'recompiled'/'recompiledfunc' or outputmethod=='func' since I need python to use them So I can only specify 'reinput' to get inputmethod == 're' and/or 'reoutput' to get outputmethod == 'eval' '--reboth' gives '--reinput --reoutput' Can specify that inputterm and outputterm are filenames and the files contain the actual inputterm/outputterm """ # Get argparse:{{{ parser = argparse.ArgumentParser() # Input/output: parser.add_argument("inputterm", type=str, help="What I will change from. If I want to match a backslash, I only need to write 1 backslash since I escape the text before applying it to a regex.") parser.add_argument("outputterm", type=str, help="What I will change to. If I want to output a backslash, i only need to write 1 backslash.") parser = add_fileinputs(parser) # inputmethod/outputmethod: parser.add_argument('--reinput', help = "inputterm that is inputted into re.compile (inputmethod = 're'). I need two backslashes if I want to write backslash, since when I input in the regex \\\\ -> \\", action = 'store_true') parser.add_argument('--reoutput', help = "outputterm is text that is executed. Allows me to input matches. Something like 'match.group(1) + \"hello\". (outputmethod = 'eval'). I need two backslashes if I want to write a backslash, since eval('\\\\') = '\\'", action = 'store_true') parser.add_argument("-r", "--reboth", action='store_true', help="Equivalent to setting --reinput --reoutput.") # if want to put inputterm/outputterm in a file rather than on command line: parser.add_argument("--fileboth", action='store_true', help="Both inputterm and outputterm are filenames which should be read to get the actual inputterm and outputterm. Equivalent to setting --fileinput --fileoutput.") parser.add_argument("--fileinput", action='store_true', help="inputterm is actually a filename which should be read to get the actual inputterm. If a newline is the last character, ignore this.") parser.add_argument("--fileoutput", action='store_true', help="outputterm is actually a filename which should be read to get the actual outputterm. If a newline is the last character, ignore this.") args = parser.parse_args() # End get argparse:}}} # Get files to do search and replace on: if filelist is None: filelist = process_fileinputs(args) # get inputmethod/outputmethod if args.reboth is True or args.reinput is True: inputmethod = 're' else: inputmethod = None if args.reboth is True or args.reoutput is True: outputmethod = 'eval' else: outputmethod = None # read files if inputterm and/or outputterm given in a file: if args.fileboth is True or args.fileinput is True: if not os.path.isfile(args.inputterm): raise ValueError('Since args.fileinput is True, args.inputterm should be a filename. args.inputterm: ' + str(args.inputterm)) with open(args.inputterm) as f: inputterm = f.read() if inputterm[-1] == '\n': inputterm = inputterm[: -1] args.inputterm = inputterm if args.fileboth is True or args.fileoutput is True: if not os.path.isfile(args.outputterm): raise ValueError('Since args.fileoutput is True, args.outputterm should be a filename. args.outputterm: ' + str(args.outputterm)) with open(args.outputterm) as f: outputterm = f.read() if outputterm[-1] == '\n': outputterm = outputterm[: -1] args.outputterm = outputterm # Call infrep: infrep_main([{'filenames': filelist, 'inputterm': args.inputterm, 'outputterm': args.outputterm, 'inputmethod': inputmethod, 'outputmethod': outputmethod}])