Browse Source

Made some progress in xcode so it needs to be cleaned

master
Raphael Roberts 7 years ago
parent
commit
29f1c63709
  1. 19
      ext_open/ext_open.py
  2. 3
      ext_open/opts.py
  3. 6
      ext_open/searcher.py
  4. 49
      ext_open/util.py

19
ext_open/ext_open.py

@ -2,6 +2,7 @@ from opts import *
import searcher
import opener
import argparse
from util import print2d
parser = argparse.ArgumentParser(prog='ext-open',prefix_chars=prefix_char)
for opt in prefix:
parser.add_argument(*opt.pop('ostring'),**opt)
@ -16,4 +17,20 @@ for ext in exts:
sub_parser.add_argument(*opt.pop('ostring'),**opt)
if __name__ == '__main__':
args = parser.parse_args()
print(args)
ext = arsg.ext
s = searcher.Searcher(ext,args.case_insensitive)
if args.update:
s.update()
if args.exact:
matches = s.exeact(args.query)
else:
matches = s.search(args.query,args.regex)
print2d(matches)
which = None
while not which:
try:
which = input("Which?: ")
except KeyBoardInterrupt:
quit()

3
ext_open/opts.py

@ -12,6 +12,7 @@ prefix = [
},
{
'ostring':['+m','++update-modified-times'],
'action':'store_true',
'help': '''updates the modified times for paths in config
useful for ensuring all new files are found''',
},
@ -49,4 +50,4 @@ exe = [
{'help': 'Directory to start in', 'ostring': ['+d', '++start-in'],'metavar':'dir'},
{'ostring':['query'],'help':'Query to search for'},
{'action': 'store', 'help': 'Command-line args', 'nargs': '*', 'ostring': ['arg']},
]
]

6
ext_open/searcher.py

@ -76,6 +76,10 @@ class Searcher:
else:
self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(?,name,?)=1;'.format(self.ext),[query,self.case])
return self.cur.fetchall()
def exact(self,query):
self.cur.execute('SELECT name,fullpath FROM {} WHERE name=?'.format(self.ext),[query])
return self.cur.fetchall()
def clean(self,backhistory):
self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT ?;',[self.ext,backhistory+1])
@ -92,4 +96,4 @@ if __name__ == "__main__":
init_db()
s = Searcher('py')
s.update()
# s.update(True)
# s.update(True)

49
ext_open/util.py

@ -1,7 +1,9 @@
from ctypes import windll
import datetime
from pydoc import pager
import os
import re
# https://stackoverflow.com/a/827397
def is_admin():
return windll.shell32.IsUserAnAdmin() != 0
@ -29,4 +31,47 @@ def no_parents(paths):
if not path.startswith(el1):
paths.append(path)
return ret
return ret
def str_coerce(s,**kwargs):
if isinstance(s,datetime.datetime):
return s.strftime(kwargs['datetime_format'])
else:
return str(s)
def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer = ' ',bottom = '=',l_end = '|',r_end = '|',interactive = False):
l = [[str_coerce(s,datetime_format = datetime_format) for s in row] for row in l]
max_col = []
for row in l:
for i,col in enumerate(row):
try:
max_col[i] = max(max_col[i],len(col))
except IndexError:
max_col.append(len(col))
fmt_row = '{content}'
if l_end:
fmt_row = f'{l_end} ' + fmt_row
if r_end:
fmt_row = fmt_row + f' {r_end}'
done = []
for row in l:
content = seperator.join(col.ljust(max_col[i],spacer if i < len(row)-1 or r_end else ' ') for i,col in enumerate(row))
done.append(fmt_row.format(content = content))
if bottom:
bottom = bottom*len(done[0])
row_sep = ('\n'+bottom+'\n')
else:
row_sep = '\n'
final = row_sep.join(done)
if bottom:
final = '\n'.join((bottom,final,bottom))
if interactive:
if not bottom:
final += '\n'
pager(final)
else:
return final
Loading…
Cancel
Save