DRIVER=hid-generic
HID_ID=0003:00000627:00000001
HID_NAME=QEMU QEMU USB Tablet
HID_PHYS=usb-0000:07:00.0-1/input0
HID_UNIQ=28754-0000:00:02.5:00.0-1
MODALIAS=hid:b0003g0001v00000627p00000001
  B   ˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þBðB³�”�ÆèdzW�üÿÂƒp.Á_ŠI|¥�+Wé3ž¯ÁÀ     # Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
"""
Gandi driver base classes
"""

import time
import hashlib

from libcloud.utils.py3 import b
from libcloud.common.base import ConnectionKey
from libcloud.common.xmlrpc import XMLRPCResponse, XMLRPCConnection

# Global constants

DEFAULT_TIMEOUT = 600  # operation pooling max seconds
DEFAULT_INTERVAL = 20  # seconds between 2 operation.info


class GandiException(Exception):
    """
    Exception class for Gandi driver
    """

    def __str__(self):
        # pylint: disable=unsubscriptable-object

        return "({}) {}".format(self.args[0], self.args[1])

    def __repr__(self):
        # pylint: disable=unsubscriptable-object

        return '<GandiException code {} "{}">'.format(self.args[0], self.args[1])


class GandiResponse(XMLRPCResponse):
    """
    A Base Gandi Response class to derive from.
    """


class GandiConnection(XMLRPCConnection, ConnectionKey):
    """
    Connection class for the Gandi driver
    """

    responseCls = GandiResponse
    host = "rpc.gandi.net"
    endpoint = "/xmlrpc/"

    def __init__(
        self,
        key,
        secure=True,
        timeout=None,
        retry_delay=None,
        backoff=None,
        proxy_url=None,
    ):
        # Note: Method resolution order in this case is
        # XMLRPCConnection -> Connection and Connection doesn't take key as the
        # first argument so we specify a keyword argument instead.
        # Previously it was GandiConnection -> ConnectionKey so it worked fine.
        super().__init__(
            key=key,
            secure=secure,
            timeout=timeout,
            retry_delay=retry_delay,
            backoff=backoff,
            proxy_url=proxy_url,
        )
        self.driver = BaseGandiDriver

    def request(self, method, *args):
        args = (self.key,) + args

        return super().request(method, *args)


class BaseGandiDriver:
    """
    Gandi base driver

    """

    connectionCls = GandiConnection
    name = "Gandi"

    # Specific methods for gandi
    def _wait_operation(self, id, timeout=DEFAULT_TIMEOUT, check_interval=DEFAULT_INTERVAL):
        """Wait for an operation to succeed"""

        for i in range(0, timeout, check_interval):
            try:
                # pylint: disable=no-member
                op = self.connection.request("operation.info", int(id)).object

                if op["step"] == "DONE":
                    return True

                if op["step"] in ["ERROR", "CANCEL"]:
                    return False
            except (KeyError, IndexError):
                pass
            except Exception as e:
                raise GandiException(1002, e)

            time.sleep(check_interval)

        return False


class BaseObject:
    """Base class for objects not conventional"""

    uuid_prefix = ""

    def __init__(self, id, state, driver):
        self.id = str(id) if id else None
        self.state = state
        self.driver = driver
        self.uuid = self.get_uuid()

    def get_uuid(self):
        """Unique hash for this object
