diff options
| -rw-r--r-- | python-template.py | 27 | ||||
| -rwxr-xr-x[-rw-r--r--] | wow3.py | 101 |
2 files changed, 96 insertions, 32 deletions
diff --git a/python-template.py b/python-template.py new file mode 100644 index 0000000..fda7a42 --- /dev/null +++ b/python-template.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 + +import argparse +import sys + +#this is a template for a python program that can be run from command line or used as a module + +def parseArgs(): + parser = argparse.ArgumentParser(description='the program does a thing') + parser.add_argument('-v', '--verbose', action='store_true', help='verbose') + parser.add_argument('-i', '--input', type=str, required=True, help='input file') + parser.add_argument('-o', '--output', type=str, required=False, help='output file') + args = parser.parse_args() + return args + +def main(): + args = parseArgs() + if args.verbose: + print(f"Input file: {args.input}") + if args.output: + print(f"Output file: {args.output}") + + + + +if __name__ == '__main__': + main()
\ No newline at end of file @@ -4,15 +4,21 @@ import argparse import sys import time import os +import random +import subprocess +import chardet +import filetype #Words Of Wisdom # output some random text from some given collection of files, # - primarily used to grab some random "words of wisdom" from my journals and writings +# paths=[] #the paths to scan recursively for files from which to grab text -samplefiles=[] #the paths of the individual files from which we want to grab text +#samplefiles=[] #the paths of all the individual files from which we can grab text matchpattern='' #if we want to filter the files by some text pattern that the filename must match time_min = -1 #threshold time. dont use files that are older v=False + def attemptReadSampleFile(filepath): if v: print('checking {}'.format(filepath)) if os.path.isfile(filepath): @@ -43,27 +49,11 @@ def attemptReadSampleFile(filepath): else: if v: print('not a file') return None - -def parseArgs(): - parser = argparse.ArgumentParser(description='output some random text from some given collection of files') - parser.add_argument('-v', '--verbose', action='store_true', help='verbose') - - parser.add_argument('-p', '--path', type=str, required=False, help='a path to scan', action='append', default=['~/doc']) - parser.add_argument('-o', '--output', type=str, required=False, help='output file') - args = parser.parse_args() - return args - -def main(): - args = parseArgs() - if args.verbose: - print(f"Input file: {args.input}") - if args.output: - print(f"Output file: {args.output}") - - if args.path: - paths.append(args.path) +def getSampleFiles(paths): + """grab all the possible sample files (recursive files from given paths)""" + samples = [] tStart = time.time() for p in paths: if v: print('path {}'.format(p)) @@ -71,28 +61,75 @@ def main(): for root,dirs,files in os.walk(p): if v: print('walk {}: {} files, {} dirs'.format(root, len(files), len(dirs))) for f in files: - samplefiles.append(root + '/' + f) + samples.append(root + '/' + f) else: - samplefiles.append(p) + samples.append(p) tEnd = time.time() tDuration = tEnd - tStart - print('gathered {} candidate files in {} seconds, from paths {}'.format(len(samplefiles), tDuration, str(paths))) - #pick random file until we get an acceptable one + if v: + print('gathered {} candidate files in {} seconds, from paths {}'.format(len(samples), tDuration, str(paths))) + return samples + + +def getRandomFileTextContent(samplefiles): + """pick random file until we get an acceptable one + @return tuple(filename, textcontent)""" fi = random.randint(0, len(samplefiles)) - t = attemptReadSampleFile(samplefiles[fi]) - while t == None: + if v: print('candidate file: {}'.format(samplefiles[fi])) + t = attemptReadSampleFile(samplefiles[fi]) #check if valid file + while t == None: #this file was invalid, try again del samplefiles[fi] fi = random.randint(0, len(samplefiles)) t = attemptReadSampleFile(samplefiles[fi]) + if len(samplefiles) == 0: #no files are valid + return (None, None) mt = time.ctime(os.path.getmtime(samplefiles[fi])) - print('{} ;\n last modified {} :\n {}'.format(samplefiles[fi], mt, t)) + if v: print('{} ;\n last modified {} :\n '.format(samplefiles[fi], mt)) + return (samplefiles[fi], t) + +#TODO params +def getExcerpt(text): + """grab a random excerpt from the given stringh, which is assumed to be at least one paragraph length with multiple lines """ + offset = random.randint(0,5) + lines = text.splitlines() + if offset >= len(lines)/2: + return text + li = random.randint(offset, len(lines)-offset) #line index + res = '\n'.join(lines[li-offset: li+offset]) + if len(res) < 7: + res = '\n'.join(lines) + return res + + +def parseArgs(): + global v + #get args from cmd line + parser = argparse.ArgumentParser(description='output some random text from some given collection of files') + parser.add_argument('-v', '--verbose', action='store_true', help='verbose') + + parser.add_argument('-p', '--paths', type=str, required=False, help='a path to scan', action='append', default=[]) + parser.add_argument('-o', '--output', type=str, required=False, help='output file') + args = parser.parse_args() + + if (args.verbose): v = True + + if args.paths: paths.append(args.paths) + + if len(paths) == 0: paths.append('./') + + if v: print('using paths: {}'.format(paths)) + + return args + +def main(): + args = parseArgs() + samplefiles = getSampleFiles(args.paths) + chosenfile = getRandomFileTextContent(samplefiles) + excerpt = getExcerpt(chosenfile[1]) + print(excerpt) + print('\n - {}'.format(chosenfile[0])) - lines = t.splitlines() - li = random.randint(0, len(t)) #line index - #ci = random.randint(0, len(t)) #character index - res = '\n'.join(lines[li: li+7]) - print(res) if __name__ == '__main__': main()
\ No newline at end of file |
