From c985289e59d186adb4a5c692d717f217590ea237 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Sun, 4 Nov 2018 19:32:21 -0600 Subject: [PATCH] Tested everything in ext_open and fixed case insensitive search first version --- ext_open/config.ini | 9 +++++---- ext_open/ext_open.py | 44 ++++++++++++++++++++++++++++++++------------ ext_open/opts.json | 22 ++++++++++++++++++---- ext_open/opts.py | 14 ++++++++------ ext_open/searcher.py | 6 +++--- 5 files changed, 66 insertions(+), 29 deletions(-) diff --git a/ext_open/config.ini b/ext_open/config.ini index 0292130..0eac6cf 100644 --- a/ext_open/config.ini +++ b/ext_open/config.ini @@ -11,10 +11,11 @@ commit_interval = 30 [exe] ; comma separated paths - paths= %userprofile%,%programfiles%,%programfiles(x86)%, + paths = %userprofile%,%programfiles%,%programfiles(x86)%, [py] paths = "%userprofile%\Documents\local_repo\windows\ext_open" + ; editor = ${global:editor} idle_edit = pythonw -m idlelib -e - editor = ${global:editor} - execute= python - windowless = pythonw \ No newline at end of file + execute = python + windowless = pythonw + interactive = python -i \ No newline at end of file diff --git a/ext_open/ext_open.py b/ext_open/ext_open.py index b09faa2..d7a75d8 100644 --- a/ext_open/ext_open.py +++ b/ext_open/ext_open.py @@ -1,30 +1,39 @@ +from config import config from opts import * -import searcher -import opener -import argparse from util import picker +import argparse +import opener import pyperclip +import searcher parser = argparse.ArgumentParser(prog='ext-open',prefix_chars=prefix_char) for opt in prefix: parser.add_argument(*opt.pop('ostring'),**opt) sub_parsers = parser.add_subparsers(dest='ext') exe_parser = sub_parsers.add_parser('exe',prefix_chars=prefix_char) for opt in exe: + opt = opt.copy() exe_parser.add_argument(*opt.pop('ostring'),**opt) for ext in exts: sub_parser = sub_parsers.add_parser(ext['ext'],prefix_chars=prefix_char) for opt in ext['opts']+common: opt = opt.copy() sub_parser.add_argument(*opt.pop('ostring'),**opt) -debug = True if __name__ == '__main__': args = parser.parse_args() -if not debug and __name__ == "__main__": + print(args) ext = args.ext - s = searcher.Searcher(ext,args.case_insensitive) - if args.update: + s = searcher.Searcher(ext,args.case_sensitive) + if args.trim_history is not None: + if args.trim_history.lower() == 'all': + if input('Are you sure?\n(y/n): ') == 'y': + s.clear() + elif args.trim_history.isdecimal: + n = int(args.trim_history) + s.clean(n) + del args.__dict__['trim_history'] + if args.__dict__.pop('update'): s.update() - if args.exact: + if args.__dict__.pop('exact'): matches = s.exact(args.query) else: matches = s.search(args.query,args.regex) @@ -34,17 +43,28 @@ if not debug and __name__ == "__main__": if len(matches) == 1: row = matches[0] else: - row = picker(matches) + if len(matches) <= 20: + row = picker(matches) + else: + row = picker(matches,True) path = row[-1] if args.path: print(path) pyperclip.copy(path) else: if args.ext == 'exe': - opener.standalone(path,args.args,args.windowless,args.cwd) + opener.standalone(path,args.arg,args.windowless,args.start_in) else: if args.edit: - opener.edit(path,args.args) + opener.edit(path,args.arg) else: mode = 'execute' - opener.dependant(path,args.args,mode,args.cwd) + ext_argnames = cur.get(args.ext).argnames + bools = filter(lambda arg: isinstance(args.__dict__[arg],bool),args.__dict__.keys()) + canidates = filter(lambda arg: arg in ext_argnames,bools) + for canidate in canidates: + if canidate in config[ext].keys(): + mode = canidate + break + + opener.dependant(path,args.arg,mode,args.start_in) \ No newline at end of file diff --git a/ext_open/opts.json b/ext_open/opts.json index 9500e2e..6e98722 100644 --- a/ext_open/opts.json +++ b/ext_open/opts.json @@ -1,8 +1,9 @@ + { "bools": [ { "action": "store_true", - "ostring": ["+i", "++case-insensitive"] + "ostring": ["+c", "++case-sensitive"] }, { "action": "store_true", @@ -34,7 +35,8 @@ { "help": "Directory to start in", "metavar": "dir", - "ostring": ["+d", "++start-in"] + "ostring": ["+d", "++start-in"], + "default": null }, { "help": "Query to search for", @@ -50,7 +52,18 @@ "exts": [ { "ext": "py", - "opts": [] + "opts": [ + { + "action": "store_true", + "help": "open in python interactively", + "ostring": ["+I", "++interactive"] + }, + { + "action": "store_true", + "help": "open in python interactively", + "ostring": ["+l", "++idle-edit"] + } + ] }, { "ext": "java", @@ -61,7 +74,8 @@ { "help": "Directory to start in", "metavar": "dir", - "ostring": ["+d", "++start-in"] + "ostring": ["+d", "++start-in"], + "default": null }, { "help": "Query to search for", diff --git a/ext_open/opts.py b/ext_open/opts.py index 6c8ea41..d86f39e 100644 --- a/ext_open/opts.py +++ b/ext_open/opts.py @@ -1,6 +1,7 @@ +import copy import json import os -import copy +import shutil class ArgumentHandler: def __init__(self,args_file): @@ -13,11 +14,11 @@ class ArgumentHandler: ext = _ext['ext'] self.exts.add(ext) args = _ext['opts'] - self.set(ext,ArgumentContainer(args,self.data['prefix'])) + self.set(ext,ArgumentContainer(args,self.data['prefix_char'])) for key,value in self.data.items(): if key != 'exts' and isinstance(value,list): - self.set(key,ArgumentContainer(value,self.data['prefix'])) + self.set(key,ArgumentContainer(value,self.data['prefix_char'])) self.prefix_char = self.data['prefix_char'] @@ -51,7 +52,7 @@ class ArgumentHandler: backup_name = self.args_file + '.bak' if os.path.exists(backup_name): os.remove(backup_name) - os.copy(self.args_file,self.backup_name) + shutil.copy(self.args_file,backup_name) with open(self.args_file,'w') as file: json.dump(self.data,file) @@ -76,7 +77,7 @@ class ArgumentContainer: @property def argnames(self): if self._argnames is None: - self._argnames = set(max(arg['ostring'],key=len).lstrip(self.prefix) for arg in self.args) + self._argnames = set(max(arg['ostring'],key=len).lstrip(self.prefix).replace('-','_') for arg in self.args) return self._argnames def to_dict(self): @@ -92,4 +93,5 @@ class ArgumentContainer: cur = ArgumentHandler('opts.json') globals().update((key,cur.data[key]) for key in ['bools', 'exe', 'exts', 'other', 'prefix', 'prefix_char']) -common = bools+other \ No newline at end of file +common = bools+other +exe += bools \ No newline at end of file diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 9fe6f8a..ceb2251 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -28,7 +28,7 @@ def _in(expr,item,case): if case: return (1 if expr in item else 0) else: - return (1 if expr.lower() in item else 0) + return (1 if expr.lower() in item.lower() else 0) ext_database.create_function("REGEXP", 3, regexp) ext_database.create_function("INSIDE", 3, _in) @@ -94,6 +94,6 @@ class Searcher: if __name__ == "__main__": init_db() - s = Searcher('py') - s.update() + s = Searcher('exe',False) + # s.update() # s.update(True)