<commit_before>import json
import functools
from consts import *
from errors import *
from ebclient.eb_configuration import Endpoint
from ebclient.registration import *

__author__ = 'dusanklinec'


class Config(object):
    """Configuration object, handles file read/write"""

    def __init__(self, json_db=None, eb_config=None, *args, **kwargs):
        self.json = json_db
        self.eb_config = eb_config

        pass

    @classmethod
    def from_json(cls, json_string):
        return cls(json_db=json.loads(json_string))

    @classmethod
    def from_file(cls, file_name):
        with open(file_name, 'r') as f:
            read_lines = [x.strip() for x in f.read().split('\n')]
            lines = []
            for line in read_lines:
                if line.startswith('//'):
                    continue
                lines.append(line)

            return Config.from_json('\n'.join(lines))

    def ensure_config(self):
        if self.json is None:
            self.json = {}
        if 'config' not in self.json:
            self.json['config'] = {}

    def has_nonempty_config(self):
        return self.json is not None and 'config' in self.json and len(self.json['config']) > 0

    def get_config(self, key):
        if not self.has_nonempty_config():
            return None
        return self.json['config'][key] if key in self.json['config'] else None

    def set_config(self, key, val):
        self.ensure_config()
        self.json['config'][key] = val

    def has_identity(self):
        return self.username is not None

    def has_apikey(self):
        return self.apikey is not None

    def to_string(self):
        return json.dumps(self.json, indent=2) if self.has_nonempty_config() else ""

    def resolve_endpoint(self, purpose=SERVER_PROCESS_DATA, protocol=PROTOCOL_HTTPS, environment=ENVIRONMENT_DEVELOPMENT):
        """
        Resolves required endpoint from the configuration according to the parameters
        :param purpose:
        :param protocol:
        :return:
        """
        if not self.has_nonempty_config() or self.servers is None:
            raise ValueError('Configuration has no servers')

        candidate_list = []
        for server in self.servers:
            endpoint_key = 'useEndpoints'
            if purpose == SERVER_ENROLLMENT:
                endpoint_key = 'enrolEndpoints'
            elif purpose == SERVER_REGISTRATION:
                endpoint_key = 'registerEndpoints'
            elif purpose != SERVER_PROCESS_DATA:
                raise ValueError('Endpoint purpose unknown')

            if endpoint_key not in server:
                continue
            if environment is not None and server['environment'] != environment:
                continue

            endpoints = server[endpoint_key]
            for endpoint in endpoints:
                if protocol is not None and endpoint['protocol'] != protocol:
                    continue

                # Construct a candidate
                candidate = Endpoint(scheme=endpoint['protocol'],
                                     host=server['fqdn'],
                                     port=endpoint['port'])

                candidate_list.append(candidate)
            pass

        if len(candidate_list) == 0:
            raise NoSuchEndpoint('No such endpoint found')

        return candidate_list[0], candidate_list

    # username
    @property<commit_msg>configuration - return EBEndpoint, extended, also with server record<commit_after>import json
import functools
from consts import *
from errors import *
from ebclient.eb_configuration import Endpoint
from ebclient.registration import *

__author__ = 'dusanklinec'


class EBEndpoint(Endpoint):
    """
    Extends normal endpoint, with added reference to the configuration
    """
    def __init__(self, scheme=None, host=None, port=None, server=None, *args, **kwargs):
        super(EBEndpoint, self).__init__(
            scheme=scheme,
            host=host,
            port=port)
        self.server = server


class Config(object):
    """Configuration object, handles file read/write"""

    def __init__(self, json_db=None, eb_config=None, *args, **kwargs):
        self.json = json_db
        self.eb_config = eb_config

        pass

    @classmethod
    def from_json(cls, json_string):
        return cls(json_db=json.loads(json_string))

    @classmethod
    def from_file(cls, file_name):
        with open(file_name, 'r') as f:
            read_lines = [x.strip() for x in f.read().split('\n')]
            lines = []
            for line in read_lines:
                if line.startswith('//'):
                    continue
                lines.append(line)

            return Config.from_json('\n'.join(lines))

    def ensure_config(self):
        if self.json is None:
            self.json = {}
        if 'config' not in self.json:
            self.json['config'] = {}

    def has_nonempty_config(self):
        return self.json is not None and 'config' in self.json and len(self.json['config']) > 0

    def get_config(self, key):
        if not self.has_nonempty_config():
            return None
        return self.json['config'][key] if key in self.json['config'] else None

    def set_config(self, key, val):
        self.ensure_config()
        self.json['config'][key] = val

    def has_identity(self):
        return self.username is not None

    def has_apikey(self):
        return self.apikey is not None

    def to_string(self):
        return json.dumps(self.json, indent=2) if self.has_nonempty_config() else ""

    def resolve_endpoint(self, purpose=SERVER_PROCESS_DATA, protocol=PROTOCOL_HTTPS, environment=None, *args, **kwargs):
        """
        Resolves required endpoint from the configuration according to the parameters
        :param purpose:
        :param protocol:
        :return:
        """
        if not self.has_nonempty_config() or self.servers is None:
            raise ValueError('Configuration has no servers')

        candidate_list = []
        for server in self.servers:
            endpoint_key = 'useEndpoints'
            if purpose == SERVER_ENROLLMENT:
                endpoint_key = 'enrolEndpoints'
            elif purpose == SERVER_REGISTRATION:
                endpoint_key = 'registerEndpoints'
            elif purpose != SERVER_PROCESS_DATA:
                raise ValueError('Endpoint purpose unknown')

            if endpoint_key not in server:
                continue
            if environment is not None and server['environment'] != environment:
                continue

            endpoints = server[endpoint_key]
            for endpoint in endpoints:
                if protocol is not None and endpoint['protocol'] != protocol:
                    continue

                # Construct a candidate
                candidate = EBEndpoint(scheme=endpoint['protocol'],
                                       host=server['fqdn'],
                                       port=endpoint['port'],
                                       server=server)

                candidate_list.append(candidate)
            pass

        if len(candidate_list) == 0:
            raise NoSuchEndpoint('No such endpoint found')

        return candidate_list[0], candidate_list

    # username
    @property
{'fraction_non_alphanumeric': 0.0907100834587495, 'fraction_numerical': 0.0010945409768778219, 'mean_word_length': 2.047102959566486, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7508288', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1898, 'n_words': 533}