|
| 1 | +"""This module contains common functions-helpers of the client and agents. |
| 2 | +
|
| 3 | +Copyright (c) 2018 http://reportportal.io . |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +""" |
| 14 | + |
| 15 | +import logging |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def gen_attributes(rp_attributes): |
| 21 | + """Generate list of attributes for the API request. |
| 22 | +
|
| 23 | + Example of input list: |
| 24 | + ['tag_name:tag_value1', 'tag_value2'] |
| 25 | + Output of the function for the given input list: |
| 26 | + [{'key': 'tag_name', 'value': 'tag_value1'}, {'value': 'tag_value2'}] |
| 27 | +
|
| 28 | + :param rp_attributes: List of attributes(tags) |
| 29 | + :return: Correctly created list of dictionaries |
| 30 | + to be passed to RP |
| 31 | + """ |
| 32 | + attrs = [] |
| 33 | + for rp_attr in rp_attributes: |
| 34 | + try: |
| 35 | + key, value = rp_attr.split(':') |
| 36 | + attr_dict = {'key': key, 'value': value} |
| 37 | + except ValueError as exc: |
| 38 | + logger.exception(str(exc)) |
| 39 | + attr_dict = {'value': rp_attr} |
| 40 | + |
| 41 | + if all(attr_dict.values()): |
| 42 | + attrs.append(attr_dict) |
| 43 | + continue |
| 44 | + logger.debug('Failed to process "{0}" attribute, attribute value' |
| 45 | + ' should not be empty.'.format(rp_attr)) |
| 46 | + return attrs |
0 commit comments