def __get_by_name(name): try: return KeyValuePair.get_by_name(name) except ValueError as e: LOG.debug('Database lookup for name="%s" ' 'resulted in exception : %s.', name, e) return None
def __get_by_name(name): try: return [KeyValuePair.get_by_name(name)] except ValueError as e: LOG.debug('Database lookup for name="%s" ' 'resulted in exception : %s.', name, e) return []
def _get_kv(self, key): kvp = None try: kvp = KeyValuePair.get_by_name(key) except ValueError: # ValueErrors are expected in case of partial lookups pass # A good default value for un-matched value is empty string since that will be used # for rendering templates. return kvp.value if kvp else ''
def test_kvp_crud(self): saved = KeyValuePairModelTest._create_save_kvp() retrieved = KeyValuePair.get_by_name(saved.name) self.assertEqual(saved.id, retrieved.id, 'Same KeyValuePair was not returned.') # test update self.assertEqual(retrieved.value, '0123456789ABCDEF') retrieved.value = 'ABCDEF0123456789' saved = KeyValuePair.add_or_update(retrieved) retrieved = KeyValuePair.get_by_name(saved.name) self.assertEqual(retrieved.value, 'ABCDEF0123456789', 'Update of key value failed') # cleanup KeyValuePairModelTest._delete([retrieved]) try: retrieved = KeyValuePair.get_by_name(saved.name) except ValueError: retrieved = None self.assertIsNone(retrieved, 'managed to retrieve after failure.')
def _construct_system_context(data, context): """Identify the system context in the data.""" # The following regex will look for all occurrences of "{{system.*}}", # "{{ system.* }}", "{{ system.*}}", and "{{system.* }}" in the data. regex = '{{\s*' + SYSTEM_PREFIX + '.(.*?)\s*}}' keys = re.findall(regex, json.dumps(data)) if not keys: return context kvps = {} for key in keys: kvp = KeyValuePair.get_by_name(key) kvps[key] = kvp.value if kvps: if SYSTEM_PREFIX not in context: context[SYSTEM_PREFIX] = {} context[SYSTEM_PREFIX].update(kvps) return context