示例#1
0
    def run(cls, args):
        available = {}

        def find_study_classes(pkg_or_module, pkg_or_module_path):
            for cls_name in dir(pkg_or_module):
                if cls_name.startswith('_'):
                    continue
                cls = getattr(pkg_or_module, cls_name)
                try:
                    if (issubclass(cls, (Study, MultiStudy))
                            and 'desc' in cls.__dict__):
                        try:
                            old_path = available[cls]
                        except KeyError:
                            available[cls] = pkg_or_module_path
                        else:
                            if len(pkg_or_module_path) < len(old_path):
                                available[cls] = pkg_or_module_path
                except TypeError:
                    pass

        search_paths = [cls.default_path] + args.search_paths
        for search_path in search_paths:
            base_module = import_module(search_path)
            for pkg_name in find_packages(op.dirname(base_module.__file__)):
                pkg_path = search_path + '.' + pkg_name
                pkg = import_module(pkg_path)
                find_study_classes(pkg, pkg_path)
                for module_info in iter_modules([op.dirname(pkg.__file__)]):
                    module_path = pkg_path + '.' + module_info.name
                    module = import_module(module_path)
                    find_study_classes(module, module_path)
        msg = ("\nThe following Study classes are available:")
        to_print = []
        for avail_cls, module_path in sorted(available.items(),
                                             key=lambda x: x[0].__name__):
            if module_path.startswith(DEFAULT_STUDY_CLASS_PATH):
                module_path = module_path[(len(DEFAULT_STUDY_CLASS_PATH) + 1):]
            full_name = module_path + '.' + avail_cls.__name__
            to_print.append((full_name, avail_cls.desc))

        desc_start = max(len(l[0]) for l in to_print) + DEFAULT_SPACER
        for cls_name, desc in to_print:
            spaces = ' ' * (desc_start - len(cls_name))
            msg += '\n{}{}{}{}'.format(
                ' ' * DEFAULT_INDENT, cls_name, spaces,
                wrap_text(desc, DEFAULT_LINE_LENGTH,
                          desc_start + DEFAULT_INDENT))
        print(msg + '\n')
示例#2
0
 def parser(cls):
     usage = "banana <command> [<args>]\n\nAvailable commands:"
     desc_start = max(len(k) for k in cls.commands.keys()) + DEFAULT_SPACER
     for name, cmd_cls in cls.commands.items():
         spaces = ' ' * (desc_start - len(name))
         usage += '\n{}{}{}{}'.format(
             ' ' * DEFAULT_INDENT, name, spaces,
             wrap_text(cmd_cls.desc, DEFAULT_LINE_LENGTH,
                       desc_start + DEFAULT_INDENT))
     parser = ArgumentParser(description="Base banana command", usage=usage)
     parser.add_argument('command', help="The sub-command to run")
     parser.add_argument('--version',
                         '-v',
                         action='version',
                         version='%(prog)s {}'.format(__version__))
     return parser
示例#3
0
 def static_menu(cls):
     ITEM_INDENT = 4
     LINE_LENGTH = 79
     DESC_INDENT = 8
     MIN_WRAP = 30
     cls_name = '.'.join([cls.__module__, cls.__name__])
     menu = ("\n{} Menu \n".format(cls_name) + '-' * len(cls_name) +
             "------")
     menu += '\n\nInputs:'
     for spec in cls.acquired_data_specs():
         if spec.default is not None:
             qual_str = ' (default={})'.format(spec.default)
         elif spec.optional:
             qual_str = ' (optional)'
         else:
             qual_str = ''
         if isinstance(spec, BaseFileset):
             format_wrap_ind = (ITEM_INDENT + len(spec.name) + 3 +
                                len(qual_str))
             if format_wrap_ind > LINE_LENGTH - MIN_WRAP:
                 format_wrap_ind = LINE_LENGTH - MIN_WRAP
                 indent_frmt_wrap = True
             else:
                 indent_frmt_wrap = False
             menu += '\n{}{}{} :{}{}\n{}'.format(
                 ' ' * ITEM_INDENT, spec.name, qual_str,
                 ('\n' if indent_frmt_wrap else ' '),
                 wrap_text(', '.join(f.name for f in spec.valid_formats),
                           LINE_LENGTH,
                           format_wrap_ind,
                           prefix_indent=indent_frmt_wrap),
                 wrap_text(spec.desc,
                           LINE_LENGTH,
                           DESC_INDENT,
                           prefix_indent=True))
         else:
             menu += '\n{}{}{} : {}{}\n{}'.format(
                 ' ' * ITEM_INDENT, spec.name, qual_str,
                 spec.dtype.__name__, (' array' if spec.array else ''),
                 wrap_text(spec.desc,
                           LINE_LENGTH,
                           DESC_INDENT,
                           prefix_indent=True))
     menu += '\n\nDerivatives:'
     for spec in cls.derived_data_specs():
         if isinstance(spec, BaseFileset):
             menu += '\n{}{} : {}\n{}'.format(
                 ' ' * ITEM_INDENT, spec.name, spec.format.name,
                 wrap_text(spec.desc,
                           LINE_LENGTH,
                           DESC_INDENT,
                           prefix_indent=True))
         else:
             menu += '\n{}{} : {}{}\n{}'.format(
                 ' ' * ITEM_INDENT, spec.name, spec.dtype.__name__,
                 (' array' if spec.array else ''),
                 wrap_text(spec.desc,
                           LINE_LENGTH,
                           DESC_INDENT,
                           prefix_indent=True))
     menu += '\n\nParameters:'
     for spec in cls.param_specs():
         menu += '\n{}{}{} : {}\n{}'.format(
             ' ' * ITEM_INDENT, spec.name, (' [{}]'.format(", ".join(
                 str(c) for c in spec.choices)) if spec.choices else ''),
             spec.dtype.__name__,
             wrap_text(spec.desc,
                       LINE_LENGTH,
                       DESC_INDENT,
                       prefix_indent=True))
     return menu