<commit_before>from collections import namedtuple
from corehq.apps.app_manager.xpath_validator.config import get_xpath_validator_path
from corehq.apps.app_manager.xpath_validator.exceptions import XpathValidationError
from dimagi.utils.subprocess_manager import subprocess_context

XpathValidationResponse = namedtuple('XpathValidationResponse', ['is_valid', 'message'])


def validate_xpath(xpath, allow_case_hashtags=False):
    with subprocess_context() as subprocess:
        path = get_xpath_validator_path()
        if allow_case_hashtags:
            cmd = ['node', path, '--allow-case-hashtags']
        else:
            cmd = ['node', path]
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate(xpath)
        exit_code = p.wait()
        if exit_code == 0:
            return XpathValidationResponse(is_valid=True, message=None)<commit_msg>encode xpath<commit_after>from collections import namedtuple
from corehq.apps.app_manager.xpath_validator.config import get_xpath_validator_path
from corehq.apps.app_manager.xpath_validator.exceptions import XpathValidationError
from dimagi.utils.subprocess_manager import subprocess_context

XpathValidationResponse = namedtuple('XpathValidationResponse', ['is_valid', 'message'])


def validate_xpath(xpath, allow_case_hashtags=False):
    with subprocess_context() as subprocess:
        path = get_xpath_validator_path()
        if allow_case_hashtags:
            cmd = ['node', path, '--allow-case-hashtags']
        else:
            cmd = ['node', path]
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate(xpath.encode('utf-8'))
        exit_code = p.wait()
        if exit_code == 0:
            return XpathValidationResponse(is_valid=True, message=None)
{'fraction_non_alphanumeric': 0.09309462915601023, 'fraction_numerical': 0.001534526854219949, 'mean_word_length': 3.6132075471698113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '669980', 'n_tokens_mistral': 587, 'n_tokens_neox': 585, 'n_words': 110}