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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
- """
- Extract and pretty-print JSON data from the database.
- Usage:
- inspect-json.py --gr-work [ID...]
- inspect-json.py --gr-book [ID...]
- inspect-json.py --ol-edition [ID...]
- inspect-json.py --ol-work [ID...]
- """
- import sys
- import re
- import json
- from docopt import docopt
- from bookdata import tracking, db, script_log
- class GR:
- def __init__(self, type):
- self.type = type
- def __call__(self, ids):
- with db.connect() as dbc, dbc.cursor() as cur:
- if ids:
- return [self._id_rec(cur, r) for r in ids]
- else:
- return [self._top_rec(cur)]
- def _id_rec(self, cur, id):
- t = self.type
- _log.info('fetching %s %s', t, id)
- q = f'''
- SELECT gr_{t}_data
- FROM gr.raw_{t} JOIN gr.{t}_ids USING (gr_{t}_rid)
- WHERE gr_{t}_id = %s
- '''
- cur.execute(q, [id])
- rec = cur.fetchone()
- if rec is None:
- _log.error('%s %s not found', t, id)
- else:
- return rec[0]
- def _top_rec(self, cur):
- t = self.type
- _log.info('fetching one %s', t)
- q = f'SELECT gr_{t}_data FROM gr.raw_{t} LIMIT 1'
- cur.execute(q)
- data, = cur.fetchone()
- _log.debug('got %r', data)
- return data
- class OL:
- def __init__(self, type):
- self.type = type
- def __call__(self, ids):
- with db.connect() as dbc, dbc.cursor() as cur:
- if ids:
- return [self._id_rec(cur, r) for r in ids]
- else:
- return [self._top_rec(cur)]
- def _id_rec(self, cur, id):
- t = self.type
- _log.info('fetching %s %s', t, id)
- q = f'''
- SELECT {t}_data
- FROM ol.{t}
- WHERE {t}_key = %s
- '''
- cur.execute(q, [id])
- rec = cur.fetchone()
- if rec is None:
- _log.error('%s %s not found', t, id)
- else:
- return rec[0]
- def _top_rec(self, cur):
- t = self.type
- _log.info('fetching one %s', t)
- q = f'SELECT {t}_data FROM ol.{t} LIMIT 1'
- cur.execute(q)
- data, = cur.fetchone()
- _log.debug('got %r', data)
- return data
- __gr_work = GR('work')
- __gr_book = GR('book')
- __ol_edition = OL('edition')
- __ol_work = OL('work')
- _log = script_log(__name__)
- opts = docopt(__doc__)
- rec_ids = opts.get('ID', None)
- recs = None
- for k in opts.keys():
- fn = k.replace('-', '_')
- if k.startswith('--') and opts[k] and fn in globals():
- f = globals()[fn]
- recs = f(rec_ids)
- if recs is None:
- _log.error('could not find an operation to perform')
- for rec in recs:
- print(json.dumps(rec, indent=2))
|