Skip to content

Commit 210f06e

Browse files
authored
Common artributes' generator for agents.
1 parent 3486d2d commit 210f06e

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

reportportal_client/helpers.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

reportportal_client/helpers.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from logging import Logger
2+
from typing import Dict, List
3+
4+
logger: Logger
5+
6+
def gen_attributes(rp_attributes: List) -> List[Dict]: ...

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup, find_packages
44

5-
__version__ = '5.0.4'
5+
__version__ = '5.0.5'
66

77
with open('requirements.txt') as f:
88
requirements = f.read().splitlines()

tests/test_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""This modules contains unit tests for the helpers module."""
2+
3+
from reportportal_client.helpers import gen_attributes
4+
5+
6+
def test_gen_attributes():
7+
"""Test functionality of the gen_attributes function."""
8+
expected_out = [{'value': 'Tag'}, {'key': 'Key', 'value': 'Value'}]
9+
out = gen_attributes(['Tag', 'Key:Value', ''])
10+
assert expected_out == out

0 commit comments

Comments
 (0)