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.

87 lines
3.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import sqlite3
  2. import os
  3. import re
  4. import update as _update
  5. from config import config
  6. import time
  7. # https://stackoverflow.com/a/5365533
  8. osp = os.path
  9. DB_PATH = osp.join(osp.dirname(__file__),'ext.db')
  10. ext_database = sqlite3.connect(DB_PATH)
  11. def init_db():
  12. cur = ext_database.cursor()
  13. cur.execute('CREATE TABLE IF NOT EXISTS updates ( `ext` TEXT, `update_time` INTEGER, `file_count` INTEGER);')
  14. def regexp(expr, item,case):
  15. if case:
  16. return (1 if re.search(expr,item) is not None else 0)
  17. else:
  18. return (1 if re.search(expr,item,flags=re.I) is not None else 0)
  19. def _in(expr,item,case):
  20. if case:
  21. return (1 if expr in item else 0)
  22. else:
  23. return (1 if expr.lower() in item else 0)
  24. ext_database.create_function("REGEXP", 3, regexp)
  25. ext_database.create_function("INSIDE", 3, _in)
  26. class Searcher:
  27. def __init__(self,ext,case_sensitive=True):
  28. self.cur = ext_database.cursor()
  29. self.ext = ext
  30. self.case = case_sensitive
  31. self.index = '{}_index'.format(ext)
  32. self.__init_table__()
  33. def __init_table__(self):
  34. self.cur.execute('CREATE TABLE IF NOT EXISTS {} (`name` TEXT,`fullpath` TEXT, `update_time` INTEGER,UNIQUE(name,fullpath));'.format(self.ext))
  35. self.cur.execute('CREATE INDEX IF NOT EXISTS {} ON {} ( `name`, `fullpath` )'.format(self.index,self.ext))
  36. self.cur.execute(
  37. '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);',
  38. {'ext':self.ext}
  39. )
  40. self.commit()
  41. def update(self,update_mtimes):
  42. self.cur.execute('SELECT MAX(update_time) FROM updates WHERE ext=?;',[self.ext])
  43. last_update = self.cur.fetchone()[0]
  44. # input(str(last_update))
  45. _time = int(time.time())
  46. files = _update.update(
  47. self.ext,
  48. config.getlist(self.ext,'paths'),
  49. last_update,
  50. update_mtimes
  51. )
  52. self.cur.executemany('INSERT OR IGNORE INTO {} VALUES (?,?,?)'.format(self.ext),(row + [_time] for row in files))
  53. self.cur.execute('INSERT INTO updates VALUES (?, ?, ?)',[self.ext,_time,len(files)])
  54. self.commit()
  55. def commit(self):
  56. ext_database.commit()
  57. def close(self):
  58. ext_database.close()
  59. def search(self,query,regex=False):
  60. if regex:
  61. self.cur.execute('SELECT name,fullpath FROM {} WHERE REGEXP(?,name,?)=1;'.format(self.ext),[query,self.case])
  62. else:
  63. self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(?,name,?)=1;'.format(self.ext),[query,self.case])
  64. return self.cur.fetchall()
  65. def clean(self,backhistory):
  66. self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT ?;',[self.ext,backhistory+1])
  67. res = self.cur.fetchall()
  68. threshold = res[-1][0]
  69. self.cur.execute('DELETE FROM {} WHERE update_time < ?;'.format(self.ext),[threshold])
  70. self.commit()
  71. def clear(self):
  72. self.cur.execute('DELETE FROM {};'.format(self.ext))
  73. self.commit()
  74. if __name__ == "__main__":
  75. init_db()
  76. s = Searcher('py')
  77. # s.update(True)