blob: 54856c903d8d627132888c9be54b8dfaab8aea8f (
plain)
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
|
import sys
from typing import Iterable, Optional
from collections import Counter
from chocula.config import ChoculaConfig
from chocula.database import DirectoryInfo
class DirectoryLoader():
source_slug: str = "GENERIC"
def __init__(self, config: ChoculaConfig):
self.config = config
def open_file(self) -> Iterable:
raise NotImplementedError()
def parse_record(self, record) -> Optional[DirectoryInfo]:
raise NotImplementedError()
def index_file(self, db) -> Counter:
print(f"##### Loading {self.source_slug}...", file=sys.stderr)
counts: Counter = Counter()
cur = db.db.cursor()
for record in self.open_file():
counts['total'] += 1
info = self.parse_record(record)
if info:
status = db.insert_directory(info, cur=cur)
counts[status] += 1
cur.close()
db.db.commit()
return counts
|