Browse Source

Added options, case sensitve/insensitive query,etc

master
Raphael Roberts 7 years ago
parent
commit
0ccf1e52d3
  1. 8
      ext_open/config.ini
  2. 5
      ext_open/config.py
  3. 4
      ext_open/ext_open.py
  4. 18
      ext_open/opener.py
  5. 23
      ext_open/opts.py
  6. 28
      ext_open/searcher.py

8
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
paths = "%userprofile%\Documents\local_repo\windows\ext_open"
idle_edit = pythonw -m idlelib -e
editor = ${global:editor}
execute= python

5
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)

4
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()

18
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)

23
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',
},
]

28
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):

Loading…
Cancel
Save