Skip to content

Commit 009e5ac

Browse files
committed
refact(index) reuse code for index argparse,
add also --dry into `promnesia demo` sub-command.
1 parent ef468ad commit 009e5ac

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/promnesia/__main__.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def do_demo(
132132
params: Sequence[str],
133133
port: Optional[str],
134134
config_file: Optional[Path],
135+
dry: bool=False,
135136
name: str='demo',
136137
sources_subset: Iterable[Union[str, int]]=(),
137138
overwrite_db: bool=False,
@@ -151,7 +152,7 @@ def do_demo(
151152
)
152153
config.instance = cfg
153154

154-
errors = list(_do_index(sources_subset=sources_subset, overwrite_db=overwrite_db))
155+
errors = list(_do_index(dry=dry, sources_subset=sources_subset, overwrite_db=overwrite_db))
155156
if len(errors) > 0:
156157
logger.error('%d errors during indexing (see logs above for backtraces)', len(errors))
157158
for e in errors:
@@ -287,30 +288,33 @@ def _ordinal_or_name(s: str) -> Union[str, int]:
287288
def main() -> None:
288289
# TODO longer, literate description?
289290

291+
def add_index_args(parser: argparse.ArgumentParser) -> None:
292+
parser.add_argument('--config', type=Path, default=default_config_path(), help='Config path')
293+
parser.add_argument('--dry', action='store_true', help="Dry run, won't touch the database, only print the results out")
294+
parser.add_argument(
295+
'--sources',
296+
required=False,
297+
action="extend",
298+
nargs="+",
299+
type=_ordinal_or_name,
300+
metavar="SOURCE",
301+
help="Source names (or their 0-indexed position) to index.",
302+
)
303+
parser.add_argument(
304+
'--overwrite',
305+
required=False,
306+
action="store_true",
307+
help="Empty db before populating it with newly indexed visits."
308+
" If interrupted, db is left untouched."
309+
)
310+
290311
F = lambda prog: argparse.ArgumentDefaultsHelpFormatter(prog, width=120)
291312
p = argparse.ArgumentParser(formatter_class=F) # type: ignore
292313
subp = p.add_subparsers(dest='mode', )
293314
ep = subp.add_parser('index', help='Create/update the link database', formatter_class=F)
294-
ep.add_argument('--config', type=Path, default=default_config_path(), help='Config path')
295-
ep.add_argument('--dry', action='store_true', help="Dry run, won't touch the database, only print the results out")
315+
add_index_args(ep)
296316
# TODO use some way to override or provide config only via cmdline?
297317
ep.add_argument('--intermediate', required=False, help="Used for development, you don't need it")
298-
ep.add_argument(
299-
'--sources',
300-
required=False,
301-
action="extend",
302-
nargs="+",
303-
type=_ordinal_or_name,
304-
metavar="SOURCE",
305-
help="Source names (or their 0-indexed position) to index.",
306-
)
307-
ep.add_argument(
308-
'--overwrite',
309-
required=False,
310-
action="store_true",
311-
help="Empty db before populating it with newly indexed visits."
312-
" If interrupted, db is left untouched."
313-
)
314318

315319
sp = subp.add_parser('serve', help='Serve a link database', formatter_class=F) # type: ignore
316320
server.setup_parser(sp)
@@ -324,29 +328,13 @@ def main() -> None:
324328
ap.add_argument('--name', type=str, default='demo' , help='Set custom source name')
325329
add_port_arg(ap)
326330
ap.add_argument('--no-serve', action='store_const', const=None, dest='port', help='Pass to only index without running server')
327-
ap.add_argument('--config', type=Path, required=False , help='Config to run against. If omitted, will use empty base config')
328331
ap.add_argument(
329332
'--as',
330333
choices=list(sorted(demo_sources().keys())),
331334
default='guess',
332335
help='Promnesia source to index as (see https://github.com/karlicoss/promnesia/tree/master/src/promnesia/sources for the full list)',
333336
)
334-
ap.add_argument(
335-
'--sources',
336-
required=False,
337-
action="extend",
338-
nargs="+",
339-
type=_ordinal_or_name,
340-
metavar="SOURCE",
341-
help="Source names (or their 0-indexed position) to index.",
342-
)
343-
ap.add_argument(
344-
'--overwrite',
345-
required=False,
346-
action="store_true",
347-
help="Empty db before populating it with newly indexed visits."
348-
" If interrupted, db is left untouched."
349-
)
337+
add_index_args(ap)
350338
ap.add_argument('params', nargs='*', help='Optional extra params for the indexer')
351339

352340
isp = subp.add_parser('install-server', help='Install server as a systemd service (for autostart)', formatter_class=F)
@@ -404,6 +392,7 @@ def main() -> None:
404392
params=args.params,
405393
port=args.port,
406394
config_file=args.config,
395+
dry=args.dry,
407396
name=args.name,
408397
sources_subset=args.sources,
409398
overwrite_db=args.overwrite,

0 commit comments

Comments
 (0)