Exemple #1
0
 def gen_json_from_data(self, data, context):
     super(JsonListGenerator, self).gen_json_from_data(data, context)
     result_list = []
     for items in data:
         if isinstance(items, str):
             # this is component without context
             result_list += self.registry.get_component(type(self),
                                                        items).gen_json()
         else:
             # TODO add check for dictionary
             for (item_type, item_data) in items.iteritems():
                 if item_type not in self.component_item_types:
                     # this is named component with context
                     for context in Context.create_context(
                             item_data, get_placeholders(item_type)):
                         result_list += self.registry.get_component(
                             type(self), item_type).gen_json(context)
                 else:
                     # this is inplace defined component
                     item = self.registry.create_component(
                         item_type, {
                             item_type: item_data
                         }).gen_json()
                     if isinstance(item, list):
                         result_list += item
                     else:
                         result_list.append(item)
     return result_list
Exemple #2
0
def test_context_expands_scalar_value():
    data = {
        'single': 'first',
        'wrapped': '{single}',
        'double-wrapped': '{wrapped}'
    }
    contexts = [
        context for context in Context.create_context(
            data, keys_to_expand=('expanded-list', 'expanded-dict'))
    ]
    assert len(contexts) == 1
    expected = {
        'double-wrapped': 'first',
        'single': 'first',
        'wrapped': 'first',
        'single-escaped': '{not-expanded}',
        'double-escaped': '{{not-expanded}}'
    }
    to_expand = {
        'single': '{single}',
        'wrapped': '{wrapped}',
        'double-wrapped': '{double-wrapped}',
        'single-escaped': '{{not-expanded}}',
        'double-escaped': '{{{{not-expanded}}}}'
    }
    assert contexts[0].expand_placeholders(to_expand) == expected
def test_context_inserts_list_value():
    data = {'single': 'first', 'inserted-list': ['list0', 'list1']}
    contexts = [
        context for context in Context.create_context(
            data, keys_to_expand=('expanded-list', 'expanded-dict'))
    ]
    assert len(contexts) == 1
    expected = {'single': 'first', 'inserted-list': ['list0', 'list1']}
    to_expand = {'single': '{single}', 'inserted-list': '{inserted-list}'}
    assert contexts[0].expand_placeholders(to_expand) == expected
def test_context_expands_scalar_value():
    data = {
        'single': 'first',
        'wrapped': '{single}',
        'double-wrapped': '{wrapped}'
    }
    contexts = [context for context in Context.create_context(data, keys_to_expand=('expanded-list', 'expanded-dict'))]
    assert len(contexts) == 1
    expected = {'double-wrapped': 'first', 'single': 'first', 'wrapped': 'first'}
    to_expand = {'single': '{single}', 'wrapped': '{wrapped}', 'double-wrapped': '{double-wrapped}'}
    assert contexts[0].expand_placeholders(to_expand) == expected
 def _gen_item_json_with_context(self, items, result_list):
     # TODO add check for dictionary
     for (item_type, item_data) in items.items():
         if item_type not in self.component_item_types:
             # this is named component with context
             for context in Context.create_context(item_data, get_placeholders(item_type)):
                 result_list += self.registry.get_component(type(self), item_type).gen_json(context)
         else:
             # this is inplace defined component
             item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
             if isinstance(item, list):
                 result_list += item
             else:
                 result_list.append(item)
 def _gen_item_json_with_context(self, items, result_list):
     # TODO add check for dictionary
     for (item_type, item_data) in items.iteritems():
         if item_type not in self.component_item_types:
             # this is named component with context
             for context in Context.create_context(item_data, get_placeholders(item_type)):
                 result_list += self.registry.get_component(type(self), item_type).gen_json(context)
         else:
             # this is inplace defined component
             item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
             if isinstance(item, list):
                 result_list += item
             else:
                 result_list.append(item)
def test_context_expands_list_value():
    data = {
        'single': 'first',
        'expanded-list': [
            'list0',
            'list1'
        ]
    }
    contexts = [context for context in Context.create_context(data, keys_to_expand=('expanded-list', 'expanded-dict'))]
    assert len(contexts) == 2
    expected0 = {'single': 'first', 'expanded-list': 'list0'}
    expected1 = {'single': 'first', 'expanded-list': 'list1'}
    to_expand = {'single': '{single}', 'expanded-list': '{expanded-list}'}
    assert contexts[0].expand_placeholders(to_expand) == expected0
    assert contexts[1].expand_placeholders(to_expand) == expected1
 def gen_json_from_data(self, data, context):
     super(JsonListGenerator, self).gen_json_from_data(data, context)
     result_list = []
     for items in data:
         if isinstance(items, str):
             # this is component without context
             result_list += self.registry.get_component(type(self), items).gen_json()
         else:
             # TODO add check for dictionary
             for (item_type, item_data) in items.iteritems():
                 if item_type not in self.component_item_types:
                     # this is named component with context
                     for context in Context.create_context(item_data, get_placeholders(item_type)):
                         result_list += self.registry.get_component(type(self), item_type).gen_json(context)
                 else:
                     # this is inplace defined component
                     item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
                     if isinstance(item, list):
                         result_list += item
                     else:
                         result_list.append(item)
     return result_list
def test_context_expands_dict_value():
    data = {
        'single':
        'first',
        'expanded-dict': [{
            'dict0': {
                'dict-value': '00'
            }
        }, {
            'dict1': {
                'dict-value': '10'
            }
        }]
    }
    contexts = [
        context for context in Context.create_context(
            data, keys_to_expand=('expanded-list', 'expanded-dict'))
    ]
    assert len(contexts) == 2
    expected0 = {
        'single': 'first',
        'expanded-dict': 'dict0',
        'dict-value': '00'
    }
    expected1 = {
        'single': 'first',
        'expanded-dict': 'dict1',
        'dict-value': '10'
    }
    to_expand = {
        'single': '{single}',
        'expanded-dict': '{expanded-dict}',
        'dict-value': '{dict-value}'
    }
    assert contexts[0].expand_placeholders(to_expand) == expected0
    assert contexts[1].expand_placeholders(to_expand) == expected1
 def get_contexts(self, context=None):
     if context is None:
         context = {}
     data = self.data.copy()
     data.update(context)
     return Context.create_context(data, self._placeholders)
 def get_contexts(self, context=None):
     if context is None:
         context = {}
     data = self.data.copy()
     data.update(context)
     return Context.create_context(data, self._placeholders)