Skip to content

Commit bdd39cd

Browse files
Implementing basic command line search functionality for search widget
1 parent 4a935e3 commit bdd39cd

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

TODO.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# TODO
2+
3+
### FOR... GUI SEARCH AND LAUNCH
4+
5+
* Implement ability to track when files were last modified on **your** machine. We could then give a summary of what you might be 'rusty' on.
6+
7+
### FOR... PROBLEMS
8+
9+
* Document and Explain all code exercises that don't have it.

gui_search_and_launch/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# GUI SEARCH AND LAUNCH WIDGET
2+
3+
Use this to quick find the code exercises and questions that you want. There are *a lot* of files in this repository, and it's not getting any smaller.
4+
5+
### !STATUS! Command-Line Program in development...
6+
7+
### USAGE
8+
9+
Open up the widget and enter in a query as a series of **tags**. All files in this repository have a header with tags, so that their contents are revealed to this searcher.
10+
11+
The widget will return as list of files which you can launch from there.

gui_search_and_launch/__init__.py

Whitespace-only changes.

gui_search_and_launch/core.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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()

search_and_launch.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from gui_search_and_launch.core import main
2+
3+
if __name__ == '__main__':
4+
main()

0 commit comments

Comments
 (0)