示例#1
0
 def test_split_by_comma(self):
     string = 'bla,bla,smth/else,another(bla,bla/bla),yet/bla'
     expected = [
         'bla', 'bla', 'smth/else', 'another(bla,bla/bla)', 'yet/bla'
     ]
     res = utils.split_by_comma(string)
     self.assertEqual(res, expected)
示例#2
0
文件: common.py 项目: tshrklr/gce-api
    def _format_output(self, request, action, action_result):
        # TODO(ft): this metod must be safe and ignore unknown fields
        fields = request.params.get('fields', None)
        # TODO(ft): GCE can also format results of other action
        if action not in ('index', 'show') or fields is None:
            return action_result

        if action == 'show':
            action_result = utils.apply_template(fields, action_result)
            return action_result
        sp = utils.split_by_comma(fields)
        top_level = []
        items = []
        for string in sp:
            if 'items' in string:
                items.append(string)
            else:
                top_level.append(string)
        res = {}
        if len(items) > 0:
            res['items'] = []
        for string in top_level:
            dct = utils.apply_template(string, action_result)
            for key, val in dct.items():
                res[key] = val
        for string in items:
            if '(' in string:
                dct = utils.apply_template(string, action_result)
                for key, val in dct.items():
                    res[key] = val
            elif string.startswith('items/'):
                string = string[len('items/'):]
                for element in action_result['items']:
                    dct = utils.apply_template(string, element)
                    res['items'].append(dct)

        return res
示例#3
0
    def _format_output(self, request, action, action_result):
        # TODO(ft): this metod must be safe and ignore unknown fields
        fields = request.params.get('fields', None)
        # TODO(ft): GCE can also format results of other action
        if action not in ('index', 'show') or fields is None:
            return action_result

        if action == 'show':
            action_result = utils.apply_template(fields, action_result)
            return action_result
        sp = utils.split_by_comma(fields)
        top_level = []
        items = []
        for string in sp:
            if 'items' in string:
                items.append(string)
            else:
                top_level.append(string)
        res = {}
        if len(items) > 0:
            res['items'] = []
        for string in top_level:
            dct = utils.apply_template(string, action_result)
            for key, val in dct.items():
                res[key] = val
        for string in items:
            if '(' in string:
                dct = utils.apply_template(string, action_result)
                for key, val in dct.items():
                    res[key] = val
            elif string.startswith('items/'):
                string = string[len('items/'):]
                for element in action_result['items']:
                    dct = utils.apply_template(string, element)
                    res['items'].append(dct)

        return res
示例#4
0
 def test_split_by_comma(self):
     string = 'bla,bla,smth/else,another(bla,bla/bla),yet/bla'
     expected = ['bla', 'bla', 'smth/else', 'another(bla,bla/bla)',
                 'yet/bla']
     res = utils.split_by_comma(string)
     self.assertEqual(res, expected)