Beginning of CLI interface + protection against infinite recursion.

This commit is contained in:
Sybren A. Stüvel
2018-02-28 16:47:13 +01:00
parent 8ae400acbe
commit ca964181fe
14 changed files with 314 additions and 27 deletions
+48
View File
@@ -0,0 +1,48 @@
"""Commandline entry points."""
import argparse
import logging
from . import common, pack, list_deps
def cli_main():
parser = argparse.ArgumentParser(description='BAT: Blender Asset Tracer')
# func is set by subparsers to indicate which function to run.
parser.set_defaults(func=None,
loglevel=logging.WARNING)
loggroup = parser.add_mutually_exclusive_group()
loggroup.add_argument('-v', '--verbose', dest='loglevel',
action='store_const', const=logging.INFO,
help='Log INFO level and higher')
loggroup.add_argument('-d', '--debug', dest='loglevel',
action='store_const', const=logging.DEBUG,
help='Log everything')
loggroup.add_argument('-q', '--quiet', dest='loglevel',
action='store_const', const=logging.ERROR,
help='Log at ERROR level and higher')
subparsers = parser.add_subparsers(help='sub-command help')
pack.add_parser(subparsers)
list_deps.add_parser(subparsers)
args = parser.parse_args()
config_logging(args)
from blender_asset_tracer import __version__
log = logging.getLogger(__name__)
log.debug('Running BAT version %s', __version__)
if not args.func:
parser.error('No subcommand was given')
return args.func(args)
def config_logging(args):
"""Configures the logging system based on CLI arguments."""
logging.basicConfig(
level=args.loglevel,
format='%(asctime)-15s %(levelname)8s %(name)-40s %(message)s',
)
+16
View File
@@ -0,0 +1,16 @@
"""Common functionality for CLI parsers."""
def add_flag(argparser, flag_name: str, **kwargs):
"""Add a CLI argument for the flag.
The flag defaults to False, and when present on the CLI stores True.
"""
argparser.add_argument('-%s' % flag_name[0],
'--%s' % flag_name,
default=False,
action='store_true',
**kwargs)
+36
View File
@@ -0,0 +1,36 @@
"""List dependencies of a blend file."""
import logging
import pathlib
from . import common
log = logging.getLogger(__name__)
def add_parser(subparsers):
"""Add argparser for this subcommand."""
parser = subparsers.add_parser('list', help=__doc__)
parser.set_defaults(func=cli_list)
parser.add_argument('blendfile', type=pathlib.Path)
common.add_flag(parser, 'recursive', help='Also report dependencies of dependencies')
common.add_flag(parser, 'json', help='Output as JSON instead of human-readable text')
def cli_list(args):
from blender_asset_tracer import tracer
bpath = args.blendfile
if not bpath.exists():
log.fatal('File %s does not exist', args.blendfile)
return 3
reported_files = set()
for usage in tracer.deps(bpath, recursive=args.recursive):
for path in usage.files():
path = path.resolve()
if path in reported_files:
log.debug('Already reported %s', path)
continue
print(path)
reported_files.add(path)
+12
View File
@@ -0,0 +1,12 @@
"""Create a BAT-pack for the given blend file."""
def add_parser(subparsers):
"""Add argparser for this subcommand."""
parser = subparsers.add_parser('pack', help=__doc__)
parser.set_defaults(func=cli_pack)
def cli_pack(args):
raise NotImplementedError('bat pack not implemented yet')