@ -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 ( )