qa4sm-api 0.1__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.
qa4sm_api/__init__.py ADDED
@@ -0,0 +1,16 @@
1
+ import sys
2
+
3
+ if sys.version_info[:2] >= (3, 8):
4
+ # TODO: Import directly (no need for conditional) when `python_requires = >= 3.8`
5
+ from importlib.metadata import PackageNotFoundError, version # pragma: no cover
6
+ else:
7
+ from importlib_metadata import PackageNotFoundError, version # pragma: no cover
8
+
9
+ try:
10
+ # Change here if project is renamed and does not equal the package name
11
+ dist_name = "qa4sm-api"
12
+ __version__ = version(dist_name)
13
+ except PackageNotFoundError: # pragma: no cover
14
+ __version__ = "unknown"
15
+ finally:
16
+ del version, PackageNotFoundError
qa4sm_api/cli.py ADDED
@@ -0,0 +1,220 @@
1
+ import os
2
+ import click
3
+ from importlib.metadata import version
4
+
5
+ from qa4sm_api.globals import (
6
+ DEFAULT_INSTANCE, KNOWN_INSTANCES, QA4SM_DOTRC_PATH,
7
+ _connect_with_credentials, _write_dotrc, _load_dotrc
8
+ )
9
+ from qa4sm_api.client_api import Connection
10
+
11
+
12
+ instance_option = click.option(
13
+ "--instance", "-i",
14
+ default=DEFAULT_INSTANCE,
15
+ show_default=True,
16
+ help=(
17
+ "QA4SM instance to send trigger the validation run on. "
18
+ f"Common values: {', '.join(KNOWN_INSTANCES)}"
19
+ ),
20
+ )
21
+
22
+ @click.group(short_help="QA4SM API wrapper commands.")
23
+ @click.version_option(version=version("qa4sm_api"), prog_name="QA4SM Client API")
24
+ def cli():
25
+ pass
26
+
27
+
28
+ @cli.group(short_help="API SETUP COMMANDS, see `qa4sm api --help`.")
29
+ def api():
30
+ """
31
+ API setup commands
32
+ """
33
+ pass
34
+
35
+
36
+ @cli.group(short_help="DOWNLOAD COMMANDS, see `qa4sm download --help`.")
37
+ def download():
38
+ """
39
+ Download data from QA4SM.
40
+ """
41
+ pass
42
+
43
+
44
+ def setup_api(instance=DEFAULT_INSTANCE):
45
+ """
46
+ Login to instance via username and password. Retrieve API token and store
47
+ it in ~/.qa4smapirc for future use.
48
+ """
49
+ click.echo(f"Logging in to: {instance}")
50
+
51
+ # Prompt for credentials (password input is hidden)
52
+ username = click.prompt("Username")
53
+ password = click.prompt("Password", hide_input=True)
54
+
55
+ # Retrieve token from the remote instance
56
+ try:
57
+ cred_access = _connect_with_credentials(instance, username, password)
58
+ except Exception as exc:
59
+ raise click.ClickException(f"Login failed: {exc}") from exc
60
+
61
+ if os.path.isfile(QA4SM_DOTRC_PATH):
62
+ access = _load_dotrc(QA4SM_DOTRC_PATH)
63
+ if instance not in access.keys():
64
+ access[instance] = dict()
65
+ access[instance]['token'] = cred_access[instance]['token']
66
+ access[instance]['username'] = username
67
+ action = "Added"
68
+ else:
69
+ access = dict()
70
+ access[instance] = {'token': cred_access[instance]['token'],
71
+ 'username': username}
72
+ action = "Updated"
73
+
74
+ _write_dotrc(access, QA4SM_DOTRC_PATH)
75
+
76
+ click.echo(
77
+ click.style(
78
+ f"✓ {action} token for [{instance}] in {QA4SM_DOTRC_PATH}",
79
+ fg="green",
80
+ )
81
+ )
82
+
83
+
84
+ @api.command("setup",
85
+ short_help="Add token to `.qa4smapirc` file.")
86
+ @instance_option
87
+ def cli_setup(instance: str) -> None:
88
+ """
89
+ Authenticate with a QA4SM instance using your username and password
90
+ to retrieve and store a token for the chosen instance )in ~/.qa4smapirc.
91
+ """
92
+ setup_api(instance)
93
+
94
+
95
+ @api.command("check",
96
+ short_help="Check whether you can access an instance with "
97
+ "the stored token.")
98
+ @instance_option
99
+ def cli_check(instance: str) -> None:
100
+ """
101
+ Authenticate with a QA4SM instance using your username and password
102
+ to retrieve and store a token for the chosen instance )in ~/.qa4smapirc.
103
+ """
104
+ qa4sm = Connection(instance, token='file')
105
+ user = qa4sm.session.user
106
+ if user is not None:
107
+ click.echo(f"Success, you can now send API commands to {instance}!")
108
+ else:
109
+ click.echo("Failed! Please make sure you have configured your "
110
+ ".qa4smapirc file correctly.")
111
+
112
+ @cli.command(
113
+ "validate",
114
+ short_help="Start a new validation run."
115
+ )
116
+ @click.argument(
117
+ "CONFIG_FILE",
118
+ type=click.File("r"),
119
+ )
120
+ @instance_option
121
+ def cli_validate(conf: str, instance: str) -> None:
122
+ """
123
+ Start a validation run on QA4SM using the settings from the passed
124
+ configuration.
125
+
126
+ \b
127
+ Arguments:
128
+ CONFIG_FILE Path to the validation configuration to send. Hint: use the
129
+ `qa4sm download config` command to download a template from an existing
130
+ run.
131
+ """
132
+ # The above docstring is written to be displayed when calling --help
133
+ qa4sm = Connection(instance, token='file')
134
+ response = qa4sm.run_config_validation(conf, override=None)
135
+
136
+ if not response.empty:
137
+ run_id = response.name
138
+ click.echo(f"Validation run started. See "
139
+ f"{qa4sm.session.base_url}ui/validation-result/{run_id}.")
140
+ else:
141
+ click.echo("No response received. No validation run started")
142
+
143
+
144
+ @download.command(
145
+ "config",
146
+ short_help="Download validation run configuration file."
147
+ )
148
+ @click.argument(
149
+ "RUN_ID",
150
+ type=click.STRING,
151
+ )
152
+ @click.option(
153
+ "--out_path", "-o",
154
+ default=None,
155
+ metavar="PATH",
156
+ help=(
157
+ "Path where the downloaded config file is stored. If not specified, "
158
+ "the current working directory is used. "
159
+ "[default: current directory]"
160
+ ),
161
+ )
162
+ @instance_option
163
+ def cli_download_conf(run_id: str, out_path: str, instance: str) -> None:
164
+ """
165
+ Download the cofiguration file of an existing online validation run.
166
+
167
+ \b
168
+ Arguments:
169
+ RUN_ID Validation run ID (UUID) to download. The ID is indicated, "
170
+ "e.g., in the validation run URL."
171
+ """
172
+ # The above docstring is written to be displayed when calling --help
173
+ out_path = out_path or os.getcwd()
174
+ qa4sm = Connection(instance, token='file')
175
+ conf = qa4sm.download_configuration(run_id, out_path)
176
+
177
+ if conf.empty:
178
+ print(f"Could not download configuration for run {run_id} from "
179
+ f"{instance}. Make sure the run exists.")
180
+ else:
181
+ print(f"Configuration stored at "
182
+ f"{os.path.join(out_path, f"{run_id}.json")}")
183
+
184
+
185
+ @download.command(
186
+ "results",
187
+ short_help="Download validation run results file."
188
+ )
189
+ @click.argument(
190
+ "RUN_ID",
191
+ type=click.STRING,
192
+ )
193
+ @click.option(
194
+ "--out_path", "-o",
195
+ default=None,
196
+ metavar="PATH",
197
+ help=(
198
+ "Path where the downloaded results are stored. If not specified, "
199
+ "the current working directory is used. "
200
+ "[default: current directory]"
201
+ ),
202
+ )
203
+ @instance_option
204
+ def cli_download_results(run_id: str, out_path: str, instance: str) -> None:
205
+ """
206
+ Download the configuration file of an existing online validation run.
207
+
208
+ \b
209
+ Arguments:
210
+ RUN_ID Validation run ID (UUID) to download. The ID is indicated
211
+ in the validation run URL.
212
+ """
213
+ # The above docstring is written to be displayed when calling --help
214
+ out_path = out_path or os.getcwd()
215
+ qa4sm = Connection(instance, token='file')
216
+ qa4sm.download_results(run_id, out_path, force_download=True)
217
+
218
+
219
+ if __name__ == "__main__":
220
+ setup_api()