<commit_before># -*- coding: utf-8 -*-
u"""
Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
This file is part of Toolium.

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.
"""

import datetime
import os


class DriverWrappersPool(object):
    driver_wrappers = []

    # Configuration and output folders
    config_directory = None
    output_directory = None

    # Screenshots configuration
    screenshots_directory = None
    screenshots_number = None

    # Videos configuration
    videos_directory = None
    videos_number = None

    # Visual Testing configuration
    visual_output_directory = None
    visual_number = None

    @classmethod
    def is_empty(cls):
        """Check if the wrappers pool is empty or the main driver is not connected

        :returns: true id the wrappers pool is empty
        """
        return len(cls.driver_wrappers) == 0 or cls.driver_wrappers[0].driver is None

    @classmethod
    def get_default_wrapper(cls):
        """Returns the default (first) driver wrapper

        :returns: default driver wrapper
        :rtype: toolium.driver_wrapper.DriverWrapper
        """
        if len(cls.driver_wrappers) == 0:
            # Create a new driver wrapper if the pool is empty
            from toolium.driver_wrapper import DriverWrapper
            DriverWrapper(main_driver=True)
        return cls.driver_wrappers[0]

    @classmethod
    def add_wrapper(cls, driver_wrapper):
        """Add a driver wrapper to the wrappers pool

        :param driver_wrapper: driver_wrapper instance
        """
        cls.driver_wrappers.append(driver_wrapper)

    @classmethod
    def capture_screenshots(cls, name):
        """Capture a screenshot in each driver

        :param name: screenshot name suffix
        """
        screenshot_name = '{}_driver{}' if len(cls.driver_wrappers) > 1 else '{}'
        driver_index = 1
        for driver_wrapper in cls.driver_wrappers:
            if driver_wrapper.utils:
                from toolium.jira import add_attachment
                add_attachment(driver_wrapper.utils.capture_screenshot(screenshot_name.format(name, driver_index)))
            driver_index += 1

    @classmethod
    def download_video(cls, driver_wrapper, name, test_passed=True):
        """Download saved videos if video is enabled or if test fails

        :oaram driver_wrapper: driver wrapper instance
        :param name: destination file name
        :param test_passed: True if the test has passed
        """
        video_name = '{}_driver{}' if len(cls.driver_wrappers) > 1 else '{}'
        driver_index = 1
        if ((driver_wrapper.config.getboolean_optional('Server', 'video_enabled')
             or not test_passed) and driver_wrapper.remote_video_node):
            video_name = video_name if test_passed else 'error_{}'.format(video_name)
            driver_wrapper.utils.download_remote_video(driver_wrapper.remote_video_node, driver_wrapper.session_id,
                                                       video_name.format(name, driver_index))

    @classmethod
    def close_drivers_and_download_videos(cls, name, test_passed=True, maintain_default=False):
        """Stop all drivers and download saved videos if video is enabled or if test fails
<commit_msg>Fix exception in after_scenario if driver has not been created<commit_after># -*- coding: utf-8 -*-
u"""
Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
This file is part of Toolium.

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.
"""

import datetime
import os


class DriverWrappersPool(object):
    driver_wrappers = []

    # Configuration and output folders
    config_directory = None
    output_directory = None

    # Screenshots configuration
    screenshots_directory = None
    screenshots_number = None

    # Videos configuration
    videos_directory = None
    videos_number = None

    # Visual Testing configuration
    visual_output_directory = None
    visual_number = None

    @classmethod
    def is_empty(cls):
        """Check if the wrappers pool is empty or the main driver is not connected

        :returns: true id the wrappers pool is empty
        """
        return len(cls.driver_wrappers) == 0 or cls.driver_wrappers[0].driver is None

    @classmethod
    def get_default_wrapper(cls):
        """Returns the default (first) driver wrapper

        :returns: default driver wrapper
        :rtype: toolium.driver_wrapper.DriverWrapper
        """
        if len(cls.driver_wrappers) == 0:
            # Create a new driver wrapper if the pool is empty
            from toolium.driver_wrapper import DriverWrapper
            DriverWrapper(main_driver=True)
        return cls.driver_wrappers[0]

    @classmethod
    def add_wrapper(cls, driver_wrapper):
        """Add a driver wrapper to the wrappers pool

        :param driver_wrapper: driver_wrapper instance
        """
        cls.driver_wrappers.append(driver_wrapper)

    @classmethod
    def capture_screenshots(cls, name):
        """Capture a screenshot in each driver

        :param name: screenshot name suffix
        """
        screenshot_name = '{}_driver{}' if len(cls.driver_wrappers) > 1 else '{}'
        driver_index = 1
        for driver_wrapper in cls.driver_wrappers:
            if driver_wrapper.driver:
                from toolium.jira import add_attachment
                add_attachment(driver_wrapper.utils.capture_screenshot(screenshot_name.format(name, driver_index)))
            driver_index += 1

    @classmethod
    def download_video(cls, driver_wrapper, name, test_passed=True):
        """Download saved videos if video is enabled or if test fails

        :oaram driver_wrapper: driver wrapper instance
        :param name: destination file name
        :param test_passed: True if the test has passed
        """
        video_name = '{}_driver{}' if len(cls.driver_wrappers) > 1 else '{}'
        driver_index = 1
        if ((driver_wrapper.config.getboolean_optional('Server', 'video_enabled')
             or not test_passed) and driver_wrapper.remote_video_node):
            video_name = video_name if test_passed else 'error_{}'.format(video_name)
            driver_wrapper.utils.download_remote_video(driver_wrapper.remote_video_node, driver_wrapper.session_id,
                                                       video_name.format(name, driver_index))

    @classmethod
    def close_drivers_and_download_videos(cls, name, test_passed=True, maintain_default=False):
        """Stop all drivers and download saved videos if video is enabled or if test fails

{'fraction_non_alphanumeric': 0.07008229360233607, 'fraction_numerical': 0.004778338200159278, 'mean_word_length': 2.920395421436004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6489266', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1964, 'n_words': 673}