<commit_before>
import string, sys, copy, os, os.path
import xmlparser
import mk
import utils
import errors
from errors import ReaderError
import config

def evalConstExpr(e, str, target=None):
    try:
        return mk.evalExpr(str, use_options=0, target=target)
    except NameError, err:
        raise ReaderError(e, "can't use options or conditional variables in this context (%s)" % err)


def evalWeakCondition(e):
    """Evaluates e's 'cond' property, if present, and returns 0 or 1 if it
       can be evaluated to a constant. If it can't (i.e. it is a strong
       condition) do it, raises exception."""
    if 'cond' not in e.props:
        return 1
    condstr = e.props['cond']
    typ = mk.evalCondition(condstr)
    # Condition never met when generating this target:
    if typ == '0':
        return 0
    # Condition always met:
    elif typ == '1':
        return 1
    else:
        raise ReaderError(e,
                "'%s': only weak condition allowed in this context" % condstr)


def handleSet(e, target=None, add_dict=None):
    errors.pushCtx(e)
    name = e.props['var']
    if (name in mk.override_vars) and target == None:
        errors.popCtx()
        return # can't change value of variable overriden with -D=xxx
    
    doEval = not ('eval' in e.props and e.props['eval'] == '0')
    overwrite = not ('overwrite' in e.props and e.props['overwrite'] == '0')
    isCond = (len(e.children) > 0)
    value = e.value

    # Handle conditions:
    if isCond:
        noValueSet = 1
        for e_if in e.children:
            if e_if.name != 'if':
                raise ReaderError(e_if, "malformed <set> command")
        
            # Preprocess always true or always false conditions:
            condstr = e_if.props['cond']
            if condstr == 'target':
                if target == None:
                    raise ReaderError(e_if, "'target' condition can't be used at global scope")
                cond = target.cond
                if cond == None:
                    isCond = 0
                    value = e_if.value
                    break
            else:
                typ = mk.evalCondition(condstr)
                # Condition never met when generating this target:
                if typ == '0':
                    continue
                # Condition always met:
                elif typ == '1':
                    isCond = 0
                    value = e_if.value
                    break
                elif typ != None:
                    raise ReaderError(e, "malformed condition: '%s'" % condstr)<commit_msg>fix for previous coomit<commit_after>
import string, sys, copy, os, os.path
import xmlparser
import mk
import utils
import errors
from errors import ReaderError
import config

def evalConstExpr(e, str, target=None):
    try:
        return mk.evalExpr(str, use_options=0, target=target)
    except NameError, err:
        raise ReaderError(e, "can't use options or conditional variables in this context (%s)" % err)


def evalWeakCondition(e):
    """Evaluates e's 'cond' property, if present, and returns 0 or 1 if it
       can be evaluated to a constant. If it can't (i.e. it is a strong
       condition) do it, raises exception."""
    if 'cond' not in e.props:
        return 1
    condstr = e.props['cond']
    typ = mk.evalCondition(condstr)
    # Condition never met when generating this target:
    if typ == '0':
        return 0
    # Condition always met:
    elif typ == '1':
        return 1
    else:
        raise ReaderError(e,
                "'%s': only weak condition allowed in this context" % condstr)


def handleSet(e, target=None, add_dict=None):
    errors.pushCtx(e)
    name = e.props['var']
    if (name in mk.override_vars) and target == None:
        errors.popCtx()
        return # can't change value of variable overriden with -D=xxx
    
    doEval = not ('eval' in e.props and e.props['eval'] == '0')
    overwrite = not ('overwrite' in e.props and e.props['overwrite'] == '0')
    isCond = (len(e.children) > 0)
    value = e.value

    # Handle conditions:
    if isCond:
        noValueSet = 1
        for e_if in e.children:
            if e_if.name != 'if':
                raise ReaderError(e_if, "malformed <set> command")
        
            # Preprocess always true or always false conditions:
            condstr = e_if.props['cond']
            if condstr == 'target':
                if target == None:
                    raise ReaderError(e_if, "'target' condition can't be used at global scope")
                cond = target.cond
                if cond == None:
                    isCond = 0
                    value = e_if.value
                    break
            else:
                typ = mk.evalCondition(condstr)
                # Condition never met when generating this target:
                if typ == '0':
                    continue
                # Condition always met:
                elif typ == '1':
                    noValueSet = 0
                    isCond = 0
                    value = e_if.value
                    break
                elif typ != None:
                    raise ReaderError(e, "malformed condition: '%s'" % condstr)
{'fraction_non_alphanumeric': 0.09594308786771774, 'fraction_numerical': 0.006344933666602577, 'mean_word_length': 1.759681697612732, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4973405', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1410, 'n_words': 514}