|
1 | 1 | import asyncio |
2 | 2 | import os |
3 | 3 | from dataclasses import dataclass, field |
4 | | -from typing import Any |
| 4 | +from typing import Any, Self |
5 | 5 |
|
6 | 6 | import click |
7 | 7 | import dotenv |
@@ -29,12 +29,14 @@ def qf() -> None: |
29 | 29 |
|
30 | 30 | @qf.command() |
31 | 31 | @click.argument("symbol") |
32 | | -def profile(symbol: str) -> None: |
| 32 | +@click.pass_context |
| 33 | +def profile(ctx: click.Context, symbol: str) -> None: |
33 | 34 | """Company profile""" |
| 35 | + app = QfApp.from_context(ctx) |
34 | 36 | data = asyncio.run(get_profile(symbol))[0] |
35 | | - main.print(data.pop("description")) |
| 37 | + app.print(data.pop("description")) |
36 | 38 | df = pd.DataFrame(data.items(), columns=["Key", "Value"]) |
37 | | - main.print(df_to_rich(df)) |
| 39 | + app.print(df_to_rich(df)) |
38 | 40 |
|
39 | 41 |
|
40 | 42 | @qf.command() |
@@ -80,9 +82,13 @@ async def get_profile(symbol: str) -> list[dict]: |
80 | 82 |
|
81 | 83 |
|
82 | 84 | @dataclass |
83 | | -class App: |
| 85 | +class QfApp: |
84 | 86 | console: Console = field(default_factory=Console) |
85 | 87 |
|
| 88 | + @classmethod |
| 89 | + def from_context(cls, ctx: click.Context) -> Self: |
| 90 | + return ctx.obj # type: ignore |
| 91 | + |
86 | 92 | def __call__(self) -> None: |
87 | 93 | os.makedirs(settings.SETTINGS_DIRECTORY, exist_ok=True) |
88 | 94 | history = FileHistory(str(settings.HIST_FILE_PATH)) |
@@ -116,12 +122,12 @@ def handle_command(self, text: str) -> None: |
116 | 122 | if not text: |
117 | 123 | return |
118 | 124 | elif text == "help": |
119 | | - return qf.main(["--help"], standalone_mode=False) |
| 125 | + return qf.main(["--help"], standalone_mode=False, obj=self) |
120 | 126 | elif text == "exit": |
121 | 127 | raise click.Abort() |
122 | 128 |
|
123 | 129 | try: |
124 | | - qf.main(text.split(), standalone_mode=False) |
| 130 | + qf.main(text.split(), standalone_mode=False, obj=self) |
125 | 131 | except click.exceptions.MissingParameter as e: |
126 | 132 | self.error(e) |
127 | 133 | except click.exceptions.NoSuchOption as e: |
|
0 commit comments