Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-28768: Add compatibility with fastavro>=1.2 #24

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions python/lsst/alert/packet/schema.py
Expand Up @@ -112,6 +112,7 @@ def resolve_schema_definition(to_resolve, seen_names=None):

Notes
-----
This method is only needed for old fastavro (<=0.24).
The schema is resolved in terms of the types which have been parsed
and stored by fastavro (ie, are found in
`fastavro.schema._schema.SCHEMA_DEFS`).
Expand Down Expand Up @@ -190,7 +191,12 @@ class Schema(object):
cache after loading.
"""
def __init__(self, schema_definition):
self.definition = resolve_schema_definition(schema_definition)
if hasattr(fastavro.schema._schema, 'SCHEMA_DEFS'):
# Old fastavro
self.definition = resolve_schema_definition(schema_definition)
else:
# New fastavro
self.definition = schema_definition

def serialize(self, record):
"""Create an Avro representation of data following this schema.
Expand Down Expand Up @@ -309,11 +315,13 @@ def from_file(cls, filename=None):
root_name = PurePath(filename).stem

schema_definition = fastavro.schema.load_schema(filename)
# fastavro gives a back a list if it recursively loaded more than one
# file, otherwise a dict.
if isinstance(schema_definition, dict):
schema_definition = [schema_definition]
if hasattr(fastavro.schema._schema, 'SCHEMA_DEFS'):
# Old fastavro gives a back a list if it recursively loaded more than one
# file, otherwise a dict.
if isinstance(schema_definition, dict):
schema_definition = [schema_definition]

schema_definition = next(schema for schema in schema_definition
if schema['name'] == root_name)

schema_definition = next(schema for schema in schema_definition
if schema['name'] == root_name)
return cls(schema_definition)