<commit_before>"""
Work on cases based on XForms. In our world XForms are special couch documents.
"""
import logging

from couchdbkit.resource import ResourceNotFound
from dimagi.utils.chunked import chunked
from casexml.apps.case.exceptions import IllegalCaseId, NoDomainProvided
from casexml.apps.case import settings
from dimagi.utils.couch.database import iter_docs

from casexml.apps.case import const
from casexml.apps.case.models import CommCareCase
from casexml.apps.case.xml.parser import case_update_from_block


class CaseDbCache(object):
    """
    A temp object we use to keep a cache of in-memory cases around
    so we can get the latest updates even if they haven't been saved
    to the database. Also provides some type checking safety.
    """
    def __init__(self, domain=None, strip_history=False, initial_case_ids=None):
        self.cache = {}
        self.domain = domain
        self.strip_history = strip_history
        if initial_case_ids:
            self.populate(initial_case_ids)

    def validate_doc(self, doc):
        # some forms recycle case ids as other ids (like xform ids)
        # disallow that hard.
        if doc.doc_type not in ["CommCareCase", "CommCareCase-Deleted"]:
            raise IllegalCaseId(
                "Bad case doc type! "
                "This usually means you are using a bad value for case_id."
            )
        if self.domain and doc.domain != self.domain:
            raise IllegalCaseId("Bad case id")

    def get(self, case_id):
        if case_id in self.cache:
            return self.cache[case_id]

        try: 
            case_doc = CommCareCase.get(case_id, strip_history=self.strip_history)
        except ResourceNotFound:
            return None

        self.validate_doc(case_doc)
<commit_msg>remove unused argument / bad practice of doing work in __init__<commit_after>"""
Work on cases based on XForms. In our world XForms are special couch documents.
"""
import logging

from couchdbkit.resource import ResourceNotFound
from dimagi.utils.chunked import chunked
from casexml.apps.case.exceptions import IllegalCaseId, NoDomainProvided
from casexml.apps.case import settings
from dimagi.utils.couch.database import iter_docs

from casexml.apps.case import const
from casexml.apps.case.models import CommCareCase
from casexml.apps.case.xml.parser import case_update_from_block


class CaseDbCache(object):
    """
    A temp object we use to keep a cache of in-memory cases around
    so we can get the latest updates even if they haven't been saved
    to the database. Also provides some type checking safety.
    """
    def __init__(self, domain=None, strip_history=False):
        self.cache = {}
        self.domain = domain
        self.strip_history = strip_history

    def validate_doc(self, doc):
        # some forms recycle case ids as other ids (like xform ids)
        # disallow that hard.
        if doc.doc_type not in ["CommCareCase", "CommCareCase-Deleted"]:
            raise IllegalCaseId(
                "Bad case doc type! "
                "This usually means you are using a bad value for case_id."
            )
        if self.domain and doc.domain != self.domain:
            raise IllegalCaseId("Bad case id")

    def get(self, case_id):
        if case_id in self.cache:
            return self.cache[case_id]

        try: 
            case_doc = CommCareCase.get(case_id, strip_history=self.strip_history)
        except ResourceNotFound:
            return None

        self.validate_doc(case_doc)

{'fraction_non_alphanumeric': 0.0688385269121813, 'fraction_numerical': 0.0, 'mean_word_length': 2.776470588235294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '3797665', 'n_tokens_mistral': 985, 'n_tokens_neox': 960, 'n_words': 339}