# Path: katprep/management/base.py
# class BaseConnector(metaclass=ABCMeta):
#     """
#     Basic management connector that connects on creation.
#     """
# 
#     def __init__(self, username, password, **kwargs):
#         self._username = username
#         self._password = password
#         self._session = None
#         self._connect()
# 
#     @abstractmethod
#     def _connect(self):
#         pass
# 
# class PowerManager(metaclass=ABCMeta):
#     @abstractmethod
#     def _manage_power(self, vm_name, action="poweroff"):
#         """
#         Powers a particual virtual machine on/off forcefully.
# 
#         :param vm_name: Name of the virtual machine
#         :type vm_name: str
#         :param action: action (poweroff, poweron)
#         :type action: str
#         """
# 
#     @abstractmethod
#     def powerstate_vm(self, vm_name):
#         """
#         Returns the power state of a particular virtual machine.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         # TODO: powerstate enum
# 
#     @abstractmethod
#     def restart_vm(self, vm_name, force=False):
#         """
#         Restarts a particular VM (default: soft reboot using guest tools).
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param force: Flag whether a hard reboot is requested
#         :type force: bool
#         """
# 
#     # Aliases
#     def poweroff_vm(self, vm_name):
#         """
#         Turns off a particual virtual machine forcefully.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         return self._manage_power(vm_name, "poweroff")
# 
#     def poweron_vm(self, vm_name):
#         """
#         Turns on a particual virtual machine forcefully.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         return self._manage_power(vm_name, action="poweron")
# 
# class SnapshotManager(metaclass=ABCMeta):
#     """
#     Managers with snapshot support.
# 
#     This base class provides a general interface for manager classes
#     that support the use of snapshots.
#     """
# 
#     @abstractmethod
#     def _manage_snapshot(self, vm_name, snapshot_title, snapshot_text, action="create"):
#         """
#         Helper function to perform creating, reverting or removing a snapshot.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         :param snapshot_text: Descriptive text for the snapshot
#         :type snapshot_text: str
#         :param action: The action to perform. create, remove or revert.
#         :type action: str
#         """
# 
#     @abstractmethod
#     def has_snapshot(self, vm_name, snapshot_title):
#         """
#         Returns whether a particular virtual machine is currently protected
#         by a snapshot. This requires specifying a VM name.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
# 
#     def create_snapshot(self, vm_name, snapshot_title, snapshot_text):
#         """
#         Creates a snapshot for a particular virtual machine.
#         This requires specifying a VM, comment title and text.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         :param snapshot_text: Descriptive text for the snapshot
#         :type snapshot_text: str
#         """
#         return self._manage_snapshot(
#             vm_name, snapshot_title, snapshot_text, action="create"
#         )
# 
#     def remove_snapshot(self, vm_name, snapshot_title):
#         """
#         Removes a snapshot for a particular virtual machine.
#         This requires specifying a VM and a comment title.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
#         return self._manage_snapshot(vm_name, snapshot_title, "", action="remove")
# 
#     def revert_snapshot(self, vm_name, snapshot_title):
#         """
#         Reverts to  a snapshot for a particular virtual machine.
#         This requires specifying a VM and a comment title.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
#         return self._manage_snapshot(vm_name, snapshot_title, "", action="revert")
# 
# Path: katprep/exceptions.py
# class EmptySetException(Exception):
#     """
#     Dummy class for empty result sets
# 
#     .. class:: EmptySetException
#     """
# 
# class InvalidCredentialsException(Exception):
#     """
#     Dummy class for invalid credentials
# 
#     .. class:: InvalidCredentialsException
#     """
# 
# class SessionException(Exception):
#     """
#     Dummy class for session errors
# 
#     .. class:: SessionException
#     """
# 
# class UnsupportedRequestException(Exception):
#     """
#     Dummy class for unsupported requests
# 
#     .. class:: UnsupportedRequest
#     """


import logging
import socket
import libvirt
from .base import BaseConnector, PowerManager, SnapshotManager
from ..exceptions import (EmptySetException, InvalidCredentialsException,
SessionException, UnsupportedRequestException)
        This is done by checking if the URI contains one of the well-known
        libvirt protocols.

        :param uri: a libvirt URI
        :type uri: str
        """
        prefixes = {
            "lxc", "qemu", "xen", "hyperv", "vbox", "openvz", "uml", "phyp",
            "vz", "bhyve", "esx", "vpx", "vmwareplayer", "vmwarews",
            "vmwarefusion"
        }
        #check whether a valid prefix was found
        for prefix in prefixes:
            if prefix in uri and "://" in uri:
                return True
        return False



    def _connect(self):
        """This function establishes a connection to the hypervisor."""
        #create weirdo auth dict
        auth = [
            [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
            self.retrieve_credentials, None
            ]
        #authenticate
        try:
            self._session = libvirt.openAuth(self.URI, auth, 0)
            if not self._session:

# Path: katprep/management/base.py
# class BaseConnector(metaclass=ABCMeta):
#     """
#     Basic management connector that connects on creation.
#     """
# 
#     def __init__(self, username, password, **kwargs):
#         self._username = username
#         self._password = password
#         self._session = None
#         self._connect()
# 
#     @abstractmethod
#     def _connect(self):
#         pass
# 
# class PowerManager(metaclass=ABCMeta):
#     @abstractmethod
#     def _manage_power(self, vm_name, action="poweroff"):
#         """
#         Powers a particual virtual machine on/off forcefully.
# 
#         :param vm_name: Name of the virtual machine
#         :type vm_name: str
#         :param action: action (poweroff, poweron)
#         :type action: str
#         """
# 
#     @abstractmethod
#     def powerstate_vm(self, vm_name):
#         """
#         Returns the power state of a particular virtual machine.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         # TODO: powerstate enum
# 
#     @abstractmethod
#     def restart_vm(self, vm_name, force=False):
#         """
#         Restarts a particular VM (default: soft reboot using guest tools).
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param force: Flag whether a hard reboot is requested
#         :type force: bool
#         """
# 
#     # Aliases
#     def poweroff_vm(self, vm_name):
#         """
#         Turns off a particual virtual machine forcefully.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         return self._manage_power(vm_name, "poweroff")
# 
#     def poweron_vm(self, vm_name):
#         """
#         Turns on a particual virtual machine forcefully.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         """
#         return self._manage_power(vm_name, action="poweron")
# 
# class SnapshotManager(metaclass=ABCMeta):
#     """
#     Managers with snapshot support.
# 
#     This base class provides a general interface for manager classes
#     that support the use of snapshots.
#     """
# 
#     @abstractmethod
#     def _manage_snapshot(self, vm_name, snapshot_title, snapshot_text, action="create"):
#         """
#         Helper function to perform creating, reverting or removing a snapshot.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         :param snapshot_text: Descriptive text for the snapshot
#         :type snapshot_text: str
#         :param action: The action to perform. create, remove or revert.
#         :type action: str
#         """
# 
#     @abstractmethod
#     def has_snapshot(self, vm_name, snapshot_title):
#         """
#         Returns whether a particular virtual machine is currently protected
#         by a snapshot. This requires specifying a VM name.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
# 
#     def create_snapshot(self, vm_name, snapshot_title, snapshot_text):
#         """
#         Creates a snapshot for a particular virtual machine.
#         This requires specifying a VM, comment title and text.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         :param snapshot_text: Descriptive text for the snapshot
#         :type snapshot_text: str
#         """
#         return self._manage_snapshot(
#             vm_name, snapshot_title, snapshot_text, action="create"
#         )
# 
#     def remove_snapshot(self, vm_name, snapshot_title):
#         """
#         Removes a snapshot for a particular virtual machine.
#         This requires specifying a VM and a comment title.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
#         return self._manage_snapshot(vm_name, snapshot_title, "", action="remove")
# 
#     def revert_snapshot(self, vm_name, snapshot_title):
#         """
#         Reverts to  a snapshot for a particular virtual machine.
#         This requires specifying a VM and a comment title.
# 
#         :param vm_name: Name of a virtual machine
#         :type vm_name: str
#         :param snapshot_title: Snapshot title
#         :type snapshot_title: str
#         """
#         return self._manage_snapshot(vm_name, snapshot_title, "", action="revert")
# 
# Path: katprep/exceptions.py
# class EmptySetException(Exception):
#     """
#     Dummy class for empty result sets
# 
#     .. class:: EmptySetException
#     """
# 
# class InvalidCredentialsException(Exception):
#     """
#     Dummy class for invalid credentials
# 
#     .. class:: InvalidCredentialsException
#     """
# 
# class SessionException(Exception):
#     """
#     Dummy class for session errors
# 
#     .. class:: SessionException
#     """
# 
# class UnsupportedRequestException(Exception):
#     """
#     Dummy class for unsupported requests
# 
#     .. class:: UnsupportedRequest
#     """


# Path: katprep/management/libvirt.py
import logging
import socket
import libvirt
from .base import BaseConnector, PowerManager, SnapshotManager
from ..exceptions import (EmptySetException, InvalidCredentialsException,
SessionException, UnsupportedRequestException)

        This is done by checking if the URI contains one of the well-known
        libvirt protocols.

        :param uri: a libvirt URI
        :type uri: str
        """
        prefixes = {
            "lxc", "qemu", "xen", "hyperv", "vbox", "openvz", "uml", "phyp",
            "vz", "bhyve", "esx", "vpx", "vmwareplayer", "vmwarews",
            "vmwarefusion"
        }
        #check whether a valid prefix was found
        for prefix in prefixes:
            if prefix in uri and "://" in uri:
                return True
        return False



    def _connect(self):
        """This function establishes a connection to the hypervisor."""
        #create weirdo auth dict
        auth = [
            [libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE],
            self.retrieve_credentials, None
            ]
        #authenticate
        try:
            self._session = libvirt.openAuth(self.URI, auth, 0)
            if not self._session:

                raise SessionException("Unable to establish connection to hypervisor!")