-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathparser.py
83 lines (62 loc) · 2.69 KB
/
parser.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
75
76
77
78
79
80
81
82
83
import logging
import os
from datetime import datetime
import items
import utils as u
from extract import extract
logger = logging.getLogger(__name__)
class Parser():
def __init__(self, data_root, h_index_fname, db):
self.data_root = data_root
self.h_index_fname = h_index_fname
self.db = db
def update(self):
def get_idx(dir):
return int(dir[7:])
listing = sorted((fn for fn in os.listdir(self.data_root)
if fn.startswith('datadir')),
key=get_idx)
cur_datadir_idx = get_idx(self.db['cur_datadir'])
for datadir in u.full_circle(listing, cur_datadir_idx):
self.update_datadir(datadir)
def update_datadir(self, datadir):
logger.info('Entering {}'.format(datadir))
h_idx_file = items.IndexFile(os.path.join(self.data_root,
datadir,
self.h_index_fname))
h_idx_file_rev = h_idx_file.header.revision
logger.info('Index revision is {}'.format(h_idx_file_rev))
db_dir_entry = self.db['datadirs'][datadir]
# Skip if revision has not changed
if db_dir_entry['revision'] == h_idx_file_rev:
logger.info('Revision unchanged, nothing to update')
return
cur_sec_idx = db_dir_entry['cur_section']
for sec in u.full_circle(h_idx_file.sections, cur_sec_idx):
logger.debug('Entering section {}'.format(sec.idx))
if sec.idx == cur_sec_idx:
next_vrec_idx = db_dir_entry['last_vrec'] + 1
else:
next_vrec_idx = 0
next_vrecs = u.islice_from(sec.video_records, next_vrec_idx)
for i, vrec in enumerate(next_vrecs):
if vrec.start_dt == datetime.utcfromtimestamp(0):
logger.debug(
'Skipping extraction of incomplete vrecat {}:{:x}'
.format(vrec._h_idx_file.name, vrec._pos)
)
continue
try:
extract(vrec)
db_dir_entry['last_vrec'] = next_vrec_idx + i
db_dir_entry['cur_section'] = sec.idx
self.db['cur_datadir'] = datadir
self.db.save()
except FileExistsError as e:
logger.info(
'File {} exists, will not overwrite'
.format(e.filename)
)
logger.info('Done processing revision {}'.format(h_idx_file_rev))
db_dir_entry['revision'] = h_idx_file_rev
self.db.save()