Source code for vdat.list_plugins
# Virus Data Analysis Tool: a data reduction GUI for HETDEX/VIRUS data
# Copyright (C) 2017 "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
'''Entry point of the executable to use to list plugins'''
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse as ap
import functools
import os
import sys
from colorama import init, Fore, Style
import pkg_resources as pkgr
# initialize colorama
init(autoreset=True)
[docs]def parse(argv=None):
"""Create the parser and parse the command line arguments
Parameters
----------
argv : list
command line, if None taken from ``sys.argv``
Returns
-------
Namespace
parsed command line
"""
def_formatter = ap.ArgumentDefaultsHelpFormatter
# shared options
common_parser = ap.ArgumentParser(add_help=False)
common_parser.add_argument("--verbose", '-v', action="store_true",
help="Increase verbosity")
common_parser.add_argument('ep', nargs='?', help='''If given, shows only
the specified entry point name and set verbose
to true''')
# main parser
parser = ap.ArgumentParser(description="""List the plugins""",
formatter_class=def_formatter)
subparser = parser.add_subparsers(title='Subcommands',
dest='subparser_name',
description="""Type '%(prog)s cmd -h' for
detailed information about the
subcommands""")
# Gui tabs plugins
from vdat.gui.fplane import TABS_ENTRY_POINT
description = ("List the available tab types. Tabs are advertised under"
" the '{}' entry point group".format(TABS_ENTRY_POINT))
tab_types = subparser.add_parser('tab_types', description=description,
help='List the tab types',
parents=[common_parser],
formatter_class=def_formatter)
tab_types.set_defaults(func=functools.partial(list_plugins,
TABS_ENTRY_POINT))
args = parser.parse_args(args=argv)
if not args.subparser_name:
parser.print_usage()
print("{}: error: too few"
" arguments".format(os.path.basename(sys.argv[0])))
sys.exit(2)
return args
[docs]def main(argv=None):
"""Main function of the implementation of the ``vdat_plugins`` executable
Parameters
----------
argv : list
command line, if None taken from ``sys.argv``
"""
args = parse(argv=argv)
args.func(args)
[docs]def list_plugins(entry_point_name, args):
"""List the plugins.
Parameters
----------
entry_point_name : string
name of the entry point to explore
args : Namespace
arguments to make the copy run
"""
verbose = args.verbose if args.ep is None else True
i = -1
for i, ep in enumerate(pkgr.iter_entry_points(entry_point_name,
name=args.ep)):
print('Plugin ' + Style.BRIGHT + Fore.GREEN + ep.name)
if verbose:
msg = ('implemented by {col}{style}{mod}:{func}{reset},'
' belonging to package {col}{style}{dist}{reset}')
print(msg.format(col=Fore.GREEN, style=Style.NORMAL,
reset=Fore.RESET,
mod=ep.module_name, func=ep.attrs[0],
dist=ep.dist))
print('Full documentation:')
doc = ep.load().__doc__
if not doc.startswith(' '):
doc = ' ' + doc
print(doc)
if i == -1:
if args.ep:
msg = ('No plugin found for {style}{name}{reset} in entry point'
' group {style}{group}{reset}')
else:
msg = 'No plugin found for entry point group {style}{group}{reset}'
print(Fore.RED + msg.format(group=entry_point_name, name=args.ep,
style=Style.BRIGHT, reset=Style.NORMAL))
if __name__ == '__main__': # pragma: no cover
main()