#
# Copyright (c) 2010 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from ovirtsdk.utils.comperator import Comparator
from ovirtsdk.infrastructure.errors import ImmutableError

class Base(object):
    ''' Decorator base class '''

    def __init__(self, context):
        self.__context = context

    @property
    def context(self):
        return self.__context

    def __getattr__(self, item):
        if not self.__dict__.has_key('superclass'):
            return self.__getattribute__(item)
        return self.superclass.__getattribute__(item)

    def __eq__(self, other):
        return Comparator.compare(self, other)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __setattr__(self, name, value):
        if name in ['__context', 'context']:
            raise ImmutableError(name)
        else:
            super(Base, self).__setattr__(name, value)

#
# Copyright (c) 2010 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from ovirtsdk.utils.comperator import Comparator
from ovirtsdk.infrastructure.errors import ImmutableError

class Base(object):
    ''' Decorator base class '''

    def __init__(self, context):
        self.__context = context

    @property
    def context(self):
        return self.__context

    def __getattr__(self, item):
        if not self.__dict__.has_key('superclass'):
            return self.__getattribute__(item)
        return self.superclass.__getattribute__(item)

    def __eq__(self, other):
        return Comparator.compare(self, other)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __setattr__(self, name, value):
        if name in ['__context', 'context']:
            raise ImmutableError(name)
        else:
            super(Base, self).__setattr__(name, value)

    def export(self, outfile, level, namespace_='', name_='', namespacedef_='', pretty_print=True):
        # This empty method is necessary in order to avoid exceptions when the
        # infrastructure tries to invoke it on a collection decorator that is
        # used as a parameter.
        pass

Add empty export method to decorator base
sdk: Add empty export method to decorator base

Decorators for resources and collections extend a common base class, for
example:

  class VMSnapshotDisks(Base)

The resource decorators also extend the corresponding parameter class:

  class VMSnapshotDisk(params.Disk, Base)

This means that resource decorators implement the "export" method,
responsible for generating the XML representation of the entity, but
collection decorators don't implement it.

There are situations where decorators are used as parameters, for
example, when creating a VM from a snapshot one could use the following
code:

  snapshot = vm.snapshots.get(id="...")

The resulting object is a decorator, and it contains references to
decorators of collections, for example to the collection of disks. Later
this object can be used as a parameter, as follows:

  snapshots = ovirtsdk.xml.params.Snapshots()
  snapshots.add_snapshot(snapshot)
  newvm = ovirtsdk.xml.params.VM(
    name="newvm",
    snapshots=snapshots,
    ...)
  api.vms.add(newvm)

When doing this the infrastructure will try to generate the XML
document, calling the "export" method on the new VM object, and this
will recursively call the "export" methods of all the referenced
objects, including the collection decorators, which will fail because
they don't have such method.

This usage is not good practice, and not efficient, it is better to
avoid using decorators as parameters:

  snapshot = ovirtsdk.params.Snapshot(id="...")
  snapshots = ovirtsdk.params.Snapshots()
  snapshots.add_snapshot(snapshot)
  newvm = ovirtsdk.xml.params.VM(
    name="newvm",
    snapshots=snapshots,
    ...)
  api.vms.add(newvm)

As this is difficult to enforce this patch adds to the Base class an
empty "export" method, so that these operations won't fail.

Change-Id: I6d2e6b9a42ad1a878f8edbbd41f3bb9d60db2bc8
Bug-Url: https://bugzilla.redhat.com/1024696
Signed-off-by: Juan Hernandez <59e5b8140de97cc91c3fb6c5342dce948469af8c@redhat.com>
