diff --git a/ext_open/config.ini b/ext_open/config.ini index a29126e..6f2693d 100644 --- a/ext_open/config.ini +++ b/ext_open/config.ini @@ -1,6 +1,8 @@ [global] no_admin = no editor = "%programfiles%\notepad++\notepad++.exe" + db_path = X:\Users\Ralphie\Documents\local_repo\windows\ext_open\ext_open + modified_time_updater = %userprofile%\portables\foldertimeupdate\FolderTimeUpdate.exe [exe] ; comma separated paths paths= %userprofile%,%programfiles%,%programfiles(x86)%, diff --git a/ext_open/config.py b/ext_open/config.py index d106ce6..2c59cbb 100644 --- a/ext_open/config.py +++ b/ext_open/config.py @@ -16,5 +16,8 @@ class MyParser(configparser.ConfigParser): if data == fallback: return data return osp.expandvars(data) + def getpaths(self,section,option,fallback=None): + data = self.getlist(section,option,fallback) + return list(map(osp.expandvars,data)) config = MyParser() config.read(CONFIG_PATH) \ No newline at end of file diff --git a/ext_open/ext.db-journal b/ext_open/ext.db-journal deleted file mode 100644 index a73675e..0000000 Binary files a/ext_open/ext.db-journal and /dev/null differ diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 9c26c3b..100e8e8 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -1,24 +1,28 @@ -import sqlite3 +from config import config import os import re -import update as _update -from config import config +import sqlite3 +import subprocess import time -import threading -# https://stackoverflow.com/a/5365533 +import update osp = os.path -DB_PATH = osp.join(osp.dirname(__file__),'ext.db') +DB_PATH = config.getpath('global','db_path') +PARENT = osp.join(osp.dirname(__file__)) +UPDATE_PY = update.__file__ 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);') cur.execute('CREATE TABLE IF NOT EXISTS `update_status` ( `ext` TEXT UNIQUE, `status` INTEGER, PRIMARY KEY(`ext`);') +# https://stackoverflow.com/a/5365533 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) @@ -33,66 +37,28 @@ class Searcher: def __init__(self,ext,case_sensitive=True): self.cur = ext_database.cursor() self.ext = ext - self.updating = False - self.update_thread = None + self.update_proc = None self.case = case_sensitive self.index = '{}_index'.format(ext) self.__init_table__() def __init_table__(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 OR IGNORE INTO update_status VALUES (?, 0)',[self.ext]) -); + self.cur.execute('CREATE INDEX IF NOT EXISTS {} ON {} ( `name`, `fullpath` );'.format(self.index,self.ext)) + self.cur.execute('INSERT OR IGNORE INTO update_status VALUES (?, 0);',[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=False): - if not self.get_updating(ext_database): - self.update_thread = threading.Thread(target = self._update_,args = (update_mtimes,)) - self.update_thread.start() - def set_updating(self,con,updating = True): - cur = con.cursor() - if updating: - status = 1 - else: - status = 0 - cur.execute('UPDATE update_status SET status=? WHERE ext=?;'[self.ext,self.status]) - con.commit() - def get_updating(self,con): - cur = con.cursor() - cur.execute('SELECT status FROM update_status WHERE ext=?;',[self.ext]) - return cur.fetchone()[0][0] == 1 - def _update_(self,update_mtimes): - con = sqlite3.connect(DB_PATH) - try: - self.set_updating(con) - thread_cur = con.cursor() - thread_cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[self.ext]) - last_update = thread_cur.fetchone()[0] - # input(str(last_update)) - _time = int(time.time()) - params = ( - '.'+self.ext, - config.getlist(self.ext,'paths'), - last_update, - update_mtimes - ) - files = list(_update.update(*params)) - count = 0 - for count,row in enumerate(files): - row.append(_time) - thread_cur.execute('INSERT OR IGNORE INTO {} VALUES (?,?,?);'.format(self.ext),row) - if (count+1) % 30 == 0: - con.commit() - thread_cur.execute('INSERT INTO updates VALUES (?, ?, ?);',[self.ext,_time,count+1]) - self.set_updating(con,False) - except Exception as e: - self.set_updating(con,False) - raise e + @property + def updating(self): + return update.get_updating(self.ext,self.cur) + + def update(self,update_mtimes=False): + if not self.updating: + self.update_proc = subprocess.Popen(['python',UPDATE_PY,'.'+self.ext]) def commit(self): ext_database.commit() diff --git a/ext_open/update.py b/ext_open/update.py index 46f9053..3c0072c 100644 --- a/ext_open/update.py +++ b/ext_open/update.py @@ -1,23 +1,20 @@ -import ctypes +from config import config +from util import no_parents,get_drives,is_admin +import argparse +import datetime import os import os.path as osp -import datetime -from util import no_parents,get_drives -from config import config +import sqlite3 import subprocess +import time FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False) TEST = True -if TEST: - import time +DB_PATH = config.getpath('global','db_path') +ext_database = sqlite3.connect(DB_PATH) -def is_admin(): - return ctypes.windll.shell32.IsUserAnAdmin() != 0 -EXE_PATH = os.path.expandvars(r"%userprofile%\portables" - r"\foldertimeupdate" - r"\FolderTimeUpdate.exe" - ) +EXE_PATH = config.getpath('global',modified_time_updater) def update_mtimes(starts): if not is_admin() and FORCE_ADMIN: @@ -42,15 +39,62 @@ def build(after,starts): except: print('Error',root,sep= ': ') -def build_ext(ext,paths): - for path in paths: - if path.endswith(ext): - name = osp.basename(path) - yield [name,path] - -def update(ext,starts,after,do_update_mtimes = False): +def _update(ext,starts,after,do_update_mtimes = False): if do_update_mtimes: update_mtimes(starts) files = build(after,starts) - for file in build_ext(ext,files): - yield file \ No newline at end of file + for file in files: + if file.endswith(ext): + name = osp.basename(file) + yield [name,file] + +def set_updating(ext,con,updating=True): + cur = con.cursor() + if updating: + status = 1 + else: + status = 0 + cur.execute('UPDATE update_status SET status=? WHERE ext=?;'[self.ext,self.status]) + con.commit() + +def get_updating(ext,cur): + cur.execute('SELECT status FROM update_status WHERE ext=?;',[self.ext]) + return cur.fetchone()[0][0] == 1 + +def update(ext,update_mtimes): + set_updating(ext,ext_database) + cur = ext_database.cursor() + try: + cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[ext]) + last_update = cur.fetchone()[0] + _time = int(time.time()) + + params = ( + '.'+ext, + ext_databasefig.getlist(ext,'paths'), + last_update, + update_mtimes + ) + + files = list(_update(*params)) + count = 0 + + for count,row in enumerate(files): + row.append(_time) + cur.execute('INSERT OR IGNORE INTO {} VALUES (?,?,?);'.format(ext),row) + if (count+1) % 30 == 0: + ext_database.commit() + + cur.execute('INSERT INTO updates VALUES (?, ?, ?);',[ext,_time,count+1]) + set_updating(ext,ext_database,False) + + except Exception as e: + set_updating(ext,ext_database,False) + raise e + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('ext') + parser.add_argument('-m','--update-modified-time',action='store_true') + args = parser.parse_args() + update(args.ext,args.update_modified_time) diff --git a/ext_open/util.py b/ext_open/util.py index c748202..1754f74 100644 --- a/ext_open/util.py +++ b/ext_open/util.py @@ -2,6 +2,9 @@ from ctypes import windll import os import re # https://stackoverflow.com/a/827397 +def is_admin(): + return ctypes.windll.shell32.IsUserAnAdmin() != 0 + def get_drives(): uppercase = map(chr,range(ord('A'),ord('A')+26)) drives = []