How to use the pystachio.naming.Namable 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_base.py View on Github external
def test_environment_find():
  oe1 = Environment(a = { 'b': 1 })
  oe2 = Environment(a = { 'b': { 'c': List(Integer)([1,2,3]) } } )
  oe = Environment(oe1, oe2)
  assert oe.find(ref('a.b')) == '1'
  assert oe.find(ref('a.b.c[0]')) == Integer(1)
  assert oe.find(ref('a.b.c[1]')) == Integer(2)
  assert oe.find(ref('a.b.c[2]')) == Integer(3)

  missing_refs = [ref('b'), ref('b.c'), ref('a.c'), ref('a.b.c[3]')]
  for r in missing_refs:
    with pytest.raises(Namable.NotFound):
      oe.find(r)

  oe = Environment(a = { 'b': { 'c': 5 } } )
  assert oe.find(ref('a.b.c')) == '5'
github wickman / pystachio / tests / test_container_types.py View on Github external
def test_list_find():
  ls = List(String)(['a', 'b', 'c'])
  assert ls.find(ref('[0]')) == String('a')
  with pytest.raises(Namable.NamingError):
    ls.find(ref('.a'))
  with pytest.raises(Namable.NamingError):
    ls.find(ref('[a]'))
  with pytest.raises(Namable.NotFound):
    ls.find(ref('[4]'))
  with pytest.raises(Namable.Unnamable):
    ls.find(ref('[1].foo'))
github wickman / pystachio / pystachio / container.py View on Github external
def find(self, ref):
    if not ref.is_index():
      raise Namable.NamingError(self, ref)
    kvalue = self.KEYTYPE(ref.action().value)
    scopes = self.scopes()
    for key, namable in self._map:
      if kvalue == key:
        if ref.rest().is_empty():
          return namable.in_scope(*scopes)
        else:
          if not isinstance(namable, Namable):
            raise Namable.Unnamable(namable)
          else:
            return namable.in_scope(*scopes).find(ref.rest())
    raise Namable.NotFound(self, ref)
github wickman / pystachio / pystachio / base.py View on Github external
def translate_to_scopes(*args, **kw):
    scopes = [arg if isinstance(arg, Namable) else Environment.wrap(arg)
              for arg in args]
    if kw:
      scopes.append(Environment(kw))
    return tuple(scopes)
github wickman / pystachio / pystachio / container.py View on Github external
def find(self, ref):
    if not ref.is_index():
      raise Namable.NamingError(self, ref)
    kvalue = self.KEYTYPE(ref.action().value)
    scopes = self.scopes()
    for key, namable in self._map:
      if kvalue == key:
        if ref.rest().is_empty():
          return namable.in_scope(*scopes)
        else:
          if not isinstance(namable, Namable):
            raise Namable.Unnamable(namable)
          else:
            return namable.in_scope(*scopes).find(ref.rest())
    raise Namable.NotFound(self, ref)
github wickman / pystachio / pystachio / objects.py View on Github external
def translate_to_scopes(*args, **kw):
    scopes = []
    for arg in args:
      if isinstance(arg, Namable):
        scopes.insert(0, arg)
      else:
        scopes.insert(0, Environment.wrap(arg))
    if kw:
      scopes.insert(0, Environment(**kw))
    return scopes
github wickman / pystachio / pystachio / naming.py View on Github external
def __init__(self, obj, ref):
      super(Namable.NamingError, self).__init__('Cannot dereference object %s by %s' % (
          obj.__class__.__name__, ref.action()))
github wickman / pystachio / pystachio / parsing.py View on Github external
:params namables: A sequence of Namable objects in which the interpolation should take place.

      Returns 2-tuple containing:
        joined string, list of unbound object ids (potentially empty)
    """
    isplits = []
    unbound = []
    for ref in splits:
      if isinstance(ref, Ref):
        resolved = False
        for namable in namables:
          try:
            value = namable.find(ref)
            resolved = True
            break
          except Namable.Error:
            continue
        if resolved:
          isplits.append(value)
        else:
          isplits.append(ref)
          unbound.append(ref)
      else:
        isplits.append(ref)
    return (''.join(map(str if Compatibility.PY3 else unicode, isplits)), unbound)
github wickman / pystachio / pystachio / container.py View on Github external
def find(self, ref):
    if not ref.is_index():
      raise Namable.NamingError(self, ref)
    kvalue = self.KEYTYPE(ref.action().value)
    scopes = self.scopes()
    for key, namable in self._map:
      if kvalue == key:
        if ref.rest().is_empty():
          return namable.in_scope(*scopes)
        else:
          if not isinstance(namable, Namable):
            raise Namable.Unnamable(namable)
          else:
            return namable.in_scope(*scopes).find(ref.rest())
    raise Namable.NotFound(self, ref)
github wickman / pystachio / pystachio / base.py View on Github external
import copy
from pprint import pformat

from .compatibility import Compatibility
from .naming import Namable, Ref
from .parsing import MustacheParser
from .typing import TypeCheck


class Environment(Namable):
  """
    A mount table for Refs pointing to Objects or arbitrary string substitutions.
  """
  __slots__ = ('_table',)

  @staticmethod
  def wrap(value):
    if isinstance(value, dict):
      return Environment(value)
    elif isinstance(value, (Environment, Object)):
      return value
    else:
      if isinstance(value, Compatibility.numeric + Compatibility.stringy):
        return str(value)
      else:
        raise ValueError(