How to use the powerapi.cli.parser.MainParser function in powerapi

To help you get started, we’ve selected a few powerapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_cant_convert_to_type():
    """
    add an argument that must catch an int value, Parse a string that
    contains only this argument with a value that is not an int test if an
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', type=int)

    with pytest.raises(BadTypeException):
        parser.parse('-a a'.split())
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_default_type():
    """
    add an argument without specifing the type it must catch. Parse a string
    that contains only this argument and test if the value contained in the
    result is a string

    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a')
    result = parser.parse('-a 1'.split())
    assert len(result) == 1
    assert 'a' in result
    assert isinstance(result['a'], str)
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_add_component_subparser_with_two_name():
    """
    add a component subparser with one short name and one long name
    parse a string and test if the value is only bind to the long name
    """
    parser = MainParser(help_arg=False)
    subparser = ComponentSubParser('titi')
    subparser.add_argument('a', 'aaa', flag=True, action=store_true, default=False)
    subparser.add_argument('n', 'name')
    parser.add_component_subparser('sub', subparser)
    check_parsing_result(parser, '--sub titi -a --name tutu', {'sub': {'titi': {'tutu': {'aaa': True, 'name': 'tutu'}}}})
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_empty_parser():
    """
    test to parse strings with a parser and retrieve the following results :

    - "" : {}
    - "-z" : UnknowArgException(z)
    - "-a" : UnknowArgException(a)
    - "-a --sub toto -b" : UnknowArgException(a)
    - "-b" : UnknowArgException(b)

    Parser description :

    - base parser arguments : None
    - subparser toto binded to the argument sub with sub arguments : None
    """
    parser = MainParser(help_arg=False)

    check_parsing_result(parser, '', {})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-z', None)

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-a', None)

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-a --sub toto -b', None)

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-b', None)
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_add_argument_2_short():
    """
    Add two short argument (an argument and a flag) to the parser

    Test if the arguments was added to the short_arg string
    """
    parser = MainParser(help_arg=False)
    assert parser.short_arg == ''
    parser.add_argument('a', flag=True)
    assert parser.short_arg == 'a'
    parser.add_argument('b')
    assert parser.short_arg == 'ab:'
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_main_parser():
    """
    test to parse strings with a parser and retrieve the following results :

    - "" : {}
    - "-z" : UnknowArgException(z)
    - "-a" : {a: True}
    - "-a --sub toto -b" : UnknowArgException(sub)
    - "-b" : UnknowArgException(b)

    Parser description :

    - base parser arguments : -a
    - subparser toto binded to the argument sub with sub arguments : None
    """
    parser = MainParser(help_arg=False)
    parser.add_argument('a', flag=True, action=store_true)

    check_parsing_result(parser, '', {})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-z', None)

    check_parsing_result(parser, '-a', {'a': True})

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-a --sub toto -b', None)

    with pytest.raises(UnknowArgException):
        check_parsing_result(parser, '-b', None)
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_formula_subparser():
    """
    test to parse strings with a formula parser and retrieve the following results :
    - "" : {}
    - "--sub toto -b" :  {a:True, sub: {'toto' : {b: True}}}
    - "-b" : BadContextException(b, [toto])

    Parser description :

    - formula subparser toto binded to the argument sub with sub arguments : -b and --name
    """
    parser = MainParser(help_arg=False)

    subparser = ComponentSubParser('toto')
    subparser.add_argument('b', flag=True, action=store_true)
    parser.add_formula_subparser('sub', subparser)

    check_parsing_result(parser, '', {})

    check_parsing_result(parser, '--sub toto -b', {'sub': {'toto': {'b': True}}})

    with pytest.raises(BadContextException):
        check_parsing_result(parser, '-b', None)
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_add_flag_long():
    """
    Add a long argument to the parser

    Test if the argument was added to the long_arg list
    """
    parser = MainParser(help_arg=False)
    assert parser.long_arg == []
    parser.add_argument('aaa', flag=True)
    assert parser.long_arg == ['aaa']
github powerapi-ng / powerapi / tests / unit / cli / test_parser.py View on Github external
def test_add_component_subparser_that_aldready_exists():
    """
    Add a component_subparser that already exists to a parser and test if an
    AlreadyAddedArgumentException is raised
    """
    parser = MainParser(help_arg=False)
    subparser = ComponentSubParser('titi')
    subparser.add_argument('n', 'name')
    parser.add_component_subparser('toto', subparser)
    subparser2 = ComponentSubParser('titi')
    subparser2.add_argument('n', 'name')
    
    with pytest.raises(AlreadyAddedArgumentException):
        parser.add_component_subparser('toto', subparser2)
github powerapi-ng / powerapi / powerapi / cli / tools.py View on Github external
def enable_log(arg, val, args, acc):
    acc[arg] = logging.DEBUG
    return args, acc

def check_csv_files(files):
    return reduce(lambda acc, f: acc and os.access(f, os.R_OK), files.split(','), True)


def extract_file_names(arg, val, args, acc):
    acc[arg] = val.split(',')
    return args, acc


class CommonCLIParser(MainParser):

    def __init__(self):
        MainParser.__init__(self)

        self.add_argument('v', 'verbose', flag=True, action=enable_log, default=logging.NOTSET,
                          help='enable verbose mode')
        self.add_argument('s', 'stream', flag=True, action=store_true, default=False, help='enable stream mode')

        subparser_mongo_input = ComponentSubParser('mongodb')
        subparser_mongo_input.add_argument('u', 'uri', help='sepcify MongoDB uri')
        subparser_mongo_input.add_argument('d', 'db', help='specify MongoDB database name', )
        subparser_mongo_input.add_argument('c', 'collection', help='specify MongoDB database collection')
        subparser_mongo_input.add_argument('n', 'name', help='specify puller name', default='puller_mongodb')
        subparser_mongo_input.add_argument('m', 'model', help='specify data type that will be storen in the database',
                                           default='HWPCReport')
        self.add_component_subparser('input', subparser_mongo_input,