Skip to content

Commit 3e91712

Browse files
author
Curtis Mason
committed
Reformatting files
1 parent 7a9d4c3 commit 3e91712

25 files changed

+283
-348
lines changed

cloudevents/sdk/converters/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents.sdk.converters import binary
16-
from cloudevents.sdk.converters import structured
15+
from cloudevents.sdk.converters import binary, structured
1716

1817
TypeBinary = binary.BinaryHTTPCloudEventConverter.TYPE
1918
TypeStructured = structured.JSONHTTPCloudEventConverter.TYPE

cloudevents/sdk/converters/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def read(
2626
event,
2727
headers: dict,
2828
body: typing.IO,
29-
data_unmarshaller: typing.Callable
29+
data_unmarshaller: typing.Callable,
3030
) -> base.BaseEvent:
3131
raise Exception("not implemented")
3232

@@ -37,8 +37,6 @@ def can_read(self, content_type: str) -> bool:
3737
raise Exception("not implemented")
3838

3939
def write(
40-
self,
41-
event: base.BaseEvent,
42-
data_marshaller: typing.Callable
40+
self, event: base.BaseEvent, data_marshaller: typing.Callable
4341
) -> (dict, object):
4442
raise Exception("not implemented")

cloudevents/sdk/converters/binary.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from cloudevents.sdk import exceptions, types
1818
from cloudevents.sdk.converters import base
1919
from cloudevents.sdk.event import base as event_base
20-
from cloudevents.sdk.event import v03, v1
20+
from cloudevents.sdk.event import v1, v03
2121

2222

2323
class BinaryHTTPCloudEventConverter(base.Converter):
@@ -44,9 +44,7 @@ def read(
4444
return event
4545

4646
def write(
47-
self,
48-
event: event_base.BaseEvent,
49-
data_marshaller: types.MarshallerType
47+
self, event: event_base.BaseEvent, data_marshaller: types.MarshallerType
5048
) -> (dict, bytes):
5149
return event.MarshalBinary(data_marshaller)
5250

cloudevents/sdk/converters/structured.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import typing
1616

1717
from cloudevents.sdk import types
18-
1918
from cloudevents.sdk.converters import base
2019
from cloudevents.sdk.event import base as event_base
2120

@@ -43,9 +42,7 @@ def read(
4342
return event
4443

4544
def write(
46-
self,
47-
event: event_base.BaseEvent,
48-
data_marshaller: types.MarshallerType
45+
self, event: event_base.BaseEvent, data_marshaller: types.MarshallerType
4946
) -> (dict, bytes):
5047
http_headers = {"content-type": self.MIME_TYPE}
5148
return http_headers, event.MarshalJSON(data_marshaller).encode("utf-8")

cloudevents/sdk/event/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def MarshalJSON(self, data_marshaller: types.MarshallerType) -> str:
206206
def UnmarshalJSON(
207207
self,
208208
b: typing.Union[str, bytes],
209-
data_unmarshaller: types.UnmarshallerType
209+
data_unmarshaller: types.UnmarshallerType,
210210
):
211211
raw_ce = json.loads(b)
212212

@@ -228,9 +228,9 @@ def UnmarshalBinary(
228228
self,
229229
headers: dict,
230230
body: typing.Union[bytes, str],
231-
data_unmarshaller: types.UnmarshallerType
231+
data_unmarshaller: types.UnmarshallerType,
232232
):
233-
if 'ce-specversion' not in headers:
233+
if "ce-specversion" not in headers:
234234
raise ValueError("Missing required attribute: 'specversion'")
235235
for header, value in headers.items():
236236
header = header.lower()
@@ -244,8 +244,7 @@ def UnmarshalBinary(
244244
raise ValueError(f"Missing required attributes: {missing_attrs}")
245245

246246
def MarshalBinary(
247-
self,
248-
data_marshaller: types.MarshallerType
247+
self, data_marshaller: types.MarshallerType
249248
) -> (dict, bytes):
250249
if data_marshaller is None:
251250
data_marshaller = json.dumps

cloudevents/sdk/event/opt.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def set(self, new_value):
2424
if self.is_required and is_none:
2525
raise ValueError(
2626
"Attribute value error: '{0}', "
27-
"" "invalid new value."
28-
.format(self.name)
27+
""
28+
"invalid new value.".format(self.name)
2929
)
3030

3131
self.value = new_value
@@ -37,7 +37,9 @@ def required(self):
3737
return self.is_required
3838

3939
def __eq__(self, obj):
40-
return isinstance(obj, Option) and \
41-
obj.name == self.name and \
42-
obj.value == self.value and \
43-
obj.is_required == self.is_required
40+
return (
41+
isinstance(obj, Option)
42+
and obj.name == self.name
43+
and obj.value == self.value
44+
and obj.is_required == self.is_required
45+
)

cloudevents/sdk/event/v03.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents.sdk.event import base
16-
from cloudevents.sdk.event import opt
15+
from cloudevents.sdk.event import base, opt
1716

1817

1918
class Event(base.BaseEvent):
20-
_ce_required_fields = {
21-
'id',
22-
'source',
23-
'type',
24-
'specversion'
25-
}
19+
_ce_required_fields = {"id", "source", "type", "specversion"}
2620

2721
_ce_optional_fields = {
28-
'datacontentencoding',
29-
'datacontenttype',
30-
'schemaurl',
31-
'subject',
32-
'time'
22+
"datacontentencoding",
23+
"datacontenttype",
24+
"schemaurl",
25+
"subject",
26+
"time",
3327
}
3428

3529
def __init__(self):
@@ -40,9 +34,7 @@ def __init__(self):
4034

4135
self.ce__datacontenttype = opt.Option("datacontenttype", None, False)
4236
self.ce__datacontentencoding = opt.Option(
43-
"datacontentencoding",
44-
None,
45-
False
37+
"datacontentencoding", None, False
4638
)
4739
self.ce__subject = opt.Option("subject", None, False)
4840
self.ce__time = opt.Option("time", None, False)

cloudevents/sdk/event/v1.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,13 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents.sdk.event import base
16-
from cloudevents.sdk.event import opt
15+
from cloudevents.sdk.event import base, opt
1716

1817

1918
class Event(base.BaseEvent):
20-
_ce_required_fields = {
21-
'id',
22-
'source',
23-
'type',
24-
'specversion'
25-
}
26-
27-
_ce_optional_fields = {
28-
'datacontenttype',
29-
'dataschema',
30-
'subject',
31-
'time'
32-
}
19+
_ce_required_fields = {"id", "source", "type", "specversion"}
20+
21+
_ce_optional_fields = {"datacontenttype", "dataschema", "subject", "time"}
3322

3423
def __init__(self):
3524
self.ce__specversion = opt.Option("specversion", "1.0", True)

cloudevents/sdk/exceptions.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
class UnsupportedEvent(Exception):
1717
def __init__(self, event_class):
18-
super().__init__(
19-
"Invalid CloudEvent class: '{0}'".format(event_class)
20-
)
18+
super().__init__("Invalid CloudEvent class: '{0}'".format(event_class))
2119

2220

2321
class InvalidDataUnmarshaller(Exception):
@@ -27,16 +25,12 @@ def __init__(self):
2725

2826
class InvalidDataMarshaller(Exception):
2927
def __init__(self):
30-
super().__init__(
31-
"Invalid data marshaller, is not a callable"
32-
)
28+
super().__init__("Invalid data marshaller, is not a callable")
3329

3430

3531
class NoSuchConverter(Exception):
3632
def __init__(self, converter_type):
37-
super().__init__(
38-
"No such converter {0}".format(converter_type)
39-
)
33+
super().__init__("No such converter {0}".format(converter_type))
4034

4135

4236
class UnsupportedEventConverter(Exception):

cloudevents/sdk/http_events.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
import typing
1818
import uuid
1919

20-
from cloudevents.sdk import converters
21-
from cloudevents.sdk import marshaller
22-
from cloudevents.sdk import types
23-
24-
from cloudevents.sdk.event import v03, v1
20+
from cloudevents.sdk import converters, marshaller, types
21+
from cloudevents.sdk.event import v1, v03
2522

2623
_marshaller_by_format = {
2724
converters.TypeStructured: lambda x: x,
@@ -43,7 +40,7 @@ def _json_or_string(content: typing.Union[str, bytes]):
4340
return content
4441

4542

46-
class CloudEvent():
43+
class CloudEvent:
4744
"""
4845
Python-friendly cloudevent class supporting v1 events
4946
Supports both binary and structured mode CloudEvents
@@ -54,7 +51,7 @@ def from_http(
5451
cls,
5552
data: typing.Union[str, bytes],
5653
headers: typing.Dict[str, str],
57-
data_unmarshaller: types.UnmarshallerType = None
54+
data_unmarshaller: types.UnmarshallerType = None,
5855
):
5956
"""Unwrap a CloudEvent (binary or structured) from an HTTP request.
6057
:param data: the HTTP request body
@@ -68,18 +65,17 @@ def from_http(
6865
data_unmarshaller = _json_or_string
6966

7067
event = marshaller.NewDefaultHTTPMarshaller().FromRequest(
71-
v1.Event(), headers, data, data_unmarshaller)
68+
v1.Event(), headers, data, data_unmarshaller
69+
)
7270
attrs = event.Properties()
73-
attrs.pop('data', None)
74-
attrs.pop('extensions', None)
71+
attrs.pop("data", None)
72+
attrs.pop("extensions", None)
7573
attrs.update(**event.extensions)
7674

7775
return cls(attrs, event.data)
7876

7977
def __init__(
80-
self,
81-
attributes: typing.Dict[str, str],
82-
data: typing.Any = None
78+
self, attributes: typing.Dict[str, str], data: typing.Any = None
8379
):
8480
"""
8581
Event Constructor
@@ -97,28 +93,31 @@ def __init__(
9793
"""
9894
self._attributes = {k.lower(): v for k, v in attributes.items()}
9995
self.data = data
100-
if 'specversion' not in self._attributes:
101-
self._attributes['specversion'] = "1.0"
102-
if 'id' not in self._attributes:
103-
self._attributes['id'] = str(uuid.uuid4())
104-
if 'time' not in self._attributes:
105-
self._attributes['time'] = datetime.datetime.now(
106-
datetime.timezone.utc).isoformat()
107-
108-
if self._attributes['specversion'] not in _required_by_version:
96+
if "specversion" not in self._attributes:
97+
self._attributes["specversion"] = "1.0"
98+
if "id" not in self._attributes:
99+
self._attributes["id"] = str(uuid.uuid4())
100+
if "time" not in self._attributes:
101+
self._attributes["time"] = datetime.datetime.now(
102+
datetime.timezone.utc
103+
).isoformat()
104+
105+
if self._attributes["specversion"] not in _required_by_version:
109106
raise ValueError(
110-
f"Invalid specversion: {self._attributes['specversion']}")
107+
f"Invalid specversion: {self._attributes['specversion']}"
108+
)
111109
# There is no good way to default 'source' and 'type', so this
112110
# checks for those (or any new required attributes).
113-
required_set = _required_by_version[self._attributes['specversion']]
111+
required_set = _required_by_version[self._attributes["specversion"]]
114112
if not required_set <= self._attributes.keys():
115113
raise ValueError(
116-
f"Missing required keys: {required_set - attributes.keys()}")
114+
f"Missing required keys: {required_set - attributes.keys()}"
115+
)
117116

118117
def to_http(
119118
self,
120119
format: str = converters.TypeStructured,
121-
data_marshaller: types.MarshallerType = None
120+
data_marshaller: types.MarshallerType = None,
122121
) -> (dict, typing.Union[bytes, str]):
123122
"""
124123
Returns a tuple of HTTP headers/body dicts representing this cloudevent
@@ -133,15 +132,17 @@ def to_http(
133132
data_marshaller = _marshaller_by_format[format]
134133
if self._attributes["specversion"] not in _obj_by_version:
135134
raise ValueError(
136-
f"Unsupported specversion: {self._attributes['specversion']}")
135+
f"Unsupported specversion: {self._attributes['specversion']}"
136+
)
137137

138138
event = _obj_by_version[self._attributes["specversion"]]()
139139
for k, v in self._attributes.items():
140140
event.Set(k, v)
141141
event.data = self.data
142142

143143
return marshaller.NewDefaultHTTPMarshaller().ToRequest(
144-
event, format, data_marshaller)
144+
event, format, data_marshaller
145+
)
145146

146147
# Data access is handled via `.data` member
147148
# Attribute access is managed via Mapping type

cloudevents/sdk/marshaller.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
import typing
1717

1818
from cloudevents.sdk import exceptions, types
19-
20-
from cloudevents.sdk.converters import base
21-
from cloudevents.sdk.converters import binary
22-
from cloudevents.sdk.converters import structured
23-
19+
from cloudevents.sdk.converters import base, binary, structured
2420
from cloudevents.sdk.event import base as event_base
2521

2622

@@ -93,8 +89,9 @@ def ToRequest(
9389
:return: dict of HTTP headers and stream of HTTP request body
9490
:rtype: tuple
9591
"""
96-
if (data_marshaller is not None
97-
and not isinstance(data_marshaller, typing.Callable)):
92+
if data_marshaller is not None and not isinstance(
93+
data_marshaller, typing.Callable
94+
):
9895
raise exceptions.InvalidDataMarshaller()
9996

10097
if converter_type is None:
@@ -123,7 +120,7 @@ def NewDefaultHTTPMarshaller() -> HTTPMarshaller:
123120

124121

125122
def NewHTTPMarshaller(
126-
converters: typing.List[base.Converter]
123+
converters: typing.List[base.Converter],
127124
) -> HTTPMarshaller:
128125
"""
129126
Creates the default HTTP marshaller with both

cloudevents/sdk/types.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# both JSON and Binary format.
1919

2020
MarshallerType = typing.Optional[
21-
typing.Callable[[typing.Any], typing.Union[bytes, str]]]
21+
typing.Callable[[typing.Any], typing.Union[bytes, str]]
22+
]
2223
UnmarshallerType = typing.Optional[
23-
typing.Callable[
24-
[typing.Union[bytes, str]], typing.Any]]
24+
typing.Callable[[typing.Union[bytes, str]], typing.Any]
25+
]

0 commit comments

Comments
 (0)