atomict 0.1.2__py3-none-any.whl → 0.1.4__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.
- atomict/__init__.py +1 -1
- atomict/__version__.py +1 -0
- atomict/api.py +182 -0
- atomict/auth.py +47 -0
- atomict/cli/commands/adsorbate.py +183 -0
- atomict/cli/commands/ai_assist.py +111 -0
- atomict/cli/commands/catalysis.py +171 -0
- atomict/cli/commands/common.py +57 -0
- atomict/cli/commands/exploration/__init__.py +7 -0
- atomict/cli/commands/exploration/soec.py +564 -0
- atomict/cli/commands/exploration/sqs.py +440 -0
- atomict/cli/commands/helpers.py +74 -0
- atomict/cli/commands/k8s.py +211 -0
- atomict/cli/commands/login.py +34 -0
- atomict/cli/commands/organization.py +76 -0
- atomict/cli/commands/project.py +876 -0
- atomict/cli/commands/simulation/__init__.py +20 -0
- atomict/cli/commands/simulation/catalysis.py +223 -0
- atomict/cli/commands/simulation/fhiaims.py +279 -0
- atomict/cli/commands/simulation/kpoint.py +380 -0
- atomict/cli/commands/simulation/vibes.py +161 -0
- atomict/cli/commands/simulation.py +176 -0
- atomict/cli/commands/task.py +181 -0
- atomict/cli/commands/upload.py +84 -0
- atomict/cli/commands/user.py +201 -0
- atomict/cli/core/client.py +202 -0
- atomict/cli/core/config.py +43 -0
- atomict/cli/core/utils.py +32 -0
- atomict/cli/main.py +124 -0
- atomict/env.py +79 -0
- atomict/exceptions.py +12 -0
- atomict/infra/distwork/environment.py +26 -0
- atomict/infra/distwork/task.py +49 -0
- atomict/infra/logging.py +22 -0
- atomict/io/cif.py +131 -0
- atomict/io/fhiaims.py +24 -0
- atomict/io/json.py +22 -0
- atomict/io/utils.py +11 -0
- atomict/ml/__init__.py +0 -0
- atomict/ml/model.py +13 -0
- atomict/project/__init__.py +1 -0
- atomict/project/link.py +26 -0
- atomict/project/note.py +18 -0
- atomict/project/project.py +32 -0
- atomict/project/tags.py +48 -0
- atomict/project/workbench.py +6 -0
- atomict/reality/__init__.py +0 -0
- atomict/reality/oqmd.py +5 -0
- atomict/reality/slabs.py +5 -0
- atomict/simulation/__init__.py +0 -0
- atomict/simulation/cantera.py +35 -0
- atomict/simulation/catalysis.py +20 -0
- atomict/simulation/counterfactual.py +28 -0
- atomict/simulation/ea.py +64 -0
- atomict/simulation/fhi_aims.py +72 -0
- atomict/simulation/kpoint.py +74 -0
- atomict/simulation/mlrelax.py +83 -0
- atomict/simulation/phonon.py +12 -0
- atomict/simulation/qe.py +74 -0
- atomict/simulation/sqs.py +24 -0
- atomict/simulation/utils.py +83 -0
- atomict/user/__init__.py +0 -0
- atomict/user/files.py +31 -0
- atomict/user/workspace.py +112 -0
- atomict/utils/fhiaims/__init__.py +0 -0
- atomict/utils/fhiaims/geometry.py +65 -0
- atomict/utils/tenacity.py +36 -0
- {atomict-0.1.2.dist-info → atomict-0.1.4.dist-info}/LICENSE +0 -0
- atomict-0.1.4.dist-info/METADATA +105 -0
- atomict-0.1.4.dist-info/RECORD +77 -0
- {atomict-0.1.2.dist-info → atomict-0.1.4.dist-info}/WHEEL +1 -1
- atomict-0.1.4.dist-info/entry_points.txt +2 -0
- {atomict-0.1.2.dist-info → atomict-0.1.4.dist-info}/top_level.txt +0 -1
- atomict/__main__.py +0 -3
- atomict/cli.py +0 -59
- atomict/datasets/__init__.py +0 -25
- atomict/datasets/io.py +0 -47
- atomict/datasets/public.py +0 -32
- atomict/utils/requests.py +0 -19
- atomict/version.py +0 -2
- atomict-0.1.2.dist-info/METADATA +0 -97
- atomict-0.1.2.dist-info/RECORD +0 -19
- atomict-0.1.2.dist-info/entry_points.txt +0 -2
- tests/test_sdk.py +0 -16
- /atomict/{auth → cli}/__init__.py +0 -0
- /atomict/{utils → infra}/__init__.py +0 -0
- {tests → atomict/infra/distwork}/__init__.py +0 -0
- /tests/test_cli.py → /atomict/io/__init__.py +0 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# cli/commands/simulation.py
|
|
2
|
+
import logging
|
|
3
|
+
from typing import Any, Dict, Optional
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
from rich.console import Console
|
|
7
|
+
from rich.panel import Panel
|
|
8
|
+
from rich.progress import Progress
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
|
|
11
|
+
console = Console()
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.group(name="simulation")
|
|
16
|
+
def simulation():
|
|
17
|
+
"""Manage simulations"""
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def format_simulation_table(simulations: list[Dict[str, Any]]) -> Table:
|
|
22
|
+
"""Create a table with the most relevant simulation fields"""
|
|
23
|
+
table = Table(show_header=True)
|
|
24
|
+
|
|
25
|
+
# Key fields we want to show in the list view
|
|
26
|
+
columns = {
|
|
27
|
+
"ID": lambda x: str(x["id"]),
|
|
28
|
+
"Name": lambda x: x.get("name", "N/A"),
|
|
29
|
+
"Status": lambda x: x.get("task", {}).get("status", "N/A"),
|
|
30
|
+
"Progress": lambda x: f"{x.get('task', {}).get('progress', 0)}%",
|
|
31
|
+
"Results": lambda x: x.get("num_results", "0"),
|
|
32
|
+
"Created": lambda x: x.get("created_at", "N/A"),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
# Add columns
|
|
36
|
+
for col_name in columns:
|
|
37
|
+
table.add_column(col_name)
|
|
38
|
+
|
|
39
|
+
# Add rows
|
|
40
|
+
for sim in simulations:
|
|
41
|
+
row = [columns[col_name](sim) for col_name in columns]
|
|
42
|
+
table.add_row(*row)
|
|
43
|
+
|
|
44
|
+
return table
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def format_simulation_detail(sim: Dict[str, Any]) -> None:
|
|
48
|
+
"""Display detailed simulation information"""
|
|
49
|
+
# Main info panel
|
|
50
|
+
console.print(Panel(f"[bold]{sim.get('name', 'Unnamed Simulation')}[/bold]"))
|
|
51
|
+
|
|
52
|
+
# Basic info table
|
|
53
|
+
basic_info = Table(show_header=True, title="Basic Information")
|
|
54
|
+
basic_info.add_column("Property")
|
|
55
|
+
basic_info.add_column("Value")
|
|
56
|
+
|
|
57
|
+
basic_fields = {
|
|
58
|
+
"ID": "id",
|
|
59
|
+
"Name": "name",
|
|
60
|
+
"Created": "created_at",
|
|
61
|
+
"Edited": "edited_at",
|
|
62
|
+
"Bulk ID": "bulk_id",
|
|
63
|
+
"Slab ID": "slab_id",
|
|
64
|
+
"Calculator": "calculator_class",
|
|
65
|
+
"Max Force": "fmax",
|
|
66
|
+
"Steps": "steps",
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for label, field in basic_fields.items():
|
|
70
|
+
value = sim.get(field, "N/A")
|
|
71
|
+
basic_info.add_row(label, str(value))
|
|
72
|
+
|
|
73
|
+
console.print(basic_info)
|
|
74
|
+
console.print()
|
|
75
|
+
|
|
76
|
+
# Task status if present
|
|
77
|
+
if task := sim.get("task"):
|
|
78
|
+
task_info = Table(show_header=True, title="Task Status")
|
|
79
|
+
task_info.add_column("Property")
|
|
80
|
+
task_info.add_column("Value")
|
|
81
|
+
|
|
82
|
+
task_fields = {
|
|
83
|
+
"Status": "status",
|
|
84
|
+
"Progress": "progress",
|
|
85
|
+
"Running On": "running_on",
|
|
86
|
+
"Error": "error",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for label, field in task_fields.items():
|
|
90
|
+
value = task.get(field, "N/A")
|
|
91
|
+
task_info.add_row(label, str(value))
|
|
92
|
+
|
|
93
|
+
console.print(task_info)
|
|
94
|
+
console.print()
|
|
95
|
+
|
|
96
|
+
# Results summary if present
|
|
97
|
+
results = Table(show_header=True, title="Results Summary")
|
|
98
|
+
results.add_column("Metric")
|
|
99
|
+
results.add_column("Count")
|
|
100
|
+
|
|
101
|
+
result_fields = {
|
|
102
|
+
"Total Results": "num_results",
|
|
103
|
+
"Dissociated": "num_results_dissociated",
|
|
104
|
+
"Desorbed": "num_results_desorbed",
|
|
105
|
+
"Surface Changed": "num_results_surface_changed",
|
|
106
|
+
"Intercalated": "num_results_intercalated",
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
for label, field in result_fields.items():
|
|
110
|
+
value = sim.get(field, "0")
|
|
111
|
+
results.add_row(label, str(value))
|
|
112
|
+
|
|
113
|
+
console.print(results)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@simulation.command()
|
|
117
|
+
@click.argument("id", required=False)
|
|
118
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
119
|
+
def get(id: Optional[str] = None, json_output: bool = False):
|
|
120
|
+
"""Get simulation details or list all simulations"""
|
|
121
|
+
from atomict.cli.core.client import get_client
|
|
122
|
+
|
|
123
|
+
client = get_client()
|
|
124
|
+
|
|
125
|
+
if id:
|
|
126
|
+
result = client.get(f"/api/catalysis-simulation/{id}/")
|
|
127
|
+
if json_output:
|
|
128
|
+
console.print_json(data=result)
|
|
129
|
+
return
|
|
130
|
+
|
|
131
|
+
format_simulation_detail(result)
|
|
132
|
+
else:
|
|
133
|
+
results = client.get_all("/api/catalysis-simulation/")
|
|
134
|
+
if json_output:
|
|
135
|
+
console.print_json(data=results)
|
|
136
|
+
return
|
|
137
|
+
|
|
138
|
+
console.print(format_simulation_table(results))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@simulation.command()
|
|
142
|
+
@click.argument("id")
|
|
143
|
+
@click.option("--output", "-o", help="Output file path")
|
|
144
|
+
def download_results(id: str, output: Optional[str]):
|
|
145
|
+
"""Download simulation results file"""
|
|
146
|
+
from pathlib import Path
|
|
147
|
+
|
|
148
|
+
from atomict.cli.core.client import get_client
|
|
149
|
+
|
|
150
|
+
client = get_client()
|
|
151
|
+
|
|
152
|
+
with console.status("[bold green]Downloading simulation results..."):
|
|
153
|
+
response = client.get(f"/api/catalysis-simulation-results/{id}/download/")
|
|
154
|
+
|
|
155
|
+
# Get filename from response headers or use provided output path
|
|
156
|
+
if output:
|
|
157
|
+
filename = Path(output)
|
|
158
|
+
else:
|
|
159
|
+
content_disposition = response.headers.get("content-disposition", "")
|
|
160
|
+
filename = (
|
|
161
|
+
Path(content_disposition.split("filename=")[-1].strip('"'))
|
|
162
|
+
if content_disposition
|
|
163
|
+
else Path(f"simulation_{id}_results.dat")
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
total_size = int(response.headers.get("content-length", 0))
|
|
167
|
+
|
|
168
|
+
with open(filename, "wb") as f:
|
|
169
|
+
with Progress() as progress:
|
|
170
|
+
task = progress.add_task("[cyan]Downloading...", total=total_size)
|
|
171
|
+
|
|
172
|
+
for chunk in response.iter_bytes(chunk_size=8192):
|
|
173
|
+
f.write(chunk)
|
|
174
|
+
progress.update(task, advance=len(chunk))
|
|
175
|
+
|
|
176
|
+
console.print(f"[green]Results downloaded to: {filename}[/green]")
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# cli/commands/task.py
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
from rich.console import Console
|
|
6
|
+
from rich.panel import Panel
|
|
7
|
+
|
|
8
|
+
from atomict.cli.commands.common import create_table
|
|
9
|
+
from atomict.cli.commands.helpers import format_datetime, get_status_string
|
|
10
|
+
from atomict.cli.core.client import get_client
|
|
11
|
+
from atomict.cli.core.utils import get_pagination_info
|
|
12
|
+
|
|
13
|
+
console = Console()
|
|
14
|
+
|
|
15
|
+
STATUS_MAP = {
|
|
16
|
+
"draft": 0,
|
|
17
|
+
"ready": 1,
|
|
18
|
+
"running": 2,
|
|
19
|
+
"completed": 3,
|
|
20
|
+
"error": 4,
|
|
21
|
+
"paused": 5,
|
|
22
|
+
"aborted": 6
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@click.group(name="task")
|
|
27
|
+
def task():
|
|
28
|
+
"""Manage tasks and their status"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@task.command()
|
|
33
|
+
@click.argument("id")
|
|
34
|
+
def cancel(id: str):
|
|
35
|
+
"""Cancel a running task"""
|
|
36
|
+
client = get_client()
|
|
37
|
+
|
|
38
|
+
data = {"status": 6}
|
|
39
|
+
|
|
40
|
+
client.patch(f"/api/tasks/{id}/", data=data)
|
|
41
|
+
console.print(f"[green]Task {id} has been cancelled[/green]")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@task.command()
|
|
45
|
+
@click.argument("id", required=False)
|
|
46
|
+
@click.option("--search", help="Search tasks by ID, type, status, or error message")
|
|
47
|
+
@click.option(
|
|
48
|
+
"--ordering",
|
|
49
|
+
type=click.Choice(
|
|
50
|
+
["created_at", "-created_at", "status", "-status", "task_type", "-task_type"]
|
|
51
|
+
),
|
|
52
|
+
default="-created_at",
|
|
53
|
+
help="Order results by field (prefix with - for descending)",
|
|
54
|
+
)
|
|
55
|
+
@click.option(
|
|
56
|
+
"--status",
|
|
57
|
+
type=click.Choice(["draft", "ready", "running", "completed", "error", "paused", "aborted"], case_sensitive=False),
|
|
58
|
+
help="Filter by status",
|
|
59
|
+
)
|
|
60
|
+
@click.option("--all", "fetch_all", is_flag=True, help="Fetch all results")
|
|
61
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
62
|
+
def get(
|
|
63
|
+
id: Optional[str] = None,
|
|
64
|
+
search: Optional[str] = None,
|
|
65
|
+
ordering: Optional[str] = "-created_at",
|
|
66
|
+
status: Optional[str] = None,
|
|
67
|
+
fetch_all: bool = False,
|
|
68
|
+
json_output: bool = False,
|
|
69
|
+
):
|
|
70
|
+
"""Get task details or list all tasks
|
|
71
|
+
|
|
72
|
+
When no ID is provided, lists all tasks with optional filtering and ordering.
|
|
73
|
+
"""
|
|
74
|
+
client = get_client()
|
|
75
|
+
params = {"depth": 2}
|
|
76
|
+
|
|
77
|
+
if id:
|
|
78
|
+
result = client.get(f"/api/tasks/{id}/")
|
|
79
|
+
if json_output:
|
|
80
|
+
console.print_json(data=result)
|
|
81
|
+
return
|
|
82
|
+
|
|
83
|
+
console.print(Panel.fit(f"[bold]Task Details[/bold]"))
|
|
84
|
+
console.print(f"ID: {result['id']}")
|
|
85
|
+
console.print(f"Type: {result.get('task_type', 'N/A')}")
|
|
86
|
+
console.print(f"Model Name: {result.get('model_name', 'N/A')}")
|
|
87
|
+
console.print(f"Status: {get_status_string(result.get('status', 'N/A'))}")
|
|
88
|
+
console.print(f"Created: {format_datetime(result.get('created_at'))}")
|
|
89
|
+
console.print(f"Updated: {format_datetime(result.get('updated_at'))}")
|
|
90
|
+
if result.get("error"):
|
|
91
|
+
console.print(f"Error: {result['error']}")
|
|
92
|
+
if result.get("input_params"):
|
|
93
|
+
console.print("\n[bold]Input Parameters:[/bold]")
|
|
94
|
+
console.print_json(data=result["input_params"])
|
|
95
|
+
else:
|
|
96
|
+
if search is not None:
|
|
97
|
+
params["search"] = search
|
|
98
|
+
if ordering:
|
|
99
|
+
params["ordering"] = ordering
|
|
100
|
+
if status:
|
|
101
|
+
params["status"] = STATUS_MAP[status.lower()]
|
|
102
|
+
|
|
103
|
+
if fetch_all:
|
|
104
|
+
results = client.get_all("/api/tasks/", params=params)
|
|
105
|
+
else:
|
|
106
|
+
results = client.get("/api/tasks/", params=params)
|
|
107
|
+
|
|
108
|
+
if json_output:
|
|
109
|
+
console.print_json(data=results)
|
|
110
|
+
return
|
|
111
|
+
|
|
112
|
+
columns = [
|
|
113
|
+
("ID", "id", None),
|
|
114
|
+
("Project name", "project", lambda x: x.get("name", "N/A")),
|
|
115
|
+
("Progress", "progress", None),
|
|
116
|
+
("Status", "status", get_status_string),
|
|
117
|
+
("Error", "error", None),
|
|
118
|
+
("Created", "created_at", format_datetime),
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
items, footer_string = get_pagination_info(results)
|
|
122
|
+
|
|
123
|
+
if not items:
|
|
124
|
+
console.print(
|
|
125
|
+
f"[white]No tasks found with the given criteria:[/white]\n[green]{params}"
|
|
126
|
+
)
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
table = create_table(
|
|
130
|
+
columns=columns, items=items, title="Tasks", caption=footer_string
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
console.print(table)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@task.command()
|
|
137
|
+
@click.argument("task_id")
|
|
138
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
139
|
+
def get_status_history(task_id: str, json_output: bool = False):
|
|
140
|
+
"""Get status history for a task"""
|
|
141
|
+
client = get_client()
|
|
142
|
+
results = client.get("/api/task-status-history/", params={"id": task_id})
|
|
143
|
+
|
|
144
|
+
if json_output:
|
|
145
|
+
console.print_json(data=results)
|
|
146
|
+
return
|
|
147
|
+
|
|
148
|
+
columns = [
|
|
149
|
+
("NewStatus", "new_status", get_status_string),
|
|
150
|
+
("Timestamp", "timestamp", format_datetime),
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
items, footer_string = get_pagination_info(results)
|
|
154
|
+
|
|
155
|
+
if not items:
|
|
156
|
+
console.print(f"[white]No status history found for task {task_id}[/white]")
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
table = create_table(
|
|
160
|
+
columns=columns,
|
|
161
|
+
items=items,
|
|
162
|
+
title=f"Status History: {task_id}",
|
|
163
|
+
caption=footer_string,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
console.print(table)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@task.command(hidden=True)
|
|
170
|
+
@click.argument("id")
|
|
171
|
+
@click.argument(
|
|
172
|
+
"status",
|
|
173
|
+
type=click.Choice(list(STATUS_MAP.keys()), case_sensitive=False)
|
|
174
|
+
)
|
|
175
|
+
def update_status(id: str, status: str):
|
|
176
|
+
"""Update a task's status (Development only)"""
|
|
177
|
+
client = get_client()
|
|
178
|
+
status_code = STATUS_MAP[status.lower()]
|
|
179
|
+
data = {"status": status_code}
|
|
180
|
+
client.patch(f"/api/tasks/{id}/", data=data)
|
|
181
|
+
console.print(f"[green]Task {id} status updated to {status}[/green]")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# cli/commands/upload.py
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import click
|
|
5
|
+
from rich.console import Console
|
|
6
|
+
|
|
7
|
+
from atomict.cli.commands.common import create_table
|
|
8
|
+
from atomict.cli.commands.helpers import format_datetime
|
|
9
|
+
from atomict.cli.core.client import get_client
|
|
10
|
+
from atomict.cli.core.utils import get_pagination_info
|
|
11
|
+
|
|
12
|
+
console = Console()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.group(name="upload")
|
|
16
|
+
def upload():
|
|
17
|
+
"""Manage file uploads"""
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# TBD: not currently supported via API
|
|
22
|
+
# @upload.command()
|
|
23
|
+
# @click.argument('file_path', type=click.Path(exists=True))
|
|
24
|
+
# @click.option('--type', 'file_type', help='Type of file being uploaded')
|
|
25
|
+
# @click.option('--description', help='Description of the file')
|
|
26
|
+
# def create(file_path: str, file_type: Optional[str], description: Optional[str]):
|
|
27
|
+
# """Upload a new file"""
|
|
28
|
+
# from atomict.cli.core.client import get_client
|
|
29
|
+
|
|
30
|
+
# client = get_client()
|
|
31
|
+
# path = Path(file_path)
|
|
32
|
+
|
|
33
|
+
# with Progress() as progress:
|
|
34
|
+
# task = progress.add_task(f"Uploading {path.name}...", total=path.stat().st_size)
|
|
35
|
+
|
|
36
|
+
# # Implementation would depend on your API's upload endpoint
|
|
37
|
+
# # This is a placeholder for the upload logic
|
|
38
|
+
# with open(path, 'rb') as f:
|
|
39
|
+
# data = {
|
|
40
|
+
# 'file': f,
|
|
41
|
+
# 'type': file_type,
|
|
42
|
+
# 'description': description
|
|
43
|
+
# }
|
|
44
|
+
# result = client.post('/api/user-upload/', data)
|
|
45
|
+
|
|
46
|
+
# progress.update(task, completed=path.stat().st_size)
|
|
47
|
+
|
|
48
|
+
# console.print(f"[green]Successfully uploaded {path.name}[/green]")
|
|
49
|
+
# console.print(f"Upload ID: {result['id']}")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@upload.command()
|
|
53
|
+
@click.option("--limit", type=int, help="Number of results to return")
|
|
54
|
+
@click.option("--type", "file_type", help="Filter by file type")
|
|
55
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
56
|
+
def list(limit: Optional[int], file_type: Optional[str], json_output: bool):
|
|
57
|
+
"""List uploaded files"""
|
|
58
|
+
params = {}
|
|
59
|
+
if limit:
|
|
60
|
+
params["limit"] = limit
|
|
61
|
+
if file_type:
|
|
62
|
+
params["type"] = file_type
|
|
63
|
+
|
|
64
|
+
client = get_client()
|
|
65
|
+
results = client.get_all("/api/user-upload/", params)
|
|
66
|
+
|
|
67
|
+
if json_output:
|
|
68
|
+
console.print_json(data=results)
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
columns = [
|
|
72
|
+
("ID", "id", None),
|
|
73
|
+
("Filename", "orig_name", None),
|
|
74
|
+
("Type", "type", None),
|
|
75
|
+
("Size", "size", None),
|
|
76
|
+
("Uploaded", "uploaded", format_datetime),
|
|
77
|
+
]
|
|
78
|
+
items, footer_string = get_pagination_info(results)
|
|
79
|
+
|
|
80
|
+
table = create_table(
|
|
81
|
+
columns=columns, items=items, title="Uploads", caption=footer_string
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
console.print(table)
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from rich.console import Console
|
|
5
|
+
from rich.panel import Panel
|
|
6
|
+
|
|
7
|
+
from atomict.cli.commands.common import create_table
|
|
8
|
+
from atomict.cli.commands.helpers import format_datetime
|
|
9
|
+
from atomict.cli.core.client import get_client
|
|
10
|
+
from atomict.cli.core.utils import get_pagination_info
|
|
11
|
+
|
|
12
|
+
console = Console()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@click.group(name="user")
|
|
16
|
+
def user_group():
|
|
17
|
+
"""Manage users and user uploads"""
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@user_group.command()
|
|
22
|
+
@click.argument("id", required=False)
|
|
23
|
+
@click.option("--search", help="Search term")
|
|
24
|
+
@click.option("--ordering", help="Field to order results by")
|
|
25
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
26
|
+
@click.option("--all", "fetch_all", is_flag=True, help="Fetch all results")
|
|
27
|
+
def get(
|
|
28
|
+
id: Optional[str] = None,
|
|
29
|
+
search: Optional[str] = None,
|
|
30
|
+
ordering: Optional[str] = None,
|
|
31
|
+
json_output: bool = False,
|
|
32
|
+
fetch_all: bool = False,
|
|
33
|
+
):
|
|
34
|
+
"""Get user details or list all users"""
|
|
35
|
+
client = get_client()
|
|
36
|
+
|
|
37
|
+
if id:
|
|
38
|
+
result = client.get(f"/api/user/{id}/")
|
|
39
|
+
if json_output:
|
|
40
|
+
console.print_json(data=result)
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
console.print(Panel.fit(f"[bold]User Details[/bold]"))
|
|
44
|
+
console.print(f"Username: {result.get('username', 'N/A')}")
|
|
45
|
+
console.print(f"Email: {result.get('email', 'N/A')}")
|
|
46
|
+
console.print(f"First Name: {result.get('first_name', 'N/A')}")
|
|
47
|
+
console.print(f"Last Name: {result.get('last_name', 'N/A')}")
|
|
48
|
+
console.print(f"Date Joined: {format_datetime(result.get('date_joined'))}")
|
|
49
|
+
console.print(f"Last Login: {format_datetime(result.get('last_login'))}")
|
|
50
|
+
else:
|
|
51
|
+
params = {}
|
|
52
|
+
if search is not None:
|
|
53
|
+
params["search"] = search
|
|
54
|
+
if ordering:
|
|
55
|
+
params["ordering"] = ordering
|
|
56
|
+
|
|
57
|
+
if fetch_all:
|
|
58
|
+
results = client.get_all("/api/user/", params=params)
|
|
59
|
+
else:
|
|
60
|
+
results = client.get("/api/user/", params=params)
|
|
61
|
+
|
|
62
|
+
if json_output:
|
|
63
|
+
console.print_json(data=results)
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
columns = [
|
|
67
|
+
("Username", "username", None),
|
|
68
|
+
("Email", "email", None),
|
|
69
|
+
("First Name", "first_name", None),
|
|
70
|
+
("Last Name", "last_name", None),
|
|
71
|
+
("OpenAPI Key", "openapi_key", None),
|
|
72
|
+
("Date Joined", "date_joined", format_datetime),
|
|
73
|
+
("Last Login", "last_login", format_datetime),
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
items, footer_string = get_pagination_info(results)
|
|
77
|
+
|
|
78
|
+
if not items:
|
|
79
|
+
console.print(
|
|
80
|
+
f"[white]No users found with the given criteria:[/white]\n[green]{params}"
|
|
81
|
+
)
|
|
82
|
+
return
|
|
83
|
+
|
|
84
|
+
table = create_table(
|
|
85
|
+
columns=columns, items=items, title="Users", caption=footer_string
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
console.print(table)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@user_group.command()
|
|
92
|
+
@click.argument("id", required=False)
|
|
93
|
+
@click.option("--user", help="User ID to filter by")
|
|
94
|
+
@click.option("--search", help="Search term")
|
|
95
|
+
@click.option("--ordering", help="Field to order results by")
|
|
96
|
+
@click.option("--json-output", is_flag=True, help="Output in JSON format")
|
|
97
|
+
@click.option("--all", "fetch_all", is_flag=True, help="Fetch all results")
|
|
98
|
+
def get_upload(
|
|
99
|
+
id: Optional[str] = None,
|
|
100
|
+
user: Optional[str] = None,
|
|
101
|
+
search: Optional[str] = None,
|
|
102
|
+
ordering: Optional[str] = None,
|
|
103
|
+
json_output: bool = False,
|
|
104
|
+
fetch_all: bool = False,
|
|
105
|
+
):
|
|
106
|
+
"""Get user upload details or list all uploads
|
|
107
|
+
|
|
108
|
+
When listing uploads, --user can be provided to filter by user.
|
|
109
|
+
"""
|
|
110
|
+
client = get_client()
|
|
111
|
+
|
|
112
|
+
if id:
|
|
113
|
+
result = client.get(f"/api/user-upload/{id}/")
|
|
114
|
+
if json_output:
|
|
115
|
+
console.print_json(data=result)
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
console.print(Panel.fit(f"[bold]User Upload Details[/bold]"))
|
|
119
|
+
console.print(f"ID: {result['id']}")
|
|
120
|
+
console.print(f"File Name: {result.get('orig_name', 'N/A')}")
|
|
121
|
+
console.print(f"Size: {result.get('size', 'N/A')} bytes")
|
|
122
|
+
console.print(f"Type: {result.get('type', 'N/A')}")
|
|
123
|
+
console.print(f"Description: {result.get('users_description', 'N/A')}")
|
|
124
|
+
console.print(f"Uploaded: {format_datetime(result.get('uploaded'))}")
|
|
125
|
+
else:
|
|
126
|
+
params = {}
|
|
127
|
+
if search:
|
|
128
|
+
params["search"] = search
|
|
129
|
+
if user:
|
|
130
|
+
params["user"] = user
|
|
131
|
+
if ordering:
|
|
132
|
+
params["ordering"] = ordering
|
|
133
|
+
|
|
134
|
+
if fetch_all:
|
|
135
|
+
results = client.get_all("/api/user-upload/", params=params)
|
|
136
|
+
else:
|
|
137
|
+
results = client.get("/api/user-upload/", params=params)
|
|
138
|
+
|
|
139
|
+
if json_output:
|
|
140
|
+
console.print_json(data=results)
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
columns = [
|
|
144
|
+
("ID", "id", None),
|
|
145
|
+
("File Name", "orig_name", None),
|
|
146
|
+
("Size", "size", lambda x: f"{x} bytes"),
|
|
147
|
+
("Type", "type", None),
|
|
148
|
+
("Description", "users_description", None),
|
|
149
|
+
("Uploaded", "uploaded", format_datetime),
|
|
150
|
+
]
|
|
151
|
+
|
|
152
|
+
items, footer_string = get_pagination_info(results)
|
|
153
|
+
|
|
154
|
+
if not items:
|
|
155
|
+
console.print(
|
|
156
|
+
f"[white]No uploads found with the given criteria:[/white]\n[green]{params}"
|
|
157
|
+
)
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
table = create_table(
|
|
161
|
+
columns=columns, items=items, title="User Uploads", caption=footer_string
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
console.print(table)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@user_group.command()
|
|
168
|
+
@click.argument("id")
|
|
169
|
+
@click.option(
|
|
170
|
+
"--output",
|
|
171
|
+
"-o",
|
|
172
|
+
help="Output file path (default: current directory with original filename)",
|
|
173
|
+
)
|
|
174
|
+
def download(id: str, output: Optional[str] = None):
|
|
175
|
+
"""Download a user upload file"""
|
|
176
|
+
client = get_client()
|
|
177
|
+
|
|
178
|
+
try:
|
|
179
|
+
with console.status(f"[bold blue]Downloading file..."):
|
|
180
|
+
result = client.get(
|
|
181
|
+
f"/api/user-upload/{id}/", params={"include_content": "true"}
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
filename = result.get("orig_name", f"file-{id}")
|
|
185
|
+
if not output:
|
|
186
|
+
output = filename
|
|
187
|
+
|
|
188
|
+
# The file content is base64 encoded in the response
|
|
189
|
+
file_content = result.get("file_content_base64")
|
|
190
|
+
if not file_content:
|
|
191
|
+
console.print("[red]No file content received[/red]")
|
|
192
|
+
return
|
|
193
|
+
|
|
194
|
+
import base64
|
|
195
|
+
|
|
196
|
+
with open(output, "wb") as f:
|
|
197
|
+
f.write(base64.b64decode(file_content))
|
|
198
|
+
|
|
199
|
+
console.print(f"[green]Successfully downloaded to: {output}[/green]")
|
|
200
|
+
except Exception as e:
|
|
201
|
+
click.echo(f"[red]Error downloading file: {str(e)}[/red]", err=True)
|