-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Why** Our users can add advanced attributes as attributes and we should treat them from the beginning as annotations. This approach requires us to think about annotations and attributes in the attribute manager system we already have. This pull request checks each attribute provided by the user and converts it to the attributes/annotations in the flight --------- Co-authored-by: kdysput <[email protected]>
- Loading branch information
1 parent
326c1ea
commit e19dfac
Showing
7 changed files
with
163 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sys | ||
|
||
# unicode is not available in Python3. However due to the Python2 support | ||
# We need to use it to verify primitive values. | ||
primitive_types = ( | ||
(int, float, bool, type(None), str) | ||
if sys.version_info.major >= 3 | ||
else (int, float, bool, type(None), str, unicode) | ||
) | ||
|
||
|
||
def get_report_attributes(provider_attributes): | ||
attributes = {} | ||
annotations = {} | ||
|
||
# Iterate through input_dict and split based on value types | ||
for key, value in provider_attributes.items(): | ||
if isinstance(value, primitive_types): | ||
attributes[key] = value | ||
else: | ||
annotations[key] = value | ||
|
||
# Return both dictionaries | ||
return attributes, annotations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from backtracepython.attributes.attribute_provider import AttributeProvider | ||
from backtracepython.version import version_string | ||
|
||
|
||
class UserAttributeProvider(AttributeProvider): | ||
def __init__(self, attributes): | ||
self.attributes = attributes | ||
|
||
def get(self): | ||
return self.attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from backtracepython.client import get_attributes, set_attribute, set_attributes | ||
from backtracepython.report import BacktraceReport | ||
|
||
|
||
def test_setting_client_attribute(): | ||
key = "foo" | ||
value = "bar" | ||
set_attribute(key, value) | ||
|
||
client_attributes = get_attributes() | ||
assert client_attributes[key] == value | ||
|
||
|
||
def test_overriding_client_attribute(): | ||
current_attributes = get_attributes() | ||
key = list(current_attributes.keys())[0] | ||
previous_value = list(current_attributes.values())[0] | ||
|
||
new_value = "bar" | ||
set_attribute(key, new_value) | ||
|
||
client_attributes = get_attributes() | ||
assert client_attributes[key] == new_value | ||
assert new_value != previous_value | ||
|
||
|
||
def test_primitive_values_in_attributes(): | ||
primitive_attributes = { | ||
"string": "test", | ||
"int": 123, | ||
"float": 123123.123, | ||
"boolean": False, | ||
"None": None, | ||
} | ||
|
||
set_attributes(primitive_attributes) | ||
new_report = BacktraceReport() | ||
report_attributes = new_report.get_attributes() | ||
|
||
for primitive_value_key in primitive_attributes: | ||
assert primitive_value_key in report_attributes | ||
assert ( | ||
report_attributes[primitive_value_key] | ||
== primitive_attributes[primitive_value_key] | ||
) | ||
|
||
|
||
def test_complex_objects_in_annotations(): | ||
objects_to_test = ( | ||
{"foo": 1, "bar": 2}, | ||
("foo", "bar", "baz"), | ||
lambda: None, | ||
BacktraceReport(), | ||
) | ||
|
||
for index, value in enumerate(objects_to_test): | ||
set_attribute(index, value) | ||
|
||
new_report = BacktraceReport() | ||
report_annotations = new_report.get_annotations() | ||
|
||
assert len(report_annotations) == len(objects_to_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters