From 3f1119153365e32919097f8ca7e6bda20658daf8 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Sat, 27 Oct 2018 01:05:51 -0500 Subject: [PATCH] Merged update_mtimes and build to update Created config --- config.ini | 6 ++++++ config.py | 16 ++++++++++++++++ opener.py | 2 +- opts.py | 2 +- searcher.py | 12 +++++++++++- build.py => update.py | 37 +++++++++++++++++++++++++++++++------ update_mtimes.py | 13 ------------- util.py | 3 +-- 8 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 config.ini create mode 100644 config.py rename build.py => update.py (50%) delete mode 100644 update_mtimes.py diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..f9a5700 --- /dev/null +++ b/config.ini @@ -0,0 +1,6 @@ +[global] + no_admin = yes +[exe] + ; comma separated paths + paths= %userprofile%,%programfiles%,%programfiles(x86)%, + \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..5972414 --- /dev/null +++ b/config.py @@ -0,0 +1,16 @@ +import configparser +import re +import os.path as osp +# https://stackoverflow.com/a/11866695 +CONFIG_PATH = osp.join(osp.dirname(__file__),'config.ini') +class MyParser(configparser.ConfigParser): + def __init__(self): + super().__init__(interpolation = configparser.ExtendedInterpolation()) + def getlist(self,section, option,fallback=None): + data = self.get(section,option,fallback=fallback) + if data == fallback: + 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 diff --git a/opener.py b/opener.py index 6de1274..9cc965b 100644 --- a/opener.py +++ b/opener.py @@ -39,7 +39,7 @@ def standalone(path,params=[],windowless =False,cwd = None): else: subprocess.Popen([fullpath]+params,cwd=cwd) - + def dependant(path,params=[],mode = 'execute'): ext = get_ext(path) if ext: diff --git a/opts.py b/opts.py index 59bfadd..54316c8 100644 --- a/opts.py +++ b/opts.py @@ -4,7 +4,7 @@ prefix = [ 'help':'update the listings for ext', 'action':'store_true', }, - + ] common = [ { diff --git a/searcher.py b/searcher.py index 144d7e8..3d32516 100644 --- a/searcher.py +++ b/searcher.py @@ -1,11 +1,17 @@ import sqlite3 import os +import re +import update +# 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`) )') +def regexp(expr, item): + return re.search(expr,item) is not None +ext_database.create_function("REGEXP", 2, regexp) class Searcher: def __init__(self,ext): self.cur = ext_database.cursor() @@ -22,4 +28,8 @@ class Searcher: def _commit_(self): ext_database.commit() def search(query,regex=False): - pass \ No newline at end of file + if regex: + 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 diff --git a/build.py b/update.py similarity index 50% rename from build.py rename to update.py index 9e6738c..51d57e0 100644 --- a/build.py +++ b/update.py @@ -1,8 +1,28 @@ +import ctypes import os import os.path as osp import datetime from util import no_parents,get_drives -def build(after,*starts): +from config import config + +FORCE_ADMIN = not config.getboolean('global','no_admin',fallback=False) + + +def is_admin(): + return ctypes.windll.shell32.IsUserAnAdmin() != 0 + +EXE_PATH = os.path.expandvars(r"%userprofile%\portables" + r"\foldertimeupdate" + r"\FolderTimeUpdate.exe" + ) + +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]) + +def build(after,starts): if isinstance(after,datetime.datetime): after = after.timestamp() var_unsubbed = map(osp.expandvars,starts) @@ -20,14 +40,19 @@ def build(after,*starts): except: print('Error',root,sep= ': ') return ret - + def build_ext(ext,paths): - ret = {} + ret = [] for path in paths: - if path.endswith(ext): + if path.endswith(ext): name = osp.basename(path) try: ret[name].append(path) except KeyError: - ret[name] = [path] - return ret \ No newline at end of file + ret.append([name,path]) + return ret + +def update(after,*starts,update_mtimes = False): + if update_mtimes: + update_mtimes(starts) + return build(after,starts) \ No newline at end of file diff --git a/update_mtimes.py b/update_mtimes.py deleted file mode 100644 index 7032917..0000000 --- a/update_mtimes.py +++ /dev/null @@ -1,13 +0,0 @@ -import ctypes -import os -def is_admin(): - return ctypes.windll.shell32.IsUserAnAdmin() != 0 -exe_path = os.path.expandvars(r"%userprofile%\portables" - r"\foldertimeupdate" - r"\FolderTimeUpdate.exe" - ) -def update(*starts,force_admin = True): - 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]) diff --git a/util.py b/util.py index 5b3a19d..68c49cb 100644 --- a/util.py +++ b/util.py @@ -1,7 +1,6 @@ from ctypes import windll import os -#https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-drive-letters-in-python -#u=RichieHindle +# https://stackoverflow.com/a/827397 def get_drives(): uppercase = map(chr,range(ord('A'),ord('A')+26)) drives = []