<commit_before>'''
This module provides the class PIDClientCredentials
which handles the credentials for Handle server
Interaction and for the Search Servlet.

Author: <NAME> (DKRZ), 2015-2016

'''

import json
import os
import logging
from b2handle.handleexceptions import CredentialsFormatError
import util
import utilhandle

LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(util.NullHandler())

class PIDClientCredentials(object):
    '''
    Provides authentication information to access a Handle server, either by
    specifying username and password or by providing a json file containing
    the relevant information.

    '''

    @staticmethod
    def load_from_JSON(json_filename):
        '''
        Create a new instance of a PIDClientCredentials with information read
        from a local JSON file.

        :param json_filename: The path to the json credentials file. The json
            file should have the following format:

                .. code:: json

                    {
                        "baseuri": "https://url.to.your.handle.server",
                        "username": "index:prefix/suffix",
                        "password": "<PASSWORD>",
                        "prefix": "prefix_to_use_for_writing_handles",
                        "handleowner": "username_to_own_handles"
                    }

            Any additional key-value-pairs are stored in the instance as
            config.
        :raises: :exc:`~b2handle.handleexceptions.CredentialsFormatError`
        :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`
        :return: An instance.
        '''

        jsonfilecontent = json.loads(open(json_filename, 'r').read())
        if 'baseuri' in jsonfilecontent:
            jsonfilecontent['handle_server_url'] = jsonfilecontent['baseuri']
            del jsonfilecontent['baseuri']
        instance = PIDClientCredentials(**jsonfilecontent)
        return instance


    def __init__(self, **args):
        '''
        Initialize client credentials instance.

        The constructor checks if enough arguments are passed to
        authenticate at a handle server or search servlet. For this,
        the following parameters are checked. Depending on the
        chosen authentication method, only a subset of them are
        required.

        All other parameters passed are stored and can be retrieved<commit_msg>Changed name of param from 'baseuri' to 'handle_server_url' so it is consistent. CAUTION: JSON credentials files have to be changed.<commit_after>'''
This module provides the class PIDClientCredentials
which handles the credentials for Handle server
Interaction and for the Search Servlet.

Author: <NAME> (DKRZ), 2015-2016

'''

import json
import os
import logging
from b2handle.handleexceptions import CredentialsFormatError
import util
import utilhandle

LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(util.NullHandler())

class PIDClientCredentials(object):
    '''
    Provides authentication information to access a Handle server, either by
    specifying username and password or by providing a json file containing
    the relevant information.

    '''

    @staticmethod
    def load_from_JSON(json_filename):
        '''
        Create a new instance of a PIDClientCredentials with information read
        from a local JSON file.

        :param json_filename: The path to the json credentials file. The json
            file should have the following format:

                .. code:: json

                    {
                        "handle_server_url": "https://url.to.your.handle.server",
                        "username": "index:prefix/suffix",
                        "password": "<PASSWORD>",
                        "prefix": "prefix_to_use_for_writing_handles",
                        "handleowner": "username_to_own_handles"
                    }

            Any additional key-value-pairs are stored in the instance as
            config.
        :raises: :exc:`~b2handle.handleexceptions.CredentialsFormatError`
        :raises: :exc:`~b2handle.handleexceptions.HandleSyntaxError`
        :return: An instance.
        '''

        jsonfilecontent = json.loads(open(json_filename, 'r').read())
        instance = PIDClientCredentials(**jsonfilecontent)
        return instance


    def __init__(self, **args):
        '''
        Initialize client credentials instance.

        The constructor checks if enough arguments are passed to
        authenticate at a handle server or search servlet. For this,
        the following parameters are checked. Depending on the
        chosen authentication method, only a subset of them are
        required.

        All other parameters passed are stored and can be retrieved
{'fraction_non_alphanumeric': 0.0704818009678098, 'fraction_numerical': 0.004628655585945719, 'mean_word_length': 2.503316138540899, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '5603112', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1132, 'n_words': 410}