-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_static_narrative.py
132 lines (99 loc) · 4.04 KB
/
create_static_narrative.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
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
"""Script to create a static narrative from the commandline.
Arguments:
-u, --user <str> KBase user ID
-t, --token <str> valid KBase token for the KBase user above
-w, --ws <int> numeric ID of the workspace to generate the static narrative for
-x, --skip-permissions-checks
if this argument is omitted, the script checks that user has admin
privileges for the workspace and that the workspace is readable by
all. This is useful for generating a static narrative for workspaces
that the user has access to but has not made public.
Invocation:
1. Be in StaticNarrative (the root dir of this module).
2. Make sure lib/ is on the PYTHONPATH
> export PYTHONPATH=$PYTHONPATH:$(pwd)/lib
3. Create a 'deploy.cfg' file with the appropriate values for `kbase_endpoint`
and `scratch`. Set the env var KB_DEPLOYMENT_CONFIG to the path to this file.
> export KB_DEPLOYMENT_CONFIG=$(pwd)/local_deploy.cfg
4. Run it with:
> python scripts/create_static_narrative.py -u <username> -t <token> -w <ws_id>
If the workspace object for the narrative is 12345/6/7, the generated static
narrative will appear in the directory
<scratch dir from deploy.cfg> / 12345 / 6 / 7
"""
import argparse
import sys
from configparser import ConfigParser
from os import environ
from StaticNarrative.config import generate_config
from lib.StaticNarrative.creator import StaticNarrativeCreator
DEPLOY = "KB_DEPLOYMENT_CONFIG"
SERVICE = "KB_SERVICE_NAME"
def get_config_file() -> str:
"""Get the path to the config file.
:return: path to the config file
:rtype: str
"""
return environ.get(DEPLOY, "./test/deploy.cfg")
def get_service_name() -> str:
"""Get the service name.
:return: service name
:rtype: str
"""
return environ.get(SERVICE, "StaticNarrative")
def get_config() -> None | dict[str, str]:
"""Retrieve the config from the config file.
:return: parsed config as a dictionary
:rtype: None | dict[str, str]
"""
if not get_config_file():
return None
config = ConfigParser()
config.read(get_config_file())
return dict(config.items(get_service_name() or "StaticNarrative"))
def parse_args(args: list[str]) -> argparse.Namespace:
"""Parse input arguments.
:param args: input argument list
:type args: list[str]
:raises ValueError: if one or more of the parameters are missing
:return: parsed arguments
:rtype: argparse.Namespace
"""
p = argparse.ArgumentParser()
p.add_argument("-u", "--user", dest="user_id", default=None, help="User ID")
p.add_argument("-t", "--token", dest="token", default=None, help="User auth token")
p.add_argument("-w", "--ws", dest="ws_id", default=None, help="Workspace ID with Narrative")
p.add_argument(
"-x",
"--skip-permissions-checks",
dest="skip_permissions_checks",
default=None,
help="Skip the workspace permissions checks; omit the argument to ensure that permission checks are run.",
)
parsed_args = p.parse_args(args)
errs = []
if not parsed_args.token:
errs.append("token - a valid Workspace admin auth token - is required!")
if not parsed_args.ws_id:
errs.append("ws_id - a valid Workspace id - is required!")
if errs:
err_str = "\n".join(["Could not create a static narrative:", *errs])
raise ValueError(err_str)
return parsed_args
def main(args: list[str]) -> None:
"""Run!
:param args: input args as a list
:type args: list[str]
"""
parsed_args = parse_args(args)
config = generate_config(get_config())
if config is None:
msg = f"No configuration data found. Please check {DEPLOY} env var points to a valid config file."
raise RuntimeError(msg)
sn = StaticNarrativeCreator(config, token=parsed_args.token)
sn.create_local_static_narrative(
parsed_args.ws_id, parsed_args.user_id, parsed_args.skip_permissions_checks
)
if __name__ == "__main__":
main(sys.argv[1:])
sys.exit(0)