summaryrefslogtreecommitdiffstats
path: root/cli/openbb_cli/controllers/settings_controller.py
blob: f01742f6cb203d21daf2522e37a483921b2bd636 (plain)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Settings Controller Module."""

import argparse
from typing import List, Optional

from openbb_cli.config.constants import AVAILABLE_FLAIRS
from openbb_cli.config.menu_text import MenuText

# pylint: disable=too-many-lines,no-member,too-many-public-methods,C0302
# pylint:disable=import-outside-toplevel
from openbb_cli.controllers.base_controller import BaseController
from openbb_cli.controllers.utils import all_timezones, is_timezone_valid
from openbb_cli.session import Session

session = Session()


class SettingsController(BaseController):
    """Settings Controller class."""

    CHOICES_COMMANDS: List[str] = [
        "tab",
        "interactive",
        "cls",
        "watermark",
        "promptkit",
        "thoughts",
        "reporthtml",
        "exithelp",
        "rcontext",
        "richpanel",
        "tbhint",
        "overwrite",
        "version",
        "console_style",
        "flair",
        "timezone",
        "n_rows",
        "n_cols",
        "obbject_msg",
        "obbject_res",
        "obbject_display",
    ]
    PATH = "/settings/"
    CHOICES_GENERATION = True

    def __init__(self, queue: Optional[List[str]] = None):
        """Initialize the Constructor."""
        super().__init__(queue)

        self.update_completer(self.choices_default)

    def print_help(self):
        """Print help."""
        settings = session.settings

        mt = MenuText("settings/")
        mt.add_info("Feature flags")
        mt.add_setting(
            "interactive",
            settings.USE_INTERACTIVE_DF,
            description="open dataframes in interactive window",
        )
        mt.add_setting(
            "cls",
            settings.USE_CLEAR_AFTER_CMD,
            description="clear console after each command",
        )
        mt.add_setting(
            "promptkit",
            settings.USE_PROMPT_TOOLKIT,
            description="enable prompt toolkit (autocomplete and history)",
        )
        mt.add_setting(
            "exithelp",
            settings.ENABLE_EXIT_AUTO_HELP,
            description="automatically print help when quitting menu",
        )
        mt.add_setting(
            "rcontext",
            settings.REMEMBER_CONTEXTS,
            description="remember contexts between menus",
        )
        mt.add_setting(
            "richpanel",
            settings.ENABLE_RICH_PANEL,
            description="colorful rich CLI panel",
        )
        mt.add_setting(
            "tbhint",
            settings.TOOLBAR_HINT,
            description="displays usage hints in the bottom toolbar",
        )
        mt.add_setting(
            "overwrite",
            settings.FILE_OVERWRITE,
            description="whether to overwrite Excel files if they already exists",
        )
        mt.add_setting(
            "version",
            settings.SHOW_VERSION,
            description="whether to show the version in the bottom right corner",
        )
        mt.add_setting(
            "obbject_msg",
            settings.SHOW_MSG_OBBJECT_REGISTRY,
            description="show obbject registry message after a new result is added",
        )
        mt.add_raw("\n")
        mt.add_info("Preferences")
        mt.add_cmd("console_style", description="apply a custom rich style to the CLI")
        mt.add_cmd("flair", description="choose flair icon")
        mt.add_cmd("timezone", description="pick timezone")
        mt.add_cmd(
            "n_rows", description="number of rows to show on non interactive tables"
        )
        mt.add_cmd(
            "n_cols", description="number of columns to show on non interactive tables"
        )
        mt.add_cmd(
            "obbject_res",
            description="define the maximum number of obbjects allowed in the registry",
        )
        mt.add_cmd(
            "obbject_display",
            description="define the maximum number of cached results to display on the help menu",
        )

        session.console.print(text=mt.menu_text, menu="Settings")

    def call_overwrite(self, _):
        """Process overwrite command."""
        session.settings.set_item("FILE_OVERWRITE", not session.settings.FILE_OVERWRITE)

    def call_version(self, _):
        """Process version command."""
        session.settings.SHOW_VERSION = not session.settings.SHOW_VERSION

    def call_interactive(self, _):
        """Process interactive command."""
        session.settings.set_item(
            "USE_INTERACTIVE_DF", not session.settings.USE_INTERACTIVE_DF
        )

    def call_cls(self, _):
        """Process cls command."""
        session.settings.set_item(
            "USE_CLEAR_AFTER_CMD", not session.settings.USE_CLEAR_AFTER_CMD
        )

    def call_promptkit(self, _):
        """Process promptkit command."""
        session.settings.set_item(
            "USE_PROMPT_TOOLKIT", not session.settings.USE_PROMPT_TOOLKIT
        )

    def call_exithelp(self, _):
        """Process exithelp command."""
        session.settings.set_item(
            "ENABLE_EXIT_AUTO_HELP", not session.settings.ENABLE_EXIT_AUTO_HELP
        )

    def call_rcontext(self, _):
        """Process rcontext command."""
        session.settings.set_item(
            "REMEMBER_CONTEXTS", not session.settings.REMEMBER_CONTEXTS
        )

    def call_dt(self, _):
        """Process dt command."""
        session.settings.set_item("USE_DATETIME", not session.settings.USE_DATETIME)

    def call_richpanel(self, _):
        """Process richpanel command."""
        session.settings.set_item(
            "ENABLE_RICH_PANEL", not session.settings.ENABLE_RICH_PANEL
        )

    def call_tbhint(self, _):
        """Process tbhint command."""
        if session.settings.TOOLBAR_HINT:
            session.console.print("Will take effect when running CLI again.")
        session.settings.set_item("TOOLBAR_HINT", not session.settings.TOOLBAR_HINT)

    def call_obbject_msg(self, _):
        """Process obbject_msg command."""
        session.settings.set_item(
            "SHOW_MSG_OBBJECT_REGISTRY",
            not session.settings.SHOW_MSG_OBBJECT_REGISTRY,
        )

    def call_console_style(self, other_args: List[str]) -> None:
        """Process cosole_style command."""
        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="console_style",
            description="Change your custom console style.",
            add_help=False,
        )
        parser.add_argument(
            "-s",
            "--style",
            dest="style",
            action="store",
            required=False,
            choices=session.style.available_styles,
        )
        ns_parser = self.parse_simple_args(parser, other_args)

        if ns_parser and ns_parser.style:
            session.style.apply(ns_parser.style)
            session.settings.set_item("RICH_STYLE", ns_parser.style)
        elif not other_args:
            session.console.print(
                f"Current console style: {session.settings.RICH_STYLE}"
            )

    def call_flair(self, other_args: List[str]) -> None:
        """Process flair command."""
        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="flair",
            description="Change your custom flair.",
            add_help=False,
        )
        parser.add_argument(
            "-f",
            "--flair",
            dest="flair",
            action="store",
            required=False,
            choices=list(AVAILABLE_FLAIRS.keys()),
        )
        ns_parser = self.parse_simple_args(parser, other_args)

        if ns_parser and ns_parser.flair:
            session.settings.set_item("FLAIR", ns_parser.flair)
        elif not other_args:
            session.console.print(f"Current flair: {session.settings.FLAIR}")

    def call_timezone(self, other_args: List[str]) -> None:
        """Process timezone command."""
        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            prog="timezone",
            description="Change your custom timezone.",
            add_help=False,
        )
        parser.add_argument(
            "-t",
            "--timezone",
            dest="timezone",
            action="store",
            required=False,
            type=str,
            choices=all_timezones,
        )
        ns_parser = self.parse_simple_args(parser, other_args)

        if ns_parser and ns_parser.timezone:
            if is_timezone_valid(ns_parser.timezone):
                session.settings.set_item("TIMEZONE", ns_parser.timezone)
            else:
                session.console.print(
                    "Invalid timezone. Please enter a valid timezone."
                )
                session.console.print(
                    f"Available timezones are: {', '.join(all_timezones)}"
                )
        elif not other_args:
            session.console.print(f"Current timezone: {session.settings.TIMEZONE}")

    def call_n_rows(self, other_args: List[str]) -> None:
        """Process n_rows command."""
        parser = argparse.ArgumentParser</