gruplac-json 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ """gruplac-json: turn a public GrupLAC page (Minciencias, Colombia) into one JSON document.
2
+
3
+ >>> from gruplac_json import extract
4
+ >>> data = extract("COL0008423") # downloads the page; Scienti ids if an apis.colav.co key is set
5
+ >>> data["name"], len(data["members"]), len(data["works"])
6
+ """
7
+
8
+ from importlib.metadata import PackageNotFoundError, version
9
+
10
+ try:
11
+ __version__ = version("gruplac-json")
12
+ except PackageNotFoundError: # running from a source tree that was never installed
13
+ __version__ = "0.0.0"
14
+
15
+ from .parser import build, extract, fetch, resolve
16
+
17
+ __all__ = ["__version__", "build", "extract", "fetch", "resolve"]
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
gruplac_json/cli.py ADDED
@@ -0,0 +1,68 @@
1
+ """Command line entry point: `gruplac-json`."""
2
+
3
+ import argparse
4
+ import gzip
5
+ import json
6
+ import sys
7
+
8
+ from . import __version__
9
+ from .parser import decode, extract, fetch, resolve
10
+
11
+
12
+ def main(argv=None):
13
+ ap = argparse.ArgumentParser(
14
+ prog="gruplac-json",
15
+ description="Build a JSON from a research group's public GrupLAC page (Minciencias, Colombia)",
16
+ )
17
+ ap.add_argument("group", help="COL code (COL0008423) or GrupLAC number (00000000001676; leading zeros optional)")
18
+ ap.add_argument("-o", "--output", help="output file (default gruplac_<COL or number>.json; '-' for stdout)")
19
+ ap.add_argument("--html", help="parse a saved page (.html or .html.gz) instead of downloading it")
20
+ ap.add_argument("--save-html", help="also save the downloaded page here")
21
+ ap.add_argument("--scienti", choices=["auto", "off"], default="auto", help="add Scienti ids (needs an apis.colav.co key)")
22
+ ap.add_argument("--institution", help="Scienti dump (udea, univalle, uec, unaula); default: try them in turn")
23
+ ap.add_argument("--model-year", help="Scienti model year (default: the dump's latest)")
24
+ ap.add_argument("--apikey", help="apis.colav.co key (else $COLAV_APIKEY or ~/.config/colav/apikey)")
25
+ ap.add_argument("--ror", choices=["auto", "off"], default="auto", help="look institutions up in the ROR registry")
26
+ ap.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
27
+ a = ap.parse_args(argv)
28
+ log = lambda *x: print(*x, file=sys.stderr)
29
+ try:
30
+ nro, col = resolve(a.group)
31
+ except ValueError as e:
32
+ ap.error(str(e))
33
+ if a.html:
34
+ with open(a.html, "rb") as fh:
35
+ raw = fh.read()
36
+ html = decode(gzip.decompress(raw) if raw[:2] == b"\x1f\x8b" else raw)
37
+ else:
38
+ html = fetch(nro)
39
+ if a.save_html:
40
+ with open(a.save_html, "w", encoding="latin-1", errors="xmlcharrefreplace") as fh: # as the page declares
41
+ fh.write(html)
42
+ data = extract(
43
+ col or nro,
44
+ html=html,
45
+ scienti=a.scienti,
46
+ institution=a.institution,
47
+ model_year=a.model_year,
48
+ apikey=a.apikey,
49
+ ror=a.ror == "auto",
50
+ log=log,
51
+ )
52
+ out = a.output or f"gruplac_{col or nro}.json"
53
+ text = json.dumps(data, ensure_ascii=False, indent=1)
54
+ if out == "-":
55
+ print(text)
56
+ else:
57
+ with open(out, "w", encoding="utf-8") as fh:
58
+ fh.write(text + "\n")
59
+ c = data["counts"]
60
+ log(
61
+ f"{out}: {data['name']} - {c['members']} members, {c['works']} works, {c['projects']} projects, "
62
+ f"{c['events']} events, {sum(len(v) for v in data['other_products'].values())} other products"
63
+ )
64
+ return 0
65
+
66
+
67
+ if __name__ == "__main__":
68
+ raise SystemExit(main())