<commit_before>from __future__ import absolute_import

# Copyright 2015-2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Refer to the README and COPYING files for full details of the license
#

import logging
import uuid

import libvirt

from . import config
from . import domain
from . import doms
from . import errors
from . import runner
from . import xmlfile


class Connection(object):

    _log = logging.getLogger('convirt.Connection')

    def __init__(self):
        self._event_handlers = {}

    def close(self):
        """
        Does nothing succesfully
        """

#    def domainEventRegisterAny(self, dom, eventID, cb, opaque):
#        pass

    def listAllDomains(self, flags=0):
        # flags are unused
        return doms.get_all()

    def listDomainsID(self):
        return [dom.ID() for dom in doms.get_all()]

    def lookupByUUIDString(self, guid):
        self._log.debug('looking for container %r', guid)
        try:
            dom = doms.get_by_uuid(guid)
        except KeyError:
            errors.throw(code=libvirt.VIR_ERR_NO_DOMAIN)
        else:
            self._log.debug('found container %r', guid)
            return dom

    def lookupByID(self, intid):
        # hack?
        # python supports 128-bit ints, so this is a legitimate int in python.
        return self.lookupByUUIDString(str(uuid.UUID(int=intid)))

#    def getAllDomainStats(self, flags):
#        pass
#
#    def domainListGetStats(self, doms, flags):
#        pass
#
    def createXML(self, domxml, flags):
        # flags are unused
        return domain.Domain.create(domxml)

    def getLibVersion(self):
        return 0x001002018  # TODO

    def recoveryAllDomains(self):
        conf = config.environ.current()
        for rt_uuid in runner.get_all():
            self._log.debug('trying to recover container %r', rt_uuid)
            xml_file = xmlfile.XMLFile(rt_uuid, conf)
            try:
                domain.Domain.recover(rt_uuid, xml_file.read(), conf)
            except Exception:  # FIXME: too coarse
                self._log.exception('failed to recover container %r',
                                    rt_uuid)
            else:
                self._log.debug('recovered container %r', rt_uuid)
        return doms.get_all()

    def __getattr__(self, name):
        # virConnect does not expose non-callable attributes.
        return self._fake_method
    
    def _fake_method(self, *args):
        errors.throw()<commit_msg>TODO: move recoveryAllDomains somewhere else.<commit_after>from __future__ import absolute_import

# Copyright 2015-2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Refer to the README and COPYING files for full details of the license
#

import logging
import uuid

import libvirt

from . import config
from . import domain
from . import doms
from . import errors
from . import runner
from . import xmlfile


class Connection(object):

    _log = logging.getLogger('convirt.Connection')

    def __init__(self):
        self._event_handlers = {}

    def close(self):
        """
        Does nothing succesfully
        """

#    def domainEventRegisterAny(self, dom, eventID, cb, opaque):
#        pass

    def listAllDomains(self, flags=0):
        # flags are unused
        return doms.get_all()

    def listDomainsID(self):
        return [dom.ID() for dom in doms.get_all()]

    def lookupByUUIDString(self, guid):
        self._log.debug('looking for container %r', guid)
        try:
            dom = doms.get_by_uuid(guid)
        except KeyError:
            errors.throw(code=libvirt.VIR_ERR_NO_DOMAIN)
        else:
            self._log.debug('found container %r', guid)
            return dom

    def lookupByID(self, intid):
        # hack?
        # python supports 128-bit ints, so this is a legitimate int in python.
        return self.lookupByUUIDString(str(uuid.UUID(int=intid)))

#    def getAllDomainStats(self, flags):
#        pass
#
#    def domainListGetStats(self, doms, flags):
#        pass
#
    def createXML(self, domxml, flags):
        # flags are unused
        return domain.Domain.create(domxml)

    def getLibVersion(self):
        return 0x001002018  # TODO

    # TODO: this is not the best place
    def recoveryAllDomains(self):
        conf = config.environ.current()
        for rt_uuid in runner.get_all():
            self._log.debug('trying to recover container %r', rt_uuid)
            xml_file = xmlfile.XMLFile(rt_uuid, conf)
            try:
                domain.Domain.recover(rt_uuid, xml_file.read(), conf)
            except Exception:  # FIXME: too coarse
                self._log.exception('failed to recover container %r',
                                    rt_uuid)
            else:
                self._log.debug('recovered container %r', rt_uuid)
        return doms.get_all()

    def __getattr__(self, name):
        # virConnect does not expose non-callable attributes.
        return self._fake_method
    
    def _fake_method(self, *args):
        errors.throw()
{'fraction_non_alphanumeric': 0.08170292690561903, 'fraction_numerical': 0.010643293160118954, 'mean_word_length': 2.5618729096989967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1419105', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1766, 'n_words': 632}