Skip to content

Commit 8330338

Browse files
committed
Command line tools optional
1 parent da0e8f8 commit 8330338

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dist
2727
.venv
2828
.mypy_cache
2929
.pytest_cache
30+
.ruff_cache
3031
.python-version
3132

3233
# Jupyter

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ sympy = "^1.12"
5050
ipywidgets = "^8.0.7"
5151

5252
[tool.poetry.scripts]
53-
qf = "quantflow.cli:main"
53+
qf = "quantflow.cli.script:main"
5454

5555
[build-system]
5656
requires = ["poetry-core>=1.0.0"]

quantflow/cli/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
try:
2-
from .app import App
3-
except ImportError:
4-
raise ImportError(
5-
"Cannot run qf command line, " "quantflow needs to be installed with cli extras"
6-
) from None
7-
8-
main = App()

quantflow/cli/app.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import os
33
from dataclasses import dataclass, field
4-
from typing import Any
4+
from typing import Any, Self
55

66
import click
77
import dotenv
@@ -29,12 +29,14 @@ def qf() -> None:
2929

3030
@qf.command()
3131
@click.argument("symbol")
32-
def profile(symbol: str) -> None:
32+
@click.pass_context
33+
def profile(ctx: click.Context, symbol: str) -> None:
3334
"""Company profile"""
35+
app = QfApp.from_context(ctx)
3436
data = asyncio.run(get_profile(symbol))[0]
35-
main.print(data.pop("description"))
37+
app.print(data.pop("description"))
3638
df = pd.DataFrame(data.items(), columns=["Key", "Value"])
37-
main.print(df_to_rich(df))
39+
app.print(df_to_rich(df))
3840

3941

4042
@qf.command()
@@ -80,9 +82,13 @@ async def get_profile(symbol: str) -> list[dict]:
8082

8183

8284
@dataclass
83-
class App:
85+
class QfApp:
8486
console: Console = field(default_factory=Console)
8587

88+
@classmethod
89+
def from_context(cls, ctx: click.Context) -> Self:
90+
return ctx.obj # type: ignore
91+
8692
def __call__(self) -> None:
8793
os.makedirs(settings.SETTINGS_DIRECTORY, exist_ok=True)
8894
history = FileHistory(str(settings.HIST_FILE_PATH))
@@ -116,12 +122,12 @@ def handle_command(self, text: str) -> None:
116122
if not text:
117123
return
118124
elif text == "help":
119-
return qf.main(["--help"], standalone_mode=False)
125+
return qf.main(["--help"], standalone_mode=False, obj=self)
120126
elif text == "exit":
121127
raise click.Abort()
122128

123129
try:
124-
qf.main(text.split(), standalone_mode=False)
130+
qf.main(text.split(), standalone_mode=False, obj=self)
125131
except click.exceptions.MissingParameter as e:
126132
self.error(e)
127133
except click.exceptions.NoSuchOption as e:

quantflow/cli/script.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
try:
2+
from .app import QfApp
3+
except ImportError:
4+
raise ImportError(
5+
"Cannot run qf command line, "
6+
"quantflow needs to be installed with cli & data extras, "
7+
"pip install quantflow[cli, data]"
8+
) from None
9+
10+
main = QfApp()

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ pip install quantflow
2828

2929
## Command line tools
3030

31-
When installing with the extra `data` dependencies, it is possible to use the command line tool `qf`
31+
The command line tools are available when installing with the extra `cli` and `data` dependencies.
3232

33+
```bash
34+
pip install quantflow[cli,data]
35+
```
36+
37+
It is possible to use the command line tool `qf` to download data and run pricing and calibration scripts.

0 commit comments

Comments
 (0)