Skip to content

Commit

Permalink
Remove cool comments
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Nov 24, 2020
1 parent 3eea60f commit 4bddae1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.threading import ThreadingIntegration

from tribler_common.sentry_reporter.sentry_tools import first, parse_os_environ, parse_stacktrace, safe_delete, safe_get
from tribler_common.sentry_reporter.sentry_tools import get_first_item, parse_os_environ, parse_stacktrace, delete_item, get_value

PLATFORM_DETAILS = 'platform.details'
STACKTRACE = '_stacktrace'
Expand Down Expand Up @@ -135,24 +135,24 @@ def send(event, post_data, sys_info):

# tags
tags = event[TAGS]
tags['version'] = safe_get(post_data, 'version')
tags['machine'] = safe_get(post_data, 'machine')
tags['os'] = safe_get(post_data, 'os')
tags['platform'] = first(safe_get(sys_info, 'platform'))
tags[('%s' % PLATFORM_DETAILS)] = first(safe_get(sys_info, PLATFORM_DETAILS))
tags['version'] = get_value(post_data, 'version')
tags['machine'] = get_value(post_data, 'machine')
tags['os'] = get_value(post_data, 'os')
tags['platform'] = get_first_item(get_value(sys_info, 'platform'))
tags[('%s' % PLATFORM_DETAILS)] = get_first_item(get_value(sys_info, PLATFORM_DETAILS))

# context
context = event[CONTEXTS]
reporter = context[REPORTER]
version = safe_get(post_data, 'version')
version = get_value(post_data, 'version')

context['browser'] = {'version': version, 'name': 'Tribler'}

reporter[STACKTRACE] = parse_stacktrace(safe_get(post_data, 'stack'))
reporter['comments'] = safe_get(post_data, 'comments')
reporter[STACKTRACE] = parse_stacktrace(get_value(post_data, 'stack'))
reporter['comments'] = get_value(post_data, 'comments')

reporter[OS_ENVIRON] = parse_os_environ(safe_get(sys_info, OS_ENVIRON))
safe_delete(sys_info, OS_ENVIRON)
reporter[OS_ENVIRON] = parse_os_environ(get_value(sys_info, OS_ENVIRON))
delete_item(sys_info, OS_ENVIRON)
reporter[SYSINFO] = sys_info

sentry_sdk.capture_event(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from tribler_common.sentry_reporter.sentry_reporter import BREADCRUMBS, \
CONTEXTS, EXTRA, LOGENTRY, OS_ENVIRON, REPORTER, STACKTRACE, SYSINFO
from tribler_common.sentry_reporter.sentry_tools import modify, safe_delete
from tribler_common.sentry_reporter.sentry_tools import modify_value, delete_item


class SentryScrubber:
Expand Down Expand Up @@ -54,19 +54,19 @@ def scrub_event(self, event):
return event

for field_name in self.event_fields_to_cut:
safe_delete(event, field_name)
delete_item(event, field_name)

modify(event, EXTRA, self.scrub_entity_recursively)
modify(event, LOGENTRY, self.scrub_entity_recursively)
modify(event, BREADCRUMBS, self.scrub_entity_recursively)
modify_value(event, EXTRA, self.scrub_entity_recursively)
modify_value(event, LOGENTRY, self.scrub_entity_recursively)
modify_value(event, BREADCRUMBS, self.scrub_entity_recursively)

reporter = event.get(CONTEXTS, {}).get(REPORTER, None)
if not reporter:
return event

modify(reporter, OS_ENVIRON, self.scrub_entity_recursively)
modify(reporter, STACKTRACE, self.scrub_entity_recursively)
modify(reporter, SYSINFO, self.scrub_entity_recursively)
modify_value(reporter, OS_ENVIRON, self.scrub_entity_recursively)
modify_value(reporter, STACKTRACE, self.scrub_entity_recursively)
modify_value(reporter, SYSINFO, self.scrub_entity_recursively)

return event

Expand Down
57 changes: 5 additions & 52 deletions src/tribler-common/tribler_common/sentry_reporter/sentry_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,15 @@ def parse_stacktrace(stacktrace):
return [line for line in stacktrace.split('\n') if line]


def first(items, default=None):
""" Safe return the first item in a list.
Args:
items: a sequence
default: a default value to return in case no items in list.
Returns:
The first item in list.
"""
def get_first_item(items, default=None):
return items[0] if items else default


def last(items, default=None):
""" Safe return the last item in a list.
Args:
items: a sequence
default: a default value to return in case no items in list.
Returns:
The last item in list.
"""
def get_last_item(items, default=None):
return items[-1] if items else default


def safe_delete(d, key):
""" Safe delete item from a dictionary.
Args:
d: the dictionary.
key: a key to remove.
Returns:
The dictionary that was passed to the method.
"""
def delete_item(d, key):
if not d:
return d

Expand All @@ -90,31 +63,11 @@ def safe_delete(d, key):
return d


def safe_get(d, key, default=None):
""" Safe get value from dictionary
Args:
d: a dictionary
key: a key
default: the value that will be returned in case of 'key' is unreached
Returns:
Value addressed by key or `default`
"""
def get_value(d, key, default=None):
return d.get(key, default) if d else default


def modify(d, key, function):
""" Safe modify value in a dictionary.
Args:
d: the dictionary.
key: the key, that adressed the value to modify.
function: modify function.
Returns:
The dictionary that was passed to the method.
"""
def modify_value(d, key, function):
if not d or not key or not function:
return d

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
from tribler_common.sentry_reporter.sentry_tools import first, last, modify, parse_os_environ, parse_stacktrace, \
safe_delete, safe_get
from tribler_common.sentry_reporter.sentry_tools import get_first_item, get_last_item, modify_value, parse_os_environ, parse_stacktrace, \
delete_item, get_value


def test_first():
assert first(None, '') == ''
assert first([], '') == ''
assert first(['some'], '') == 'some'
assert first(['some', 'value'], '') == 'some'
assert get_first_item(None, '') == ''
assert get_first_item([], '') == ''
assert get_first_item(['some'], '') == 'some'
assert get_first_item(['some', 'value'], '') == 'some'

assert first((), '') == ''
assert first(('some', 'value'), '') == 'some'
assert get_first_item((), '') == ''
assert get_first_item(('some', 'value'), '') == 'some'

assert first(None, None) is None
assert get_first_item(None, None) is None


def test_last():
assert last(None, '') == ''
assert last([], '') == ''
assert last(['some'], '') == 'some'
assert last(['some', 'value'], '') == 'value'
assert get_last_item(None, '') == ''
assert get_last_item([], '') == ''
assert get_last_item(['some'], '') == 'some'
assert get_last_item(['some', 'value'], '') == 'value'

assert last((), '') == ''
assert last(('some', 'value'), '') == 'value'
assert get_last_item((), '') == ''
assert get_last_item(('some', 'value'), '') == 'value'

assert last(None, None) is None
assert get_last_item(None, None) is None


def test_delete():
assert safe_delete({}, None) == {}
assert delete_item({}, None) == {}

assert safe_delete({'key': 'value'}, None) == {'key': 'value'}
assert safe_delete({'key': 'value'}, 'missed_key') == {'key': 'value'}
assert safe_delete({'key': 'value'}, 'key') == {}
assert delete_item({'key': 'value'}, None) == {'key': 'value'}
assert delete_item({'key': 'value'}, 'missed_key') == {'key': 'value'}
assert delete_item({'key': 'value'}, 'key') == {}


def test_parse_os_environ():
Expand Down Expand Up @@ -59,20 +59,20 @@ def test_parse_stacktrace():


def test_modify():
assert modify(None, None, None) is None
assert modify({}, None, None) == {}
assert modify({}, '', None) == {}
assert modify_value(None, None, None) is None
assert modify_value({}, None, None) == {}
assert modify_value({}, '', None) == {}

assert modify({}, 'key', lambda value: '') == {}
assert modify({'a': 'b'}, 'key', lambda value: '') == {'a': 'b'}
assert modify({'a': 'b', 'key': 'value'}, 'key', lambda value: '') == {'a': 'b', 'key': ''}
assert modify_value({}, 'key', lambda value: '') == {}
assert modify_value({'a': 'b'}, 'key', lambda value: '') == {'a': 'b'}
assert modify_value({'a': 'b', 'key': 'value'}, 'key', lambda value: '') == {'a': 'b', 'key': ''}


def test_safe_get():
assert safe_get(None, None, None) is None
assert safe_get(None, None, {}) == {}
assert get_value(None, None, None) is None
assert get_value(None, None, {}) == {}

assert safe_get(None, 'key', {}) == {}
assert get_value(None, 'key', {}) == {}

assert safe_get({'key': 'value'}, 'key', {}) == 'value'
assert safe_get({'key': 'value'}, 'key1', {}) == {}
assert get_value({'key': 'value'}, 'key', {}) == 'value'
assert get_value({'key': 'value'}, 'key1', {}) == {}

0 comments on commit 4bddae1

Please sign in to comment.