diff --git a/ext_open/config.ini b/ext_open/config.ini index f9a5700..f3a8801 100644 --- a/ext_open/config.ini +++ b/ext_open/config.ini @@ -1,6 +1,9 @@ [global] - no_admin = yes + no_admin = no + [exe] ; comma separated paths paths= %userprofile%,%programfiles%,%programfiles(x86)%, +[py] + paths = %userprofile%\Documents\local_repo\windows\ext_open \ No newline at end of file diff --git a/ext_open/config.py b/ext_open/config.py index 5972414..d13bdb2 100644 --- a/ext_open/config.py +++ b/ext_open/config.py @@ -12,5 +12,4 @@ class MyParser(configparser.ConfigParser): return data return list(filter(bool,re.split(' *, *',data))) config = MyParser() -config.read(CONFIG_PATH) -print(config.getlist('exe','paths')) \ No newline at end of file +config.read(CONFIG_PATH) \ No newline at end of file diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 3d32516..7b3d53d 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -1,14 +1,16 @@ import sqlite3 import os import re -import update +import update as _update +from config import config +import time # https://stackoverflow.com/a/5365533 osp = os.path DB_PATH = osp.join(osp.dirname(__file__),'ext.db') ext_database = sqlite3.connect(DB_PATH) def init_db(): cur = ext_database.cursor() - cur.execute('CREATE TABLE "updates" ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER, PRIMARY KEY(`ext`) )') + cur.execute('CREATE TABLE IF NOT EXISTS updates ( `ext` TET, `update_time` INTEGER, `file_count` INTEGER);') def regexp(expr, item): return re.search(expr,item) is not None ext_database.create_function("REGEXP", 2, regexp) @@ -19,17 +21,41 @@ class Searcher: self.index = '{}_index'.format(ext) self.__init_table__() def __init_table__(self): - self.cur.execute('CREATE TABLE IF NOT EXISTS ? (`name` TEXT,`fullpath` TEXT,UNIQUE(name,fullpath));',[self.ext]) - self.cur.execute('CREATE INDEX IF NOT EXISTS ? ON ? ( `name`, `fullpath` )',[self.index,self.ext]) - def _update_(self,update_mtimes): - # update logic - self.cur.executemany('INSERT OR IGNORE INTO ? VALUES (?,?)',params) - self.cur.execute('INSERT INTO updates VALUES (?, ?, ?)',[self.ext,time,count]) - def _commit_(self): + self.cur.execute('CREATE TABLE IF NOT EXISTS {} (`name` TEXT,`fullpath` TEXT, `update_time` INTEGER,UNIQUE(name,fullpath));'.format(self.ext)) + self.cur.execute('CREATE INDEX IF NOT EXISTS {} ON {} ( `name`, `fullpath` )'.format(self.index,self.ext)) + self.cur.execute( + 'INSERT INTO updates (ext,update_time,file_count) SELECT :ext, 0, 0 WHERE NOT EXISTS(SELECT 1 FROM updates WHERE ext = :ext AND update_time = 0 AND file_count = 0);', + {'ext':self.ext} + ) + self.commit() + def update(self,update_mtimes): + self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT 1;',[self.ext]) + last_update = self.cur.fetchone()[0] + # input(str(last_update)) + _time = int(time.time()) + files = _update.update( + self.ext, + config.getlist(self.ext,'paths'), + last_update, + update_mtimes + ) + self.cur.executemany('INSERT OR IGNORE INTO {} VALUES (?,?,?)'.format(self.ext),(row + [_time] for row in files)) + self.cur.execute('INSERT INTO updates VALUES (?, ?, ?)',[self.ext,_time,len(files)]) + self.commit() + def commit(self): ext_database.commit() + def close(self): + ext_database.close() def search(query,regex=False): if regex: - self.cur.execute('SELECT * FROM ? WHERE name REGEXP ?',[self.ext,query]) + self.cur.execute('SELECT * FROM ? WHERE name REGEXP ?;',[self.ext,query]) else: - self.cur.execute('SELECT * FROM ? WHERE name LIKE ?',[self.ext,query]) - return self.cur.fetchall() \ No newline at end of file + self.cur.execute('SELECT * FROM ? WHERE name LIKE ?;',[self.ext,query]) + return self.cur.fetchall() + def clean(self,backhistory): + pass + # TODO +if __name__ == "__main__": + init_db() + s = Searcher('py') + s.update(True) \ No newline at end of file diff --git a/ext_open/update.py b/ext_open/update.py index 51d57e0..d41ca1d 100644 --- a/ext_open/update.py +++ b/ext_open/update.py @@ -4,6 +4,7 @@ import os.path as osp import datetime from util import no_parents,get_drives from config import config +import subprocess FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False) @@ -20,7 +21,7 @@ def update_mtimes(starts): if not is_admin() and FORCE_ADMIN: raise Exception("Process must be admin") for start in starts: - subprocess.check_call([EXE_PATH,'/stext','null','/BaseFolder',start]) + subprocess.check_call([EXE_PATH,'/stext','nul','/BaseFolder',start]) def build(after,starts): if isinstance(after,datetime.datetime): @@ -46,13 +47,11 @@ def build_ext(ext,paths): for path in paths: if path.endswith(ext): name = osp.basename(path) - try: - ret[name].append(path) - except KeyError: - ret.append([name,path]) + ret.append([name,path]) return ret -def update(after,*starts,update_mtimes = False): - if update_mtimes: +def update(ext,starts,after,do_update_mtimes = False): + if do_update_mtimes: update_mtimes(starts) - return build(after,starts) \ No newline at end of file + files = build(after,starts) + return build_ext(ext,files) \ No newline at end of file