Skip to content

Commit

Permalink
feat: add -c/--config flag for custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Nov 30, 2024
1 parent 22e7dc9 commit e7d356c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
17 changes: 13 additions & 4 deletions dooit/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
import click
from pathlib import Path
from platformdirs import user_data_dir, user_config_dir
Expand All @@ -6,10 +7,14 @@
VERSION = "3.1.0"


def run_dooit():
def run_dooit(config: Optional[Path] = None):
if config and not (config.exists() and config.is_file()):
print(f"Config file {config} not found.")
return

from dooit.ui.tui import Dooit

Dooit().run()
Dooit(config=config).run()


@click.group(
Expand All @@ -22,8 +27,9 @@ def run_dooit():
is_flag=True,
help="Show version and exit.",
)
@click.option("-c", "--config", default=None, help="Path to config file.")
@click.pass_context
def main(ctx, version: bool) -> None:
def main(ctx, version: bool, config: str) -> None:
if version:
return print(f"dooit - {VERSION}")

Expand All @@ -37,7 +43,10 @@ def main(ctx, version: bool) -> None:
)
return

run_dooit()
if config:
run_dooit(Path(config).expanduser())
else:
run_dooit()


@main.command(help="Migrate data from v2 to v3.")
Expand Down
10 changes: 7 additions & 3 deletions dooit/ui/api/dooit_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING
from pathlib import Path
from typing import TYPE_CHECKING, Optional
from dooit.ui.api.events import BarNotification, NotificationType
from dooit.ui.api.plug import PluginManager
from .events import DooitEvent, SwitchTab, _QuitApp
Expand All @@ -21,9 +22,12 @@


class DooitAPI:
def __init__(self, app: "Dooit") -> None:
def __init__(
self,
app: "Dooit",
) -> None:
self.app = app
self.plugin_manager = PluginManager(self)
self.plugin_manager = PluginManager(self, app.config)
self.css = CssManager()
self.keys = KeyManager(self.app.get_dooit_mode)
self.layouts = LayoutManager(self.app)
Expand Down
9 changes: 5 additions & 4 deletions dooit/ui/api/plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import partial
from collections import defaultdict
from pathlib import Path
from typing import TYPE_CHECKING, Callable, List, Type
from typing import TYPE_CHECKING, Callable, List, Optional, Type
from platformdirs import user_config_dir
from textual.css.query import NoMatches

Expand All @@ -21,7 +21,7 @@
BASE_PATH = Path(__file__).parent.parent.parent

MAIN_FOLDER = "dooit"
CONFIG_FOLDER = Path(user_config_dir(MAIN_FOLDER))
CONFIG_FILE = Path(user_config_dir(MAIN_FOLDER)) / "config.py"
DEFAULT_CONFIG = BASE_PATH / "utils" / "default_config.py"


Expand All @@ -30,7 +30,8 @@ def is_running_under_pytest() -> bool:


class PluginManager:
def __init__(self, api: "DooitAPI") -> None:
def __init__(self, api: "DooitAPI", config: Optional[Path] = None) -> None:
self.config = config or CONFIG_FILE
self.events: defaultdict[Type[DooitEvent], List[Callable]] = defaultdict(list)
self.timers: defaultdict[float, List[Callable]] = defaultdict(list)
self.api = api
Expand All @@ -41,7 +42,7 @@ def scan(self):
if is_running_under_pytest():
return

load_file(self, CONFIG_FOLDER / "config.py")
load_file(self, self.config)

def _update_dooit_value(self, obj, *params):
res = obj(self.api, *params)
Expand Down
8 changes: 7 additions & 1 deletion dooit/ui/tui.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import Optional
from textual import on
from textual.app import App
Expand Down Expand Up @@ -32,9 +33,14 @@ class Dooit(App):
Binding("ctrl+c", "quit", "Quit", show=False, priority=True),
]

def __init__(self, connection_string: Optional[str] = None):
def __init__(
self,
connection_string: Optional[str] = None,
config: Optional[Path] = None,
):
super().__init__(watch_css=True)
self.dooit_mode: ModeType = "NORMAL"
self.config = config
manager.connect(connection_string)

async def base_setup(self):
Expand Down

0 comments on commit e7d356c

Please sign in to comment.