Source code for fastr.utils.cmd

import os
import importlib
import inspect
import sys
import textwrap

from fastr import exceptions
from fastr import version


[docs]def find_commands(): cmd_dir = os.path.dirname(__file__) files = os.listdir(cmd_dir) files = [x[:-3] for x in files if x.endswith('.py') and not x.startswith('_')] return files
[docs]def get_command_module(command): command_module = importlib.import_module('fastr.utils.cmd.{}'.format(command)) if not hasattr(command_module, 'get_parser') or not inspect.isfunction(getattr(command_module, 'get_parser')): raise exceptions.FastrAttributeError('Command modules should contain a "get_parser" function') if not hasattr(command_module, 'main') or not inspect.isfunction(getattr(command_module, 'main')): raise exceptions.FastrAttributeError('Command modules should contain a "main" function') return command_module
[docs]def main(): # Get all files in utils following the bin_*.py name convention commands = find_commands() # Strip the first argument, this should be a sub command # and if it is not we will display the main help if len(sys.argv) < 2: print_help() return subcommand = sys.argv[1] if subcommand not in commands: print_help() return # Path sys.argv to look like "fastr subcommand" was one command sys.argv = ['{} {}'.format(sys.argv[0], subcommand)] + sys.argv[2:] module = get_command_module(subcommand) module.main()