<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from requests.auth import HTTPBasicAuth

import copy
import functools
import json
import logging
import os
import re
import requests
import socket
import ssl
import sys
import warnings

try:
    import xmlrpclib
except ImportError:
    import xmlrpc.client as xmlrpclib

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

try:  # Python 2.7+
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        """Returns a new instance of the NullHandler class."""

        def emit(self, record):
            """This method does nothing."""
            pass

logging.getLogger('confluence').addHandler(NullHandler())


def deprecate_xmlrpc_notification(func):
    """Decorator for xmlrpc deprecation warnings."""

    @functools.wraps(func)
    def new_func(*args, **kwargs):
        # warnings.simplefilter('always', PendingDeprecationWarning)  # Un-comment this line to always display warnings.
        warnings.warn(
            "{} will be rewritten to use the Atlassian REST API in future versions.\n"
            "This change will introduce changes in the module method responses.".format(func.__name__),
            category=PendingDeprecationWarning,
            stacklevel=2)
        warnings.simplefilter('default', PendingDeprecationWarning)
        return func(*args, **kwargs)

    return new_func


class Confluence(object):
    DEFAULT_OPTIONS = {
        "server": "http://localhost:8090",
        "verify": True
    }

    def __init__(self, profile=None, url="http://localhost:8090/", username=None, password=None, appid=None):
        """
        Returns a Confluence object by loading the connection details from the `config.ini` file.

        :param profile: The name of the section from config.ini file that stores server config url/username/password
        :type  profile: ``str``

        :param url: URL of the Confluence server
        :type  url: ``str``

        :param username: username to use for authentication
        :type  username: ``str``

        :param password: password to use for authentication
        :type  password: ``<PASSWORD>``

        :return: Confluence -- an instance to a Confluence object.
        :raises: EnvironmentError

        Usage:

            >>> from confluence import Confluence
            >>>
            >>> conf = Confluence(profile='confluence')
            >>> conf.get_page("ds","Welcome to Confluence")

        Also create a `config.ini` like this and put it in current directory, user home directory or PYTHONPATH.

        .. code-block:: none

            [confluence]
            url=https://confluence.atlassian.com
            # only the `url` is mandatory
            user=...
            pass=...

        """
        self.logging = logging

        def find_file(path):
            """
            Find the file named path in the sys.path.
            Returns the full path name if found, None if not found
            """
            paths = [os.getcwd(), '.', os.path.expanduser('~')]
            paths.extend(sys.path)
            for dir_name in paths:
                possible = os.path.abspath(os.path.join(dir_name, path))
                if os.path.isfile(possible):
                    return possible
            return None

        config = ConfigParser.ConfigParser(defaults={'user': username, 'pass': password, 'appid': appid})

        config_file = find_file('config.ini')
        if config_file:
            logging.debug("Found %s config file" % config_file)

        if not profile:
            if config_file:
                config.read(config_file)
                try:
                    profile = config.get('general', 'default-confluence-profile')
                except ConfigParser.NoOptionError:
                    pass

        if profile:
            if config_file:
                config.read(config_file)
                url = config.get(profile, 'url')
                username = config.get(profile, 'user')
                password = config.get(profile, 'pass')
                appid = config.get(profile, 'appid')
            else:
                raise EnvironmentError(
                    '%s was not able to locate the config.ini file.'
                    'Place config.ini in current directory, user home directory or PYTHONPATH.' % __name__)

        options = Confluence.DEFAULT_OPTIONS
        options['server'] = url
        options['username'] = username
        options['password'] = password

        # Set the base URL for REST calls.
        self.base_url = url + '/rest/api/'

        # Create a request session.
        self.connection = requests.Session()
        self.connection.auth = HTTPBasicAuth(options['username'], options['password'])

        if not self.connection_valid():
            self.logging.critical('Connection is None.')

        self.logging.debug("Instantiated Confluence object.")

    def connection_valid(self):
        """Checks if a connection to the Confluence server can be established.

        :rtype: Boolean
        """
        if self.connection is None:
            return False
        try:
            result = self.connection.get(self.base_url + 'content')
            result.raise_for_status()

        except requests.exceptions.RequestException as err:
            self.logging.exception('Connection Error: {err}'.format(err=err))
            print(err)
            raise err

        if not result.ok:
            self.connection = None
        return self.connection is not None

    def check_response(self, response):
        """Checks if the response from REST calls is valid.

        :rtype Boolean.
        """

        if response is None:
            self.logging.debug('Retrieved a NoneObject')
            return response is not None

        elif not response.ok:
            self.logging.debug('Response not ok: {}'.format(response.reason))
            return response.ok
        else:
            try:
                len(response.json()['results'][0]) < 1
            except IndexError:  # exception caused by lists of 0 length
                self.logging.debug('Retrieved an empty list:\n'
                              'Request: {}'.format(response.request.url))
                return False

        return response is not None

    def get_user_by_key(self, key, timeout=10):
        request = self.base_url + 'user?key={key}'.format(key=key)
<commit_msg>cleanup for flake8 test<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from requests.auth import HTTPBasicAuth

import copy
import functools
import json
import logging
import os
import re
import requests
import ssl
import sys
import warnings

try:
    import xmlrpclib
except ImportError:
    import xmlrpc.client as xmlrpclib

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

try:  # Python 2.7+
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        """Returns a new instance of the NullHandler class."""

        def emit(self, record):
            """This method does nothing."""
            pass

logging.getLogger('confluence').addHandler(NullHandler())


def deprecate_xmlrpc_notification(func):
    """Decorator for xmlrpc deprecation warnings."""

    @functools.wraps(func)
    def new_func(*args, **kwargs):
        # warnings.simplefilter('always', PendingDeprecationWarning)  # Un-comment this line to always display warnings.
        warnings.warn(
            "{} will be rewritten to use the Atlassian REST API in future versions.\n"
            "This change will introduce changes in the module method responses.".format(func.__name__),
            category=PendingDeprecationWarning,
            stacklevel=2)
        warnings.simplefilter('default', PendingDeprecationWarning)
        return func(*args, **kwargs)

    return new_func


class Confluence(object):
    DEFAULT_OPTIONS = {
        "server": "http://localhost:8090",
        "verify": True
    }

    def __init__(self, profile=None, url="http://localhost:8090/", username=None, password=<PASSWORD>, appid=None):
        """
        Returns a Confluence object by loading the connection details from the `config.ini` file.

        :param profile: The name of the section from config.ini file that stores server config url/username/password
        :type  profile: ``str``

        :param url: URL of the Confluence server
        :type  url: ``str``

        :param username: username to use for authentication
        :type  username: ``str``

        :param password: password to use for authentication
        :type  password: ``str``

        :return: Confluence -- an instance to a Confluence object.
        :raises: EnvironmentError

        Usage:

            >>> from confluence import Confluence
            >>>
            >>> conf = Confluence(profile='confluence')
            >>> conf.get_page("ds","Welcome to Confluence")

        Also create a `config.ini` like this and put it in current directory, user home directory or PYTHONPATH.

        .. code-block:: none

            [confluence]
            url=https://confluence.atlassian.com
            # only the `url` is mandatory
            user=...
            pass=...

        """
        self.logging = logging

        def find_file(path):
            """
            Find the file named path in the sys.path.
            Returns the full path name if found, None if not found
            """
            paths = [os.getcwd(), '.', os.path.expanduser('~')]
            paths.extend(sys.path)
            for dir_name in paths:
                possible = os.path.abspath(os.path.join(dir_name, path))
                if os.path.isfile(possible):
                    return possible
            return None

        config = ConfigParser.ConfigParser(defaults={'user': username, 'pass': password, 'appid': appid})

        config_file = find_file('config.ini')
        if config_file:
            logging.debug("Found %s config file" % config_file)

        if not profile:
            if config_file:
                config.read(config_file)
                try:
                    profile = config.get('general', 'default-confluence-profile')
                except ConfigParser.NoOptionError:
                    pass

        if profile:
            if config_file:
                config.read(config_file)
                url = config.get(profile, 'url')
                username = config.get(profile, 'user')
                password = config.get(profile, 'pass')
                appid = config.get(profile, 'appid')
            else:
                raise EnvironmentError(
                    '%s was not able to locate the config.ini file.'
                    'Place config.ini in current directory, user home directory or PYTHONPATH.' % __name__)

        options = Confluence.DEFAULT_OPTIONS
        options['server'] = url
        options['username'] = username
        options['password'] = password

        # Set the base URL for REST calls.
        self.base_url = url + '/rest/api/'

        # Create a request session.
        self.connection = requests.Session()
        self.connection.auth = HTTPBasicAuth(options['username'], options['password'])

        if not self.connection_valid():
            self.logging.critical('Connection is None.')

        self.logging.debug("Instantiated Confluence object.")

    def connection_valid(self):
        """Checks if a connection to the Confluence server can be established.

        :rtype: Boolean
        """
        if self.connection is None:
            return False
        try:
            result = self.connection.get(self.base_url + 'content')
            result.raise_for_status()

        except requests.exceptions.RequestException as err:
            self.logging.exception('Connection Error: {err}'.format(err=err))
            print(err)
            raise err

        if not result.ok:
            self.connection = None
        return self.connection is not None

    def check_response(self, response):
        """Checks if the response from REST calls is valid.

        :rtype Boolean.
        """

        if response is None:
            self.logging.debug('Retrieved a NoneObject')
            return response is not None

        elif not response.ok:
            self.logging.debug('Response not ok: {}'.format(response.reason))
            return response.ok
        else:
            try:
                len(response.json()['results'][0]) < 1
            except IndexError:  # exception caused by lists of 0 length
                self.logging.debug('Retrieved an empty list:\n'
                                   'Request: {}'.format(response.request.url))
                return False

        return response is not None

    def get_user_by_key(self, key, timeout=10):
        request = self.base_url + 'user?key={key}'.format(key=key)

{'fraction_non_alphanumeric': 0.09536121673003803, 'fraction_numerical': 0.002661596958174905, 'mean_word_length': 2.0719458070544263, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1273604', 'n_tokens_mistral': 3473, 'n_tokens_neox': 3295, 'n_words': 1037}