Created
April 10, 2013 08:19
-
-
Save gaborbarna/5352800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maf, os | |
maf.setBasedir( os.path.dirname( os.path.abspath( __file__ ) ) ) | |
import pkgutil | |
import argparse | |
import management2.tasks | |
import inspect | |
from maf.application.management.application import Application | |
import config | |
from management2.registry import provide_resource | |
def load_modules(parent_mod, parser): | |
subparsers = parser.add_subparsers() | |
prefix = parent_mod.__name__ + '.' | |
path = parent_mod.__path__ | |
for _, modname, ispkg in pkgutil.iter_modules(path, prefix=prefix): | |
group_mod = __import__(modname, fromlist=[parent_mod.__name__]) | |
option = group_mod.__name__.split('.')[-1] | |
help = group_mod.__doc__ | |
subparser = subparsers.add_parser(option, help=help, | |
conflict_handler='resolve') | |
if ispkg: | |
load_modules(group_mod, subparser) | |
else: | |
set_option_func(subparser, group_mod, option) | |
def set_option_func(parser, module, option): | |
cls_members = inspect.getmembers(module, inspect.isclass) | |
if cls_members: | |
cls_list = [cls for _, cls in cls_members | |
if cls.__name__.lower() == option.replace('_', '')] | |
obj = cls_list[0](parser) | |
func = lambda args: obj(args) | |
parser.set_defaults(func=func) | |
if __name__ == '__main__': | |
current_config = getattr(config, 'development') | |
provide_resource('app', Application) | |
Application(current_config) | |
parser = argparse.ArgumentParser(conflict_handler='resolve') | |
load_modules(management2.tasks, parser) | |
args = parser.parse_args() | |
args.func(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources = {} | |
def provide_resource(name, cls): | |
def init_and_register(original_init): | |
def new_init(self, *args, **kwargs): | |
original_init(self, *args, **kwargs) | |
resources[name] = self | |
return new_init | |
cls.__init__ = init_and_register(cls.__init__) | |
def require_resource(*resource_types): | |
def wrap_constructor(self,*args,**kw): | |
self.__oldinit__(*args,**kw) | |
if hasattr(self,'_resource_types') : | |
for resource_type in self._resource_types: | |
self.__dict__[resource_type] = resources[resource_type] | |
def inner(cls): | |
cls._resource_types = resource_types | |
cls.__oldinit__ = cls.__init__ | |
cls.__init__ = wrap_constructor | |
return cls | |
return inner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from management2.registry import require_resource | |
@require_resource('app') | |
class ListMixin(object): | |
def __init__(self, parser): | |
parser.add_argument('--select') | |
parser.add_argument('--filter') | |
def getResults(self, args): | |
from celery.contrib import rdb; rdb.set_trace() | |
def getDefaultAttrs(self): | |
return 'id' | |
class ClientMixin(object): | |
def __init__(self, parser): | |
parser.add_argument('--client', required=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment