Skip to content

Commit

Permalink
+repr for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Oct 8, 2019
1 parent 2738c68 commit 767e993
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cabby/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,12 @@ def discover_services(self, uri=None, cache=True):
self.services = services

return services

def __repr__(self):
t = '{name}(host={host}, port={port}, discovery_path={discovery_path})'
return t.format(
name=type(self).__name__,
host=self.host,
port=self.port,
discovery_path=self.discovery_path,
)
42 changes: 41 additions & 1 deletion cabby/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def __init__(self, count, is_partial=False):
self.count = count
self.is_partial = is_partial

def __repr__(self):
t = '{cls}(count={count}, is_partial={is_partial})'
return t.format(cls=type(self).__name__, **vars(self))


class Collection(Entity):
'''Collection entity.
Expand Down Expand Up @@ -78,20 +82,28 @@ def __init__(self, name, description, type=TYPE_FEED, available=None,
self.receiving_inboxes = receiving_inboxes or []
self.volume = volume

def __repr__(self):
t = '{cls}(name={name}, type={type}, available={available})'
return t.format(cls=type(self).__name__, **vars(self))


class ContentBinding(Entity):
'''Content Binding entity.
Represents TAXII Content Binding.
:param str id: Content Binding ID
:param str subtypes: Content Subtypes IDs
:param list subtypes: Content Subtypes IDs
'''

def __init__(self, id, subtypes=None):
self.id = id
self.subtypes = subtypes or []

def __repr__(self):
t = '{cls}(id={id}, subtypes={subtypes})'
return t.format(cls=type(self).__name__, **vars(self))


class ServiceInstance(Entity):
'''Generic TAXII Service entity.
Expand All @@ -107,6 +119,10 @@ def __init__(self, protocol, address, message_bindings):
self.address = address
self.message_bindings = message_bindings

def __repr__(self):
t = '{cls}(protocol={protocol}, address={address})'
return t.format(cls=type(self).__name__, **vars(self))


class InboxService(ServiceInstance):
'''Inbox Service entity.
Expand Down Expand Up @@ -141,6 +157,10 @@ def __init__(self, protocol, message_bindings):
self.protocol = protocol
self.message_bindings = message_bindings

def __repr__(self):
t = '{cls}(protocol={protocol})'
return t.format(cls=type(self).__name__, **vars(self))


class SubscriptionParameters(Entity):
'''Subscription Parameters Entity.
Expand All @@ -162,6 +182,10 @@ def __init__(self, response_type, content_bindings=None):
self.response_type = response_type
self.content_bindings = content_bindings or []

def __repr__(self):
t = '{cls}(response_type={response_type})'
return t.format(cls=type(self).__name__, **vars(self))


class DetailedServiceInstance(Entity):
'''Detailed description of a generic TAXII Service instance
Expand Down Expand Up @@ -200,6 +224,10 @@ def __init__(self, type, version, protocol, address, message_bindings,
self.available = available
self.message = message

def __repr__(self):
t = '{cls}(type={type}, address={address})'
return t.format(cls=type(self).__name__, **vars(self))


class InboxDetailedService(DetailedServiceInstance):
'''Detailed description of TAXII Inbox Service.
Expand Down Expand Up @@ -237,6 +265,10 @@ def __init__(self, content, content_binding, timestamp):
self.binding = content_binding
self.timestamp = timestamp

def __repr__(self):
t = '{cls}(timestamp={timestamp})'
return t.format(cls=type(self).__name__, **vars(self))


class SubscriptionResponse(Entity):
'''Subscription Response entity.
Expand All @@ -251,6 +283,10 @@ def __init__(self, collection_name, message=None, subscriptions=None):
self.message = message
self.subscriptions = subscriptions or []

def __repr__(self):
t = '{cls}(collection_name={collection_name})'
return t.format(cls=type(self).__name__, **vars(self))


class Subscription(Entity):
'''Subscription entity.
Expand Down Expand Up @@ -281,3 +317,7 @@ def __init__(self, subscription_id, status=STATUS_UNKNOWN,
self.delivery_parameters = delivery_parameters
self.subscription_parameters = subscription_parameters
self.poll_instances = poll_instances

def __repr__(self):
t = '{cls}(subscription_id={id}, status={status})'
return t.format(cls=type(self).__name__, **vars(self))
66 changes: 66 additions & 0 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pytest

from cabby import entities


@pytest.mark.parametrize('obj, expected', [
(
entities.ContentBlockCount(count=10, is_partial=False),
'ContentBlockCount(count=10, is_partial=False)',
),
(
entities.Collection(name='test', description='not today'),
'Collection(name=test, type=DATA_FEED, available=None)',
),
(
entities.ContentBinding(id=17),
'ContentBinding(id=17, subtypes=[])',
),
(
entities.ServiceInstance(
protocol='http', address='eiq', message_bindings=[],
),
'ServiceInstance(protocol=http, address=eiq)',
),
(
entities.InboxService(
protocol='http', address='eiq', message_bindings=[],
),
'InboxService(protocol=http, address=eiq)',
),
(
entities.PushMethod(protocol='http', message_bindings=[]),
'PushMethod(protocol=http)',
),
(
entities.SubscriptionParameters(response_type='FULL'),
'SubscriptionParameters(response_type=FULL)',
),
(
entities.DetailedServiceInstance(
type='test', version='urn:taxii.mitre.org:services:1.0',
protocol='https', address='eiq', message_bindings=[]),
'DetailedServiceInstance(type=test, address=eiq)',
),
(
entities.InboxDetailedService(
type='test', version='urn:taxii.mitre.org:services:1.0',
protocol='https', address='eiq',
message_bindings=[], content_bindings=[]),
'InboxDetailedService(type=test, address=eiq)',
),
(
entities.ContentBlock(content='', content_binding=[], timestamp=123),
'ContentBlock(timestamp=123)',
),
(
entities.SubscriptionResponse(collection_name='eiq'),
'SubscriptionResponse(collection_name=eiq)',
),
(
entities.Subscription(subscription_id=456),
'Subscription(subscription_id=456, status=UNKNOWN)',
),
])
def test_repr(obj, expected):
assert repr(obj) == expected

0 comments on commit 767e993

Please sign in to comment.