# Path: nbx/nbmanager/util.py
# def get_invoked_arg(func, name, args, kwargs):
#     var = None
#     if name in kwargs:
#         var = kwargs[name]
#         return var
# 
#     # handle functools.wraps functions. specifically the middleware
#     if hasattr(func, '__wrapped__'):
#         argspec = inspect.getargspec(func.__wrapped__)
#     else:
#         argspec = inspect.getargspec(func)
#     if name not in argspec.args:
#         raise Exception('{name} was not found in invoked function'.format(name=name))
# 
#     index = argspec.args.index(name)
#     # we're assuming self is not in *args for method calls
#     if argspec.args[0] == 'self':
#         index -= 1
#     var = args[index]
#     return var
# 
# def set_invoked_arg(func, name, value, args, kwargs):
#     """
#     Based on a target argspec, we try to place the param
#     value in the correct args/kwargs spot so that it works
#     in the target.
#     """
#     if isinstance(args, tuple):
#         raise Exception("args must be a list")
# 
#     # handle functools.wraps functions. specifically the middleware
#     if hasattr(func, '__wrapped__'):
#         argspec = inspect.getargspec(func.__wrapped__)
#     else:
#         argspec = inspect.getargspec(func)
# 
#     # assume that if var is in kwargs, it should stay there.
#     # possible that source func had name in kwargs, but target has
#     # the name in args (positionally). not handling for now
#     if name in kwargs or name not in argspec.args:
#         kwargs[name] = value
#         return
# 
#     index = argspec.args.index(name)
#     num_args = len(argspec.args)
#     # we're assuming self is not in args for method calls
#     if argspec.args[0] == 'self':
#         index -= 1
#         num_args -= 1
# 
#     defaults_len = 0
#     if argspec.defaults:
#         defaults_len = len(argspec.defaults)
# 
#     if len(args) == num_args:
#         args[index] = value
#     elif len(args) == index: # 
#         args.append(value)
#     elif len(args) + defaults_len >= num_args:
#         args[index] = value
#     else:
#         args.insert(index, value)
# 
#     return args, kwargs
# 
# def _path_split(path):
#     bits = path.rsplit('/', 1)
#     path = ''
#     name = bits.pop()
#     if bits:
#         path = bits[0]
#     return name, path


from notebook.services.contents.manager import ContentsManager
from .util import get_invoked_arg, set_invoked_arg, _path_split
            return object.__getattribute__(self, name)

        if hasattr(self._manager, name):
            return getattr(self._manager, name)

        return object.__getattribute__(self, name)

    def _should_shim(self, name):
        return name in API

class ContentsNameApiShim(ShimManager):

    def _shim(self, name):
        current_api = getattr(ContentsManager, name)
        legacy = getattr(self._manager, name)
        def method(self, *args, **kwargs):
            args = list(args) # make mutable
            old_path = path = get_invoked_arg(current_api, 'path', args, kwargs)
            name, path = _path_split(path)
            set_invoked_arg(legacy, 'name', name, args, kwargs)
            set_invoked_arg(legacy, 'path', path, args, kwargs)

            # see if we're getting a model. update the name and path
            # in model to represent the old way
            try:
                model = get_invoked_arg(current_api, 'model', args, kwargs)
                model_path = model.get('path', '')
                model_name, model_path = _path_split(model_path)
                model['path'] = model_path
                model['name'] = model_name

# Path: nbx/nbmanager/util.py
# def get_invoked_arg(func, name, args, kwargs):
#     var = None
#     if name in kwargs:
#         var = kwargs[name]
#         return var
# 
#     # handle functools.wraps functions. specifically the middleware
#     if hasattr(func, '__wrapped__'):
#         argspec = inspect.getargspec(func.__wrapped__)
#     else:
#         argspec = inspect.getargspec(func)
#     if name not in argspec.args:
#         raise Exception('{name} was not found in invoked function'.format(name=name))
# 
#     index = argspec.args.index(name)
#     # we're assuming self is not in *args for method calls
#     if argspec.args[0] == 'self':
#         index -= 1
#     var = args[index]
#     return var
# 
# def set_invoked_arg(func, name, value, args, kwargs):
#     """
#     Based on a target argspec, we try to place the param
#     value in the correct args/kwargs spot so that it works
#     in the target.
#     """
#     if isinstance(args, tuple):
#         raise Exception("args must be a list")
# 
#     # handle functools.wraps functions. specifically the middleware
#     if hasattr(func, '__wrapped__'):
#         argspec = inspect.getargspec(func.__wrapped__)
#     else:
#         argspec = inspect.getargspec(func)
# 
#     # assume that if var is in kwargs, it should stay there.
#     # possible that source func had name in kwargs, but target has
#     # the name in args (positionally). not handling for now
#     if name in kwargs or name not in argspec.args:
#         kwargs[name] = value
#         return
# 
#     index = argspec.args.index(name)
#     num_args = len(argspec.args)
#     # we're assuming self is not in args for method calls
#     if argspec.args[0] == 'self':
#         index -= 1
#         num_args -= 1
# 
#     defaults_len = 0
#     if argspec.defaults:
#         defaults_len = len(argspec.defaults)
# 
#     if len(args) == num_args:
#         args[index] = value
#     elif len(args) == index: # 
#         args.append(value)
#     elif len(args) + defaults_len >= num_args:
#         args[index] = value
#     else:
#         args.insert(index, value)
# 
#     return args, kwargs
# 
# def _path_split(path):
#     bits = path.rsplit('/', 1)
#     path = ''
#     name = bits.pop()
#     if bits:
#         path = bits[0]
#     return name, path


# Path: nbx/nbmanager/shim.py
from notebook.services.contents.manager import ContentsManager
from .util import get_invoked_arg, set_invoked_arg, _path_split

            return object.__getattribute__(self, name)

        if hasattr(self._manager, name):
            return getattr(self._manager, name)

        return object.__getattribute__(self, name)

    def _should_shim(self, name):
        return name in API

class ContentsNameApiShim(ShimManager):

    def _shim(self, name):
        current_api = getattr(ContentsManager, name)
        legacy = getattr(self._manager, name)
        def method(self, *args, **kwargs):
            args = list(args) # make mutable
            old_path = path = get_invoked_arg(current_api, 'path', args, kwargs)
            name, path = _path_split(path)
            set_invoked_arg(legacy, 'name', name, args, kwargs)
            set_invoked_arg(legacy, 'path', path, args, kwargs)

            # see if we're getting a model. update the name and path
            # in model to represent the old way
            try:
                model = get_invoked_arg(current_api, 'model', args, kwargs)
                model_path = model.get('path', '')
                model_name, model_path = _path_split(model_path)
                model['path'] = model_path
                model['name'] = model_name

                set_invoked_arg(legacy, 'model', model, args, kwargs)