Example #1
0
def if_(condition, if_true, if_false=''):
    condition = bool(
        False if not condition or isinstance(condition, six.string_types) and
        (condition in ('0', 'false', 'undefined'
                       ) or _variable_re.match(condition)) else condition)
    return if_true.__class__(if_true) if condition else if_true.__class__(
        if_false)
Example #2
0
    def evaluate_expression(self, expr, divide=False):
        if not isinstance(expr, six.string_types):
            raise TypeError("Expected string, got %r" % (expr, ))

        if expr in ast_cache:
            ast = ast_cache[expr]

        elif _variable_re.match(expr):
            # Short-circuit for variable names
            ast = Variable(expr)

        else:
            try:
                P = SassExpression(SassExpressionScanner())
                P.reset(expr)
                ast = P.goal()
            except SyntaxError:
                if config.DEBUG:
                    raise
                return None
            except Exception:
                # TODO hoist me up since the rule is gone
                #log.exception("Exception raised: %s in `%s' (%s)", e, expr, rule.file_and_line)
                if config.DEBUG:
                    raise
                return None
            else:
                ast_cache[expr] = ast

        return ast.evaluate(self, divide=divide)
Example #3
0
File: core.py Project: eevee/pyScss
def _type_of(obj):  # -> bool, number, string, color, list
    if isinstance(obj, BooleanValue):
        return StringValue('bool')
    if isinstance(obj, NumberValue):
        return StringValue('number')
    if isinstance(obj, ColorValue):
        return StringValue('color')
    if isinstance(obj, ListValue):
        return StringValue('list')
    if isinstance(obj, basestring) and _variable_re.match(obj):
        return StringValue('undefined')
    return StringValue('string')
Example #4
0
def eval_expr(expr, rule, library, raw=False):
    # print >>sys.stderr, '>>',expr,'<<'
    results = None

    if not isinstance(expr, basestring):
        results = expr

    if results is None:
        if _variable_re.match(expr):
            expr = normalize_var(expr)
        if expr in rule.context:
            chkd = {}
            while expr in rule.context and expr not in chkd:
                chkd[expr] = 1
                _expr = rule.context[expr]
                if _expr == expr:
                    break
                expr = _expr
        if not isinstance(expr, basestring):
            results = expr

    if results is None:
        if expr in expr_cache:
            results = expr_cache[expr]
        else:
            try:
                P = Calculator(CalculatorScanner(), library)
                P.reset(expr)
                results = P.goal(rule)
            except SyntaxError:
                if config.DEBUG:
                    raise
            except Exception, e:
                log.exception("Exception raised: %s in `%s' (%s)", e, expr, rule.file_and_line)
                if config.DEBUG:
                    raise

            # TODO this is a clumsy hack for nondeterministic functions;
            # something better (and per-compiler rather than global) would be
            # nice
            if '$' not in expr and '(' not in expr:
                expr_cache[expr] = results
Example #5
0
def if_(condition, if_true, if_false=''):
    condition = bool(False if not condition or isinstance(condition, six.string_types) and (condition in ('0', 'false', 'undefined') or _variable_re.match(condition)) else condition)
    return if_true.__class__(if_true) if condition else if_true.__class__(if_false)