minor refactoring

This commit is contained in:
ducklet 2022-08-19 22:14:29 +02:00
parent 6b65efd8c7
commit af1236bc7e

View file

@ -3,6 +3,7 @@ import logging
import os import os
import stat import stat
import sys import sys
from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from . import config, metadex, utils from . import config, metadex, utils
@ -10,6 +11,18 @@ from . import config, metadex, utils
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
_commands = {}
def command(name):
def wrapper(f):
assert name not in _commands
_commands[name] = f
return f
return wrapper
def getargs(): def getargs():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.set_defaults(mode=None) parser.set_defaults(mode=None)
@ -41,114 +54,116 @@ def getargs():
subparsers = parser.add_subparsers(title="commands") subparsers = parser.add_subparsers(title="commands")
@contextmanager
def command_parser(name, **kwargs):
assert name in _commands
subparser = subparsers.add_parser(name, **kwargs)
subparser.set_defaults(mode=name)
yield subparser
# Command: scan # Command: scan
parser_scan = subparsers.add_parser("scan", help="scan a local file system") with command_parser("scan", help="scan a local file system") as subparser:
parser_scan.set_defaults(mode="scan")
parser_scan.add_argument( subparser.add_argument(
"basedir", "basedir",
type=Path, type=Path,
nargs="+", nargs="+",
help="index all files from this dir", help="index all files from this dir",
) )
parser_scan.add_argument( subparser.add_argument(
"--no-remove-missing", "--no-remove-missing",
dest="remove_missing", dest="remove_missing",
action="store_false", action="store_false",
help="do not remove files from the database which cannot be found in the file system", help="do not remove files from the database which cannot be found in the file system",
) )
parser_scan.add_argument( subparser.add_argument(
"--map-mount", "--map-mount",
nargs="*", nargs="*",
default=[], default=[],
type=str, type=str,
help="map a source host:path to any other destination during scanning for files\nExample: src=/mnt/foo,dest=foo:", help="map a source host:path to any other destination during scanning for files\nExample: src=/mnt/foo,dest=foo:",
) )
# Command: ingest-ls # Command: ingest-ls
parser_ingest_ls = subparsers.add_parser( with command_parser(
"ingest-ls", "ingest-ls",
help="ingest extra data", help="ingest extra data",
description="When ingesting data from an external source, the hostname will not be set automatically.", description="When ingesting data from an external source, the hostname will not be set automatically.",
) ) as subparser:
parser_ingest_ls.set_defaults(mode="ingest-ls")
parser_ingest_ls.add_argument( subparser.add_argument(
"infile", "infile",
nargs="?", nargs="?",
type=argparse.FileType(), type=argparse.FileType(),
default=sys.stdin, default=sys.stdin,
help="output from `ls -lR`", help="output from `ls -lR`",
) )
parser_ingest_ls.add_argument( subparser.add_argument(
"--remove-missing", "--remove-missing",
action="store_true", action="store_true",
help="Remove files not listed in the infile.", help="Remove files not listed in the infile.",
) )
parser_ingest_ls.add_argument( subparser.add_argument(
"--ref-year", "--ref-year",
type=int, type=int,
help="The year when 'ls -l' was run, to resolve relative dates.", help="The year when 'ls -l' was run, to resolve relative dates.",
) )
# Command: ingest-db # Command: ingest-db
parser_ingest_db = subparsers.add_parser("ingest-db") with command_parser("ingest-db") as subparser:
parser_ingest_db.set_defaults(mode="ingest-db")
parser_ingest_db.add_argument( subparser.add_argument(
"infile", "infile",
type=Path, type=Path,
help="a Metadex SQLite DB", help="a Metadex SQLite DB",
) )
parser_ingest_db.add_argument( subparser.add_argument(
"--map-mount", "--map-mount",
nargs="*", nargs="*",
default=[], default=[],
type=str, type=str,
help="map a source host:path to any other destination while importing", help="map a source host:path to any other destination while importing",
) )
# Command: rm # Command: rm
parser_rm = subparsers.add_parser("rm") with command_parser("rm") as subparser:
parser_rm.set_defaults(mode="rm") subparser.add_argument(
parser_rm.add_argument( "files",
"files", type=str,
type=str, nargs="+",
nargs="+", help="files to remove",
help="files to remove", )
) subparser.add_argument(
parser_rm.add_argument( "-r",
"-r", action="store_true",
action="store_true", dest="include_subfiles",
dest="include_subfiles", help="include sub-files",
help="include sub-files", )
)
# Command: ls # Command: ls
parser_ls = subparsers.add_parser("ls") with command_parser("ls") as subparser:
parser_ls.set_defaults(mode="ls") subparser.add_argument(
parser_ls.add_argument( "file",
"file", type=str,
type=str, nargs="*",
nargs="*", help="look up a file",
help="look up a file", )
) subparser.add_argument(
parser_ls.add_argument( "--type",
"--type", "-t",
"-t", choices="dfl",
choices="dfl", help="Filter searches to (d)irectories, plain (f)iles, or sym(l)inks.",
help="Filter searches to (d)irectories, plain (f)iles, or sym(l)inks.", )
) subparser.add_argument("--format", type=str, default="{date}\t{size}\t{path}")
parser_ls.add_argument("--format", type=str, default="{date}\t{size}\t{path}") subparser.add_argument(
parser_ls.add_argument( "--match", choices=("regex", "glob", "fuzzy"), default="glob"
"--match", choices=("regex", "glob", "fuzzy"), default="glob" )
) subparser.add_argument("--stop-on-error", action="store_true")
parser_ls.add_argument("--stop-on-error", action="store_true")
# Parse args. # Parse args.
@ -167,6 +182,7 @@ def getargs():
return args return args
@command("ingest-ls")
def cmd_ingest_ls(args): def cmd_ingest_ls(args):
metadex.init(args.db) metadex.init(args.db)
@ -181,6 +197,7 @@ def cmd_ingest_ls(args):
metadex.close() metadex.close()
@command("ingest-db")
def cmd_ingest_db(args): def cmd_ingest_db(args):
metadex.init(args.db) metadex.init(args.db)
@ -195,6 +212,7 @@ def cmd_ingest_db(args):
metadex.close() metadex.close()
@command("scan")
def cmd_scan(args): def cmd_scan(args):
metadex.init(args.db) metadex.init(args.db)
@ -213,6 +231,7 @@ def cmd_scan(args):
metadex.close() metadex.close()
@command("rm")
def cmd_rm(args): def cmd_rm(args):
metadex.init(args.db) metadex.init(args.db)
@ -221,6 +240,7 @@ def cmd_rm(args):
metadex.close() metadex.close()
@command("ls")
def cmd_ls(args) -> int: def cmd_ls(args) -> int:
return_code = 0 return_code = 0
@ -316,19 +336,12 @@ def main():
if config.dryrun: if config.dryrun:
log.info(f"--- DRY RUN ---") log.info(f"--- DRY RUN ---")
if args.mode == "scan": if args.mode == "ls":
return cmd_scan(args)
elif args.mode == "ingest-ls":
return cmd_ingest_ls(args)
elif args.mode == "ingest-db":
return cmd_ingest_db(args)
elif args.mode == "rm":
return cmd_rm(args)
elif args.mode == "ls":
# Since this is a read-only operation we can change some config params. # Since this is a read-only operation we can change some config params.
config.db_allow_slow = False config.db_allow_slow = False
config.dryrun = True config.dryrun = True
return cmd_ls(args) cmd = _commands[args.mode]
return cmd(args)
if __name__ == "__main__": if __name__ == "__main__":