cmem-cmemc 25.5.0rc1__py3-none-any.whl → 25.6.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.
- cmem_cmemc/command_group.py +32 -31
- cmem_cmemc/commands/acl.py +15 -6
- cmem_cmemc/commands/dataset.py +1 -54
- cmem_cmemc/commands/file.py +465 -0
- cmem_cmemc/commands/graph_insights.py +103 -35
- cmem_cmemc/commands/project.py +3 -1
- cmem_cmemc/commands/python.py +1 -1
- cmem_cmemc/commands/vocabulary.py +13 -4
- cmem_cmemc/completion.py +22 -5
- cmem_cmemc/context.py +2 -2
- cmem_cmemc/manual_helper/graph.py +2 -2
- cmem_cmemc/manual_helper/multi_page.py +5 -6
- cmem_cmemc/placeholder.py +2 -2
- cmem_cmemc/string_processor.py +12 -0
- cmem_cmemc/utils.py +1 -1
- {cmem_cmemc-25.5.0rc1.dist-info → cmem_cmemc-25.6.0.dist-info}/METADATA +2 -1
- {cmem_cmemc-25.5.0rc1.dist-info → cmem_cmemc-25.6.0.dist-info}/RECORD +20 -20
- cmem_cmemc/commands/resource.py +0 -220
- {cmem_cmemc-25.5.0rc1.dist-info → cmem_cmemc-25.6.0.dist-info}/WHEEL +0 -0
- {cmem_cmemc-25.5.0rc1.dist-info → cmem_cmemc-25.6.0.dist-info}/entry_points.txt +0 -0
- {cmem_cmemc-25.5.0rc1.dist-info → cmem_cmemc-25.6.0.dist-info}/licenses/LICENSE +0 -0
cmem_cmemc/command_group.py
CHANGED
|
@@ -24,49 +24,50 @@ class CmemcGroup(HelpColorsGroup, DYMGroup):
|
|
|
24
24
|
kwargs.setdefault(
|
|
25
25
|
"help_options_custom_colors",
|
|
26
26
|
{
|
|
27
|
+
"acl": self.color_for_command_groups,
|
|
28
|
+
"admin": self.color_for_command_groups,
|
|
27
29
|
"bootstrap": self.color_for_writing_commands,
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"upload": self.color_for_writing_commands,
|
|
33
|
-
"import": self.color_for_writing_commands,
|
|
30
|
+
"cache": self.color_for_command_groups,
|
|
31
|
+
"cancel": self.color_for_writing_commands,
|
|
32
|
+
"client": self.color_for_command_groups,
|
|
33
|
+
"config": self.color_for_command_groups,
|
|
34
34
|
"create": self.color_for_writing_commands,
|
|
35
|
-
"
|
|
35
|
+
"dataset": self.color_for_command_groups,
|
|
36
|
+
"delete": self.color_for_writing_commands,
|
|
36
37
|
"disable": self.color_for_writing_commands,
|
|
38
|
+
"enable": self.color_for_writing_commands,
|
|
39
|
+
"eval": self.color_for_writing_commands,
|
|
37
40
|
"execute": self.color_for_writing_commands,
|
|
38
|
-
"
|
|
39
|
-
"
|
|
41
|
+
"file": self.color_for_command_groups,
|
|
42
|
+
"graph": self.color_for_command_groups,
|
|
43
|
+
"import": self.color_for_writing_commands,
|
|
44
|
+
"imports": self.color_for_command_groups,
|
|
45
|
+
"insights": self.color_for_command_groups,
|
|
40
46
|
"install": self.color_for_writing_commands,
|
|
41
|
-
"
|
|
42
|
-
"reload": self.color_for_writing_commands,
|
|
43
|
-
"update": self.color_for_writing_commands,
|
|
44
|
-
"eval": self.color_for_writing_commands,
|
|
45
|
-
"cancel": self.color_for_writing_commands,
|
|
46
|
-
"admin": self.color_for_command_groups,
|
|
47
|
-
"user": self.color_for_command_groups,
|
|
48
|
-
"store": self.color_for_command_groups,
|
|
47
|
+
"io": self.color_for_writing_commands,
|
|
49
48
|
"metrics": self.color_for_command_groups,
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
49
|
+
"migrate": self.color_for_writing_commands,
|
|
50
|
+
"migrations": self.color_for_command_groups,
|
|
51
|
+
"password": self.color_for_writing_commands,
|
|
53
52
|
"project": self.color_for_command_groups,
|
|
53
|
+
"python": self.color_for_command_groups,
|
|
54
54
|
"query": self.color_for_command_groups,
|
|
55
|
+
"reload": self.color_for_writing_commands,
|
|
56
|
+
"replay": self.color_for_writing_commands,
|
|
57
|
+
"resource": self.color_for_command_groups,
|
|
55
58
|
"scheduler": self.color_for_command_groups,
|
|
59
|
+
"secret": self.color_for_writing_commands,
|
|
60
|
+
"showcase": self.color_for_writing_commands,
|
|
61
|
+
"store": self.color_for_command_groups,
|
|
62
|
+
"uninstall": self.color_for_writing_commands,
|
|
63
|
+
"update": self.color_for_writing_commands,
|
|
64
|
+
"upload": self.color_for_writing_commands,
|
|
65
|
+
"user": self.color_for_command_groups,
|
|
66
|
+
"validation": self.color_for_command_groups,
|
|
67
|
+
"variable": self.color_for_command_groups,
|
|
56
68
|
"vocabulary": self.color_for_command_groups,
|
|
57
69
|
"workflow": self.color_for_command_groups,
|
|
58
70
|
"workspace": self.color_for_command_groups,
|
|
59
|
-
"python": self.color_for_command_groups,
|
|
60
|
-
"cache": self.color_for_command_groups,
|
|
61
|
-
"resource": self.color_for_command_groups,
|
|
62
|
-
"acl": self.color_for_command_groups,
|
|
63
|
-
"client": self.color_for_command_groups,
|
|
64
|
-
"variable": self.color_for_command_groups,
|
|
65
|
-
"validation": self.color_for_command_groups,
|
|
66
|
-
"migrate": self.color_for_writing_commands,
|
|
67
|
-
"migrations": self.color_for_command_groups,
|
|
68
|
-
"imports": self.color_for_command_groups,
|
|
69
|
-
"insights": self.color_for_command_groups,
|
|
70
71
|
},
|
|
71
72
|
)
|
|
72
73
|
super().__init__(*args, **kwargs)
|
cmem_cmemc/commands/acl.py
CHANGED
|
@@ -51,6 +51,10 @@ HELP_TEXTS = {
|
|
|
51
51
|
"end of the pattern or the wildcard alone."
|
|
52
52
|
),
|
|
53
53
|
"query": "Dynamic access condition query (file or the query catalog IRI).",
|
|
54
|
+
"replace": (
|
|
55
|
+
"Replace (overwrite) existing access condition, if present. "
|
|
56
|
+
"Can be used only in combination with '--id'."
|
|
57
|
+
),
|
|
54
58
|
}
|
|
55
59
|
|
|
56
60
|
WARNING_UNKNOWN_USER = "Unknown User or no access to get user info."
|
|
@@ -255,6 +259,7 @@ def inspect_command(app: ApplicationContext, access_condition_id: str, raw: bool
|
|
|
255
259
|
type=click.STRING,
|
|
256
260
|
help=HELP_TEXTS["description"],
|
|
257
261
|
)
|
|
262
|
+
@click.option("--replace", is_flag=True, help=HELP_TEXTS["replace"])
|
|
258
263
|
@click.pass_obj
|
|
259
264
|
# pylint: disable-msg=too-many-arguments
|
|
260
265
|
def create_command( # noqa: PLR0913
|
|
@@ -271,6 +276,7 @@ def create_command( # noqa: PLR0913
|
|
|
271
276
|
write_graph_patterns: tuple[str],
|
|
272
277
|
action_patterns: tuple[str],
|
|
273
278
|
query: str,
|
|
279
|
+
replace: bool,
|
|
274
280
|
) -> None:
|
|
275
281
|
"""Create an access condition.
|
|
276
282
|
|
|
@@ -293,6 +299,9 @@ def create_command( # noqa: PLR0913
|
|
|
293
299
|
|
|
294
300
|
Example: cmemc admin acl create --group local-users --write-graph https://example.org/
|
|
295
301
|
"""
|
|
302
|
+
if replace and not id_:
|
|
303
|
+
raise click.UsageError("To replace an access condition, you must specify an ID.")
|
|
304
|
+
|
|
296
305
|
if (
|
|
297
306
|
not read_graphs
|
|
298
307
|
and not write_graphs
|
|
@@ -312,7 +321,7 @@ def create_command( # noqa: PLR0913
|
|
|
312
321
|
query_str = get_query_text(query, {"user", "group", "readGraph", "writeGraph"})
|
|
313
322
|
|
|
314
323
|
if not user and not groups and not query:
|
|
315
|
-
app.echo_warning("Access conditions without a user or group assignment
|
|
324
|
+
app.echo_warning("Access conditions without a user or group assignment affects ALL users.")
|
|
316
325
|
|
|
317
326
|
if not name:
|
|
318
327
|
name = generate_acl_name(user=user, groups=groups, query=query)
|
|
@@ -320,11 +329,11 @@ def create_command( # noqa: PLR0913
|
|
|
320
329
|
if not description:
|
|
321
330
|
description = "This access condition was created with cmemc."
|
|
322
331
|
|
|
323
|
-
|
|
324
|
-
f"
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
332
|
+
if replace and NS_ACL + id_ in [_["iri"] for _ in fetch_all_acls()]:
|
|
333
|
+
app.echo_info(f"Replacing access condition '{id_}' ... ", nl=False)
|
|
334
|
+
delete_access_condition(iri=NS_ACL + id_)
|
|
335
|
+
else:
|
|
336
|
+
app.echo_info(f"Creating access condition '{name}' ... ", nl=False)
|
|
328
337
|
create_access_condition(
|
|
329
338
|
name=name,
|
|
330
339
|
static_id=id_,
|
cmem_cmemc/commands/dataset.py
CHANGED
|
@@ -16,16 +16,14 @@ from cmem.cmempy.workspace.projects.datasets.dataset import (
|
|
|
16
16
|
update_dataset,
|
|
17
17
|
)
|
|
18
18
|
from cmem.cmempy.workspace.projects.resources.resource import (
|
|
19
|
-
create_resource,
|
|
20
19
|
get_resource_response,
|
|
21
|
-
resource_exist,
|
|
22
20
|
)
|
|
23
21
|
from cmem.cmempy.workspace.search import list_items
|
|
24
22
|
|
|
25
23
|
from cmem_cmemc import completion
|
|
26
24
|
from cmem_cmemc.command import CmemcCommand
|
|
27
25
|
from cmem_cmemc.command_group import CmemcGroup
|
|
28
|
-
from cmem_cmemc.commands.
|
|
26
|
+
from cmem_cmemc.commands.file import _upload_file_resource, resource
|
|
29
27
|
from cmem_cmemc.completion import get_dataset_file_mapping
|
|
30
28
|
from cmem_cmemc.context import ApplicationContext
|
|
31
29
|
from cmem_cmemc.exceptions import CmemcError
|
|
@@ -147,57 +145,6 @@ def _post_file_resource(
|
|
|
147
145
|
app.echo_success("done")
|
|
148
146
|
|
|
149
147
|
|
|
150
|
-
def _upload_file_resource(
|
|
151
|
-
app: ApplicationContext,
|
|
152
|
-
project_id: str,
|
|
153
|
-
local_file_name: str,
|
|
154
|
-
remote_file_name: str,
|
|
155
|
-
replace: bool,
|
|
156
|
-
) -> None:
|
|
157
|
-
"""Upload a local file as a dataset resource to a project.
|
|
158
|
-
|
|
159
|
-
Args:
|
|
160
|
-
----
|
|
161
|
-
app: the click cli app context.
|
|
162
|
-
project_id: The project ID in the workspace.
|
|
163
|
-
local_file_name: The path to the local file name
|
|
164
|
-
remote_file_name: The remote file name
|
|
165
|
-
replace: Replace resource if needed.
|
|
166
|
-
|
|
167
|
-
Raises:
|
|
168
|
-
------
|
|
169
|
-
ValueError: if resource exists and no replace
|
|
170
|
-
|
|
171
|
-
"""
|
|
172
|
-
exist = resource_exist(project_name=project_id, resource_name=remote_file_name)
|
|
173
|
-
if exist and not replace:
|
|
174
|
-
raise ClickException(
|
|
175
|
-
f"A file resource with the name '{remote_file_name}' already "
|
|
176
|
-
"exists in this project. \n"
|
|
177
|
-
"Please rename the file or use the '--replace' "
|
|
178
|
-
"parameter in order to overwrite the remote file."
|
|
179
|
-
)
|
|
180
|
-
if exist:
|
|
181
|
-
app.echo_info(
|
|
182
|
-
f"Replace content of {remote_file_name} with content from "
|
|
183
|
-
f"{local_file_name} in project {project_id} ... ",
|
|
184
|
-
nl=False,
|
|
185
|
-
)
|
|
186
|
-
else:
|
|
187
|
-
app.echo_info(
|
|
188
|
-
f"Upload {local_file_name} as a file resource "
|
|
189
|
-
f"{remote_file_name} to project {project_id} ... ",
|
|
190
|
-
nl=False,
|
|
191
|
-
)
|
|
192
|
-
create_resource(
|
|
193
|
-
project_name=project_id,
|
|
194
|
-
resource_name=remote_file_name,
|
|
195
|
-
file_resource=ClickSmartPath.open(local_file_name),
|
|
196
|
-
replace=replace,
|
|
197
|
-
)
|
|
198
|
-
app.echo_success("done")
|
|
199
|
-
|
|
200
|
-
|
|
201
148
|
def _get_metadata_out_of_parameter(parameter_dict: dict) -> dict:
|
|
202
149
|
"""Extract metadata keys out of the parameter dict.
|
|
203
150
|
|
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
"""Build project file commands for cmemc."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
from click import ClickException, Context, UsageError
|
|
7
|
+
from cmem.cmempy.config import get_cmem_base_uri
|
|
8
|
+
from cmem.cmempy.workspace.projects.resources import get_all_resources
|
|
9
|
+
from cmem.cmempy.workspace.projects.resources.resource import (
|
|
10
|
+
create_resource,
|
|
11
|
+
delete_resource,
|
|
12
|
+
get_resource_metadata,
|
|
13
|
+
get_resource_response,
|
|
14
|
+
get_resource_usage_data,
|
|
15
|
+
resource_exist,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from cmem_cmemc import completion
|
|
19
|
+
from cmem_cmemc.command import CmemcCommand
|
|
20
|
+
from cmem_cmemc.command_group import CmemcGroup
|
|
21
|
+
from cmem_cmemc.context import ApplicationContext
|
|
22
|
+
from cmem_cmemc.exceptions import CmemcError
|
|
23
|
+
from cmem_cmemc.parameter_types.path import ClickSmartPath
|
|
24
|
+
from cmem_cmemc.smart_path import SmartPath as Path
|
|
25
|
+
from cmem_cmemc.string_processor import FileSize, TimeAgo
|
|
26
|
+
from cmem_cmemc.utils import check_or_select_project, split_task_id, struct_to_table
|
|
27
|
+
|
|
28
|
+
RESOURCE_FILTER_TYPES = ["project", "regex"]
|
|
29
|
+
RESOURCE_FILTER_TYPES_HIDDEN = ["ids"]
|
|
30
|
+
RESOURCE_FILTER_TEXT = (
|
|
31
|
+
"Filter file resources based on metadata. "
|
|
32
|
+
f"First parameter CHOICE can be one of {RESOURCE_FILTER_TYPES!s}"
|
|
33
|
+
". The second parameter is based on CHOICE, e.g. a project "
|
|
34
|
+
"ID or a regular expression string."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _upload_file_resource(
|
|
39
|
+
app: ApplicationContext,
|
|
40
|
+
project_id: str,
|
|
41
|
+
local_file_name: str,
|
|
42
|
+
remote_file_name: str,
|
|
43
|
+
replace: bool,
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Upload a local file as a dataset resource to a project.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
----
|
|
49
|
+
app: the click cli app context.
|
|
50
|
+
project_id: The project ID in the workspace.
|
|
51
|
+
local_file_name: The path to the local file name
|
|
52
|
+
remote_file_name: The remote file name
|
|
53
|
+
replace: Replace resource if needed.
|
|
54
|
+
|
|
55
|
+
Raises:
|
|
56
|
+
------
|
|
57
|
+
ValueError: if resource exists and no replace
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
exist = resource_exist(project_name=project_id, resource_name=remote_file_name)
|
|
61
|
+
if exist and not replace:
|
|
62
|
+
raise CmemcError(
|
|
63
|
+
app,
|
|
64
|
+
f"A file resource with the name '{remote_file_name}' already "
|
|
65
|
+
"exists in this project. \n"
|
|
66
|
+
"Please rename the file or use the '--replace' "
|
|
67
|
+
"parameter in order to overwrite the remote file.",
|
|
68
|
+
)
|
|
69
|
+
if exist:
|
|
70
|
+
app.echo_info(
|
|
71
|
+
f"Replace content of {remote_file_name} with content from "
|
|
72
|
+
f"{local_file_name} in project {project_id} ... ",
|
|
73
|
+
nl=False,
|
|
74
|
+
)
|
|
75
|
+
else:
|
|
76
|
+
app.echo_info(
|
|
77
|
+
f"Upload {local_file_name} as a file resource "
|
|
78
|
+
f"{remote_file_name} to project {project_id} ... ",
|
|
79
|
+
nl=False,
|
|
80
|
+
)
|
|
81
|
+
create_resource(
|
|
82
|
+
project_name=project_id,
|
|
83
|
+
resource_name=remote_file_name,
|
|
84
|
+
file_resource=ClickSmartPath.open(local_file_name),
|
|
85
|
+
replace=replace,
|
|
86
|
+
)
|
|
87
|
+
app.echo_success("done")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _get_resources_filtered(
|
|
91
|
+
resources: list[dict], filter_name: str, filter_value: str | tuple[str, ...]
|
|
92
|
+
) -> list[dict]:
|
|
93
|
+
"""Get file resources but filtered according to name and value."""
|
|
94
|
+
# check for correct filter names (filter ids is used internally only)
|
|
95
|
+
if filter_name not in RESOURCE_FILTER_TYPES + RESOURCE_FILTER_TYPES_HIDDEN:
|
|
96
|
+
raise UsageError(
|
|
97
|
+
f"{filter_name} is an unknown filter name. " f"Use one of {RESOURCE_FILTER_TYPES}."
|
|
98
|
+
)
|
|
99
|
+
# filter by ID list
|
|
100
|
+
if filter_name == "ids":
|
|
101
|
+
return [_ for _ in resources if _["id"] in filter_value]
|
|
102
|
+
# filter by project
|
|
103
|
+
if filter_name == "project":
|
|
104
|
+
return [_ for _ in resources if _["project"] == str(filter_value)]
|
|
105
|
+
# filter by regex
|
|
106
|
+
if filter_name == "regex":
|
|
107
|
+
return [_ for _ in resources if re.search(str(filter_value), _["name"])]
|
|
108
|
+
# return unfiltered list
|
|
109
|
+
return resources
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@click.command(cls=CmemcCommand, name="list")
|
|
113
|
+
@click.option("--raw", is_flag=True, help="Outputs raw JSON.")
|
|
114
|
+
@click.option(
|
|
115
|
+
"--id-only",
|
|
116
|
+
is_flag=True,
|
|
117
|
+
help="Lists only resource IDs and no other metadata. "
|
|
118
|
+
"This is useful for piping the IDs into other commands.",
|
|
119
|
+
)
|
|
120
|
+
@click.option(
|
|
121
|
+
"--filter",
|
|
122
|
+
"filters_",
|
|
123
|
+
multiple=True,
|
|
124
|
+
type=(str, str),
|
|
125
|
+
shell_complete=completion.resource_list_filter,
|
|
126
|
+
help=RESOURCE_FILTER_TEXT,
|
|
127
|
+
)
|
|
128
|
+
@click.pass_obj
|
|
129
|
+
def list_command(
|
|
130
|
+
app: ApplicationContext, raw: bool, id_only: bool, filters_: tuple[tuple[str, str], ...]
|
|
131
|
+
) -> None:
|
|
132
|
+
"""List available file resources.
|
|
133
|
+
|
|
134
|
+
Outputs a table or a list of file resources.
|
|
135
|
+
"""
|
|
136
|
+
resources = get_all_resources()
|
|
137
|
+
for _ in filters_:
|
|
138
|
+
filter_name, filter_value = _
|
|
139
|
+
resources = _get_resources_filtered(resources, filter_name, filter_value)
|
|
140
|
+
if raw:
|
|
141
|
+
app.echo_info_json(resources)
|
|
142
|
+
return
|
|
143
|
+
if id_only:
|
|
144
|
+
for _ in sorted(_["id"] for _ in resources):
|
|
145
|
+
app.echo_result(_)
|
|
146
|
+
return
|
|
147
|
+
# output a user table
|
|
148
|
+
table = []
|
|
149
|
+
headers = ["ID", "Modified", "Size"]
|
|
150
|
+
for _ in resources:
|
|
151
|
+
row = [
|
|
152
|
+
_["id"],
|
|
153
|
+
_["modified"],
|
|
154
|
+
_["size"],
|
|
155
|
+
]
|
|
156
|
+
table.append(row)
|
|
157
|
+
|
|
158
|
+
caption = f"{len(table)} files of {get_cmem_base_uri()}"
|
|
159
|
+
empty_note = "No resources found."
|
|
160
|
+
if len(filters_) > 0:
|
|
161
|
+
caption += " (filtered)"
|
|
162
|
+
empty_note = "No resources found for these filters."
|
|
163
|
+
|
|
164
|
+
app.echo_info_table(
|
|
165
|
+
table,
|
|
166
|
+
headers=headers,
|
|
167
|
+
sort_column=0,
|
|
168
|
+
cell_processing={1: TimeAgo(), 2: FileSize()},
|
|
169
|
+
caption=caption,
|
|
170
|
+
empty_table_message=f"{empty_note} "
|
|
171
|
+
"Use the `dataset create` command to create a new file-based dataset, or "
|
|
172
|
+
"the `project file upload` command to create only a file resource.",
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@click.command(cls=CmemcCommand, name="delete")
|
|
177
|
+
@click.argument("resource_ids", nargs=-1, type=click.STRING, shell_complete=completion.resource_ids)
|
|
178
|
+
@click.option("--force", is_flag=True, help="Delete resource even if in use by a task.")
|
|
179
|
+
@click.option(
|
|
180
|
+
"-a",
|
|
181
|
+
"--all",
|
|
182
|
+
"all_",
|
|
183
|
+
is_flag=True,
|
|
184
|
+
help="Delete all resources. " "This is a dangerous option, so use it with care.",
|
|
185
|
+
)
|
|
186
|
+
@click.option(
|
|
187
|
+
"--filter",
|
|
188
|
+
"filters_",
|
|
189
|
+
multiple=True,
|
|
190
|
+
type=(str, str),
|
|
191
|
+
shell_complete=completion.resource_list_filter,
|
|
192
|
+
help=RESOURCE_FILTER_TEXT,
|
|
193
|
+
)
|
|
194
|
+
@click.pass_obj
|
|
195
|
+
def delete_command(
|
|
196
|
+
app: ApplicationContext,
|
|
197
|
+
resource_ids: tuple[str, ...],
|
|
198
|
+
force: bool,
|
|
199
|
+
all_: bool,
|
|
200
|
+
filters_: tuple[tuple[str, str], ...],
|
|
201
|
+
) -> None:
|
|
202
|
+
"""Delete file resources.
|
|
203
|
+
|
|
204
|
+
There are three selection mechanisms: with specific IDs - only those
|
|
205
|
+
specified resources will be deleted; by using --filter - resources based
|
|
206
|
+
on the filter type and value will be deleted; by using --all, which will
|
|
207
|
+
delete all resources.
|
|
208
|
+
"""
|
|
209
|
+
if resource_ids == () and not all_ and filters_ == ():
|
|
210
|
+
raise UsageError(
|
|
211
|
+
"Either specify at least one resource ID or use the --all or "
|
|
212
|
+
"--filter options to specify resources for deletion."
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
resources = get_all_resources()
|
|
216
|
+
if len(resource_ids) > 0:
|
|
217
|
+
for resource_id in resource_ids:
|
|
218
|
+
if resource_id not in [_["id"] for _ in resources]:
|
|
219
|
+
raise ClickException(f"Resource {resource_id} not available.")
|
|
220
|
+
# "filter" by id
|
|
221
|
+
resources = _get_resources_filtered(resources, "ids", resource_ids)
|
|
222
|
+
for _ in filters_:
|
|
223
|
+
resources = _get_resources_filtered(resources, _[0], _[1])
|
|
224
|
+
|
|
225
|
+
# avoid double removal as well as sort IDs
|
|
226
|
+
processed_ids = sorted({_["id"] for _ in resources}, key=lambda v: v.lower())
|
|
227
|
+
count = len(processed_ids)
|
|
228
|
+
for current, resource_id in enumerate(processed_ids, start=1):
|
|
229
|
+
current_string = str(current).zfill(len(str(count)))
|
|
230
|
+
app.echo_info(f"Delete resource {current_string}/{count}: {resource_id} ... ", nl=False)
|
|
231
|
+
project_id, resource_local_id = split_task_id(resource_id)
|
|
232
|
+
usage = get_resource_usage_data(project_id, resource_local_id)
|
|
233
|
+
if len(usage) > 0:
|
|
234
|
+
app.echo_error(f"in use by {len(usage)} task(s)", nl=False)
|
|
235
|
+
if force:
|
|
236
|
+
app.echo_info(" ... ", nl=False)
|
|
237
|
+
else:
|
|
238
|
+
app.echo_info("")
|
|
239
|
+
continue
|
|
240
|
+
delete_resource(project_name=project_id, resource_name=resource_local_id)
|
|
241
|
+
app.echo_success("deleted")
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@click.command(cls=CmemcCommand, name="download")
|
|
245
|
+
@click.argument("resource_ids", nargs=-1, type=click.STRING, shell_complete=completion.resource_ids)
|
|
246
|
+
@click.option(
|
|
247
|
+
"--output-dir",
|
|
248
|
+
default=".",
|
|
249
|
+
show_default=True,
|
|
250
|
+
type=ClickSmartPath(writable=True, file_okay=False),
|
|
251
|
+
help="The directory where the downloaded files will be saved. "
|
|
252
|
+
"If this directory does not exist, it will be created.",
|
|
253
|
+
)
|
|
254
|
+
@click.option(
|
|
255
|
+
"--replace",
|
|
256
|
+
is_flag=True,
|
|
257
|
+
help="Replace existing files. This is a dangerous option, " "so use it with care!",
|
|
258
|
+
)
|
|
259
|
+
@click.pass_obj
|
|
260
|
+
def download_command(
|
|
261
|
+
app: ApplicationContext, resource_ids: tuple[str], output_dir: str, replace: bool
|
|
262
|
+
) -> None:
|
|
263
|
+
"""Download file resources to the local file system.
|
|
264
|
+
|
|
265
|
+
This command downloads one or more file resources from projects to your local
|
|
266
|
+
file system. Files are saved with their resource names in the output directory.
|
|
267
|
+
|
|
268
|
+
Resources are identified by their IDs in the format PROJECT_ID:RESOURCE_NAME.
|
|
269
|
+
|
|
270
|
+
Example: cmemc project file download my-proj:my-file.csv
|
|
271
|
+
|
|
272
|
+
Example: cmemc project file download my-proj:file1.csv my-proj:file2.csv --output-dir /tmp
|
|
273
|
+
"""
|
|
274
|
+
import os
|
|
275
|
+
|
|
276
|
+
if not resource_ids:
|
|
277
|
+
raise UsageError(
|
|
278
|
+
"At least one resource ID must be specified. "
|
|
279
|
+
"Use 'project file list' to see available resources."
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
count = len(resource_ids)
|
|
283
|
+
for current, resource_id in enumerate(resource_ids, start=1):
|
|
284
|
+
try:
|
|
285
|
+
project_id, resource_name = split_task_id(resource_id)
|
|
286
|
+
except ValueError:
|
|
287
|
+
app.echo_error(f"Invalid resource ID format: {resource_id}")
|
|
288
|
+
continue
|
|
289
|
+
|
|
290
|
+
# Build output path
|
|
291
|
+
output_path = os.path.normpath(str(Path(output_dir) / resource_name))
|
|
292
|
+
|
|
293
|
+
app.echo_info(
|
|
294
|
+
f"Download resource {current}/{count}: {resource_id} to {output_path} ... ",
|
|
295
|
+
nl=False,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
if Path(output_path).exists() and replace is not True:
|
|
299
|
+
app.echo_error("target file exists")
|
|
300
|
+
continue
|
|
301
|
+
|
|
302
|
+
# Create parent directory if it doesn't exist
|
|
303
|
+
Path(output_path).parent.mkdir(exist_ok=True, parents=True)
|
|
304
|
+
|
|
305
|
+
try:
|
|
306
|
+
with (
|
|
307
|
+
get_resource_response(project_id, resource_name) as response,
|
|
308
|
+
click.open_file(output_path, "wb") as resource_file,
|
|
309
|
+
):
|
|
310
|
+
for chunk in response.iter_content(chunk_size=8192):
|
|
311
|
+
resource_file.write(chunk)
|
|
312
|
+
app.echo_success("done")
|
|
313
|
+
except (OSError, ClickException) as error:
|
|
314
|
+
app.echo_error(f"failed: {error!s}")
|
|
315
|
+
continue
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
@click.command(cls=CmemcCommand, name="upload")
|
|
319
|
+
@click.argument(
|
|
320
|
+
"input_path",
|
|
321
|
+
required=True,
|
|
322
|
+
type=ClickSmartPath(
|
|
323
|
+
allow_dash=False, dir_okay=False, readable=True, exists=True, remote_okay=True
|
|
324
|
+
),
|
|
325
|
+
)
|
|
326
|
+
@click.option(
|
|
327
|
+
"--project",
|
|
328
|
+
"project_id",
|
|
329
|
+
type=click.STRING,
|
|
330
|
+
shell_complete=completion.project_ids,
|
|
331
|
+
help="The project where you want to upload the file. If there is "
|
|
332
|
+
"only one project in the workspace, this option can be omitted.",
|
|
333
|
+
)
|
|
334
|
+
@click.option(
|
|
335
|
+
"--path",
|
|
336
|
+
"remote_name",
|
|
337
|
+
type=click.STRING,
|
|
338
|
+
shell_complete=completion.resource_paths,
|
|
339
|
+
help="The path/name of the file resource in the project (e.g., 'data/file.csv'). "
|
|
340
|
+
"If not specified, the local file name will be used.",
|
|
341
|
+
)
|
|
342
|
+
@click.option(
|
|
343
|
+
"--replace",
|
|
344
|
+
is_flag=True,
|
|
345
|
+
help="Replace existing file resource. This is a dangerous option, " "so use it with care!",
|
|
346
|
+
)
|
|
347
|
+
@click.pass_obj
|
|
348
|
+
def upload_command(
|
|
349
|
+
app: ApplicationContext, input_path: str, project_id: str, remote_name: str, replace: bool
|
|
350
|
+
) -> None:
|
|
351
|
+
"""Upload a file to a project.
|
|
352
|
+
|
|
353
|
+
This command uploads a file to a project as a file resource.
|
|
354
|
+
|
|
355
|
+
Note: If you want to create a dataset from your file, the `dataset create`
|
|
356
|
+
command is maybe the better option.
|
|
357
|
+
|
|
358
|
+
Example: cmemc project file upload my-file.csv --project my-project
|
|
359
|
+
"""
|
|
360
|
+
project_id = check_or_select_project(app, project_id)
|
|
361
|
+
local_file_name = Path(input_path).name
|
|
362
|
+
|
|
363
|
+
if remote_name and remote_name.endswith("/"):
|
|
364
|
+
app.echo_warning(
|
|
365
|
+
f"Remote path ends with a slash, so the local file name is appended: {local_file_name}."
|
|
366
|
+
)
|
|
367
|
+
remote_name = remote_name + local_file_name
|
|
368
|
+
|
|
369
|
+
# Use local filename if remote name not specified
|
|
370
|
+
if not remote_name:
|
|
371
|
+
remote_name = local_file_name
|
|
372
|
+
|
|
373
|
+
_upload_file_resource(
|
|
374
|
+
app=app,
|
|
375
|
+
remote_file_name=remote_name,
|
|
376
|
+
project_id=project_id,
|
|
377
|
+
local_file_name=input_path,
|
|
378
|
+
replace=replace,
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
@click.command(cls=CmemcCommand, name="inspect")
|
|
383
|
+
@click.argument("resource_id", type=click.STRING, shell_complete=completion.resource_ids)
|
|
384
|
+
@click.option("--raw", is_flag=True, help="Outputs raw JSON.")
|
|
385
|
+
@click.pass_obj
|
|
386
|
+
def inspect_command(app: ApplicationContext, resource_id: str, raw: bool) -> None:
|
|
387
|
+
"""Display all metadata of a file resource."""
|
|
388
|
+
project_id, resource_id = split_task_id(resource_id)
|
|
389
|
+
resource_data = get_resource_metadata(project_id, resource_id)
|
|
390
|
+
if raw:
|
|
391
|
+
app.echo_info_json(resource_data)
|
|
392
|
+
else:
|
|
393
|
+
table = struct_to_table(resource_data)
|
|
394
|
+
app.echo_info_table(table, headers=["Key", "Value"], sort_column=0)
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
@click.command(cls=CmemcCommand, name="usage")
|
|
398
|
+
@click.argument("resource_id", type=click.STRING, shell_complete=completion.resource_ids)
|
|
399
|
+
@click.option("--raw", is_flag=True, help="Outputs raw JSON.")
|
|
400
|
+
@click.pass_obj
|
|
401
|
+
def usage_command(app: ApplicationContext, resource_id: str, raw: bool) -> None:
|
|
402
|
+
"""Display all usage data of a file resource."""
|
|
403
|
+
project_id, resource_id = split_task_id(resource_id)
|
|
404
|
+
usage = get_resource_usage_data(project_id, resource_id)
|
|
405
|
+
if raw:
|
|
406
|
+
app.echo_info_json(usage)
|
|
407
|
+
return
|
|
408
|
+
# output a user table
|
|
409
|
+
table = []
|
|
410
|
+
headers = ["Task ID", "Type", "Label"]
|
|
411
|
+
for _ in usage:
|
|
412
|
+
row = [project_id + ":" + _["id"], _["taskType"], _["label"]]
|
|
413
|
+
table.append(row)
|
|
414
|
+
app.echo_info_table(
|
|
415
|
+
table,
|
|
416
|
+
empty_table_message=f"The file resource '{resource_id}' is not used in "
|
|
417
|
+
f"any task in project '{project_id}'.",
|
|
418
|
+
headers=headers,
|
|
419
|
+
sort_column=2,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
@click.group(
|
|
424
|
+
cls=CmemcGroup,
|
|
425
|
+
hidden=True,
|
|
426
|
+
)
|
|
427
|
+
@click.pass_context
|
|
428
|
+
def resource(ctx: Context) -> None:
|
|
429
|
+
"""List, inspect or delete dataset file resources.
|
|
430
|
+
|
|
431
|
+
File resources are identified by their paths and project IDs.
|
|
432
|
+
|
|
433
|
+
Warning: This command group is deprecated and will be removed with the next major release.
|
|
434
|
+
Please use the `project file` command group instead.
|
|
435
|
+
"""
|
|
436
|
+
app: ApplicationContext = ctx.obj
|
|
437
|
+
app.echo_warning(
|
|
438
|
+
"The 'dataset resource' command group is deprecated and will be removed with the next"
|
|
439
|
+
" major release. Please use the 'project file' command group instead.",
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
@click.group(cls=CmemcGroup)
|
|
444
|
+
def file() -> CmemcGroup: # type: ignore[empty-body]
|
|
445
|
+
"""List, inspect, up-/download or delete project file resources.
|
|
446
|
+
|
|
447
|
+
File resources are identified with a RESOURCE_ID which is a concatenation
|
|
448
|
+
of its project ID and its relative path, e.g. `my-project:path-to/table.csv`.
|
|
449
|
+
|
|
450
|
+
Note: To get a list of existing file resources, execute the `project file list` command
|
|
451
|
+
or use tab-completion.
|
|
452
|
+
"""
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
resource.add_command(list_command)
|
|
456
|
+
resource.add_command(delete_command)
|
|
457
|
+
resource.add_command(inspect_command)
|
|
458
|
+
resource.add_command(usage_command)
|
|
459
|
+
|
|
460
|
+
file.add_command(list_command)
|
|
461
|
+
file.add_command(delete_command)
|
|
462
|
+
file.add_command(download_command)
|
|
463
|
+
file.add_command(upload_command)
|
|
464
|
+
file.add_command(inspect_command)
|
|
465
|
+
file.add_command(usage_command)
|