1+ '''BaseLinter'''
2+
3+ import json
4+ import datetime
5+ import os
6+
7+ LOCATION = "D:\\ UW\\ Intro-to-Python\\ IntroPython-2017\\ students\\ briggsm\\ Term2_project\\ baselinter\\ baselinter\\ " # os.path.dirname(os.path.realpath('__file__'))
8+ THISDATE = str (datetime .date .today ()) # e:\MB\Better-Butter\Projects\BaseLinter
9+ LIST_of_SETS = []
10+ LINTER_NAME = ""
11+
12+
13+ def open_set_json (filename ):
14+ '''Opens a JSON file and returns the a list of sets.'''
15+ with open (filename ) as f :
16+ read_data = f .read ()
17+ in_data = json .loads (read_data )
18+ list_of_sets = []
19+ for key , value in in_data .items ():
20+ list_of_sets .append (set (value ))
21+ return list_of_sets
22+
23+
24+ def text_to_list (filename ):
25+ '''Take a text and return a list of words in order.'''
26+ try :
27+ with open (filename ) as f :
28+ in_text = f .read ()
29+ text_list = in_text .split ()
30+ return text_list
31+ except FileNotFoundError :
32+ print ("Error trying to find your file." )
33+ text_list = []
34+ return text_list
35+
36+
37+ def wordlist_to_text (wordlist ):
38+ '''Take a list of words and return a single string.'''
39+ text_out = ""
40+ for w in wordlist :
41+ text_out = text_out + w + " "
42+ text_out = text_out .strip ()
43+ return text_out
44+
45+
46+ def return_members (list_of_sets , item ):
47+ '''Returns the set of an item is a member of the set, otherwise returns None.'''
48+ for i in list_of_sets :
49+ if item in i :
50+ return i
51+ break
52+ return None
53+
54+
55+ def select_update (choices , wordlist , wordloc ):
56+ '''With choices, the wordlist, a location, select a choice and
57+ return updated wordlist.'''
58+ these_choices = list (choices )
59+ preview_list = wordlist [:]
60+ if (wordloc - 10 ) < 10 :
61+ preview_start = 0
62+ else :
63+ preview_start = wordloc - 10
64+ if wordloc + 10 > len (wordlist ):
65+ preview_end = len (wordlist )
66+ else :
67+ preview_end = wordloc + 10
68+ preview_token = "[[" + wordlist [wordloc ].strip () + "]]"
69+ preview_list [wordloc ] = preview_token
70+ preview_list = preview_list [preview_start :preview_end ]
71+ display_string = ""
72+ for w in preview_list :
73+ display_string = display_string + w + " "
74+ display_string .strip ()
75+ display_choices = ""
76+ for ind , choice in enumerate (these_choices ):
77+ display_choices = display_choices + "{} ) {} " .format (ind + 1 , choice )
78+ print ("""
79+ Select ----------------------------\n
80+ {}\n
81+ -----------------------------------\n
82+ {}
83+ -----------------------------------
84+ """ .format (display_string , display_choices ))
85+ choiceindex = input ("Select an option. > " )
86+ wordupdate = these_choices [int (choiceindex )- 1 ]
87+ wordlist [wordloc ] = wordupdate
88+ return wordlist
89+
90+
91+ def load_linter ():
92+ '''Load linters'''
93+ global LIST_of_SETS
94+ global LINTER_NAME
95+ global LOCATION
96+
97+ linters = { "American Homophone" : "\\ data\\ guide-amhomo.json" ,
98+ "MS Docs Voice Guide" : "\\ data\\ guide-msdocs.json" ,
99+ "Business Jargon" : "\\ data\\ guide-businessjargon.json" }
100+ print ("Choose a new linter.\n " )
101+ lint_list = list (linters .keys ())
102+ display_choices = ""
103+ for ind , choice in enumerate (lint_list ):
104+ display_choices = display_choices + "{} ) {} " .format (ind + 1 , choice )
105+ print (display_choices )
106+ lint_choice = input ("Select an option. > " )
107+ lint_choice = int (lint_choice ) - 1
108+ LINTER_NAME = lint_list [lint_choice ]
109+ LIST_of_SETS = open_set_json (LOCATION + "\\ " + linters [LINTER_NAME ])
110+
111+ return True
112+
113+
114+ def exit_app ():
115+ '''Save the data; close the app.'''
116+ print ("Goodbye." )
117+ return False
118+
119+ def check_file ():
120+ ''' '''
121+ try :
122+ filename = input ("File to check. > " )
123+ except FileNotFoundError :
124+ print ("Please try a valid file name." )
125+ return True
126+ wordlist = text_to_list (filename )
127+ for wordloc , word in enumerate (wordlist ):
128+ choices = return_members (LIST_of_SETS , word )
129+ if choices :
130+ wordlist = select_update (choices , wordlist , wordloc )
131+ outtext = wordlist_to_text (wordlist )
132+ filename_out = filename .split ("." )[0 ] + "-" + THISDATE + "." + filename .split ("." )[1 ]
133+ print ("Done checking. Saving file as {}" .format (filename_out ))
134+ with open (filename_out , 'w' ) as f :
135+ f .write (outtext )
136+ return True
137+
138+
139+
140+
141+ chooser = {
142+ "1" : ("Check file." , check_file ),
143+ "2" : ("Load new linter." , load_linter ),
144+ "3" : ("Quit." , exit_app )
145+ }
146+
147+
148+ def main ():
149+ '''The main logic of the Linter application interface.'''
150+ global LINTER_NAME
151+ global LIST_of_SETS
152+
153+ LINTER_NAME = "American Homophone"
154+ LIST_of_SETS = open_set_json (LOCATION + "\\ data\\ guide-amhomo.json" )
155+
156+ run = True
157+ while run == True :
158+ print ("\n Base Linter | {} | {}\n " .format (LINTER_NAME , THISDATE ))
159+ try :
160+ for k in chooser .keys ():
161+ print ("{} | {}" .format (k , chooser [k ][0 ]))
162+ sel = input ("Type a choice. > " )
163+ run = chooser [sel ][1 ]()
164+ except KeyError :
165+ print ("Please type a valid choice." )
166+ except IndexError :
167+ print ("Please type a valid choice." )
168+
169+
170+ if __name__ == "__main__" :
171+ main ()
0 commit comments