How to use the pystachio.parsing.MustacheParser.split function in pystachio

To help you get started, we’ve selected a few pystachio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wickman / pystachio / tests / test_parsing.py View on Github external
def test_mustache_re():
  assert MustacheParser.split("{{foo}}") == [ref("foo")]
  assert MustacheParser.split("{{_}}") == [ref("_")]
  with pytest.raises(Ref.InvalidRefError):
    MustacheParser.split("{{4}}")
  def chrange(a,b):
    return ''.join(map(lambda ch: str(chr(ch)), range(ord(a), ord(b)+1)))
  slash_w = chrange('a','z') + chrange('A','Z') + chrange('0','9') + '_'
  assert MustacheParser.split("{{%s}}" % slash_w) == [ref(slash_w)]

  # bracketing
  assert MustacheParser.split("{{{foo}}") == ['{', ref('foo')]
  assert MustacheParser.split("{{foo}}}") == [ref('foo'), '}']
  assert MustacheParser.split("{{{foo}}}") == ['{', ref('foo'), '}']
  assert MustacheParser.split("{{}}") == ['{{}}']
  assert MustacheParser.split("{{{}}}") == ['{{{}}}']
  assert MustacheParser.split("{{{{foo}}}}") == ['{{', ref("foo"), '}}']

  invalid_refs = ['!@', '-', '$', ':']
  for val in invalid_refs:
    with pytest.raises(Ref.InvalidRefError):
      MustacheParser.split("{{%s}}" % val)
github wickman / pystachio / tests / test_parsing.py View on Github external
def test_mustache_joining():
  oe = Environment(foo = "foo herp",
                   bar = "bar derp",
                   baz = "baz blerp")

  joined, unbound = MustacheParser.join(MustacheParser.split("{{foo}}"), oe)
  assert joined == "foo herp"
  assert unbound == []

  splits = MustacheParser.split('blech {{foo}} {{bar}} bonk {{&baz}} bling')
  joined, unbound = MustacheParser.join(splits, oe)
  assert joined == 'blech foo herp bar derp bonk {{baz}} bling'
  assert unbound == []

  splits = MustacheParser.split('{{foo}} {{bar}} {{unbound}}')
  joined, unbound = MustacheParser.join(splits, oe)
  assert joined == 'foo herp bar derp {{unbound}}'
  assert unbound == [Ref.from_address('unbound')]
github wickman / pystachio / tests / test_parsing.py View on Github external
def test_mustache_re():
  assert MustacheParser.split("{{foo}}") == [ref("foo")]
  assert MustacheParser.split("{{_}}") == [ref("_")]
  with pytest.raises(Ref.InvalidRefError):
    MustacheParser.split("{{4}}")
  def chrange(a,b):
    return ''.join(map(lambda ch: str(chr(ch)), range(ord(a), ord(b)+1)))
  slash_w = chrange('a','z') + chrange('A','Z') + chrange('0','9') + '_'
  assert MustacheParser.split("{{%s}}" % slash_w) == [ref(slash_w)]

  # bracketing
  assert MustacheParser.split("{{{foo}}") == ['{', ref('foo')]
  assert MustacheParser.split("{{foo}}}") == [ref('foo'), '}']
  assert MustacheParser.split("{{{foo}}}") == ['{', ref('foo'), '}']
  assert MustacheParser.split("{{}}") == ['{{}}']
  assert MustacheParser.split("{{{}}}") == ['{{{}}}']
  assert MustacheParser.split("{{{{foo}}}}") == ['{{', ref("foo"), '}}']

  invalid_refs = ['!@', '-', '$', ':']
  for val in invalid_refs:
    with pytest.raises(Ref.InvalidRefError):
      MustacheParser.split("{{%s}}" % val)
github wickman / pystachio / tests / test_parsing.py View on Github external
def test_mustache_re():
  assert MustacheParser.split("{{foo}}") == [ref("foo")]
  assert MustacheParser.split("{{_}}") == [ref("_")]
  with pytest.raises(Ref.InvalidRefError):
    MustacheParser.split("{{4}}")
  def chrange(a,b):
    return ''.join(map(lambda ch: str(chr(ch)), range(ord(a), ord(b)+1)))
  slash_w = chrange('a','z') + chrange('A','Z') + chrange('0','9') + '_'
  assert MustacheParser.split("{{%s}}" % slash_w) == [ref(slash_w)]

  # bracketing
  assert MustacheParser.split("{{{foo}}") == ['{', ref('foo')]
  assert MustacheParser.split("{{foo}}}") == [ref('foo'), '}']
  assert MustacheParser.split("{{{foo}}}") == ['{', ref('foo'), '}']
  assert MustacheParser.split("{{}}") == ['{{}}']
  assert MustacheParser.split("{{{}}}") == ['{{{}}}']
  assert MustacheParser.split("{{{{foo}}}}") == ['{{', ref("foo"), '}}']

  invalid_refs = ['!@', '-', '$', ':']
github wickman / pystachio / pystachio / objects.py View on Github external
def interpolate(self):
    if not isinstance(self._value, basestring):
      return self.__class__(self.coerce(self._value)), []
    else:
      splits = MustacheParser.split(self._value)
      joins, unbound = MustacheParser.join(splits, *self.scopes(), strict=False)
      if unbound:
        return self.__class__(joins), unbound
      else:
        self_copy = self.copy()
        # TODO(wickman) Are these actually the correct semantics?
        if hasattr(self_copy, 'coerce') and callable(self_copy.coerce):
          self_copy._value = self_copy.coerce(joins)
        else:
          self_copy._value = joins
        return self_copy, unbound