2
2
import json
3
3
import logging
4
4
import os
5
+ import re
5
6
import click
6
7
from click_loglevel import LogLevel
7
8
from ghtoken import get_ghtoken
12
13
from .util import commit , runcmd
13
14
14
15
15
- def set_mode (
16
- ctx : click .Context , _param : click .Parameter , value : RepoHost | None
17
- ) -> RepoHost | None :
18
- if value is not None :
19
- ctx .params .setdefault ("mode" , set ()).add (value )
20
- return value
16
+ class RepoHostSet (click .ParamType ):
17
+ name = "hostset"
18
+
19
+ def convert (
20
+ self ,
21
+ value : str | set [RepoHost ],
22
+ param : click .Parameter | None ,
23
+ ctx : click .Context | None ,
24
+ ) -> set [RepoHost ]:
25
+ if not isinstance (value , str ):
26
+ return value
27
+ selected = set ()
28
+ for v in re .split (r"\s*,\s*" , value ):
29
+ try :
30
+ selected .add (RepoHost (v ))
31
+ except ValueError :
32
+ self .fail (f"{ value !r} : invalid item { v !r} " , param , ctx )
33
+ return selected
34
+
35
+ def get_metavar (self , _param : click .Parameter ) -> str :
36
+ return "[all," + "," .join (v .value for v in RepoHost ) + "]"
21
37
22
38
23
39
@click .command ()
40
+ @click .option (
41
+ "--hosts" ,
42
+ type = RepoHostSet (),
43
+ default = "all" ,
44
+ help = "Set which repository hosts to query" ,
45
+ )
24
46
@click .option (
25
47
"-l" ,
26
48
"--log-level" ,
27
49
type = LogLevel (),
28
50
default = logging .INFO ,
29
51
help = "Set logging level [default: INFO]" ,
30
52
)
31
- @click .option (
32
- "--atris" ,
33
- flag_value = RepoHost .ATRIS ,
34
- type = click .UNPROCESSED ,
35
- callback = set_mode ,
36
- expose_value = False ,
37
- help = "Update ATRIS data" ,
38
- )
39
- @click .option (
40
- "--gin" ,
41
- flag_value = RepoHost .GIN ,
42
- type = click .UNPROCESSED ,
43
- callback = set_mode ,
44
- expose_value = False ,
45
- help = "Update GIN data" ,
46
- )
47
- @click .option (
48
- "--github" ,
49
- flag_value = RepoHost .GITHUB ,
50
- type = click .UNPROCESSED ,
51
- callback = set_mode ,
52
- expose_value = False ,
53
- help = "Update GitHub data" ,
54
- )
55
- @click .option (
56
- "--hub-datalad-org" ,
57
- flag_value = RepoHost .HUB_DATALAD_ORG ,
58
- type = click .UNPROCESSED ,
59
- callback = set_mode ,
60
- expose_value = False ,
61
- help = "Update hub.datalad.org data" ,
62
- )
63
- @click .option (
64
- "--osf" ,
65
- flag_value = RepoHost .OSF ,
66
- type = click .UNPROCESSED ,
67
- callback = set_mode ,
68
- expose_value = False ,
69
- help = "Update OSF data" ,
70
- )
71
53
@click .option (
72
54
"-R" ,
73
55
"--regen-readme" ,
74
56
is_flag = True ,
75
57
help = "Regenerate the README from the JSON file without querying" ,
76
58
)
77
- def main (log_level : int , regen_readme : bool , mode : set [RepoHost ] | None = None ) -> None :
78
- if regen_readme and mode :
79
- raise click .UsageError ("--regen-readme is mutually exclusive with mode options" )
80
-
59
+ def main (log_level : int , regen_readme : bool , hosts : set [RepoHost ]) -> None :
81
60
logging .basicConfig (
82
61
format = "%(asctime)s [%(levelname)-8s] %(name)s %(message)s" ,
83
62
datefmt = "%Y-%m-%dT%H:%M:%S%z" ,
@@ -91,20 +70,18 @@ def main(log_level: int, regen_readme: bool, mode: set[RepoHost] | None = None)
91
70
record = RepoRecord ()
92
71
93
72
reports : list [str ] = []
94
- if mode is None :
95
- mode = set (RepoHost )
96
73
if not regen_readme :
97
- if RepoHost .GITHUB in mode :
74
+ if RepoHost .GITHUB in hosts :
98
75
reports .extend (record .update_github (get_ghtoken ()))
99
- if RepoHost .OSF in mode :
76
+ if RepoHost .OSF in hosts :
100
77
reports .extend (record .update_osf ())
101
- if RepoHost .GIN in mode :
78
+ if RepoHost .GIN in hosts :
102
79
reports .extend (record .update_gin (os .environ ["GIN_TOKEN" ]))
103
- if RepoHost .HUB_DATALAD_ORG in mode :
80
+ if RepoHost .HUB_DATALAD_ORG in hosts :
104
81
reports .extend (
105
82
record .update_hub_datalad_org (os .environ ["HUB_DATALAD_ORG_TOKEN" ])
106
83
)
107
- if RepoHost .ATRIS in mode :
84
+ if RepoHost .ATRIS in hosts :
108
85
reports .extend (record .update_atris ())
109
86
with open (RECORD_FILE , "w" ) as fp :
110
87
print (record .model_dump_json (indent = 4 ), file = fp )
0 commit comments