-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_cdf.py
More file actions
executable file
·173 lines (137 loc) · 5.42 KB
/
_cdf.py
File metadata and controls
executable file
·173 lines (137 loc) · 5.42 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# The Typer parameters get mixed up if we use the __future__ import annotations in the main file.
# ruff: noqa: E402
import re
import sys
import traceback
from datetime import datetime, timezone
from pathlib import Path
from typing import NoReturn
import typer
from cognite.client.config import global_config
from rich.markup import escape
from rich.panel import Panel
from cognite_toolkit._cdf_tk.hints import Hint
# Do not warn the user about feature previews from the Cognite-SDK we use in Toolkit
global_config.disable_pypi_version_check = True
global_config.silence_feature_preview_warnings = True
from rich import print
from cognite_toolkit._cdf_tk.apps import (
AuthApp,
CoreApp,
DataApp,
DevApp,
DumpApp,
ImportApp,
LandingApp,
MigrateApp,
ModulesApp,
ProfileApp,
RepoApp,
RunApp,
)
from cognite_toolkit._cdf_tk.cdf_toml import CDFToml
from cognite_toolkit._cdf_tk.commands import (
AboutCommand,
)
from cognite_toolkit._cdf_tk.constants import HINT_LEAD_TEXT, URL, USE_SENTRY
from cognite_toolkit._cdf_tk.exceptions import (
ToolkitError,
)
from cognite_toolkit._cdf_tk.feature_flags import Flags
from cognite_toolkit._cdf_tk.plugins import Plugins
from cognite_toolkit._cdf_tk.tracker import Tracker
from cognite_toolkit._cdf_tk.utils import (
sentry_exception_filter,
)
from cognite_toolkit._version import __version__ as current_version
if USE_SENTRY:
import sentry_sdk
sentry_sdk.init(
dsn="https://20552f92b525fe551e9adc939024d526@o4508040730968064.ingest.de.sentry.io/4508160801374288",
release=current_version,
before_send=sentry_exception_filter,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
)
CDF_TOML = CDFToml.load(Path.cwd())
default_typer_kws = dict(
pretty_exceptions_short=False,
pretty_exceptions_show_locals=False,
pretty_exceptions_enable=False,
)
try:
typer.Typer(**default_typer_kws) # type: ignore [arg-type]
except AttributeError as e:
# From Typer version 0.11 -> 0.12, breaks if you have an existing installation.
raise ToolkitError(
"'cognite-toolkit' uses a dependency named 'typer'. From 'typer' version 0.11 -> 0.12 there was a "
"breaking change if you have an existing installation of 'typer'. The workaround is to uninstall "
"'typer-slim', and then, reinstall 'typer':\n"
"pip uninstall typer-slim\n"
"pip install typer\n\n"
f"This was triggered by the error: {e!r}"
)
_app = CoreApp(**default_typer_kws)
user_app = typer.Typer(**default_typer_kws, hidden=True) # type: ignore [arg-type]
landing_app = LandingApp(**default_typer_kws)
_app.add_typer(AuthApp(**default_typer_kws), name="auth")
_app.add_typer(RepoApp(**default_typer_kws), name="repo")
if Plugins.run.value.is_enabled():
_app.add_typer(RunApp(**default_typer_kws), name="run")
if Plugins.dump.value.is_enabled():
_app.add_typer(DumpApp(**default_typer_kws), name="dump")
if Plugins.dev.value.is_enabled():
_app.add_typer(DevApp(**default_typer_kws), name="dev")
if Flags.PROFILE.is_enabled():
_app.add_typer(ProfileApp(**default_typer_kws), name="profile")
if Flags.MIGRATE.is_enabled():
_app.add_typer(MigrateApp(**default_typer_kws), name="migrate")
if Flags.IMPORT_CMD.is_enabled():
_app.add_typer(ImportApp(**default_typer_kws), name="import")
if Plugins.data.value.is_enabled():
_app.add_typer(DataApp(**default_typer_kws), name="data")
_app.add_typer(ModulesApp(**default_typer_kws), name="modules")
_app.command("init")(landing_app.main_init)
@_app.command("about")
def about() -> None:
"""Display information about the Toolkit installation and configuration."""
cmd = AboutCommand()
cmd.run(lambda: cmd.execute(Path.cwd()))
def app() -> NoReturn:
# --- Main entry point ---
# Users run 'app()' directly, but that doesn't allow us to control excepton handling:
try:
_app()
except ToolkitError as err:
if "--verbose" in sys.argv:
print(Panel(traceback.format_exc(), title="Traceback", expand=False))
print(f" [bold red]ERROR ([/][red]{type(err).__name__}[/][bold red]):[/] {err}")
raise SystemExit(1)
except SystemExit:
if result := re.search(r"click.exceptions.UsageError: No such command '(\w+)'.", traceback.format_exc()):
cmd = result.group(1)
if cmd in Plugins.list():
plugin = r"[plugins]"
print(
f"{HINT_LEAD_TEXT} The plugin [bold]{cmd}[/bold] is not enabled."
f"\nEnable it in the [bold]cdf.toml[/bold] file by setting '{cmd} = true' in the "
f"[bold]{escape(plugin)}[/bold] section."
f"\nDocs to learn more: {Hint.link(URL.plugins, URL.plugins)}"
)
raise
raise SystemExit(0)
@user_app.callback(invoke_without_command=True)
def user_main(ctx: typer.Context) -> None:
"""Commands to give information about the toolkit."""
if ctx.invoked_subcommand is None:
print("Use [bold yellow]cdf user --help[/] to see available commands.")
return None
@user_app.command("info")
def user_info() -> None:
"""Print information about user"""
tracker = Tracker()
print(f"ID={tracker.get_distinct_id()!r}\nnow={datetime.now(timezone.utc).isoformat(timespec='seconds')!r}")
if __name__ == "__main__":
app()