<commit_before>"""
Module for reporting into http://www.blazemeter.com/ service

Copyright 2015 BlazeMeter Inc.

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 copy
import json
import logging
import os
import sys
import traceback
import time
import webbrowser
import zipfile
import six

from six.moves.urllib.request import Request, urlopen, ProxyHandler, build_opener, install_opener
from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urlencode, urlsplit

from bzt import ManualShutdown
from bzt.engine import Reporter, AggregatorListener, Provisioning
from bzt.modules.aggregator import DataPoint, KPISet
from bzt.modules.jmeter import JMeterExecutor
from bzt.utils import to_json, dehumanize_time, MultiPartForm


class BlazeMeterUploader(Reporter, AggregatorListener):
    """
    Reporter class

    :type client: BlazeMeterClient
    """

    def __init__(self):
        super(BlazeMeterUploader, self).__init__()
        self.browser_open = 'start'
        self.client = BlazeMeterClient(self.log)
        self.test_id = ""
        self.kpi_buffer = []
        self.bulk_size = 30

    def prepare(self):
        """
        Read options for uploading, check that they're sane
        """
        super(BlazeMeterUploader, self).prepare()
        self.client.address = self.settings.get("address", self.client.address)
        self.client.data_address = self.settings.get("data-address", self.client.data_address)
        self.client.timeout = dehumanize_time(self.settings.get("timeout", self.client.timeout))
        self.bulk_size = self.settings.get("bulk-size", self.bulk_size)
        self.browser_open = self.settings.get("browser-open", self.browser_open)
        token = self.settings.get("token", "")
        proxy_settings = self.engine.config.get("settings").get("proxy")
        if proxy_settings:
            if proxy_settings.get("address"):
                proxy_url = urlsplit(proxy_settings.get("address"))
                username = proxy_settings.get("username")
                pwd = proxy_settings.get("password")
                if username and pwd:
                    proxy_uri = "%s://%s:%s@%s" % (proxy_url.scheme, username, pwd, proxy_url.netloc)
                else:
                    proxy_uri = "%s://%s" % (proxy_url.scheme, proxy_url.netloc)
                proxy_handler = ProxyHandler({"https":proxy_uri})
                opener = build_opener(proxy_handler)
                install_opener(opener)

        if not token:
            self.log.warning("No BlazeMeter API key provided, will upload anonymously")
        self.client.token = token

        self.client.active_session_id = self.parameters.get("session-id", None)
        self.client.test_id = self.parameters.get("test-id", None)
        self.client.user_id = self.parameters.get("user-id", None)
        self.client.data_signature = self.parameters.get("signature", None)

        if not self.client.test_id:
            test_name = self.parameters.get("test", "Taurus Test")  # TODO: provide a way to put datetime into test name
            try:
                self.client.ping()  # to check connectivity and auth
                if token:
                    self.test_id = self.client.test_by_name(test_name, {"type": "external"})
            except HTTPError:
                self.log.error("Cannot reach online results storage, maybe the address/token is wrong")
                raise

    def startup(self):
        """<commit_msg>added http proxy handler<commit_after>"""
Module for reporting into http://www.blazemeter.com/ service

Copyright 2015 BlazeMeter Inc.

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 copy
import json
import logging
import os
import sys
import traceback
import time
import webbrowser
import zipfile
import six

from six.moves.urllib.request import Request, urlopen, ProxyHandler, build_opener, install_opener
from six.moves.urllib.error import HTTPError
from six.moves.urllib.parse import urlencode, urlsplit

from bzt import ManualShutdown
from bzt.engine import Reporter, AggregatorListener, Provisioning
from bzt.modules.aggregator import DataPoint, KPISet
from bzt.modules.jmeter import JMeterExecutor
from bzt.utils import to_json, dehumanize_time, MultiPartForm


class BlazeMeterUploader(Reporter, AggregatorListener):
    """
    Reporter class

    :type client: BlazeMeterClient
    """

    def __init__(self):
        super(BlazeMeterUploader, self).__init__()
        self.browser_open = 'start'
        self.client = BlazeMeterClient(self.log)
        self.test_id = ""
        self.kpi_buffer = []
        self.bulk_size = 30

    def prepare(self):
        """
        Read options for uploading, check that they're sane
        """
        super(BlazeMeterUploader, self).prepare()
        self.client.address = self.settings.get("address", self.client.address)
        self.client.data_address = self.settings.get("data-address", self.client.data_address)
        self.client.timeout = dehumanize_time(self.settings.get("timeout", self.client.timeout))
        self.bulk_size = self.settings.get("bulk-size", self.bulk_size)
        self.browser_open = self.settings.get("browser-open", self.browser_open)
        token = self.settings.get("token", "")
        proxy_settings = self.engine.config.get("settings").get("proxy")
        if proxy_settings:
            if proxy_settings.get("address"):
                proxy_url = urlsplit(proxy_settings.get("address"))
                username = proxy_settings.get("username")
                pwd = proxy_<PASSWORD>("password")
                if username and pwd:
                    proxy_uri = "%s://%s:%s@%s" % (proxy_url.scheme, username, pwd, proxy_url.netloc)
                else:
                    proxy_uri = "%s://%s" % (proxy_url.scheme, proxy_url.netloc)
                proxy_handler = ProxyHandler({"https":proxy_uri, "http":proxy_uri})
                opener = build_opener(proxy_handler)
                install_opener(opener)

        if not token:
            self.log.warning("No BlazeMeter API key provided, will upload anonymously")
        self.client.token = token

        self.client.active_session_id = self.parameters.get("session-id", None)
        self.client.test_id = self.parameters.get("test-id", None)
        self.client.user_id = self.parameters.get("user-id", None)
        self.client.data_signature = self.parameters.get("signature", None)

        if not self.client.test_id:
            test_name = self.parameters.get("test", "Taurus Test")  # TODO: provide a way to put datetime into test name
            try:
                self.client.ping()  # to check connectivity and auth
                if token:
                    self.test_id = self.client.test_by_name(test_name, {"type": "external"})
            except HTTPError:
                self.log.error("Cannot reach online results storage, maybe the address/token is wrong")
                raise

    def startup(self):
        """
{'fraction_non_alphanumeric': 0.09799974839602466, 'fraction_numerical': 0.002516039753428104, 'mean_word_length': 2.8480154888673765, 'pattern_counts': {'":': 5, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194848', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2213, 'n_words': 608}