import copy


class SpyCall(object):
    def __init__(self, deep_copy=False):
        self.deep_copy = deep_copy

        self.called = False
        self.args = None
        self.kwargs = None

        self.all_args = []
        self.all_kwargs = []

    def register(self, *args, **kwargs):
        self.called = True
        if self.deep_copy:
            self.args = copy.deepcopy(args)
            self.kwargs = copy.deepcopy(kwargs)
        else:
            self.args = args
            self.kwargs = kwargs

        super(SpyCall, self).__getattribute__('all_args').append(self.args)
        super(SpyCall, self).__getattribute__('all_kwargs').append(self.kwargs)

    def __getattribute__(self, name):
        if name == 'args':
            original_args = super(SpyCall, self).__getattribute__(name)
            if original_args is None:
                raise Exception("Method was not called. Cannot access args")

        if name == 'all_args':
            original_all_args = super(SpyCall, self).__getattribute__(name)
            if len(original_all_args) == 0:
                raise Exception("Method was not called. Cannot access all_args")

        return super(SpyCall, self).__getattribute__(name)
