-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_pull_from_protocols.py
More file actions
86 lines (63 loc) · 2.38 KB
/
1_pull_from_protocols.py
File metadata and controls
86 lines (63 loc) · 2.38 KB
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
"""This script retrieves data from protocols.io, processes it to extract simple structured data
from the protocol run, and saves it to disk in JSON format for later use.
The rules written here are specific to the format used in the tarpd project, but are designed to be
relatively portable."""
import json
import re
import logging
from protocols_client import ProtocolsClient
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
CONFIG_FILENAME = "config.json"
logger.info(f"Looking for config at {CONFIG_FILENAME}...")
with open(CONFIG_FILENAME) as fin:
config = json.load(fin)
client = ProtocolsClient()
about_me = client.get_profile()
logger.info(f"Logged in as {about_me['user']['username']}")
logger.info(f"Retrieving run {config['run_id']}")
run = client.get_run(config['run_id'])
# Extract the steps
steps = run["protocol"]["steps"]
db = {}
for step in steps:
data = json.loads(step["data"])
blocks = data["blocks"]
title = blocks[0]["text"]
key = re.search("\[(.+)\]", title)
logger.debug("Found section with title '%s' and key '%s'", title, key)
# Skip if we don't see a [group_key]
if key is None:
continue
key = key.group(1)
# Handle different forms of input data
input_data = None
if "entityMap" in data and len(data["entityMap"]) > 0:
input_data = data["entityMap"][0]
# If nothing is there, skip.
if input_data is None:
continue
# Depending on the type
typ = input_data["type"]
value = None
logger.debug("Type of entry is '%s', value=%s", typ, value)
if typ == "notes":
# Join the text together as paragraphs. Not certain this will cover all cases
# but it should get the main use-case of the notes field.
db[key] = "\n\n".join([x["text"] for x in input_data["data"]["blocks"]])
if typ == "smart_component":
# Read each key as a unique top-level key
for a in input_data["data"]["fields"]:
key = re.search("\[(.+)\]", a['name'])
if key is None:
continue
db[key.group(1)] = a['value']
if typ == "link":
# A link
db[key] = input_data["data"]["url"]
# Write to disk
logger.info("Writing database to disk at %s", config['database_filename'])
with open(config["database_filename"], 'w') as fout:
json.dump(db, fout, indent=4)
logger.info("Success.")