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.
88 lines
3.2 KiB
88 lines
3.2 KiB
import sqlite3
|
|
import os
|
|
import re
|
|
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 IF NOT EXISTS updates ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER);')
|
|
|
|
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.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 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 MAX(update_time) FROM updates WHERE ext=?;',[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(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)
|