Skip to content

Commit 442ac59

Browse files
author
Alan Christie
committed
feat: Adds load/save ER scripts
1 parent 135bf52 commit 442ac59

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
venv/
33
**/__pycache__/
44
setenv.sh
5+
*.yaml

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
im-squonk2-client >= 1.9.0, < 2.0.0
1+
im-squonk2-client >= 1.9.3, < 2.0.0
22
python-dateutil == 2.8.2
33
rich == 12.5.1
4+
pyyaml == 6.0

tools/load-er.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
"""Loads Job/Application Exchange Rates from a file.
3+
"""
4+
import argparse
5+
from pathlib import Path
6+
import sys
7+
from typing import Any, Dict, List, Optional
8+
import urllib3
9+
10+
from rich.console import Console
11+
from squonk2.auth import Auth
12+
from squonk2.dm_api import DmApi, DmApiRv
13+
import yaml
14+
15+
from common import Env, get_env
16+
17+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
18+
19+
20+
def main(c_args: argparse.Namespace) -> None:
21+
"""Main function."""
22+
23+
console = Console()
24+
25+
env: Optional[Env] = get_env()
26+
if not env:
27+
return
28+
29+
token: str = Auth.get_access_token(
30+
keycloak_url=env.keycloak_url,
31+
keycloak_realm=env.keycloak_realm,
32+
keycloak_client_id=env.keycloak_dm_client_id,
33+
username=env.keycloak_user,
34+
password=env.keycloak_user_password,
35+
)
36+
37+
# Just read the list from the chosen file
38+
file_content: str = Path(c_args.file).read_text(encoding='utf8')
39+
rates: List[Dict[str, Any]] = yaml.load(file_content, Loader=yaml.FullLoader)
40+
er_rv: DmApiRv = DmApi.set_job_exchange_rates(token, rates=rates)
41+
if not er_rv.success:
42+
console.log(f'[bold red]ERROR[/bold red] {er_rv.msg["error"]}')
43+
sys.exit(1)
44+
45+
num_rates: int = len(rates)
46+
if num_rates:
47+
console.log(f'Loaded {num_rates}')
48+
else:
49+
console.log('Loaded [bold red]nothing[/bold red]')
50+
51+
52+
if __name__ == "__main__":
53+
54+
# Parse command line arguments
55+
parser = argparse.ArgumentParser(
56+
prog="coins",
57+
description="Loads exchange rates (from a YAML file)"
58+
)
59+
parser.add_argument('file', type=str, help='The source file')
60+
args: argparse.Namespace = parser.parse_args()
61+
62+
# File must exist
63+
if not Path(args.file).is_file():
64+
parser.error("File does not exist")
65+
66+
main(args)

tools/save-er.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
"""Saves Job/Application Exchange Rates to a file.
3+
"""
4+
from datetime import datetime
5+
import argparse
6+
from pathlib import Path
7+
import sys
8+
from typing import Optional
9+
import urllib3
10+
11+
from rich.console import Console
12+
from squonk2.auth import Auth
13+
from squonk2.dm_api import DmApi, DmApiRv
14+
import yaml
15+
16+
from common import Env, get_env
17+
18+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
19+
20+
21+
def main(c_args: argparse.Namespace) -> None:
22+
"""Main function."""
23+
24+
env: Optional[Env] = get_env()
25+
if not env:
26+
return
27+
28+
console = Console()
29+
30+
token: str = Auth.get_access_token(
31+
keycloak_url=env.keycloak_url,
32+
keycloak_realm=env.keycloak_realm,
33+
keycloak_client_id=env.keycloak_dm_client_id,
34+
username=env.keycloak_user,
35+
password=env.keycloak_user_password,
36+
)
37+
38+
er_rv: DmApiRv = DmApi.get_job_exchange_rates(token)
39+
if not er_rv.success:
40+
console.log(f'[bold red]ERROR[/bold red] {er_rv.msg["error"]}')
41+
sys.exit(1)
42+
43+
filename: str = c_args.file
44+
if not filename.endswith('.yaml'):
45+
filename += '.yaml'
46+
47+
# Just write the list to the chosen file,
48+
# with a handy header detailing the source
49+
header: str = "---"
50+
header += "\n# Saved Job Exchange Rates (using save-er.py)"
51+
header += "\n# From Keycloak: " + env.keycloak_url
52+
header += "\n# Client: " + env.keycloak_dm_client_id
53+
header += "\n# Time (UTC): " + str(datetime.utcnow())
54+
header += "\n\n"
55+
56+
num_rates: int = len(er_rv.msg['exchange_rates'])
57+
rates: str = yaml.dump(er_rv.msg['exchange_rates'], default_flow_style=False)
58+
Path(filename).write_text(header + rates, encoding='utf8')
59+
60+
if num_rates:
61+
console.log(f'Saved {num_rates} (to {filename})')
62+
else:
63+
console.log('Saved [bold red]nothing[/bold red]')
64+
65+
66+
if __name__ == "__main__":
67+
68+
# Parse command line arguments
69+
parser = argparse.ArgumentParser(
70+
prog="coins",
71+
description="Saves existing exchange rates (to a YAML file)"
72+
)
73+
parser.add_argument('file', type=str, help='The destination file')
74+
args: argparse.Namespace = parser.parse_args()
75+
76+
main(args)

0 commit comments

Comments
 (0)