<commit_before>import os
import re
import subprocess
import yaml

from charmhelpers.core import host
from charmhelpers.core import hookenv
from charmhelpers.core.services import RelationContext
from cloudfoundry.releases import RELEASES


class StoredContext(dict):
    """
    A data context that always returns the data that it was first created with.
    """
    def __init__(self, file_name, config_data):
        """
        If the file exists, populate `self` with the data from the file.
        Otherwise, populate with the given data and persist it to the file.
        """
        if os.path.exists(file_name):
            self.update(self.read_context(file_name))
        else:
            self.store_context(file_name, config_data)
            self.update(config_data)

    def store_context(self, file_name, config_data):
        with open(file_name, 'w') as file_stream:
            yaml.dump(config_data, file_stream)

    def read_context(self, file_name):
        with open(file_name, 'r') as file_stream:
            data = yaml.load(file_stream)
            if not data:
                raise OSError("%s is empty" % file_name)
            return data


class NatsRelation(RelationContext):
    name = 'nats'
    interface = 'nats'
    required_keys = ['address', 'port', 'user', 'password']

    def get_credentials(self):
        return StoredContext(
            'nats_credentials.yml', {
                'user': host.pwgen(7),
                'password': host.pwgen(7),
            })

    def provide_data(self):
        return dict(self.get_credentials(),
                    port=4222,
                    address=hookenv.unit_get(
                        'private-address').encode('utf-8'))

    def erb_mapping(self):
        data = self[self.name]
        return {
            'nats.machines': [u['address'] for u in data],
            'nats.port': data[0]['port'],
            'nats.user': data[0]['user'],
            'nats.password': data[0]['password'],
        }


class MysqlRelation(RelationContext):
    name = 'db'
    interface = 'mysql'
    required_keys = ['user', 'password', 'host', 'database']
    dsn_template = "mysql2://{user}:{password}@{host}:{port}/{database}"

    def get_data(self):
        RelationContext.get_data(self)
        if self.is_ready():
            for unit in self['db']:
                if 'port' not in unit:
                    unit['port'] = '3306'
                unit['dsn'] = self.dsn_template.format(**unit)


class ClockRelation(RelationContext):
    name = 'clock'
    interface = 'clock'
    required_keys = []


class UAARelation(RelationContext):
    name = 'uaa'
    interface = 'http'
    required_keys = []


class SyslogAggregatorRelation(RelationContext):
    name = 'syslog_aggregator'
    interface = 'syslog_aggregator'
    required_keys = ['address', 'port', 'transport', 'all']

    def provide_data(self):
        return {}


class LoginRelation(RelationContext):
    name = 'login'
    interface = 'http'
    required_keys = []


class DEARelation(RelationContext):
    name = 'dea'
    interface = 'dea'
    required_keys = []


class RouterRelation(RelationContext):
    name = 'router'
    interface = 'router'
    required_keys = ['domain']<commit_msg>remove syslog context<commit_after>import os
import re
import subprocess
import yaml

from charmhelpers.core import host
from charmhelpers.core import hookenv
from charmhelpers.core.services import RelationContext
from cloudfoundry.releases import RELEASES


class StoredContext(dict):
    """
    A data context that always returns the data that it was first created with.
    """
    def __init__(self, file_name, config_data):
        """
        If the file exists, populate `self` with the data from the file.
        Otherwise, populate with the given data and persist it to the file.
        """
        if os.path.exists(file_name):
            self.update(self.read_context(file_name))
        else:
            self.store_context(file_name, config_data)
            self.update(config_data)

    def store_context(self, file_name, config_data):
        with open(file_name, 'w') as file_stream:
            yaml.dump(config_data, file_stream)

    def read_context(self, file_name):
        with open(file_name, 'r') as file_stream:
            data = yaml.load(file_stream)
            if not data:
                raise OSError("%s is empty" % file_name)
            return data


class NatsRelation(RelationContext):
    name = 'nats'
    interface = 'nats'
    required_keys = ['address', 'port', 'user', 'password']

    def get_credentials(self):
        return StoredContext(
            'nats_credentials.yml', {
                'user': host.pwgen(7),
                'password': <PASSWORD>),
            })

    def provide_data(self):
        return dict(self.get_credentials(),
                    port=4222,
                    address=hookenv.unit_get(
                        'private-address').encode('utf-8'))

    def erb_mapping(self):
        data = self[self.name]
        return {
            'nats.machines': [u['address'] for u in data],
            'nats.port': data[0]['port'],
            'nats.user': data[0]['user'],
            'nats.password': data[0]['password'],
        }


class MysqlRelation(RelationContext):
    name = 'db'
    interface = 'mysql'
    required_keys = ['user', 'password', 'host', 'database']
    dsn_template = "mysql2://{user}:{password}@{host}:{port}/{database}"

    def get_data(self):
        RelationContext.get_data(self)
        if self.is_ready():
            for unit in self['db']:
                if 'port' not in unit:
                    unit['port'] = '3306'
                unit['dsn'] = self.dsn_template.format(**unit)


class ClockRelation(RelationContext):
    name = 'clock'
    interface = 'clock'
    required_keys = []


class UAARelation(RelationContext):
    name = 'uaa'
    interface = 'http'
    required_keys = []


class LoginRelation(RelationContext):
    name = 'login'
    interface = 'http'
    required_keys = []


class DEARelation(RelationContext):
    name = 'dea'
    interface = 'dea'
    required_keys = []


class RouterRelation(RelationContext):
    name = 'router'
    interface = 'router'
    required_keys = ['domain']
{'fraction_non_alphanumeric': 0.11789071212362594, 'fraction_numerical': 0.004620041421061016, 'mean_word_length': 2.4212534059945505, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '1647203', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1847, 'n_words': 449}