-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ftva_holdings_report.py
58 lines (46 loc) · 1.75 KB
/
get_ftva_holdings_report.py
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
import argparse
import tomllib
from csv import DictWriter
from alma_api_client import AlmaAnalyticsClient
def _get_args() -> argparse.Namespace:
"""Returns the command-line arguments for this program."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--config_file", help="Path to configuration file", required=True
)
parser.add_argument(
"--output_file",
help="Path to CSV output file which will be written from report data",
required=True,
)
args = parser.parse_args()
return args
def _get_config(config_file_name: str) -> dict:
"""Returns configuration for this program, loaded from TOML file."""
with open(config_file_name, "rb") as f:
config = tomllib.load(f)
return config
def get_ftva_holdings_report(analytics_api_key: str, report_path: str) -> list[dict]:
"""Gets the FTVA holdings report data from Alma analytics.
Path to file is hard-coded.
"""
aac = AlmaAnalyticsClient(analytics_api_key)
aac.set_report_path(report_path)
report = aac.get_report()
return report
def write_report_to_file(report: list[dict], output_file_name: str) -> None:
"""Writes report to a CSV file, output_file_name."""
keys = report[0].keys()
with open(output_file_name, "w") as f:
writer = DictWriter(f, keys)
writer.writeheader()
writer.writerows(report)
def main() -> None:
args = _get_args()
config = _get_config(args.config_file)
analytics_api_key = config["alma_config"]["analytics_api_key"]
report_path = config["alma_config"]["ftva_holdings_report"]
report = get_ftva_holdings_report(analytics_api_key, report_path)
write_report_to_file(report, args.output_file)
if __name__ == "__main__":
main()