-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from rollbar/batched-transform
batched transform
- Loading branch information
Showing
12 changed files
with
396 additions
and
144 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,39 @@ | ||
class Transform(object): | ||
def default(self, o, key=None): | ||
return o | ||
|
||
def transform_circular_reference(self, o, key=None, ref_key=None): | ||
# By default, we just perform a no-op for circular references. | ||
# Subclasses should implement this method to return whatever representation | ||
# for the circular reference they need. | ||
return self.default(o, key=key) | ||
|
||
def transform_tuple(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_namedtuple(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_list(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_dict(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_number(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_py2_str(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_py3_bytes(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_unicode(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_boolean(self, o, key=None): | ||
return self.default(o, key=key) | ||
|
||
def transform_custom(self, o, key=None): | ||
return self.default(o, key=key) |
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,88 @@ | ||
from rollbar.lib.transform import Transform | ||
from rollbar.lib import ( | ||
python_major_version, | ||
number_types, | ||
type_info, | ||
) | ||
|
||
|
||
def do_transform(transform, type_name, val, key=None, **kw): | ||
fn = getattr(transform, "transform_%s" % type_name, transform.transform_custom) | ||
val = fn(val, key=key, **kw) | ||
|
||
return val | ||
|
||
|
||
if python_major_version() < 3: | ||
|
||
def string_handler(transform, s, key=None): | ||
if isinstance(s, str): | ||
return do_transform(transform, "py2_str", s, key=key) | ||
elif isinstance(s, unicode): | ||
return do_transform(transform, "unicode", s, key=key) | ||
|
||
else: | ||
|
||
def string_handler(transform, s, key=None): | ||
if isinstance(s, bytes): | ||
return do_transform(transform, "py3_bytes", s, key=key) | ||
elif isinstance(s, str): | ||
return do_transform(transform, "unicode", s, key=key) | ||
|
||
|
||
def default_handler(transform, o, key=None): | ||
if isinstance(o, bool): | ||
return do_transform(transform, "boolean", o, key=key) | ||
|
||
# There is a quirk in the current version (1.1.6) of the enum | ||
# backport enum34 which causes it to not have the same | ||
# behavior as Python 3.4+. One way to identify IntEnums is that | ||
# they are instances of numbers but not number types. | ||
if isinstance(o, number_types): | ||
if type(o) not in number_types: | ||
return do_transform(transform, "custom", o, key=key) | ||
else: | ||
return do_transform(transform, "number", o, key=key) | ||
|
||
return do_transform(transform, "custom", o, key=key) | ||
|
||
|
||
handlers = { | ||
type_info.STRING: string_handler, | ||
type_info.TUPLE: lambda transform, o, key=None: do_transform( | ||
transform, "tuple", o, key=key | ||
), | ||
type_info.NAMEDTUPLE: lambda transform, o, key=None: do_transform( | ||
transform, "namedtuple", o, key=key | ||
), | ||
type_info.LIST: lambda transform, o, key=None: do_transform( | ||
transform, "list", o, key=key | ||
), | ||
type_info.SET: lambda transform, o, key=None: do_transform( | ||
transform, "set", o, key=key | ||
), | ||
type_info.MAPPING: lambda transform, o, key=None: do_transform( | ||
transform, "dict", o, key=key | ||
), | ||
type_info.CIRCULAR: lambda transform, o, key=None, ref_key=None: do_transform( | ||
transform, "circular_reference", o, key=key, ref_key=ref_key | ||
), | ||
type_info.DEFAULT: default_handler, | ||
} | ||
|
||
|
||
class BatchedTransform(Transform): | ||
def __init__(self, transforms): | ||
super(BatchedTransform, self).__init__() | ||
self._transforms = transforms | ||
|
||
def default(self, o, key=None): | ||
for transform in self._transforms: | ||
node_type = type_info.get_type(o) | ||
handler = handlers.get(node_type, handlers.get(type_info.DEFAULT)) | ||
o = handler(transform, o, key=key) | ||
|
||
return o | ||
|
||
|
||
__all__ = ["BatchedTransform"] |
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
Oops, something went wrong.