Exemplo n.º 1
0
    def _update_scalar(self, cur, new, path):
        if self.delimiter is None or not isinstance(
                new, (types.StringTypes, RefValue)):
            # either there is no delimiter defined (and hence no references
            # are being used), or the new value is not a string (and hence
            # cannot be turned into a RefValue), and not a RefValue. We can
            # shortcut and just return the new scalar
            return new

        elif isinstance(new, RefValue):
            # the new value is (already) a RefValue, so we need not touch it
            # at all
            ret = new

        else:
            # the new value is a string, let's see if it contains references,
            # by way of wrapping it in a RefValue and querying the result
            ret = RefValue(new, self.delimiter)
            if not ret.has_references():
                # do not replace with RefValue instance if there are no
                # references, i.e. discard the RefValue in ret, just return
                # the new value
                return new

        # So we now have a RefValue. Let's, keep a reference to the instance
        # we just created, in a dict indexed by the dictionary path, instead
        # of just a list. The keys are required to resolve dependencies during
        # interpolation
        self._occurrences[path] = ret
        return ret
Exemplo n.º 2
0
 def test_single_subst_end(self):
     s = 'I like ' + _var('favcolour')
     tv = RefValue(s)
     self.assertTrue(tv.has_references())
     self.assertEqual(tv.render(CONTEXT),
                      _poor_mans_template(s, 'favcolour',
                                          CONTEXT['favcolour']))
Exemplo n.º 3
0
    def _update_scalar(self, cur, new, path):
        if self.delimiter is None or not isinstance(new, (types.StringTypes,
                                                          RefValue)):
            # either there is no delimiter defined (and hence no references
            # are being used), or the new value is not a string (and hence
            # cannot be turned into a RefValue), and not a RefValue. We can
            # shortcut and just return the new scalar
            return new

        elif isinstance(new, RefValue):
            # the new value is (already) a RefValue, so we need not touch it
            # at all
            ret = new

        else:
            # the new value is a string, let's see if it contains references,
            # by way of wrapping it in a RefValue and querying the result
            ret = RefValue(new, self.delimiter)
            if not ret.has_references():
                # do not replace with RefValue instance if there are no
                # references, i.e. discard the RefValue in ret, just return
                # the new value
                return new

        # So we now have a RefValue. Let's, keep a reference to the instance
        # we just created, in a dict indexed by the dictionary path, instead
        # of just a list. The keys are required to resolve dependencies during
        # interpolation
        self._occurrences[path] = ret
        return ret
Exemplo n.º 4
0
 def test_single_subst_start(self):
     s = _var('favcolour') + ' is my favourite colour'
     tv = RefValue(s)
     self.assertTrue(tv.has_references())
     self.assertEqual(tv.render(CONTEXT),
                      _poor_mans_template(s, 'favcolour',
                                          CONTEXT['favcolour']))
Exemplo n.º 5
0
 def test_multiple_subst(self):
     greet = PARAMETER_INTERPOLATION_DELIMITER.join(('motd', 'greeting'))
     s = _var(greet) + ' I like ' + _var('favcolour') + '!'
     tv = RefValue(s)
     self.assertTrue(tv.has_references())
     want = _poor_mans_template(s, greet, CONTEXT['motd']['greeting'])
     want = _poor_mans_template(want, 'favcolour', CONTEXT['favcolour'])
     self.assertEqual(tv.render(CONTEXT), want)
Exemplo n.º 6
0
 def test_deep_subst_solo(self):
     var = PARAMETER_INTERPOLATION_DELIMITER.join(('motd', 'greeting'))
     s = _var(var)
     tv = RefValue(s)
     self.assertTrue(tv.has_references())
     self.assertEqual(tv.render(CONTEXT),
                      _poor_mans_template(s, var,
                                          CONTEXT['motd']['greeting']))
Exemplo n.º 7
0
    def _update_scalar(self, cur, new, path):
        if self.delimiter is None or not isinstance(new, types.StringTypes):
            return new

        else:
            ret = RefValue(new, self.delimiter)
            if not ret.has_references():
                # do not replace with RefValue instance if there are no
                # references
                return new

            # finally, keep a reference to the RefValue instance we just
            # created, in a dict indexed by the dictionary path, instead of
            # just a list. The keys are required to resolve dependencies
            # during interpolation
            self._occurrences[path] = ret
            return ret
Exemplo n.º 8
0
    def _update_scalar(self, cur, new, path):
        if self.delimiter is None or not isinstance(new, types.StringTypes):
            return new

        else:
            ret = RefValue(new, self.delimiter)
            if not ret.has_references():
                # do not replace with RefValue instance if there are no
                # references
                return new

            # finally, keep a reference to the RefValue instance we just
            # created, in a dict indexed by the dictionary path, instead of
            # just a list. The keys are required to resolve dependencies
            # during interpolation
            self._occurrences[path] = ret
            return ret
Exemplo n.º 9
0
 def _test_solo_ref(self, key):
     s = _var(key)
     tv = RefValue(s)
     res = tv.render(CONTEXT)
     self.assertTrue(tv.has_references())
     self.assertEqual(res, CONTEXT[key])
Exemplo n.º 10
0
 def test_simple_string(self):
     s = 'my cat likes to hide in boxes'
     tv = RefValue(s)
     self.assertFalse(tv.has_references())
     self.assertEquals(tv.render(CONTEXT), s)
Exemplo n.º 11
0
 def test_incomplete_variable(self):
     s = PARAMETER_INTERPOLATION_SENTINELS[0] + 'incomplete'
     with self.assertRaises(IncompleteInterpolationError):
         tv = RefValue(s)
Exemplo n.º 12
0
 def test_undefined_variable(self):
     s = _var('no_such_variable')
     tv = RefValue(s)
     with self.assertRaises(UndefinedVariableError):
         tv.render(CONTEXT)