<commit_before>from optparse   import OptionParser
from sys        import argv

"""
@PACKAGE runnerutils
"""

# TODO Build a multi-args parser and a subsequent runner base class (i.e. MultiArgRunnerBase)

########
## Validates an optspec structure
##
def validate(optspec):
    good_keys = [
        'type', 'action', 'dest', 'metavar', 'trigger', 'help', 'default',
        'nargs', 'const', 'callback', 'choices', 'callback_args',
        'callback_kwargs',
    ]
    for optdef in optspec:
        if 'triggers' not in optdef.keys():
            raise OptSpecError("Missing triggers")
        for key in optdef.keys():
            if key not in good_keys:
                raise OptSpecError("Bad key value: %s" % key)

########
## Push a set of args through an optspec-built OptionParser and return the
## result.
##
def parse_args(optspec,args=argv):
    parser = create_parser_from_optspec(optspec)
    return parser.parse_args(args)

########
## Build a OptionParser out of an optspec
##
def create_parser(optspec):
    parser = OptionParser()
    for optdef in optspec:
        args = optdef['triggers']
        del optdef['triggers']
        parser.add_option(*args,**optdef)
    return parser


########
## Runner Base Classes
##
class RunnerBase(object):
    parse_ns = dict()       # a namespace for arg processing vars (useful for allowing subclasses to change behavior)
    parser   = None         # parser

    def __init__(self,args=argv):
        self.parse_args(args)

    def parse_args(self,args):
        if args is not None:
            self.parse_ns['unparsed_args'] = args

        if self.parser is None:
            self.create_parser()

        if self.parser is None:
            raise RunnerError("No parser defined")

        if self.parse_ns['unparsed_args'] is None:
            raise RunnerError("No args defined")

        (options,args_left) = self.parser.parse_args(args)
        self.parse_ns['parsed_options'] = options
        self.parse_ns['leftover_args']  = args_left

        self.__dict__.update(options.__dict__)

    def create_parser(self):
        if getattr(self,'options',None) is None:
            raise RunnerError("Cannot create a parser: self.optspec() not defined")

        parser_kwargs = {}
        if getattr(self,'parser_opts',None):<commit_msg>fixed error string<commit_after>from optparse   import OptionParser
from sys        import argv

"""
@PACKAGE runnerutils
"""

# TODO Build a multi-args parser and a subsequent runner base class (i.e. MultiArgRunnerBase)

########
## Validates an optspec structure
##
def validate(optspec):
    good_keys = [
        'type', 'action', 'dest', 'metavar', 'trigger', 'help', 'default',
        'nargs', 'const', 'callback', 'choices', 'callback_args',
        'callback_kwargs',
    ]
    for optdef in optspec:
        if 'triggers' not in optdef.keys():
            raise OptSpecError("Missing triggers")
        for key in optdef.keys():
            if key not in good_keys:
                raise OptSpecError("Bad key value: %s" % key)

########
## Push a set of args through an optspec-built OptionParser and return the
## result.
##
def parse_args(optspec,args=argv):
    parser = create_parser_from_optspec(optspec)
    return parser.parse_args(args)

########
## Build a OptionParser out of an optspec
##
def create_parser(optspec):
    parser = OptionParser()
    for optdef in optspec:
        args = optdef['triggers']
        del optdef['triggers']
        parser.add_option(*args,**optdef)
    return parser


########
## Runner Base Classes
##
class RunnerBase(object):
    parse_ns = dict()       # a namespace for arg processing vars (useful for allowing subclasses to change behavior)
    parser   = None         # parser

    def __init__(self,args=argv):
        self.parse_args(args)

    def parse_args(self,args):
        if args is not None:
            self.parse_ns['unparsed_args'] = args

        if self.parser is None:
            self.create_parser()

        if self.parser is None:
            raise RunnerError("No parser defined")

        if self.parse_ns['unparsed_args'] is None:
            raise RunnerError("No args defined")

        (options,args_left) = self.parser.parse_args(args)
        self.parse_ns['parsed_options'] = options
        self.parse_ns['leftover_args']  = args_left

        self.__dict__.update(options.__dict__)

    def create_parser(self):
        if getattr(self,'options',None) is None:
            raise RunnerError("Cannot create a parser: self.options() not defined")

        parser_kwargs = {}
        if getattr(self,'parser_opts',None):
{'fraction_non_alphanumeric': 0.11879471060047692, 'fraction_numerical': 0.0, 'mean_word_length': 2.8354114713216956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1967795', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1327, 'n_words': 383}