Example #1
0
    def _set_store_in(self, store_in):
        if not store_in:
            self.store_in_global = {}
            self.store_in_item = {}
            return

        self.store_in_global = {
            name: parse_vars(expression)
            for name, expression in store_in.items()
            if not name.startswith('this.')
        }
        # create the global var in the blueprint
        for name in self.store_in_global:
            self.blueprint.vars.setdefault(name, [])

        self.store_in_item = {
            ValueExpression(name): parse_vars(expression)
            for name, expression in store_in.items()
            if name.startswith('this.')
        }
        # create a "store" generator on the targeted item
        for expr in self.store_in_item:
            target_name, store_name = expr.attrs.rsplit('.')[-2:]
            try:
                target = self.blueprint.items[target_name]
            except KeyError:
                raise ValidationError(
                    "Error in 'store_in' section in item '{}': "
                    "The item '{}' does not exist.".format(
                        self.name, target_name))
            target.add_field(store_name, 'Store')
Example #2
0
    def add_count(self, number=None, by=None, min=None, max=None):
        number = parse_vars(number)
        min = parse_vars(min)
        max = parse_vars(max)

        for key, value in zip(('number', 'min', 'max'), (number, min, max)):
            if value is None:
                continue
            if isinstance(value, int):
                if value < 0:
                    raise ValidationError(
                        "Item '{}' count: {} must be positive.".format(
                            self.name, key))
            elif not isinstance(value, Expression):
                raise ValidationError(
                    "Item '{}' count: {} must be an integer or a variable "
                    "(got: '{}').".format(self.name, key,
                                          type(value).__name__))

        current_count = self.count
        if current_count:
            # this item already has a count, merge it
            by = by or current_count.by
            if min is None and max is None:
                number = number or current_count.number
            if number is None:
                if min is None:
                    min = current_count.min
                if max is None:
                    max = current_count.max

        if min is not None or max is not None:
            if number is not None:
                raise ValidationError(
                    "Item '{}' count: Cannot set 'number' and 'min/max'.".
                    format(self.name))

        # treat None as 0
        min = min or 0
        max = max or 0

        if isinstance(min, int) and isinstance(max, int) and min > max:
            raise ValidationError(
                "Item '{}' count: Min is greater than max.".format(self.name))

        self.count = Count(number=number,
                           by=by,
                           min=min,
                           max=max,
                           blueprint=self.blueprint)
Example #3
0
    def get_arguments(self,
                      min_length=0,
                      max_length=None,
                      chars='<a-Z><0-9> ',
                      **kwargs):
        super(Text, self).get_arguments(**kwargs)

        if min_length is None:
            min_length = 0

        if max_length is None:
            max_length = 10000

        self.min_length = parse_vars(min_length)
        self.max_length = parse_vars(max_length)
        self.chars = self.get_chars(chars)
Example #4
0
    def get_arguments(self, min=0, max=(2**32) - 1, to_string=False, **kwargs):
        super(Integer, self).get_arguments(**kwargs)

        self.min = parse_vars(min)
        self.max = parse_vars(max)
        self.to_string = to_string
Example #5
0
def test_parse_vars(mocker):
    class Expression(object):
        def __init__(self, value):
            self.value = value

        def __eq__(self, other):
            return type(self) == type(other) and self.value == other.value

        def __repr__(self):
            return "{}('{}')".format(type(self).__name__, self.value)

    class Value(Expression):
        pass

    class JinjaValue(Expression):
        pass

    class Template(Expression):
        pass

    assert Value('foo') != Template('foo')
    assert Value('foo') != Value('bar')
    assert JinjaValue('foo') != Value('foo')
    assert Template('foo') != JinjaValue('foo')

    mocker.patch('populous.vars.ValueExpression', wraps=Value)
    mocker.patch('populous.vars.JinjaValueExpression', wraps=JinjaValue)
    mocker.patch('populous.vars.TemplateExpression', wraps=Template)

    assert parse_vars(None) is None
    assert parse_vars(0) == 0
    assert parse_vars('') == ''
    assert parse_vars('foo') == 'foo'
    assert parse_vars('foo $bar') == 'foo $bar'
    assert parse_vars(' ') == ' '
    assert parse_vars(' foo ') == ' foo '
    assert parse_vars(u'foo') == u'foo'

    assert parse_vars('$foo') == Value('foo')
    assert parse_vars('$foo.bar') == Value('foo.bar')
    assert parse_vars('$foo.bar.lol') == Value('foo.bar.lol')
    assert parse_vars('$_a2.x3_') == Value('_a2.x3_')
    assert parse_vars('$foo  ') == Value('foo')

    assert parse_vars('$(foo)') == JinjaValue('foo')
    assert parse_vars('$(foo.bar)') == JinjaValue('foo.bar')
    assert parse_vars('$(foo.bar)') == JinjaValue('foo.bar')
    assert (
        parse_vars('$(foo|random|d("foo"))') ==
        JinjaValue('foo|random|d("foo")')
    )
    assert parse_vars('$(foo)  ') == JinjaValue('foo')

    assert parse_vars(r'\$foo') == '$foo'
    assert parse_vars(r'\$foo.bar') == '$foo.bar'
    assert parse_vars(r'\$foo.bar lol') == '$foo.bar lol'

    with pytest.raises(ValidationError):
        parse_vars('$foo bar')
    with pytest.raises(ValidationError):
        parse_vars('$1foo')
    with pytest.raises(ValidationError):
        parse_vars('$foo.2bar')
    with pytest.raises(ValidationError):
        parse_vars('$foo-bar')
    with pytest.raises(ValidationError):
        parse_vars('$foo-bar')
    with pytest.raises(ValidationError):
        parse_vars('$(foo')
    with pytest.raises(ValidationError):
        parse_vars('$foo)')
    with pytest.raises(ValidationError):
        parse_vars('$(foo).')

    assert parse_vars('{{ foo }}') == Template('{{ foo }}')
    assert parse_vars('{{ foo }} bar') == Template('{{ foo }} bar')
    assert parse_vars('bar {{ foo }}') == Template('bar {{ foo }}')
    assert parse_vars('  {{ foo }}  ') == Template('  {{ foo }}  ')
    assert (
        parse_vars('{% for x in foo %}{% endfor %}') ==
        Template('{% for x in foo %}{% endfor %}')
    )
Example #6
0
 def parse_vars(self, value):
     return parse_vars(value)