From 8a56096f38632b86f226b80756e3ec7be58aadc8 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 29 Oct 2018 16:36:17 -0500 Subject: [PATCH] Tested everything and we might be ready for a full release --- ext_open/config.ini | 11 +++++++++-- ext_open/opener.py | 6 ++++-- ext_open/searcher.py | 9 +++++++-- ext_open/update.py | 26 ++++++++++++++++---------- ext_open/util.py | 3 ++- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/ext_open/config.ini b/ext_open/config.ini index 6f2693d..0292130 100644 --- a/ext_open/config.ini +++ b/ext_open/config.ini @@ -1,8 +1,14 @@ [global] + ; set to yes if not an admin no_admin = no + ; default editor to open files in editor = "%programfiles%\notepad++\notepad++.exe" - db_path = X:\Users\Ralphie\Documents\local_repo\windows\ext_open\ext_open + ; path to database where file entries are to be stored + db_path = %userprofile%\Documents\local_repo\windows\ext_open\ext_open\ext.db + ; program or script used to update modified times modified_time_updater = %userprofile%\portables\foldertimeupdate\FolderTimeUpdate.exe + ; how many files inserted before committing + commit_interval = 30 [exe] ; comma separated paths paths= %userprofile%,%programfiles%,%programfiles(x86)%, @@ -10,4 +16,5 @@ 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 + execute= python + windowless = pythonw \ No newline at end of file diff --git a/ext_open/opener.py b/ext_open/opener.py index b718027..3e34b4a 100644 --- a/ext_open/opener.py +++ b/ext_open/opener.py @@ -3,6 +3,8 @@ from binaryornot.check import is_binary import os import re import shlex +if __name__ == "__main__": + __file__ = os.getcwd() EDITOR_PATH = os.path.expandvars(r'%programfiles%\Notepad++\notepad++.exe') EXT = re.compile(r'\.\w+$') def get_ext(file): @@ -25,12 +27,12 @@ def standalone(path,params=[],windowless =False,cwd = None): else: cwd = os.path.expandvars(cwd) if windowless: - cmd = ' '.join('~{}~'.format(arg) if ' ' in arg else arg for arg in [fullpath]+params) + cmd = subprocess.list2cmdline([path]+params).replace('"','~') print(cmd) subprocess.check_output(['cscript.exe',path2windowless,cmd,cwd]) else: - subprocess.Popen([fullpath]+params,cwd=cwd) + subprocess.Popen([path]+params,cwd=cwd) def dependant(path,params=[],mode = 'execute'): ext = get_ext(path) diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 100e8e8..c6a7986 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -14,7 +14,8 @@ 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`);') + cur.execute('CREATE TABLE IF NOT EXISTS `update_status` ( `ext` TEXT UNIQUE, `status` INTEGER, PRIMARY KEY(`ext`));') + ext_database.commit() # https://stackoverflow.com/a/5365533 def regexp(expr, item,case): @@ -58,7 +59,10 @@ class Searcher: def update(self,update_mtimes=False): if not self.updating: - self.update_proc = subprocess.Popen(['python',UPDATE_PY,'.'+self.ext]) + args = ['python',UPDATE_PY,self.ext] + if update_mtimes: + args.append('-m') + self.update_proc = subprocess.Popen(args) def commit(self): ext_database.commit() @@ -87,4 +91,5 @@ class Searcher: if __name__ == "__main__": init_db() s = Searcher('py') + s.update() # s.update(True) \ No newline at end of file diff --git a/ext_open/update.py b/ext_open/update.py index 3c0072c..3b38a70 100644 --- a/ext_open/update.py +++ b/ext_open/update.py @@ -7,6 +7,7 @@ import os.path as osp import sqlite3 import subprocess import time +import traceback FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False) TEST = True @@ -14,7 +15,7 @@ DB_PATH = config.getpath('global','db_path') ext_database = sqlite3.connect(DB_PATH) -EXE_PATH = config.getpath('global',modified_time_updater) +EXE_PATH = config.getpath('global','modified_time_updater') def update_mtimes(starts): if not is_admin() and FORCE_ADMIN: @@ -25,7 +26,6 @@ def update_mtimes(starts): def build(after,starts): if isinstance(after,datetime.datetime): after = after.timestamp() - starts = map(osp.expandvars,starts) starts = map(lambda item: item.replace('"','').replace("'",''),starts) for path in starts: for root,dirs,files in os.walk(path,topdown=True): @@ -54,16 +54,17 @@ def set_updating(ext,con,updating=True): status = 1 else: status = 0 - cur.execute('UPDATE update_status SET status=? WHERE ext=?;'[self.ext,self.status]) + cur.execute('UPDATE update_status SET status=? WHERE ext=?;',[status,ext]) con.commit() def get_updating(ext,cur): - cur.execute('SELECT status FROM update_status WHERE ext=?;',[self.ext]) - return cur.fetchone()[0][0] == 1 + cur.execute('SELECT status FROM update_status WHERE ext=?;',[ext]) + return cur.fetchone()[0] == 1 -def update(ext,update_mtimes): +def update(ext,do_update_mtimes): set_updating(ext,ext_database) cur = ext_database.cursor() + c = config.getint('global','commit_interval',fallback = 30) try: cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[ext]) last_update = cur.fetchone()[0] @@ -71,25 +72,29 @@ def update(ext,update_mtimes): params = ( '.'+ext, - ext_databasefig.getlist(ext,'paths'), + config.getpaths(ext,'paths'), last_update, - update_mtimes + do_update_mtimes ) - files = list(_update(*params)) + files = _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: + count += 1 + if count % c == 0: ext_database.commit() cur.execute('INSERT INTO updates VALUES (?, ?, ?);',[ext,_time,count+1]) + ext_database.commit() set_updating(ext,ext_database,False) except Exception as e: set_updating(ext,ext_database,False) + traceback.print_exc() + input() raise e if __name__ == "__main__": @@ -97,4 +102,5 @@ if __name__ == "__main__": parser.add_argument('ext') parser.add_argument('-m','--update-modified-time',action='store_true') args = parser.parse_args() + print(args) update(args.ext,args.update_modified_time) diff --git a/ext_open/util.py b/ext_open/util.py index 1754f74..ef93555 100644 --- a/ext_open/util.py +++ b/ext_open/util.py @@ -1,9 +1,10 @@ from ctypes import windll + import os import re # https://stackoverflow.com/a/827397 def is_admin(): - return ctypes.windll.shell32.IsUserAnAdmin() != 0 + return windll.shell32.IsUserAnAdmin() != 0 def get_drives(): uppercase = map(chr,range(ord('A'),ord('A')+26))