-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat_personae.py
73 lines (58 loc) · 1.95 KB
/
concat_personae.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
import argparse
import sqlite3
import yaml
from logging import getLogger,config
from pathlib import Path
def main(args):
input_dirname:str=args.input_dirname
output_filepath:str=args.output_filepath
#Set up logger
with open("./logging_config.yaml","r",encoding="utf-8") as r:
logging_config=yaml.safe_load(r)
config.dictConfig(logging_config)
logger=getLogger(__name__)
logger.debug(args)
#Get input files
input_dir=Path(input_dirname)
input_files=list(input_dir.glob("*.db"))
input_files.sort()
logger.info(f"{len(input_files)} files exist in the input directory")
#Concatenate personae
logger.info("Start concatenating personae...")
with sqlite3.connect(output_filepath) as conn:
cur=conn.cursor()
#Create table to concatenate personae
cur.execute(
"""
CREATE TABLE personae (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email STRING NOT NULL,
poh STRING NOT NULL
);
"""
)
conn.commit()
#Insert records from each DB
for input_file in input_files:
logger.info(f"Processing '{input_file.name}'")
#Attach as temp DB
cur.execute(f"ATTACH DATABASE '{str(input_file)}' AS tmpdb;")
#Insert records
cur.execute(
"""
INSERT INTO personae (email,poh)
SELECT email,poh
FROM tmpdb.personae
WHERE email IS NOT NULL AND poh IS NOT NULL;
"""
)
conn.commit()
#Detach temp DB
cur.execute("DETACH tmpdb;")
logger.info("Finished concatenating personae")
if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument("-i","--input-dirname",type=str)
parser.add_argument("-o","--output-filepath",type=str)
args=parser.parse_args()
main(args)