<commit_before>import json
from django.conf import settings
from kafka import KeyedProducer
from kafka.common import KafkaUnavailableError
from casexml.apps.case.models import CommCareCase
from corehq.apps.change_feed import data_sources
from corehq.apps.change_feed.connection import get_kafka_client
from corehq.apps.change_feed.models import ChangeMeta
from corehq.apps.change_feed.topics import get_topic
from couchforms.models import all_known_formlike_doc_types
import logging
from pillowtop.couchdb import CachedCouchDB
from pillowtop.listener import PythonPillow


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db)
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)

    def get_db_name(self):
        return self.couch_db.dbname

    def process_change(self, change, is_retry_attempt=False):
        document_type = _get_document_type(change.document)
        if document_type:
            assert change.document is not None
            change_meta = ChangeMeta(
                document_id=change.id,
                data_source_type=data_sources.COUCH,
                data_source_name=self.get_db_name(),
                document_type=document_type,
                document_subtype=_get_document_subtype(change.document),
                domain=change.document.get('domain', None),
                is_deletion=change.deleted,
            )
            self._producer.send_messages(
                bytes(get_topic(document_type)),
                bytes(change_meta.domain),
                bytes(json.dumps(change_meta.to_json())),
            )


def get_default_couch_db_change_feed_pillow():
    default_couch_db = CachedCouchDB(CommCareCase.get_db().uri, readonly=False)
    try:
        kafka_client = get_kafka_client()
    except KafkaUnavailableError:
        if settings.UNIT_TESTING:
            logging.info('Ignoring missing kafka client during unit testing')
            kafka_client = None
        else:
            raise

    return ChangeFeedPillow(couch_db=default_couch_db, kafka=kafka_client)


def _get_document_type(document_or_none):
    return document_or_none.get('doc_type', None) if document_or_none else None


def _get_document_subtype(document_or_none):
    type = _get_document_type(document_or_none)
    if type in ('CommCareCase', 'CommCareCase-Deleted'):<commit_msg>more leniency when initializing kafka pillows and kafka is not available<commit_after>import json
from django.conf import settings
from kafka import KeyedProducer
from kafka.common import KafkaUnavailableError
from casexml.apps.case.models import CommCareCase
from corehq.apps.change_feed import data_sources
from corehq.apps.change_feed.connection import get_kafka_client
from corehq.apps.change_feed.models import ChangeMeta
from corehq.apps.change_feed.topics import get_topic
from couchforms.models import all_known_formlike_doc_types
import logging
from pillowtop.couchdb import CachedCouchDB
from pillowtop.listener import PythonPillow


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db)
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)

    def get_db_name(self):
        return self.couch_db.dbname

    def process_change(self, change, is_retry_attempt=False):
        document_type = _get_document_type(change.document)
        if document_type:
            assert change.document is not None
            change_meta = ChangeMeta(
                document_id=change.id,
                data_source_type=data_sources.COUCH,
                data_source_name=self.get_db_name(),
                document_type=document_type,
                document_subtype=_get_document_subtype(change.document),
                domain=change.document.get('domain', None),
                is_deletion=change.deleted,
            )
            self._producer.send_messages(
                bytes(get_topic(document_type)),
                bytes(change_meta.domain),
                bytes(json.dumps(change_meta.to_json())),
            )


def get_default_couch_db_change_feed_pillow():
    default_couch_db = CachedCouchDB(CommCareCase.get_db().uri, readonly=False)
    try:
        kafka_client = get_kafka_client()
    except KafkaUnavailableError:
        logging.warning('Ignoring missing kafka client during unit testing')
        kafka_client = None

    return ChangeFeedPillow(couch_db=default_couch_db, kafka=kafka_client)


def _get_document_type(document_or_none):
    return document_or_none.get('doc_type', None) if document_or_none else None


def _get_document_subtype(document_or_none):
    type = _get_document_type(document_or_none)
    if type in ('CommCareCase', 'CommCareCase-Deleted'):
{'fraction_non_alphanumeric': 0.06972439325380501, 'fraction_numerical': 0.0, 'mean_word_length': 3.3889891696750905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4311743', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1446, 'n_words': 275}