# Path: shrubbery/db/utils.py
# def fetch(qs, *args):
#     qs = get_query_set(qs)
#     for arg in args:
#         try:
#             q = get_q(arg)
#         except ValueError:
#             if callable(arg):
#                 try:
#                     arg = arg()
#                 except qs.model.DoesNotExist:
#                     continue
#             if isinstance(arg, Exception):
#                 raise arg
#             return arg
#         try:
#             return qs.get(q)
#         except qs.model.DoesNotExist:
#             pass
#     raise qs.model.DoesNotExist()


from django.db import models
from django.db.models.query import QuerySet
from shrubbery.db.utils import fetch


def _create_proxy_method(name):
    def proxy(self, *args, **kwargs):
        return getattr(self.get_query_set(), name)(*args, **kwargs)
    return proxy


class ManagerBase(type):
    def __new__(cls, name, bases, attrs):
        meta = attrs.pop('Meta', None)
        new_cls = super(ManagerBase, cls).__new__(cls, name, bases, attrs)
        queryset_cls = getattr(meta, 'queryset', None)
        if queryset_cls:
            if isinstance(queryset_cls, tuple):
                queryset_cls = type('_'.join([cls.__name__ for cls in queryset_cls]), queryset_cls, {})
            if queryset_cls:
                new_cls._queryset_cls = queryset_cls
                for name, attr in queryset_cls.__dict__.items():
                    if callable(attr) and getattr(attr, '_manager_proxy_method', False):
                        setattr(new_cls, name, _create_proxy_method(name))
        else:
            if not hasattr(new_cls, '_queryset_cls'):

# Path: shrubbery/db/utils.py
# def fetch(qs, *args):
#     qs = get_query_set(qs)
#     for arg in args:
#         try:
#             q = get_q(arg)
#         except ValueError:
#             if callable(arg):
#                 try:
#                     arg = arg()
#                 except qs.model.DoesNotExist:
#                     continue
#             if isinstance(arg, Exception):
#                 raise arg
#             return arg
#         try:
#             return qs.get(q)
#         except qs.model.DoesNotExist:
#             pass
#     raise qs.model.DoesNotExist()


# Path: shrubbery/db/managers.py
from django.db import models
from django.db.models.query import QuerySet
from shrubbery.db.utils import fetch



def _create_proxy_method(name):
    def proxy(self, *args, **kwargs):
        return getattr(self.get_query_set(), name)(*args, **kwargs)
    return proxy


class ManagerBase(type):
    def __new__(cls, name, bases, attrs):
        meta = attrs.pop('Meta', None)
        new_cls = super(ManagerBase, cls).__new__(cls, name, bases, attrs)
        queryset_cls = getattr(meta, 'queryset', None)
        if queryset_cls:
            if isinstance(queryset_cls, tuple):
                queryset_cls = type('_'.join([cls.__name__ for cls in queryset_cls]), queryset_cls, {})
            if queryset_cls:
                new_cls._queryset_cls = queryset_cls
                for name, attr in queryset_cls.__dict__.items():
                    if callable(attr) and getattr(attr, '_manager_proxy_method', False):
                        setattr(new_cls, name, _create_proxy_method(name))
        else:
            if not hasattr(new_cls, '_queryset_cls'):

                new_cls._queryset_cls = QuerySet