Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

inspect-json.py 2.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Extract and pretty-print JSON data from the database.
  3. Usage:
  4. inspect-json.py --gr-work [ID...]
  5. inspect-json.py --gr-book [ID...]
  6. inspect-json.py --ol-edition [ID...]
  7. inspect-json.py --ol-work [ID...]
  8. """
  9. import sys
  10. import re
  11. import json
  12. from docopt import docopt
  13. from bookdata import tracking, db, script_log
  14. class GR:
  15. def __init__(self, type):
  16. self.type = type
  17. def __call__(self, ids):
  18. with db.connect() as dbc, dbc.cursor() as cur:
  19. if ids:
  20. return [self._id_rec(cur, r) for r in ids]
  21. else:
  22. return [self._top_rec(cur)]
  23. def _id_rec(self, cur, id):
  24. t = self.type
  25. _log.info('fetching %s %s', t, id)
  26. q = f'''
  27. SELECT gr_{t}_data
  28. FROM gr.raw_{t} JOIN gr.{t}_ids USING (gr_{t}_rid)
  29. WHERE gr_{t}_id = %s
  30. '''
  31. cur.execute(q, [id])
  32. rec = cur.fetchone()
  33. if rec is None:
  34. _log.error('%s %s not found', t, id)
  35. else:
  36. return rec[0]
  37. def _top_rec(self, cur):
  38. t = self.type
  39. _log.info('fetching one %s', t)
  40. q = f'SELECT gr_{t}_data FROM gr.raw_{t} LIMIT 1'
  41. cur.execute(q)
  42. data, = cur.fetchone()
  43. _log.debug('got %r', data)
  44. return data
  45. class OL:
  46. def __init__(self, type):
  47. self.type = type
  48. def __call__(self, ids):
  49. with db.connect() as dbc, dbc.cursor() as cur:
  50. if ids:
  51. return [self._id_rec(cur, r) for r in ids]
  52. else:
  53. return [self._top_rec(cur)]
  54. def _id_rec(self, cur, id):
  55. t = self.type
  56. _log.info('fetching %s %s', t, id)
  57. q = f'''
  58. SELECT {t}_data
  59. FROM ol.{t}
  60. WHERE {t}_key = %s
  61. '''
  62. cur.execute(q, [id])
  63. rec = cur.fetchone()
  64. if rec is None:
  65. _log.error('%s %s not found', t, id)
  66. else:
  67. return rec[0]
  68. def _top_rec(self, cur):
  69. t = self.type
  70. _log.info('fetching one %s', t)
  71. q = f'SELECT {t}_data FROM ol.{t} LIMIT 1'
  72. cur.execute(q)
  73. data, = cur.fetchone()
  74. _log.debug('got %r', data)
  75. return data
  76. __gr_work = GR('work')
  77. __gr_book = GR('book')
  78. __ol_edition = OL('edition')
  79. __ol_work = OL('work')
  80. _log = script_log(__name__)
  81. opts = docopt(__doc__)
  82. rec_ids = opts.get('ID', None)
  83. recs = None
  84. for k in opts.keys():
  85. fn = k.replace('-', '_')
  86. if k.startswith('--') and opts[k] and fn in globals():
  87. f = globals()[fn]
  88. recs = f(rec_ids)
  89. if recs is None:
  90. _log.error('could not find an operation to perform')
  91. for rec in recs:
  92. print(json.dumps(rec, indent=2))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...