You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

90 lines
3.1 KiB

from config import config
import os
import re
import sqlite3
import subprocess
import time
import update
osp = os.path
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)
else:
return (1 if expr.lower() in item else 0)
ext_database.create_function("REGEXP", 3, regexp)
ext_database.create_function("INSIDE", 3, _in)
class Searcher:
def __init__(self,ext,case_sensitive=True):
self.cur = ext_database.cursor()
self.ext = ext
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(
'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()
@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()
def close(self):
ext_database.close()
def search(self,query,regex=False):
if regex:
self.cur.execute('SELECT name,fullpath FROM {} WHERE REGEXP(?,name,?)=1;'.format(self.ext),[query,self.case])
else:
self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(?,name,?)=1;'.format(self.ext),[query,self.case])
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])
res = self.cur.fetchall()
threshold = res[-1][0]
self.cur.execute('DELETE FROM {} WHERE update_time < ?;'.format(self.ext),[threshold])
self.commit()
def clear(self):
self.cur.execute('DELETE FROM {};'.format(self.ext))
self.commit()
if __name__ == "__main__":
init_db()
s = Searcher('py')
# s.update(True)