hat.syslog.server.encoder

Data structures encoder/decoder

 1"""Data structures encoder/decoder"""
 2
 3from hat import json
 4from hat.syslog.server import common
 5from hat.syslog.encoder import (msg_from_json,
 6                                msg_to_json)
 7from hat.syslog.encoder import *  # NOQA
 8
 9
10def filter_to_json(filter: common.Filter) -> json.Data:
11    """Convert filter to json data"""
12    return dict(filter._asdict(),
13                facility=filter.facility.name if filter.facility else None,
14                severity=filter.severity.name if filter.severity else None)
15
16
17def filter_from_json(json_filter: json.Data) -> common.Filter:
18    """Create filter from json data"""
19    return common.Filter(**dict(
20        json_filter,
21        facility=(common.Facility[json_filter['facility']]
22                  if json_filter['facility'] else None),
23        severity=(common.Severity[json_filter['severity']]
24                  if json_filter['severity'] else None)))
25
26
27def entry_to_json(entry: common.Entry) -> json.Data:
28    """Convert entry to json data"""
29    return dict(entry._asdict(),
30                msg=msg_to_json(entry.msg))
31
32
33def entry_from_json(json_entry: json.Data) -> common.Entry:
34    """Create entry from json data"""
35    return common.Entry(**dict(
36        json_entry,
37        msg=msg_from_json(json_entry['msg'])))
def filter_to_json(filter: hat.syslog.server.common.Filter) -> ~Data:
11def filter_to_json(filter: common.Filter) -> json.Data:
12    """Convert filter to json data"""
13    return dict(filter._asdict(),
14                facility=filter.facility.name if filter.facility else None,
15                severity=filter.severity.name if filter.severity else None)

Convert filter to json data

def filter_from_json(json_filter: ~Data) -> hat.syslog.server.common.Filter:
18def filter_from_json(json_filter: json.Data) -> common.Filter:
19    """Create filter from json data"""
20    return common.Filter(**dict(
21        json_filter,
22        facility=(common.Facility[json_filter['facility']]
23                  if json_filter['facility'] else None),
24        severity=(common.Severity[json_filter['severity']]
25                  if json_filter['severity'] else None)))

Create filter from json data

def entry_to_json(entry: hat.syslog.server.common.Entry) -> ~Data:
28def entry_to_json(entry: common.Entry) -> json.Data:
29    """Convert entry to json data"""
30    return dict(entry._asdict(),
31                msg=msg_to_json(entry.msg))

Convert entry to json data

def entry_from_json(json_entry: ~Data) -> hat.syslog.server.common.Entry:
34def entry_from_json(json_entry: json.Data) -> common.Entry:
35    """Create entry from json data"""
36    return common.Entry(**dict(
37        json_entry,
38        msg=msg_from_json(json_entry['msg'])))

Create entry from json data