From 1a7855242c2ca59978e0cf8875d7e2db0032e334 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Thu, 8 Nov 2018 12:39:12 +0200 Subject: [PATCH] restructure gcd --- files/gcd-findmatch.py | 159 +++++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 70 deletions(-) diff --git a/files/gcd-findmatch.py b/files/gcd-findmatch.py index 9450916..43d36c5 100755 --- a/files/gcd-findmatch.py +++ b/files/gcd-findmatch.py @@ -4,106 +4,125 @@ import sys,os,re import difflib as dl # Define color codes -Green="\033[32;1m" -Red="\033[31;1m" -Reset="\033[0m" +Green = "\033[32;1m" +Red = "\033[31;1m" +Reset = "\033[0m" # Available shortcut keys -key_list="1234567890qwertyuiop" +key_list = "1234567890qwertyuiop" class getch: def get(self): + import termios, tty fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + termios.tcsetattr( + fd, + termios.TCSADRAIN, + old_settings + ) return ch def printerr(s): - sys.stderr.write(s+"\n") + sys.stderr.write(s + "\n") sys.stderr.flush() -def colorize_matches(directories,repl): - return [repl.sub("%s\g<1>%s"%(Green,Reset),d) for d in directories] +def errexit(s): + printerr(s) + sys.exit(1) + +def colorize_matches(directories, repl): + return [repl.sub("%s\g<1>%s"%(Green, Reset), d) for d in directories] def search_close_match(directories, key): """ Return a list of Components whose name closely matches key """ - match=[] - key=key.lower() + match = [] + key = key.lower() for name in directories: - s=dl.SequenceMatcher(None, name.lower(), key) - s_ratio=s.ratio() - if s_ratio > 0: - match.append(( s_ratio, name )) - match.sort(key=lambda x:x[0], reverse=True) + s = dl.SequenceMatcher( + None, + name.lower(), + key + ) + if s.ratio() > 0: + match.append(( s.ratio(), name )) + match.sort(key = lambda x: x[0], reverse = True) best_matches=[x[1] for x in match[0:10]] return best_matches -# Print help -if len(sys.argv)==1 or sys.argv[-1]=="-h": - printerr("Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders") - sys.exit(0) +def get_dirs(pat_dir): -pattern=".".join(sys.argv[1:]) -pat_base=os.path.basename(pattern) -pat_dir=os.path.dirname(pattern) + # list sub-folders in the folder + current_folders = [d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir, d))] + current_folders = [d for d in current_folders if not d.startswith(".")] + current_folders.sort() + return current_folders -if pat_dir=="": - pat_dir="." +def main(): + # Print help + if len(sys.argv) == 1 or sys.argv[-1] == "-h": + errexit("Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders") -if not os.path.exists(pat_dir): - printerr("%s: %sNo such folder%s"%(pat_dir,Red,Reset)) - sys.exit(1) + pattern = ".".join(sys.argv[1:]) + pat_base = os.path.basename(pattern) + pat_dir = os.path.dirname(pattern) -# list sub-folders in the folder -current_folders=[d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir,d))] -current_folders=[d for d in current_folders if not d.startswith(".")] -current_folders.sort() + if pat_dir == "": + pat_dir = "." -# no sub-folders -if len(current_folders)==0: - printerr("There are no subfolders in %s"%(pat_dir,)) - sys.exit(0) + if not os.path.exists(pat_dir): + errexit("%s: %sNo such folder%s"%(pat_dir, Red, Reset)) -# find matches -match_case=re.compile(".*"+pat_base+".*") -repl_case=re.compile("("+pat_base+")") + # list sub-folders in the folder + current_folders = get_dirs(pat_dir) -matching_folders=[d for d in current_folders if match_case.match(d)] -matches=colorize_matches(matching_folders, repl_case) + # no sub-folders + if len(current_folders) == 0: + errexit("There are no subfolders in %s"%( pat_dir,)) -# no matches, try case insensitive -if len(matching_folders)==0: - match_nocase=re.compile(".*"+pat_base+".*",re.I) - repl_nocase=re.compile("("+pat_base+")",re.I) - matching_folders=[d for d in current_folders if match_nocase.match(d)] - matches=colorize_matches(matching_folders, repl_nocase) + # find matches + match_case = re.compile(".*" + pat_base + ".*") + repl_case = re.compile("(" + pat_base + ")") -# No matches, try close match -if len(matching_folders)==0: - matching_folders=search_close_match(current_folders, pat_base) - matches=[d for d in matching_folders] + matching_folders = [d for d in current_folders if match_case.match(d)] + matches = colorize_matches(matching_folders, repl_case) -# One match, print and return -if len(matching_folders)==1: - printerr(matches[0]) - print(os.path.join(pat_dir,matching_folders[0])) - sys.exit(0) + # no matches, try case insensitive + if len(matching_folders) == 0: + match_nocase = re.compile(".*" + pat_base + ".*",re.I) + repl_nocase = re.compile("(" + pat_base + ")",re.I) + matching_folders = [d for d in current_folders if match_nocase.match(d)] + matches = colorize_matches(matching_folders, repl_nocase) -# Many matches, ask the user for folder index -for i,m in enumerate(matches[0:len(key_list)]): - printerr(key_list[i]+": "+m) -if len(matches)>len(key_list): - printerr("Skipping the rest...") -import termios,tty -ch=getch() -key_in=ch.get() -try: - key_index=key_list.index(key_in) - key_match=matching_folders[key_index] -except (ValueError,IndexError) as err: - printerr("%s'%s' Not in the list%s"%(Red,key_in,Reset)) - sys.exit(1) -print(os.path.join(pat_dir,key_match)) + # No matches, try close match + if len(matching_folders) == 0: + matching_folders = search_close_match(current_folders, pat_base) + matches = [d for d in matching_folders] + + # One match, print and return + if len(matching_folders) == 1: + printerr(matches[0]) + print(os.path.join(pat_dir, matching_folders[0])) + sys.exit(0) + + # Many matches, ask the user for folder index + for i,m in enumerate(matches[0:len(key_list)]): + printerr(key_list[i] + ": " + m) + + if len(matches) > len(key_list): + printerr("Skipping the rest...") + + key_in = getch().get() + try: + key_index = key_list.index(key_in) + key_match = matching_folders[key_index] + except (ValueError, IndexError) as err: + errexit("%s'%s' Not in the list%s"%(Red, key_in, Reset)) + + print(os.path.join(pat_dir, key_match)) + +if __name__ == "__main__": + main()