<commit_before>'''
Created on Jun 26, 2014

This module defines the entry point for the developer module. 

@author: craig
'''
import sys, argparse

# option decorator
def option(*args, **kwds):
    def _decorator(func):
        _option = (args, kwds)
        if hasattr(func, 'options'):
            func.options.append(_option)
        else:
            func.options = [_option]
        return func
    return _decorator

# arg decorator
arg = option

# combines option decorators
def option_group(*options):
    def _decorator(func):
        for option in options:
            func = option(func)
        return func
    return _decorator


class MetaCommander(type):
    def __new__(cls, classname, bases, classdict):
        subcmds = {}
        for name, func in classdict.items():
            if name.startswith('do_'):
                name = name[3:]
                subcmd = {
                    'name': name,
                    'func': func,
                    'options': []
                }
                if hasattr(func, 'options'):
                    subcmd['options'] = func.options
                subcmds[name] = subcmd
        classdict['_argparse_subcmds'] = subcmds
        return type.__new__(cls, classname, bases, classdict)



class Commander(object):
    __metaclass__ = MetaCommander
    name = 'app'
    description = 'a description'
    version = '0.0'
    epilog = ''
    default_args = []
    
    def cmdline(self):
        parser = argparse.ArgumentParser(
            # prog = self.name,
            formatter_class = argparse.RawDescriptionHelpFormatter,
            description=self.__doc__,
            epilog = self.epilog,
        )

        parser.add_argument('-v', '--version', action='version',
                            version = '%(prog)s '+ self.version)

        subparsers = parser.add_subparsers(
            title='subcommands',
            description='valid subcommands',
            help='additional help',
        )
        
        for name in sorted(self._argparse_subcmds.keys()):
            subcmd = self._argparse_subcmds[name]            
            subparser = subparsers.add_parser(subcmd['name'],
                                     help=subcmd['func'].__doc__)
            for args, kwds in subcmd['options']:
                subparser.add_argument(*args, **kwds)
            subparser.set_defaults(func=subcmd['func'])

        if len(sys.argv) <= 1:
            options = parser.parse_args(self.default_args)
        else:
            options = parser.parse_args()
        options.func(self, options)
    


def main():
    # only for options which are repeated across different funcs
    common_options = option_group(
        option('--log', '-l', action='store_true', help='log is on')
    )
    
    class Application(Commander):
        'Command line tool to package or sign (optional) the agent '
        name = 'volttron-dev'
        version = '0.1'
        default_args = ['--help']
        
        @option('--log', '-l', action='store_true', help='log is on')
        @arg('agent', help="agent directory or name of currently installed agent.")
        def do_package(self, options):
            "Packages agent and create a whl file"
            print options

        @option('-f', '--force', action='store_true',
                        help='force through installation')
        @common_options
        def do_sign(self, options):
            "Signs an agent's package (whl file)"
            print options
             
    app = Application()
    app.cmdline()

if __name__ == '__main__':
    main()

## This function takes the 'extra' attribute from global namespace and re-parses it to create separate namespaces for all other chained commands.<commit_msg>added call to create_package<commit_after>'''
Created on Jun 26, 2014

This module defines the entry point for the developer module. 

@author: craig
'''
import sys, argparse
from packaging import (create_package,
                       extract_package)


# option decorator
def option(*args, **kwds):
    def _decorator(func):
        _option = (args, kwds)
        if hasattr(func, 'options'):
            func.options.append(_option)
        else:
            func.options = [_option]
        return func
    return _decorator

# arg decorator
arg = option

# combines option decorators
def option_group(*options):
    def _decorator(func):
        for option in options:
            func = option(func)
        return func
    return _decorator


class MetaCommander(type):
    def __new__(cls, classname, bases, classdict):
        subcmds = {}
        for name, func in classdict.items():
            if name.startswith('do_'):
                name = name[3:]
                subcmd = {
                    'name': name,
                    'func': func,
                    'options': []
                }
                if hasattr(func, 'options'):
                    subcmd['options'] = func.options
                subcmds[name] = subcmd
        classdict['_argparse_subcmds'] = subcmds
        return type.__new__(cls, classname, bases, classdict)



class Commander(object):
    __metaclass__ = MetaCommander
    name = 'app'
    description = 'a description'
    version = '0.0'
    epilog = ''
    default_args = []
    
    def cmdline(self):
        parser = argparse.ArgumentParser(
            # prog = self.name,
            formatter_class = argparse.RawDescriptionHelpFormatter,
            description=self.__doc__,
            epilog = self.epilog,
        )

        parser.add_argument('-v', '--version', action='version',
                            version = '%(prog)s '+ self.version)

        subparsers = parser.add_subparsers(
            title='subcommands',
            description='valid subcommands',
            help='additional help',
        )
        
        for name in sorted(self._argparse_subcmds.keys()):
            subcmd = self._argparse_subcmds[name]            
            subparser = subparsers.add_parser(subcmd['name'],
                                     help=subcmd['func'].__doc__)
            for args, kwds in subcmd['options']:
                subparser.add_argument(*args, **kwds)
            subparser.set_defaults(func=subcmd['func'])

        if len(sys.argv) <= 1:
            options = parser.parse_args(self.default_args)
        else:
            options = parser.parse_args()
        options.func(self, options)
    


def main():
    # only for options which are repeated across different funcs
    common_options = option_group(
        option('--log', '-l', action='store_true', help='log is on')
    )
    
    class Application(Commander):
        'Command line tool to package or sign (optional) the agent '
        name = 'volttron-dev'
        version = '0.1'
        default_args = ['--help']
        
        @option('--log', '-l', action='store_true', help='log is on')
        @arg('agent', help="agent directory or name of currently installed agent.")
        def do_package(self, options):
            "Packages agent and create a whl file"
            #print options
            import os
            print os.path.abspath(os.curdir)
            whl_path = create_package(options.agent)
            if whl_path:
                print('whl file created at {}'.format(whl_path))

        @option('-f', '--force', action='store_true',
                        help='force through installation')
        @common_options
        def do_sign(self, options):
            "Signs an agent's package (whl file)"
            print options
             
    app = Application()
    app.cmdline()

if __name__ == '__main__':
    main()

## This function takes the 'extra' attribute from global namespace and re-parses it to create separate namespaces for all other chained commands.
{'fraction_non_alphanumeric': 0.10648385443362378, 'fraction_numerical': 0.0030753459764223477, 'mean_word_length': 1.907973174366617, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '800027', 'n_tokens_mistral': 2181, 'n_tokens_neox': 2099, 'n_words': 568}