|
| 1 | +# |
| 2 | +# Author: Jonathon Belotti <thundergolfer> |
| 3 | +# |
| 4 | +from __future__ import print_function |
| 5 | +import os |
| 6 | +from argparse import ArgumentParser |
| 7 | + |
| 8 | +def main(): |
| 9 | + |
| 10 | + parser = ArgumentParser(description='Get your search query') |
| 11 | + parser.add_argument('query', type=str, nargs='+', help='enter some tags to search for') |
| 12 | + list_of_arg_words = vars(parser.parse_args())['query'] # SO SORRY FOR THIS LINE. IN A RUSH DON'T JUDGE ME |
| 13 | + query = [tag.lower() for tag in list_of_arg_words] # search query as a list of 'tokens'. eg ['this','is','my','search'] |
| 14 | + print("query is: ", query) |
| 15 | + |
| 16 | + file_matches = [] |
| 17 | + # TODO: Make this code not kinda rubbish |
| 18 | + # Search through all questions in "problems" and "worded_questions" |
| 19 | + for folder, subfolders, files in os.walk(os.getcwd(), topdown=False): |
| 20 | + for f in files: |
| 21 | + fullpath = os.path.join(folder,f) |
| 22 | + if (('problems' in fullpath or 'worded_questions' in fullpath) |
| 23 | + and not fullpath.endswith('.md')): # This will very likely admit TOO MANY files. TODO: fix it |
| 24 | + with open(fullpath, 'r') as question_file: |
| 25 | + lines = question_file.readlines() |
| 26 | + # if at least one tag matches, add the file with its tags to our list |
| 27 | + matched_tags = match_tags( get_tags_from_file(lines), query) |
| 28 | + if matched_tags: |
| 29 | + file_matches.append( (matched_tags, '...' + fullpath[-60:] ) ) # TODO: fix this hammy way of handling filename |
| 30 | + |
| 31 | + # order the matched files by number of matched tags. Ascending order |
| 32 | + file_matches = sorted(file_matches, key=lambda x: len(x[0]), reverse=True) |
| 33 | + print("Look Here: ") # TODO replace this print to console bit with something more useful |
| 34 | + for i, match in enumerate(file_matches): |
| 35 | + tags, name = match[0], match[1] |
| 36 | + print(str(i), ': ', name) |
| 37 | + print("\tMatches on: " , tags) |
| 38 | + |
| 39 | +def match_tags( tags, query_tags ): |
| 40 | + return [tag.lower() for tag in tags if tag.lower() in query_tags] |
| 41 | + |
| 42 | +def get_tags_from_file( lines ): |
| 43 | + """ |
| 44 | + Extract the tags from a file and return them as a list |
| 45 | + """ |
| 46 | + tags_header = "##$$##" |
| 47 | + for i, line in enumerate(lines): |
| 48 | + if tags_header in line: |
| 49 | + break |
| 50 | + else: # didn't break out of loop so no tags header |
| 51 | + return [] |
| 52 | + tags_line = lines[i+1] |
| 53 | + just_tags = (''.join(ch for ch in tags_line if (ch.isalnum()) or ch == ',')).split(',') |
| 54 | + return [word.strip() for word in just_tags] # remove trailing and leading whitespace |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + main() |
0 commit comments