<commit_before># -*- coding: UTF-8 -*-
from collections import defaultdict
import itertools
import networkx as nx

import db

from x import utils


CLIENTS_CONFIG_KEY = 'clients-data-file'
CLIENTS = utils.read_config_file(CLIENTS_CONFIG_KEY)


def guess_mapping(emitter, receiver):
    """Guess connection mapping between emitter and receiver.

    Suppose emitter and receiver have inputs:
    ip, ssh_key, ssh_user

    Then we return a connection mapping like this:

    {
        'ip': '<receiver>.ip',
        'ssh_key': '<receiver>.ssh_key',
        'ssh_user': '<receiver>.ssh_user'
    }

    :param emitter:
    :param receiver:
    :return:
    """
    guessed = {}
    for key in emitter.requires:
        if key in receiver.requires:
            guessed[key] = key

    return guessed


def connect(emitter, receiver, mapping=None):
    mapping = mapping or {}
    guessed = guess_mapping(emitter, receiver)
    guessed.update(mapping)

    for src, dst in guessed.items():
        # Disconnect all receiver inputs
        # Check if receiver input is of list type first
        if receiver.input_types.get(dst, '') != 'list':
            disconnect_receiver_by_input(receiver, dst)

        CLIENTS.setdefault(emitter.name, {})
        CLIENTS[emitter.name].setdefault(src, [])
        CLIENTS[emitter.name][src].append((receiver.name, dst))

        # Copy emitter's values to receiver
        if src in emitter.args:
            receiver.update({dst: emitter.args[src]}, emitter=emitter)

    receiver.save()
    utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)


def disconnect(emitter, receiver):
    for src, destinations in CLIENTS[emitter.name].items():
        destinations = [
            destination for destination in destinations<commit_msg>Connect -- provided mapping overrides guessed<commit_after># -*- coding: UTF-8 -*-
from collections import defaultdict
import itertools
import networkx as nx

import db

from x import utils


CLIENTS_CONFIG_KEY = 'clients-data-file'
CLIENTS = utils.read_config_file(CLIENTS_CONFIG_KEY)


def guess_mapping(emitter, receiver):
    """Guess connection mapping between emitter and receiver.

    Suppose emitter and receiver have inputs:
    ip, ssh_key, ssh_user

    Then we return a connection mapping like this:

    {
        'ip': '<receiver>.ip',
        'ssh_key': '<receiver>.ssh_key',
        'ssh_user': '<receiver>.ssh_user'
    }

    :param emitter:
    :param receiver:
    :return:
    """
    guessed = {}
    for key in emitter.requires:
        if key in receiver.requires:
            guessed[key] = key

    return guessed


def connect(emitter, receiver, mapping=None):
    #mapping = mapping or {}
    guessed = guess_mapping(emitter, receiver)
    #guessed.update(mapping)
    mapping = mapping or guessed

    for src, dst in guessed.items():
        # Disconnect all receiver inputs
        # Check if receiver input is of list type first
        if receiver.input_types.get(dst, '') != 'list':
            disconnect_receiver_by_input(receiver, dst)

        CLIENTS.setdefault(emitter.name, {})
        CLIENTS[emitter.name].setdefault(src, [])
        CLIENTS[emitter.name][src].append((receiver.name, dst))

        # Copy emitter's values to receiver
        if src in emitter.args:
            receiver.update({dst: emitter.args[src]}, emitter=emitter)

    receiver.save()
    utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)


def disconnect(emitter, receiver):
    for src, destinations in CLIENTS[emitter.name].items():
        destinations = [
            destination for destination in destinations
{'fraction_non_alphanumeric': 0.10218775962337302, 'fraction_numerical': 0.0005538631957906397, 'mean_word_length': 2.9823594266813673, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6384108', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1035, 'n_words': 289}