Skip to content

Commit 59799b1

Browse files
authored
Optional dependencies. (#10)
* Optional dependencies. * Optional dependencies.
1 parent a387939 commit 59799b1

4 files changed

Lines changed: 53 additions & 36 deletions

File tree

error_tracker/django/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
import re
10+
import json
1011

1112
from django.http import RawPostDataException
1213

@@ -36,11 +37,13 @@ def _get_form_data(request):
3637
except RawPostDataException:
3738
pass
3839
if body is not None:
39-
from rest_framework.request import Request
40-
if isinstance(body, Request):
41-
form = body.data
42-
elif len(body) > 0:
43-
import json
40+
try:
41+
from rest_framework.request import Request
42+
if isinstance(body, Request):
43+
form = body.data
44+
except ImportError:
45+
pass
46+
if len(form) == 0 and len(body) > 0:
4447
try:
4548
form = json.loads(body, encoding="UTF-8")
4649
except Exception:

error_tracker/libs/exception_formatter.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Exception formatter that captures frame details in string format.
44
#
5-
# :copyright: 2019 Sonu Kumar
5+
# :copyright: 2020 Sonu Kumar
66
# :license: BSD-3-Clause
77
#
88

@@ -11,8 +11,6 @@
1111
import sys
1212
import traceback
1313

14-
from django.http import QueryDict
15-
1614
try:
1715
import builtins
1816
except ImportError:
@@ -24,7 +22,22 @@
2422

2523
import re
2624
import types
27-
from werkzeug.datastructures import ImmutableMultiDict
25+
26+
27+
def convert_if_possible(x):
28+
try:
29+
from werkzeug.datastructures import ImmutableMultiDict
30+
if type(x) == ImmutableMultiDict:
31+
return "ImmutableMultiDict({%s})", x.to_dict(flat=False)
32+
except ImportError:
33+
pass
34+
try:
35+
from django.http import QueryDict
36+
if type(x) == QueryDict:
37+
return "QueryDict({%s})", x.dict()
38+
except ImportError:
39+
pass
40+
return None, None
2841

2942

3043
def format_frame(x, max_elements, max_string, max_recursion, masking=None):
@@ -71,38 +84,34 @@ def _it_to_string(fmt, it, per_element=_per_element):
7184

7285
if x is builtins.__dict__:
7386
return "<builtins>"
74-
elif type(x) == dict:
87+
if type(x) == dict:
7588
return _it_to_string("{%s}", sorted(x.items()), per_element=_per_dict_element)
76-
elif type(x) == list:
89+
if type(x) == list:
7790
return _it_to_string("[%s]", x)
78-
elif type(x) == tuple:
91+
if type(x) == tuple:
7992
return _it_to_string("(%s)" if len(x) != 1
8093
or max_recursion <= 0
8194
or max_elements <= 0
8295
else "(%s,)", x)
83-
elif type(x) == set:
96+
if type(x) == set:
8497
return _it_to_string("set([%s])", sorted(x))
85-
elif type(x) == frozenset:
98+
if type(x) == frozenset:
8699
return _it_to_string("frozenset([%s])", sorted(x))
87-
elif isinstance(x, six.string_types) and max_string < len(x):
100+
if isinstance(x, six.string_types) and max_string < len(x):
88101
return repr(x[:max_string] + "...")
89-
elif type(x) == ImmutableMultiDict:
90-
x = x.to_dict(flat=False)
91-
return _it_to_string("ImmutableMultiDict({%s})", sorted(x.items()),
92-
per_element=_per_dict_element)
93-
elif type(x) == QueryDict:
94-
x = x.dict()
95-
return _it_to_string("QueryDict({%s})", sorted(x.items()),
102+
103+
converted, x = convert_if_possible(x)
104+
if converted is not None:
105+
return _it_to_string(converted, sorted(x.items()),
96106
per_element=_per_dict_element)
97-
else:
98-
try:
99-
if issubclass(x, dict):
100-
x = dict(x)
101-
return _it_to_string("Dict({%s})", sorted(x.items()),
102-
per_element=_per_dict_element)
103-
except TypeError:
104-
pass
105-
return repr(x)
107+
try:
108+
if issubclass(x, dict):
109+
x = dict(x)
110+
return _it_to_string("Dict({%s})", sorted(x.items()),
111+
per_element=_per_dict_element)
112+
except TypeError:
113+
pass
114+
return repr(x)
106115

107116

108117
def can_be_skipped(key, value):

error_tracker/libs/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import warnings
1111
from hashlib import sha256
1212
import six
13-
from django.utils.module_loading import import_string
1413

1514
from error_tracker.libs.exception_formatter import format_exception
1615
from error_tracker.libs.mixins import MaskingMixin
@@ -75,6 +74,7 @@ def get_class_from_path(module_path, super_class, raise_exception=True,
7574
:param warning_message: any warning message
7675
:return: imported class
7776
"""
77+
from django.utils.module_loading import import_string
7878
try:
7979
cls = import_string(module_path)
8080
cls_fields = cls.__dict__.keys()

setup.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ def grep(attrname):
4242
zip_safe=False,
4343
platforms='any',
4444
install_requires=[
45-
"Flask",
46-
"Django",
47-
"Flask-SQLAlchemy",
4845
"six",
49-
"djangorestframework"
5046
],
47+
extras_require={
48+
"Django": ["Django"],
49+
"DRF": ["djangorestframework"],
50+
"Flask": ["Flask", "Flask-SQLAlchemy"],
51+
},
5152
tests_require=[
5253
"Flask-Mail",
5354
'pyquery'
55+
"Django",
56+
"djangorestframework",
57+
"Flask",
58+
"Flask-SQLAlchemy"
5459
],
5560
classifiers=[
5661
'Development Status :: 5 - Production/Stable',

0 commit comments

Comments
 (0)