-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_mapper.py
More file actions
165 lines (133 loc) · 4.49 KB
/
resource_mapper.py
File metadata and controls
165 lines (133 loc) · 4.49 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:Mod: resource_mapper
:Synopsis:
:Author:
servilla
:Created:
6/24/21
"""
import asyncio
import logging
from typing import Optional
from pathlib import Path
import aiohttp
import click
import daiquiri
from lxml import etree
logfile = str(Path.cwd()) + Path(__file__).stem + ".log"
daiquiri.setup(
level=logging.INFO,
outputs=(
daiquiri.output.File(logfile),
"stdout",
),
)
logger = daiquiri.getLogger(__name__)
async def get_resource_info(pid: str, pasta: str) -> Optional[dict]:
pid_parts = pasta_identifier(pid)
if pid_parts is None:
msg = f"'{pid} is not in a recognized PASTA identifier format"
raise ValueError(msg)
else:
scope, identifier, revision = pid_parts[0], pid_parts[1], pid_parts[2]
url = f"https://{pasta}.lternet.edu/package/rmd/eml/{scope}/{identifier}/{revision}"
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
resp.raise_for_status()
resource_xml = await resp.text()
resource = dict()
root = etree.fromstring(resource_xml.encode("UTF-8"))
resource["date_created"] = root.find("dateCreated").text
resource["doi"] = root.find("doi").text
resource["identifier"] = root.find("identifier").text
resource["package_id"] = root.find("packageId").text
resource["principal_owner"] = root.find("principalOwner").text
resource["resource_id"] = root.find("resourceId").text
resource["resource_type"] = root.find("resourceType").text
resource["revision"] = root.find("revision").text
resource["scope"] = root.find("scope").text
return resource
async def get_block(pids: list, pasta: str, csv: str, verbose: bool):
tasks = []
for pid in pids:
tasks.append((pid, loop.create_task(get_resource_info(pid, pasta))))
for pid, t in tasks:
try:
ri = await t
if verbose or csv:
row = f"{ri['package_id']},{ri['doi']},{ri['date_created']},\"{ri['principal_owner']}\""
if verbose:
print(row)
if csv:
with open(csv, "a+") as f:
f.write(row + "\n")
except Exception as e:
logger.error(e)
def is_pasta_identifier(pid: str) -> bool:
is_pid = True
if pasta_identifier(pid) is None:
is_pid = False
return is_pid
def pasta_identifier(pid: str) -> Optional[list]:
pid_parts = pid.split(".")
if len(pid_parts) != 3:
return None
else:
try:
int(pid_parts[1])
int(pid_parts[2])
except ValueError as e:
logger.error(e)
return None
return pid_parts
csv_help = "Write output to CSV file"
block_size_help = "Number of concurrent requests to PASTA (default 5)"
env_help = (
"PASTA environment (production, staging, development) to query (default production)"
)
verbose_help = "Display to stdout (default false)"
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument("pid")
@click.option("-c", "--csv", default=None, help=csv_help)
@click.option("-b", "--block_size", default=5, help=block_size_help)
@click.option("-e", "--env", default="production", help=env_help)
@click.option("-v", "--verbose", is_flag=True, help=verbose_help)
def main(pid: str, csv: str, block_size: int, env: str, verbose: bool) -> int:
if Path(pid).is_file():
with open(pid) as f:
pids = [_.strip() for _ in f.readlines()]
elif is_pasta_identifier(pid):
pids = [pid]
else:
msg = f"{pid} is neither a file or a PASTA identifier"
logger.error(msg)
return 1
if env.lower() == "production":
pasta = "pasta"
elif env.lower() == "staging":
pasta = "pasta-s"
elif env.lower() == "development":
pasta = "pasta-d"
else:
msg = f"'{env} not a recognized PASTA environment"
logger.error(msg)
return 1
if verbose or csv:
row = "package_id,doi,date_created,principal_owner"
if verbose:
print(row)
if csv:
with open(csv, "w+") as f:
f.write(row + "\n")
global loop
loop = asyncio.get_event_loop()
for i in range(0, len(pids), block_size):
loop.run_until_complete(
get_block(pids[i : i + block_size], pasta, csv, verbose)
)
return 0
if __name__ == "__main__":
main()