<commit_before># -*- coding: utf-8 -*-
from threading import Lock
from pyVmomi import vim


class DvPortGroupCreator(object):
    def __init__(self, pyvmomi_service, synchronous_task_waiter):
        """

        :param pyvmomi_service:
        :param synchronous_task_waiter:
        :type synchronous_task_waiter: cloudshell.cp.vcenter.common.vcenter.task_waiter.SynchronousTaskWaiter
        :return:
        """
        self.pyvmomi_service = pyvmomi_service
        self.synchronous_task_waiter = synchronous_task_waiter
        self._lock = Lock()

    def get_or_create_network(self,
                              si,
                              vm,
                              dv_port_name,
                              dv_switch_name,
                              dv_switch_path,
                              port_group_path,
                              vlan_id,
                              vlan_spec,
                              logger):
        network = None
        self._lock.acquire()
        try:
            # check if the network is attached to the vm and gets it, the function doesn't goes to the vcenter
            network = self.pyvmomi_service.get_network_by_name_from_vm(vm, dv_port_name)

            # if we didn't found the network on the vm
            if network is None:
                # try to get it from the vcenter
                try:
                    network = self.pyvmomi_service.find_portgroup(si,
                                                                  '{0}/{1}'.format(dv_switch_path, dv_switch_name),
                                                                  dv_port_name)
                except KeyError:
                    network = None

            # if we still couldn't get the network ---> create it(can't find it, play god!)
            if network is None:
                self._create_dv_port_group(dv_port_name,
                                           dv_switch_name,
                                           dv_switch_path,
                                           si,
                                           vlan_spec,
                                           vlan_id,
                                           logger)
                network = self.pyvmomi_service.find_network_by_name(si, dv_switch_path, dv_port_name)

            if not network:
                raise ValueError('Could not get or create vlan named: {0}'.format(dv_port_name))
        finally:
            self._lock.release()
            return network

    def _create_dv_port_group(self, dv_port_name, dv_switch_name, dv_switch_path, si, spec, vlan_id, logger):
        dv_switch = self.pyvmomi_service.get_folder(si, '{0}/{1}'.format(dv_switch_path, dv_switch_name))
        if not dv_switch:
            raise Exception('DV Switch {0} not found in path {1}'.format(dv_switch_name, dv_switch_path))

        task = DvPortGroupCreator.dv_port_group_create_task(dv_port_name, dv_switch, spec, vlan_id,
                                                            logger=logger)
        self.synchronous_task_waiter.wait_for_task(task=task,
                                                   logger=logger,<commit_msg>throwing the right error when the dvswitch wasn't found<commit_after># -*- coding: utf-8 -*-
from threading import Lock
from pyVmomi import vim


class DvPortGroupCreator(object):
    def __init__(self, pyvmomi_service, synchronous_task_waiter):
        """

        :param pyvmomi_service:
        :param synchronous_task_waiter:
        :type synchronous_task_waiter: cloudshell.cp.vcenter.common.vcenter.task_waiter.SynchronousTaskWaiter
        :return:
        """
        self.pyvmomi_service = pyvmomi_service
        self.synchronous_task_waiter = synchronous_task_waiter
        self._lock = Lock()

    def get_or_create_network(self,
                              si,
                              vm,
                              dv_port_name,
                              dv_switch_name,
                              dv_switch_path,
                              port_group_path,
                              vlan_id,
                              vlan_spec,
                              logger):
        network = None
        error = None
        self._lock.acquire()
        try:
            # check if the network is attached to the vm and gets it, the function doesn't goes to the vcenter
            network = self.pyvmomi_service.get_network_by_name_from_vm(vm, dv_port_name)

            # if we didn't found the network on the vm
            if network is None:
                # try to get it from the vcenter
                try:
                    network = self.pyvmomi_service.find_portgroup(si,
                                                                  '{0}/{1}'.format(dv_switch_path, dv_switch_name),
                                                                  dv_port_name)
                except KeyError:
                    network = None

            # if we still couldn't get the network ---> create it(can't find it, play god!)
            if network is None:
                self._create_dv_port_group(dv_port_name,
                                           dv_switch_name,
                                           dv_switch_path,
                                           si,
                                           vlan_spec,
                                           vlan_id,
                                           logger)
                network = self.pyvmomi_service.find_network_by_name(si, dv_switch_path, dv_port_name)

            if not network:
                raise ValueError('Could not get or create vlan named: {0}'.format(dv_port_name))
        except ValueError as e:
            error = e
        finally:
            self._lock.release()
            if error:
                raise error
            return network

    def _create_dv_port_group(self, dv_port_name, dv_switch_name, dv_switch_path, si, spec, vlan_id, logger):
        dv_switch = self.pyvmomi_service.get_folder(si, '{0}/{1}'.format(dv_switch_path, dv_switch_name))
        if not dv_switch:
            raise ValueError('DV Switch {0} not found in path {1}'.format(dv_switch_name, dv_switch_path))

        task = DvPortGroupCreator.dv_port_group_create_task(dv_port_name, dv_switch, spec, vlan_id,
                                                            logger=logger)
        self.synchronous_task_waiter.wait_for_task(task=task,
                                                   logger=logger,
{'fraction_non_alphanumeric': 0.06183206106870229, 'fraction_numerical': 0.0024427480916030535, 'mean_word_length': 1.2488843117061448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4889742', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1603, 'n_words': 371}