-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path__init__.py
More file actions
355 lines (322 loc) · 10 KB
/
Copy path__init__.py
File metadata and controls
355 lines (322 loc) · 10 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import json
import logging
import logging.config
import os
from pathlib import Path
from typing import Any, List, Literal, Optional
import click
from robotcode.core.utils.cli import show_hidden_arguments
from robotcode.core.utils.logging import LoggingDescriptor
from robotcode.plugin import (
Application,
ColoredOutput,
OutputFormat,
pass_application,
)
from robotcode.plugin._agent_detection import is_running_in_ai_agent
from robotcode.plugin.click_helper.aliases import AliasedGroup
from robotcode.plugin.click_helper.types import EnumChoice
from robotcode.plugin.manager import PluginManager
from .__version__ import __version__
from .commands import config, profiles
old_make_metavar = click.Parameter.make_metavar
def my_make_metavar(self: click.Parameter, *args: Any, **kwargs: Any) -> str:
metavar = old_make_metavar(self, *args, **kwargs)
if isinstance(self, click.Option) and self.multiple:
metavar += " *"
return metavar
click.Parameter.make_metavar = my_make_metavar # type: ignore[method-assign]
class RobotCodeFormatter(logging.Formatter):
def __init__(self, *args: Any, defaults: Any = None, **kwargs: Any) -> None:
defaults = defaults or {}
if defaults.get("indent") is None:
defaults["indent"] = ""
super().__init__(*args, defaults=defaults, **kwargs)
@click.group(
cls=AliasedGroup,
context_settings={"auto_envvar_prefix": "ROBOTCODE"},
invoke_without_command=False,
)
@click.option(
"-c",
"--config",
"config_files",
type=click.Path(exists=True, path_type=Path),
multiple=True,
show_envvar=True,
help="""\
Config file to use. If not specified, the default config file is used.
""",
)
@click.option(
"-p",
"--profile",
"profiles",
type=str,
multiple=True,
show_envvar=True,
help="""\
The Execution Profile to use. If not specified, the default profile is used.
""",
)
@click.option(
"-r",
"--root",
"root",
type=click.Path(exists=True, path_type=Path, dir_okay=True, file_okay=False, resolve_path=True),
show_envvar=True,
help="Specifies the root path to be used for the project. It will be automatically detected if not provided.",
)
@click.option(
"--no-vcs",
is_flag=True,
show_envvar=True,
help="Ignore version control system directories (e.g., .git, .hg) when detecting the project root.",
)
@click.option(
"-f",
"--format",
"format",
type=EnumChoice(OutputFormat),
default=None,
help="Set the output format.",
show_default=True,
)
@click.option(
"-d",
"--dry",
is_flag=True,
show_envvar=True,
help="Dry run, do not execute any commands.",
)
@click.option(
"--color / --no-color",
"color",
default=None,
help=(
"Force or disable colored output. Default (no flag): auto-detect — colors only when stdout is a TTY,"
" disabled if `NO_COLOR` is set, forced if `FORCE_COLOR` is set."
),
show_envvar=True,
)
@click.option(
"--pager / --no-pager",
"pager",
default=None,
help=(
"Force or disable the pager. Default (no flag): auto-page when the rendered output exceeds the terminal height."
),
show_envvar=True,
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Enables verbose mode.",
show_envvar=True,
)
@click.option("--log", is_flag=True, help="Enables logging.", show_envvar=True)
@click.option(
"--log-level",
type=click.Choice(["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]),
help="Sets the log level.",
default="CRITICAL",
show_default=True,
show_envvar=True,
)
@click.option(
"--log-format",
type=str,
help="Sets the log format. See python logging documentation for more information.",
default=logging.BASIC_FORMAT,
show_default=True,
show_envvar=True,
)
@click.option(
"--log-style",
type=click.Choice(["%", "{", "$"]),
help="Sets the log style. See python logging documentation for more information.",
default="%",
show_default=True,
show_envvar=True,
)
@click.option(
"--log-filename",
type=click.Path(
file_okay=True,
dir_okay=False,
writable=True,
exists=False,
path_type=str,
),
help="Write log output to a file instead to console.",
default=None,
show_default=True,
show_envvar=True,
)
@click.option(
"--log-calls",
is_flag=True,
help="Enables logging of method/function calls.",
show_envvar=True,
)
@click.option(
"--log-config",
type=click.Path(
file_okay=True,
dir_okay=False,
readable=True,
exists=True,
path_type=str,
),
help="Path to a logging configuration file. This must be a valid Python logging configuration file in JSON format."
" If this option is set, the other logging options are ignored.",
default=None,
show_default=True,
show_envvar=True,
)
@click.option(
"--default-path",
"-dp",
type=click.Path(exists=False, resolve_path=False, path_type=str),
multiple=True,
hidden=show_hidden_arguments(),
help="Default path to use if no path is given or defined in a profile. "
"**This is an internal option for running in vscode**",
)
@click.option(
"--launcher-script",
hidden=show_hidden_arguments(),
type=str,
help="Path to the launcher script. This is an internal option.",
)
@click.option(
"--debugpy",
is_flag=True,
hidden=show_hidden_arguments(),
show_envvar=True,
help="Starts a debugpy session. "
"**This is an internal option and should only be use if you want to debug _RobotCode_.**",
)
@click.option(
"--debugpy-port",
type=int,
default=5678,
show_default=True,
show_envvar=True,
hidden=show_hidden_arguments(),
help="Defines the port to use for the debugpy session.",
)
@click.option(
"--debugpy-wait-for-client",
is_flag=True,
hidden=show_hidden_arguments(),
show_envvar=True,
help="Waits for a debugpy client to connect before starting the debugpy session.",
)
@click.version_option(version=__version__, prog_name="robotcode")
@pass_application
def robotcode(
app: Application,
config_files: Optional[List[Path]],
profiles: Optional[List[str]],
root: Optional[Path],
no_vcs: bool,
format: Optional[OutputFormat],
dry: bool,
verbose: bool,
color: Optional[bool],
pager: Optional[bool],
log: bool,
log_level: str,
log_format: str,
log_style: Literal["%", "{", "$"],
log_filename: Optional[str],
log_calls: bool,
log_config: Optional[Path],
default_path: Optional[List[str]],
launcher_script: Optional[str] = None,
debugpy: bool = False,
debugpy_port: int = 5678,
debugpy_wait_for_client: bool = False,
) -> None:
"""\b
_____ _ _ _____ _
| __ \\ | | | | / ____| | |
| |__) |___ | |__ ___ | |_| | ___ __| | ___
| _ // _ \\| '_ \\ / _ \\| __| | / _ \\ / _ |/ _ \\
| | \\ \\ (_) | |_) | (_) | |_| |___| (_) | (_| | __/
|_| \\_\\___/|_.__/ \\___/ \\__|\\_____\\___/ \\__,_|\\___|
A CLI tool for Robot Framework.
"""
app.config.config_files = config_files
app.config.profiles = profiles
app.config.dry = dry
app.config.verbose = verbose
app.config.root = root
app.config.no_vcs = no_vcs
if color is None:
app.config.colored_output = ColoredOutput.AUTO
elif color:
app.config.colored_output = ColoredOutput.YES
else:
app.config.colored_output = ColoredOutput.NO
app.config.pager = pager
# AI-agent auto-detect: when running inside a known agent session
# (Claude Code, Copilot, OpenCode, …) demote colour and pager
# defaults so the agent's stdout capture stays clean. Explicit CLI
# flags and `NO_COLOR` / `FORCE_COLOR` keep their precedence.
if is_running_in_ai_agent():
if color is None and not os.environ.get("NO_COLOR") and not os.environ.get("FORCE_COLOR"):
app.config.colored_output = ColoredOutput.NO
if pager is None:
app.config.pager = False
app.config.output_format = format
app.config.launcher_script = launcher_script
app.config.default_paths = default_path
app.config.log_enabled = log
app.config.log_level = log_level
app.config.log_calls = log_calls
if log_config:
if log_calls:
LoggingDescriptor.set_call_tracing(True)
app.verbose(f"Loading logging configuration from '{log_config}'")
try:
with open(log_config, "r", encoding="utf-8") as f:
config = json.load(f)
logging.config.dictConfig(config)
except Exception as e:
app.error(f"Failed to load logging configuration from '{log_config}': {e}")
elif log:
if log_calls:
LoggingDescriptor.set_call_tracing(True)
logging.basicConfig(
level=log_level,
format=log_format,
style=log_style,
filename=log_filename,
)
try:
logging.root.handlers[0].formatter = RobotCodeFormatter(fmt=log_format, style=log_style)
except TypeError:
pass
if debugpy:
from robotcode.core.utils.debugpy import (
start_debugpy,
wait_for_debugpy_connected,
)
app.verbose(f"Try to start a debugpy session on port {debugpy_port}")
real_port = start_debugpy(debugpy_port, False)
if real_port is not None:
if real_port != debugpy_port:
app.verbose(f"Debugpy session started on port {real_port}")
if debugpy_wait_for_client:
app.verbose("Waiting for debugpy client to connect...")
wait_for_debugpy_connected()
app.verbose("Debugpy session started")
else:
app.verbose("Could not start debugpy session. Enable logging for more information.")
robotcode.add_command(config)
robotcode.add_command(profiles)
for c in PluginManager.instance().cli_commands:
robotcode.add_command(c)