-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
438 additions
and
62 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
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,50 @@ | ||
from pyensign.utils import ulid | ||
from pyensign.topics import Topic | ||
|
||
|
||
class Project(object): | ||
""" | ||
A project is a collection of topics. Similar to a traditional database, if you have | ||
access to the project, you have access to all of its topics. | ||
""" | ||
|
||
def __init__(self, id): | ||
self.id = ulid.parse(id) | ||
self.num_topics = 0 | ||
self.num_readonly_topics = 0 | ||
self.events = 0 | ||
self.duplicates = 0 | ||
self.data_size_bytes = 0 | ||
self.topics = [] | ||
|
||
def __repr__(self): | ||
return "Project(id={})".format(self.id) | ||
|
||
def __str__(self): | ||
s = "Project" | ||
s += "\n\tid={}".format(self.id) | ||
s += "\n\tnum_topics={}".format(self.num_topics) | ||
s += "\n\tnum_readonly_topics={}".format(self.num_readonly_topics) | ||
s += "\n\tevents={}".format(self.events) | ||
s += "\n\tduplicates={}".format(self.duplicates) | ||
s += "\n\tdata_size_bytes={}".format(self.data_size_bytes) | ||
for topic in self.topics: | ||
s += "\n\t" | ||
s += str(topic).replace("\t", "\t\t") | ||
return s | ||
|
||
@classmethod | ||
def from_info(cls, pb_val): | ||
""" | ||
Convert a protocol buffer ProjectInfo into a Project. | ||
""" | ||
|
||
project = cls(pb_val.project_id) | ||
project.num_topics = pb_val.num_topics | ||
project.num_readonly_topics = pb_val.num_readonly_topics | ||
project.events = pb_val.events | ||
project.duplicates = pb_val.duplicates | ||
project.data_size_bytes = pb_val.data_size_bytes | ||
for topic in pb_val.topics: | ||
project.topics.append(Topic.from_info(topic)) | ||
return project |
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,121 @@ | ||
from pyensign.utils import ulid | ||
from pyensign.events import Type | ||
|
||
|
||
class Topic(object): | ||
""" | ||
A Topic is a stream of ordered events. Topics have a human readable name but also | ||
have a unique ID. | ||
""" | ||
|
||
def __init__(self, id=None, name="", topic_str=""): | ||
if id: | ||
self.id = ulid.parse(id) | ||
else: | ||
self.id = None | ||
self.name = name | ||
self.topic_str = topic_str | ||
self.project_id = None | ||
self.event_offset_id = None | ||
self.events = 0 | ||
self.duplicates = 0 | ||
self.data_size_bytes = 0 | ||
self.types = [] | ||
|
||
def __hash__(self): | ||
if self.id is None: | ||
raise ValueError("cannot hash topic with no ID") | ||
return hash(str(self.id)) | ||
|
||
def __eq__(self, other): | ||
return self.id == other.id | ||
|
||
def __repr__(self): | ||
repr = "Topic(" | ||
if self.id: | ||
repr += "id={}, ".format(self.id) | ||
if self.name: | ||
repr += "name={}, ".format(self.name) | ||
if self.topic_str: | ||
repr += "topic_str={}".format(self.topic_str) | ||
repr += ")" | ||
return repr | ||
|
||
def __str__(self): | ||
s = "Topic" | ||
if self.id: | ||
s += "\n\tid={}".format(self.id) | ||
if self.name: | ||
s += "\n\tname={}".format(self.name) | ||
if self.project_id: | ||
s += "\n\tproject_id={}".format(self.project_id) | ||
if self.event_offset_id: | ||
s += "\n\tevent_offset_id={}".format(self.event_offset_id) | ||
if self.topic_str: | ||
s += "\n\ttopic_str={}".format(self.topic_str) | ||
s += "\n\tevents={}".format(self.events) | ||
s += "\n\tduplicates={}".format(self.duplicates) | ||
s += "\n\tdata_size_bytes={}".format(self.data_size_bytes) | ||
s += "\n\ttypes:{}".format(len(self.types)) | ||
for type in self.types: | ||
s += "\n\t" | ||
s += str(type).replace("\t", "\t\t") | ||
return s | ||
|
||
@classmethod | ||
def from_info(cls, pb_val): | ||
""" | ||
Convert a protocol buffer TopicInfo into a Topic. | ||
""" | ||
|
||
topic = cls(id=pb_val.topic_id) | ||
topic.project_id = ulid.parse(pb_val.project_id) | ||
topic.event_offset_id = pb_val.event_offset_id | ||
topic.events = pb_val.events | ||
topic.duplicates = pb_val.duplicates | ||
topic.data_size_bytes = pb_val.data_size_bytes | ||
for type in pb_val.types: | ||
topic.types.append(EventType.from_info(type)) | ||
return topic | ||
|
||
|
||
class EventType(object): | ||
""" | ||
An EventType represents a type of event that was published to a topic, which | ||
includes the schema type and the MIME type. | ||
""" | ||
|
||
def __init__(self, type, mimetype): | ||
self.type = type | ||
self.mimetype = mimetype | ||
self.events = 0 | ||
self.duplicates = 0 | ||
self.data_size_bytes = 0 | ||
|
||
def __repr__(self): | ||
repr = "EventType(" | ||
repr += "type={}".format(self.type) | ||
repr += ", mimetype={}".format(self.mimetype) | ||
repr += ")" | ||
return repr | ||
|
||
def __str__(self): | ||
s = "EventType" | ||
s += "\n\ttype={}".format(self.type) | ||
s += "\n\tmimetype={}".format(self.mimetype) | ||
s += "\n\tevents={}".format(self.events) | ||
s += "\n\tduplicates={}".format(self.duplicates) | ||
s += "\n\tdata_size_bytes={}".format(self.data_size_bytes) | ||
return s | ||
|
||
@classmethod | ||
def from_info(cls, pb_val): | ||
""" | ||
Convert a protocol buffer EventType into an EventType. | ||
""" | ||
|
||
type = cls(Type.convert(pb_val.type), pb_val.mimetype) | ||
type.events = pb_val.events | ||
type.duplicates = pb_val.duplicates | ||
type.data_size_bytes = pb_val.data_size_bytes | ||
return type |
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,16 @@ | ||
from ulid import ULID | ||
|
||
|
||
def parse(id): | ||
""" | ||
Parse a ULID from its string or bytes representation. | ||
""" | ||
|
||
if isinstance(id, ULID): | ||
return id | ||
elif isinstance(id, str): | ||
return ULID.from_str(id) | ||
elif isinstance(id, bytes): | ||
return ULID.from_bytes(id) | ||
else: | ||
raise TypeError("cannot parse ULID from {}".format(type(id))) |
Oops, something went wrong.