hop.models

class hop.models.AvroBlob(content: str | int | float | bool | None | Dict[str, Any] | List[Any] | List[str | int | float | bool | None | Dict[str, Any] | List[Any]], schema: dict = None, single_record: bool = True)[source]

Defines an unformatted message blob.

classmethod deserialize(data, single_record=True)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

classmethod load(blob_input, single_record=True)[source]

Create a blob message from input avro data.

Parameters:
  • blob_input – The encoded Avro data or file object.

  • single_record – True if input avro data only contains one record. True by default.

Returns:

The Blob.

classmethod load_file(filename, single_record=True)[source]

Create a new message model from a file.

Parameters:
  • filename – The path to a file.

  • single_record – True if input avro data only contains one record. True by default.

Returns:

The message model.

class hop.models.Blob(content: bytes)[source]

Defines an opaque message blob.

classmethod deserialize(data)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

classmethod load(blob_input)[source]

Create a blob message from input data.

Parameters:

blob_input – The unstructured message data (bytes) or file object.

Returns:

The Blob.

classmethod load_file(filename)[source]

Create a new message model from a file.

Parameters:

filename – The path to a file.

Returns:

The message model.

class hop.models.ExternalMessage(url: str)[source]

Defines a message which refers to data stored externally at some URL

classmethod load(input)[source]

Create a blob message from input text.

Parameters:

blob_input – The unstructured message text or file object.

Returns:

The Blob.

class hop.models.GCNCircular(header: dict, body: str)[source]

Defines a GCN Circular structure.

The parsed GCN circular is formatted as a dictionary with the following schema:

{‘headers’: {‘title’: …, ‘number’: …, …}, ‘body’: …}

classmethod load(email_input)[source]

Create a new GCNCircular from an RFC 822 formatted circular.

Parameters:

email_input – A file object or string.

Returns:

The GCNCircular.

serialize()[source]

Wrap the message with its format and content.

Returns:

A dictionary with “format” and “content” keys. The value stored under “format” is the format label. The value stored under “content” is the actual encoded data.

class hop.models.GCNTextNotice(raw: bytes, fields: dict)[source]

A GCN Notice in the plain text format.

The original message data is stored in the raw property, and the parsed message is available via the fields property, which is a dictionary.

classmethod deserialize(data)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

classmethod load(input)[source]

Create a new message model from a file object or string. This base implementation has no functionality and should not be called.

Parameters:

input – A file object or string.

Returns:

The message model.

classmethod load_file(filename)[source]

Create a new message model from a file.

Parameters:

filename – The path to a file.

Returns:

The message model.

class hop.models.JSONBlob(content: str | int | float | bool | None | Dict[str, Any] | List[Any])[source]

Defines an unformatted message blob.

classmethod deserialize(data)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

classmethod load(blob_input)[source]

Create a blob message from input text.

Parameters:

blob_input – The unstructured message text or file object.

Returns:

The Blob.

class hop.models.MessageModel[source]

An abstract message model.

classmethod deserialize(data)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

abstractmethod classmethod load(input_)[source]

Create a new message model from a file object or string. This base implementation has no functionality and should not be called.

Parameters:

input – A file object or string.

Returns:

The message model.

classmethod load_file(filename)[source]

Create a new message model from a file.

Parameters:

filename – The path to a file.

Returns:

The message model.

serialize()[source]

Wrap the message with its format and content, for transmission to Kafka.

Returns:

A dictionary with “format” and “content” keys. The value stored under “format” is the format label. The value stored under “content” is the actual encoded data.

class hop.models.VOEvent(ivorn: str, role: str = <factory>, version: str = <factory>, Who: dict = <factory>, What: dict = <factory>, WhereWhen: dict = <factory>, How: dict = <factory>, Why: dict = <factory>, Citations: dict = <factory>, Description: dict = <factory>, Reference: dict = <factory>, _raw: bytes = <factory>)[source]

Defines a VOEvent 2.0 structure.

Implements the schema defined by:

http://www.ivoa.net/Documents/VOEvent/20110711/

classmethod deserialize(raw)[source]

Unwrap a message produced by serialize() (the “content” value).

Returns:

An instance of the model class.

static dict_factory(input: list)[source]

Create a dictionary from a list of tuples, filtering out keys which begin with underscores.

Parameters:

input – An existing dictionary.

Returns:

A copy of input without any keys beginning with underscores.

static ensure_bytes(data_source)[source]

Turn a string, a file-like object, or a generator into bytes. This is useful if the data needs to be used more than once, e.g. parsing and being stored, when the data source may be single-pass only.

Parameters:

data_source – A source of data, which may already be bytes, or a string, file-like object, or a generator.

Returns:

The bytes extracted or converted from data_source.

static is_attribute(key: str)[source]

Identify keys which are properly XML attributes, as opposed to sub-elements. This works because in the VOEvent v2.0 schema there are no elements and attributes which share names.

Parameters:

key – The key to check.

Returns:

True if the key corresponds to an attribute, False if it matches an element type.

classmethod label_attributes_dict(d: dict, attr_prefix: str = '@')[source]

Recursively add a prefix to dictionary keys so that xmltodict.unparse can restore attributes correctly.

Parameters:
  • d – A dictionary in which to patch attribute names.

  • attr_prefix – The prefix to add to keys which should be attributes.

Returns:

None; d is modified in-place.

classmethod label_attributes_list(d: list, attr_prefix: str = '@')[source]

Recursively add a prefix to dictionary keys so that xmltodict.unparse can restore attributes correctly.

Parameters:
  • d – A list in which to patch attribute names.

  • attr_prefix – The prefix to add to keys which should be attributes.

Returns:

None; d is modified in-place.

classmethod load(xml_input)[source]

Create a new VOEvent from an XML-formatted VOEvent.

Parameters:

xml_input – A file object, string, or generator.

Returns:

The VOEvent.

classmethod load_file(filename)[source]

Create a new VOEvent from an XML-formatted VOEvent file.

Parameters:

filename – Name of the VOEvent file.

Returns:

The VOEvent.