rbx.cp 0.5.16__py3-none-any.whl → 0.5.18__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.
@@ -0,0 +1,132 @@
1
+ import functools
2
+ import importlib
3
+ import importlib.resources
4
+ import pathlib
5
+ import shlex
6
+ import sys
7
+ from typing import Dict
8
+
9
+ import typer
10
+ from pydantic import BaseModel, Field
11
+
12
+ from rbx import config, console, utils
13
+
14
+ app = typer.Typer(no_args_is_help=True)
15
+
16
+ _CONFIG_FILE_NAME = 'default_setter_config.yml'
17
+ _CONFIG_FILE_NAME_MAC = 'default_setter_config.mac.yml'
18
+
19
+
20
+ class SanitizersConfig(BaseModel):
21
+ enabled: bool = Field(
22
+ False,
23
+ description='Whether to use sanitizers when running solutions.',
24
+ )
25
+
26
+ command_substitutions: Dict[str, str] = Field(
27
+ {},
28
+ description='Substitutions to apply to commands before running them with sanitizers.',
29
+ )
30
+
31
+
32
+ class WarningsConfig(BaseModel):
33
+ enabled: bool = Field(
34
+ False,
35
+ description='Whether to use warning flags when running solutions.',
36
+ )
37
+
38
+
39
+ class SetterConfig(BaseModel):
40
+ sanitizers: SanitizersConfig = Field(
41
+ default_factory=SanitizersConfig, # type: ignore
42
+ description='Configuration for sanitizers.',
43
+ )
44
+ warnings: WarningsConfig = Field(
45
+ default_factory=WarningsConfig, # type: ignore
46
+ description='Configuration for warnings.',
47
+ )
48
+
49
+ command_substitutions: Dict[str, str] = Field(
50
+ {},
51
+ description='Substitutions to apply to commands before running them.',
52
+ )
53
+
54
+ def substitute_command(self, command: str, sanitized: bool = False) -> str:
55
+ exe = shlex.split(command)[0]
56
+ if sanitized and exe in self.sanitizers.command_substitutions:
57
+ exe = self.sanitizers.command_substitutions[exe]
58
+ return ' '.join([exe, *shlex.split(command)[1:]])
59
+ if exe in self.command_substitutions:
60
+ exe = self.command_substitutions[exe]
61
+ return ' '.join([exe, *shlex.split(command)[1:]])
62
+ return command
63
+
64
+
65
+ def get_default_setter_config_path() -> pathlib.Path:
66
+ cfg_name = _CONFIG_FILE_NAME
67
+ if sys.platform == 'darwin':
68
+ cfg_name = _CONFIG_FILE_NAME_MAC
69
+
70
+ with importlib.resources.as_file(
71
+ importlib.resources.files('rbx') / 'resources' / cfg_name
72
+ ) as file:
73
+ return file
74
+
75
+
76
+ def get_default_setter_config() -> SetterConfig:
77
+ return utils.model_from_yaml(
78
+ SetterConfig, get_default_setter_config_path().read_text()
79
+ )
80
+
81
+
82
+ def get_setter_config_path() -> pathlib.Path:
83
+ return config.get_app_path() / 'setter_config.yml'
84
+
85
+
86
+ @functools.cache
87
+ def get_setter_config() -> SetterConfig:
88
+ config_path = get_setter_config_path()
89
+ if not config_path.is_file():
90
+ utils.create_and_write(
91
+ config_path, get_default_setter_config_path().read_text()
92
+ )
93
+ return utils.model_from_yaml(SetterConfig, config_path.read_text())
94
+
95
+
96
+ def save_setter_config(config: SetterConfig):
97
+ config_path = get_setter_config_path()
98
+ config_path.write_text(utils.model_to_yaml(config))
99
+ get_setter_config.cache_clear()
100
+
101
+
102
+ @app.command(help='Show the path to the setter config.')
103
+ def path():
104
+ print(get_setter_config_path())
105
+
106
+
107
+ @app.command('list, ls')
108
+ def list():
109
+ """
110
+ Pretty print the config file.
111
+ """
112
+ console.console.print_json(utils.model_json(get_setter_config()))
113
+
114
+
115
+ @app.command(help='Open the setter config in an editor.')
116
+ def edit():
117
+ # Ensure config is created before calling the editor.
118
+ get_setter_config()
119
+
120
+ config.open_editor(get_setter_config_path())
121
+
122
+
123
+ @app.command()
124
+ def reset():
125
+ """
126
+ Reset the config file to the default one.
127
+ """
128
+ if not typer.confirm('Do you really want to reset your config to the default one?'):
129
+ return
130
+ cfg_path = get_setter_config_path()
131
+ cfg_path.unlink(missing_ok=True)
132
+ get_setter_config() # Reset the config.