-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdbfilter.py
74 lines (54 loc) · 2.31 KB
/
dbfilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from utils.reflected import ReflectedEntryTable
from sqlalchemy import delete, create_engine, or_, text
import argparse
from pathlib import Path
def parse():
parser = argparse.ArgumentParser(description="Data analyzer program")
parser.add_argument("--db", help="DB to be scanned")
parser.add_argument("--remove-non-bookmarked", action="store_true", help="remove non bookmarked")
parser.add_argument("--remove-votes-threshold", type=int, help="remove entries below threshold")
parser.add_argument("--integrity", action="store_true", help="update other tables")
parser.add_argument("--truncate", help="truncates table")
parser.add_argument("-i", "--ignore-case", action="store_true", help="Ignores case")
parser.add_argument("-v", "--verbosity", help="Verbosity level")
parser.add_argument("-s", "--summary", help="Print summary")
args = parser.parse_args()
return parser, args
def main():
parser, args = parse()
if not Path(args.db).exists():
print("File does not exist")
return
engine = create_engine("sqlite:///" + args.db)
if args.remove_votes_threshold:
r = ReflectedEntryTable(engine)
table = r.get_table("linkdatamodel")
delete_query = delete(table).where(
or_(table.c.page_rating_votes < args.remove_votes_threshold, table.c.page_rating_votes.is_(None))
)
print(delete_query.compile(engine, compile_kwargs={"literal_binds": True}))
with engine.connect() as connection:
connection.execute(delete_query)
connection.commit()
r.close()
if args.remove_non_bookmarked:
r = ReflectedEntryTable(engine)
table = r.get_table("linkdatamodel")
delete_query = delete(table).where(
or_(table.c.bookmarked == False, table.c.bookmarked.is_(None))
)
print(delete_query.compile(engine, compile_kwargs={"literal_binds": True}))
with engine.connect() as connection:
connection.execute(delete_query)
connection.commit()
r.close()
if args.truncate:
r = ReflectedEntryTable(engine)
r.truncate_table(args.truncate)
r.close()
if args.integrity:
pass
if args.summary:
r = ReflectedEntryTable(engine)
r.print_summary()
main()