diff --git a/ext_open/config.ini b/ext_open/config.ini index f3a8801..a29126e 100644 --- a/ext_open/config.ini +++ b/ext_open/config.ini @@ -1,9 +1,11 @@ [global] no_admin = no - + editor = "%programfiles%\notepad++\notepad++.exe" [exe] ; comma separated paths paths= %userprofile%,%programfiles%,%programfiles(x86)%, [py] - paths = %userprofile%\Documents\local_repo\windows\ext_open - \ No newline at end of file + paths = "%userprofile%\Documents\local_repo\windows\ext_open" + idle_edit = pythonw -m idlelib -e + editor = ${global:editor} + execute= python \ No newline at end of file diff --git a/ext_open/config.py b/ext_open/config.py index d13bdb2..d106ce6 100644 --- a/ext_open/config.py +++ b/ext_open/config.py @@ -11,5 +11,10 @@ class MyParser(configparser.ConfigParser): if data == fallback: return data return list(filter(bool,re.split(' *, *',data))) + def getpath(self,section, option,fallback=None): + data = self.get(section,option,fallback=fallback) + if data == fallback: + return data + return osp.expandvars(data) config = MyParser() config.read(CONFIG_PATH) \ No newline at end of file diff --git a/ext_open/ext_open.py b/ext_open/ext_open.py index 5da5818..7288540 100644 --- a/ext_open/ext_open.py +++ b/ext_open/ext_open.py @@ -1,4 +1,6 @@ from opts import * +import searcher +import opener import argparse parser = argparse.ArgumentParser(prog='ext-open',prefix_chars = '+') for opt in prefix: @@ -13,4 +15,4 @@ for ext in exts: opt = opt.copy() sub_parser.add_argument(*opt.pop('ostring'),**opt) if __name__ == '__main__': - args = parser.parse_args() + args = parser.parse_args() \ No newline at end of file diff --git a/ext_open/opener.py b/ext_open/opener.py index 9cc965b..b718027 100644 --- a/ext_open/opener.py +++ b/ext_open/opener.py @@ -2,6 +2,7 @@ import subprocess from binaryornot.check import is_binary import os import re +import shlex EDITOR_PATH = os.path.expandvars(r'%programfiles%\Notepad++\notepad++.exe') EXT = re.compile(r'\.\w+$') def get_ext(file): @@ -9,15 +10,6 @@ def get_ext(file): if match: return match.group(0) -handlers = { - '.py' : { - 'editor': EDITOR_PATH, - 'execute': 'python', - 'idle_edit': 'pythonw -m idlelib -e', - 'windowless': 'pythonw' - } -} - def edit(path,params=[]): call_chain = [EDITOR_PATH] + params if not is_binary(path): @@ -43,10 +35,8 @@ def standalone(path,params=[],windowless =False,cwd = None): def dependant(path,params=[],mode = 'execute'): ext = get_ext(path) if ext: - handler = handlers[ext][mode] - if ' ' in handler: - handler = handler.split(' ') - else: - handler = [handler] + handler = config.getpath(ext,mode) + handler = shlex.split(handler) + args = handler+[path]+params subprocess.Popen(args) diff --git a/ext_open/opts.py b/ext_open/opts.py index 54316c8..d115b37 100644 --- a/ext_open/opts.py +++ b/ext_open/opts.py @@ -4,6 +4,16 @@ prefix = [ 'help':'update the listings for ext', 'action':'store_true', }, + { + 'ostring':['+t','++trim+history'], + 'help': 'trim backup history, either all or N updates', + 'metavar': '(all | N)' + }, + { + 'ostring':['+m','++update+modified+times'], + 'help': '''updates the modified times for paths in config +useful for ensuring all new files are found''', + }, ] common = [ @@ -12,9 +22,15 @@ common = [ 'help': 'open in editor', 'action': 'store_true', }, + { + 'ostring':['+r','++regex'], + 'action':'store_true', + 'help':'use regex to search', + }, + { 'ostring':['arg'], - 'help':'commnad-line args', + 'help':'command-line args', 'action':'store', 'nargs':'*' } @@ -30,4 +46,9 @@ exts = [ } ] exe = [ + { + 'ostring':['+w','++windowless'], + 'help':'open windowless', + 'action': 'store_true', + }, ] \ No newline at end of file diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 5195067..4d6619f 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -11,18 +11,27 @@ ext_database = sqlite3.connect(DB_PATH) def init_db(): cur = ext_database.cursor() cur.execute('CREATE TABLE IF NOT EXISTS updates ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER);') -def regexp(expr, item): - return re.search(expr,item) is not None -def _in(expr,item): - return (1 if item in expr else 0) -ext_database.create_function("REGEXP", 2, regexp) -ext_database.create_function("INSIDE", 2, _in) + +def regexp(expr, item,case): + if case: + return (1 if re.search(expr,item) is not None else 0) + else: + return (1 if re.search(expr,item,flags=re.I) is not None else 0) +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) + +ext_database.create_function("REGEXP", 3, regexp) +ext_database.create_function("INSIDE", 3, _in) class Searcher: - def __init__(self,ext): + def __init__(self,ext,case_sensitive=True): self.cur = ext_database.cursor() self.ext = ext + self.case = case_sensitive self.index = '{}_index'.format(ext) self.__init_table__() @@ -34,7 +43,6 @@ class Searcher: {'ext':self.ext} ) self.commit() - def update(self,update_mtimes): self.cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[self.ext]) last_update = self.cur.fetchone()[0] @@ -58,9 +66,9 @@ class Searcher: def search(self,query,regex=False): if regex: - self.cur.execute('SELECT name,fullpath FROM {} WHERE name REGEXP ?;'.format(self.ext),[query]) + self.cur.execute('SELECT name,fullpath FROM {} WHERE REGEXP(?,name,?)=1;'.format(self.ext),[query,self.case]) else: - self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(name,?)=1;'.format(self.ext),[query]) + self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(?,name,?)=1;'.format(self.ext),[query,self.case]) return self.cur.fetchall() def clean(self,backhistory):