Skip to content

Commit 205dc1c

Browse files
committed
Cut down on magic strings
1 parent 6c0f541 commit 205dc1c

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/find_datalad_repos/__main__.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from click_loglevel import LogLevel
77
from ghtoken import get_ghtoken
88
from .config import README_FOLDER, RECORD_FILE
9+
from .core import RepoHost
910
from .readmes import mkreadmes
1011
from .record import RepoRecord
1112
from .util import commit, runcmd
1213

1314

1415
def set_mode(
15-
ctx: click.Context, _param: click.Parameter, value: str | None
16-
) -> str | None:
16+
ctx: click.Context, _param: click.Parameter, value: RepoHost | None
17+
) -> RepoHost | None:
1718
if value is not None:
1819
ctx.params.setdefault("mode", set()).add(value)
1920
return value
@@ -29,28 +30,32 @@ def set_mode(
2930
)
3031
@click.option(
3132
"--gin",
32-
flag_value="gin",
33+
flag_value=RepoHost.GIN,
34+
type=click.UNPROCESSED,
3335
callback=set_mode,
3436
expose_value=False,
3537
help="Update GIN data",
3638
)
3739
@click.option(
3840
"--github",
39-
flag_value="github",
41+
flag_value=RepoHost.GITHUB,
42+
type=click.UNPROCESSED,
4043
callback=set_mode,
4144
expose_value=False,
4245
help="Update GitHub data",
4346
)
4447
@click.option(
4548
"--hub-datalad-org",
46-
flag_value="hub.datalad.org",
49+
flag_value=RepoHost.HUB_DATALAD_ORG,
50+
type=click.UNPROCESSED,
4751
callback=set_mode,
4852
expose_value=False,
4953
help="Update hub.datalad.org data",
5054
)
5155
@click.option(
5256
"--osf",
53-
flag_value="osf",
57+
flag_value=RepoHost.OSF,
58+
type=click.UNPROCESSED,
5459
callback=set_mode,
5560
expose_value=False,
5661
help="Update OSF data",
@@ -61,7 +66,7 @@ def set_mode(
6166
is_flag=True,
6267
help="Regenerate the README from the JSON file without querying",
6368
)
64-
def main(log_level: int, regen_readme: bool, mode: set[str] | None = None) -> None:
69+
def main(log_level: int, regen_readme: bool, mode: set[RepoHost] | None = None) -> None:
6570
if regen_readme and mode:
6671
raise click.UsageError("--regen-readme is mutually exclusive with mode options")
6772

@@ -78,14 +83,16 @@ def main(log_level: int, regen_readme: bool, mode: set[str] | None = None) -> No
7883
record = RepoRecord()
7984

8085
reports: list[str] = []
86+
if mode is None:
87+
mode = set(RepoHost)
8188
if not regen_readme:
82-
if mode is None or "github" in mode:
89+
if RepoHost.GITHUB in mode:
8390
reports.extend(record.update_github(get_ghtoken()))
84-
if mode is None or "osf" in mode:
91+
if RepoHost.OSF in mode:
8592
reports.extend(record.update_osf())
86-
if mode is None or "gin" in mode:
93+
if RepoHost.GIN in mode:
8794
reports.extend(record.update_gin(os.environ["GIN_TOKEN"]))
88-
if mode is None or "hub.datalad.org" in mode:
95+
if RepoHost.HUB_DATALAD_ORG in mode:
8996
reports.extend(
9097
record.update_hub_datalad_org(os.environ["HUB_DATALAD_ORG_TOKEN"])
9198
)

src/find_datalad_repos/core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
from abc import ABC, abstractmethod
33
from collections.abc import Iterable
4+
from enum import Enum
45
from types import TracebackType
56
from typing import Any, Generic, Self, TypeVar
67

@@ -12,6 +13,13 @@
1213
S = TypeVar("S", bound="Searcher")
1314

1415

16+
class RepoHost(Enum):
17+
GITHUB = 1
18+
GIN = 2
19+
OSF = 3
20+
HUB_DATALAD_ORG = 4
21+
22+
1523
class Updater(ABC, Generic[T, U, S]):
1624
@classmethod
1725
@abstractmethod

0 commit comments

Comments
 (0)