<commit_before>import sys

try:
    from cStringIO import StringIO
except ImportError:
    try:
        from StringIO import StringIO
    except ImportError:
        from io import StringIO

from django.conf import settings
from django.utils import regex_helper, simplejson

from pushserver import signals
from pushserver.utils import urllib

# Set by runpushserver command
# Using this host fails if it is bind-all address (eg. 0.0.0.0),
# in this case settings should be manually configured
current_host = None

def publisher_url(channel):
    push_server = getattr(settings, 'PUSH_SERVER', {})
    publisher_host = push_server.get('publisher_host', {
        'location': current_host,
        'secure': False,
    })
    publisher_host_location = publisher_host['location']
    publisher_host_secure = publisher_host['secure']
    publisher_pattern = None
    for location in push_server.get('locations', ()):
        if location.get('type') == 'publisher':
            publisher_pattern = location.get('url')
            # TODO: Currently we support only the first publisher URL
            break
    if not publisher_host_location or not publisher_pattern:
        raise ValueError("Missing required settings")
    publisher = regex_helper.normalize(publisher_pattern)
    if len(publisher) != 1 or len(publisher[0][1]) != 1:
        raise ValueError("Non-reversible reg-exp: '%s'" % (publisher_pattern,))
    publisher, (arg,) = publisher[0]
    publisher = publisher % {
        arg: channel,
    }
    return 'http{0}://{1}{2}'.format(
        ('s' if publisher_host_secure else ''),
        publisher_host_location,
        publisher,
    )

def subscriber_url(channel):
    push_server = getattr(settings, 'PUSH_SERVER', {})
    subscriber_host = push_server.get('subscriber_host', {
        'location': current_host,
        'secure': False,
    })
    subscriber_host_location = subscriber_host['location']
    subscriber_host_secure = subscriber_host['secure']
    subscriber_pattern = None
    for location in push_server.get('locations', ()):
        if location.get('type') == 'subscriber':
            subscriber_pattern = location.get('url')
            # TODO: Currently we support only the first subscriber URL
            break
    if not subscriber_host_location or not subscriber_pattern:
        raise ValueError("Missing required settings")
    subscriber = regex_helper.normalize(subscriber_pattern)
    if len(subscriber) != 1 or len(subscriber[0][1]) != 1:
        raise ValueError("Non-reversible reg-exp: '%s'" % (subscriber_pattern,))
    subscriber, (arg,) = subscriber[0]
    subscriber = subscriber % {
        arg: channel,
    }
    return 'http{0}://{1}{2}'.format(
        ('s' if subscriber_host_secure else ''),
        subscriber_host_location,
        subscriber,
    )

def send_update(channel_id, data, already_serialized=False, ignore_errors=False):
    if already_serialized:<commit_msg>Updates according to @mitar recommandations<commit_after>import sys

try:
    from cStringIO import StringIO
except ImportError:
    try:
        from StringIO import StringIO
    except ImportError:
        from io import StringIO

from django.conf import settings
from django.utils import regex_helper, simplejson

from pushserver import signals
from pushserver.utils import urllib

# Set by runpushserver command
# Using this host fails if it is bind-all address (eg. 0.0.0.0),
# in this case settings should be manually configured
current_host = None

def publisher_url(channel):
    push_server = getattr(settings, 'PUSH_SERVER', {})
    publisher_host = push_server.get('publisher_host', {})
    publisher_host_location = publisher_host.get('location', current_host)
    publisher_host_secure = publisher_host.get('secure', False)
    publisher_pattern = None
    for location in push_server.get('locations', ()):
        if location.get('type') == 'publisher':
            publisher_pattern = location.get('url')
            # TODO: Currently we support only the first publisher URL
            break
    if not publisher_host_location or not publisher_pattern:
        raise ValueError("Missing required settings")
    publisher = regex_helper.normalize(publisher_pattern)
    if len(publisher) != 1 or len(publisher[0][1]) != 1:
        raise ValueError("Non-reversible reg-exp: '%s'" % (publisher_pattern,))
    publisher, (arg,) = publisher[0]
    publisher = publisher % {
        arg: channel,
    }
    return 'http{0}://{1}{2}'.format(
        ('s' if publisher_host_secure else ''),
        publisher_host_location,
        publisher,
    )

def subscriber_url(channel):
    push_server = getattr(settings, 'PUSH_SERVER', {})
    subscriber_host = push_server.get('subscriber_host', {})
    subscriber_host_location = subscriber_host.get('location', current_host)
    subscriber_host_secure = subscriber_host.get('secure', False)
    subscriber_pattern = None
    for location in push_server.get('locations', ()):
        if location.get('type') == 'subscriber':
            subscriber_pattern = location.get('url')
            # TODO: Currently we support only the first subscriber URL
            break
    if not subscriber_host_location or not subscriber_pattern:
        raise ValueError("Missing required settings")
    subscriber = regex_helper.normalize(subscriber_pattern)
    if len(subscriber) != 1 or len(subscriber[0][1]) != 1:
        raise ValueError("Non-reversible reg-exp: '%s'" % (subscriber_pattern,))
    subscriber, (arg,) = subscriber[0]
    subscriber = subscriber % {
        arg: channel,
    }
    return 'http{0}://{1}{2}'.format(
        ('s' if subscriber_host_secure else ''),
        subscriber_host_location,
        subscriber,
    )

def send_update(channel_id, data, already_serialized=False, ignore_errors=False):
    if already_serialized:
{'fraction_non_alphanumeric': 0.105217988328184, 'fraction_numerical': 0.006865774116031583, 'mean_word_length': 3.4788624135280553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6930907', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1661, 'n_words': 433}