Skip to content

Commit a236ad3

Browse files
committed
Use black to format code
1 parent 86a2d7c commit a236ad3

36 files changed

+2106
-2016
lines changed

.pre-commit-config.yaml

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exclude: '^py_zipkin/encoding/protobuf/zipkin_pb2.py$'
22
repos:
33
- repo: git://github.com/pre-commit/pre-commit-hooks
4-
rev: v2.1.0
4+
rev: v2.5.0
55
hooks:
66
- id: trailing-whitespace
77
- id: end-of-file-fixer
@@ -11,17 +11,16 @@ repos:
1111
- id: debug-statements
1212
- id: name-tests-test
1313
exclude: tests/test_helpers.py
14-
- id: flake8
15-
args:
16-
- --max-line-length=83
17-
exclude: docs/source/conf.py
1814
- id: requirements-txt-fixer
1915
- repo: https://github.com/asottile/reorder_python_imports.git
20-
rev: v1.3.5
16+
rev: v1.9.0
2117
hooks:
2218
- id: reorder-python-imports
2319
language_version: python2.7
24-
- repo: https://github.com/pre-commit/mirrors-autopep8
25-
rev: v1.4.3
20+
- repo: https://github.com/psf/black
21+
rev: 19.10b0
2622
hooks:
27-
- id: autopep8
23+
- id: black
24+
language_version: python3.7
25+
exclude: setup.py
26+
args: [--target-version, py27]

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ matrix:
1717
- python: 3.7
1818
env: TOXENV=py37
1919
- python: 3.7
20-
env: TOXENV=flake8
20+
env: TOXENV=black
2121
before_install: pip install -U pip==18.0
2222
install: pip install tox coveralls
2323
script: tox
2424
after_success: coveralls
25-
sudo: false
2625
deploy:
2726
provider: pypi
2827
user: yelplabs

py_zipkin/encoding/__init__.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def detect_span_version_and_encoding(message):
3333
if six.PY2:
3434
message = six.b(message) # pragma: no cover
3535
else:
36-
message = message.encode('utf-8') # pragma: no cover
36+
message = message.encode("utf-8") # pragma: no cover
3737

3838
if len(message) < 2:
3939
raise ZipkinError("Invalid span format. Message too short.")
@@ -44,10 +44,10 @@ def detect_span_version_and_encoding(message):
4444
return Encoding.V2_PROTO3
4545
return Encoding.V1_THRIFT
4646

47-
str_msg = message.decode('utf-8')
47+
str_msg = message.decode("utf-8")
4848

4949
# JSON case for list of spans
50-
if str_msg[0] == '[':
50+
if str_msg[0] == "[":
5151
span_list = json.loads(str_msg)
5252
if len(span_list) > 0:
5353
# Assumption: All spans in a list are the same version
@@ -57,12 +57,8 @@ def detect_span_version_and_encoding(message):
5757
for span in span_list:
5858
if any(word in span for word in _V2_ATTRIBUTES):
5959
return Encoding.V2_JSON
60-
elif (
61-
'binaryAnnotations' in span or
62-
(
63-
'annotations' in span and
64-
'endpoint' in span['annotations']
65-
)
60+
elif "binaryAnnotations" in span or (
61+
"annotations" in span and "endpoint" in span["annotations"]
6662
):
6763
return Encoding.V1_JSON
6864
return Encoding.V2_JSON

py_zipkin/encoding/_decoders.py

+47-46
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from py_zipkin.thrift import zipkin_core
1818

1919
_HEX_DIGITS = "0123456789abcdef"
20-
_DROP_ANNOTATIONS = {'cs', 'sr', 'ss', 'cr'}
20+
_DROP_ANNOTATIONS = {"cs", "sr", "ss", "cr"}
2121

22-
log = logging.getLogger('py_zipkin.encoding')
22+
log = logging.getLogger("py_zipkin.encoding")
2323

2424

2525
def get_decoder(encoding):
@@ -33,12 +33,10 @@ def get_decoder(encoding):
3333
if encoding == Encoding.V1_THRIFT:
3434
return _V1ThriftDecoder()
3535
if encoding == Encoding.V1_JSON:
36-
raise NotImplementedError(
37-
'{} decoding not yet implemented'.format(encoding))
36+
raise NotImplementedError("{} decoding not yet implemented".format(encoding))
3837
if encoding == Encoding.V2_JSON:
39-
raise NotImplementedError(
40-
'{} decoding not yet implemented'.format(encoding))
41-
raise ZipkinError('Unknown encoding: {}'.format(encoding))
38+
raise NotImplementedError("{} decoding not yet implemented".format(encoding))
39+
raise ZipkinError("Unknown encoding: {}".format(encoding))
4240

4341

4442
class IDecoder(object):
@@ -56,7 +54,6 @@ def decode_spans(self, spans):
5654

5755

5856
class _V1ThriftDecoder(IDecoder):
59-
6057
def decode_spans(self, spans):
6158
"""Decodes an encoded list of spans.
6259
@@ -89,22 +86,18 @@ def _convert_from_thrift_endpoint(self, thrift_endpoint):
8986
"""
9087
ipv4 = None
9188
ipv6 = None
92-
port = struct.unpack('H', struct.pack('h', thrift_endpoint.port))[0]
89+
port = struct.unpack("H", struct.pack("h", thrift_endpoint.port))[0]
9390

9491
if thrift_endpoint.ipv4 != 0:
9592
ipv4 = socket.inet_ntop(
96-
socket.AF_INET,
97-
struct.pack('!i', thrift_endpoint.ipv4),
93+
socket.AF_INET, struct.pack("!i", thrift_endpoint.ipv4),
9894
)
9995

10096
if thrift_endpoint.ipv6:
10197
ipv6 = socket.inet_ntop(socket.AF_INET6, thrift_endpoint.ipv6)
10298

10399
return Endpoint(
104-
service_name=thrift_endpoint.service_name,
105-
ipv4=ipv4,
106-
ipv6=ipv6,
107-
port=port,
100+
service_name=thrift_endpoint.service_name, ipv4=ipv4, ipv6=ipv6, port=port,
108101
)
109102

110103
def _decode_thrift_annotations(self, thrift_annotations):
@@ -127,17 +120,18 @@ def _decode_thrift_annotations(self, thrift_annotations):
127120
thrift_annotation.host,
128121
)
129122

130-
if 'cs' in all_annotations and 'sr' not in all_annotations:
123+
if "cs" in all_annotations and "sr" not in all_annotations:
131124
kind = Kind.CLIENT
132-
timestamp = all_annotations['cs']
133-
duration = all_annotations['cr'] - all_annotations['cs']
134-
elif 'cs' not in all_annotations and 'sr' in all_annotations:
125+
timestamp = all_annotations["cs"]
126+
duration = all_annotations["cr"] - all_annotations["cs"]
127+
elif "cs" not in all_annotations and "sr" in all_annotations:
135128
kind = Kind.SERVER
136-
timestamp = all_annotations['sr']
137-
duration = all_annotations['ss'] - all_annotations['sr']
129+
timestamp = all_annotations["sr"]
130+
duration = all_annotations["ss"] - all_annotations["sr"]
138131

139132
annotations = {
140-
name: self.seconds(ts) for name, ts in all_annotations.items()
133+
name: self.seconds(ts)
134+
for name, ts in all_annotations.items()
141135
if name not in _DROP_ANNOTATIONS
142136
}
143137

@@ -152,7 +146,7 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
152146
remote_endpoint = None
153147

154148
for binary_annotation in thrift_binary_annotations:
155-
if binary_annotation.key == 'sa':
149+
if binary_annotation.key == "sa":
156150
remote_endpoint = self._convert_from_thrift_endpoint(
157151
thrift_endpoint=binary_annotation.host,
158152
)
@@ -167,8 +161,10 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
167161
elif annotation_type == zipkin_core.AnnotationType.STRING:
168162
tags[key] = value
169163
else:
170-
log.warning('Only STRING and BOOL binary annotations are '
171-
'supported right now and can be properly decoded.')
164+
log.warning(
165+
"Only STRING and BOOL binary annotations are "
166+
"supported right now and can be properly decoded."
167+
)
172168

173169
if binary_annotation.host:
174170
local_endpoint = self._convert_from_thrift_endpoint(
@@ -200,23 +196,28 @@ def _decode_thrift_span(self, thrift_span):
200196
duration = None
201197

202198
if thrift_span.parent_id:
203-
parent_id = self._convert_unsigned_long_to_lower_hex(
204-
thrift_span.parent_id,
205-
)
199+
parent_id = self._convert_unsigned_long_to_lower_hex(thrift_span.parent_id,)
206200

207201
if thrift_span.annotations:
208-
annotations, local_endpoint, kind, timestamp, duration = \
209-
self._decode_thrift_annotations(thrift_span.annotations)
202+
(
203+
annotations,
204+
local_endpoint,
205+
kind,
206+
timestamp,
207+
duration,
208+
) = self._decode_thrift_annotations(thrift_span.annotations)
210209

211210
if thrift_span.binary_annotations:
212-
tags, local_endpoint, remote_endpoint = \
213-
self._convert_from_thrift_binary_annotations(
214-
thrift_span.binary_annotations,
215-
)
211+
(
212+
tags,
213+
local_endpoint,
214+
remote_endpoint,
215+
) = self._convert_from_thrift_binary_annotations(
216+
thrift_span.binary_annotations,
217+
)
216218

217219
trace_id = self._convert_trace_id_to_string(
218-
thrift_span.trace_id,
219-
thrift_span.trace_id_high,
220+
thrift_span.trace_id, thrift_span.trace_id_high,
220221
)
221222

222223
return Span(
@@ -278,15 +279,15 @@ def _write_hex_long(self, data, pos, value):
278279
:param value: the value to write
279280
:type value: unsigned long
280281
"""
281-
self._write_hex_byte(data, pos + 0, (value >> 56) & 0xff)
282-
self._write_hex_byte(data, pos + 2, (value >> 48) & 0xff)
283-
self._write_hex_byte(data, pos + 4, (value >> 40) & 0xff)
284-
self._write_hex_byte(data, pos + 6, (value >> 32) & 0xff)
285-
self._write_hex_byte(data, pos + 8, (value >> 24) & 0xff)
286-
self._write_hex_byte(data, pos + 10, (value >> 16) & 0xff)
287-
self._write_hex_byte(data, pos + 12, (value >> 8) & 0xff)
288-
self._write_hex_byte(data, pos + 14, (value & 0xff))
282+
self._write_hex_byte(data, pos + 0, (value >> 56) & 0xFF)
283+
self._write_hex_byte(data, pos + 2, (value >> 48) & 0xFF)
284+
self._write_hex_byte(data, pos + 4, (value >> 40) & 0xFF)
285+
self._write_hex_byte(data, pos + 6, (value >> 32) & 0xFF)
286+
self._write_hex_byte(data, pos + 8, (value >> 24) & 0xFF)
287+
self._write_hex_byte(data, pos + 10, (value >> 16) & 0xFF)
288+
self._write_hex_byte(data, pos + 12, (value >> 8) & 0xFF)
289+
self._write_hex_byte(data, pos + 14, (value & 0xFF))
289290

290291
def _write_hex_byte(self, data, pos, byte):
291-
data[pos + 0] = ord(_HEX_DIGITS[int((byte >> 4) & 0xf)])
292-
data[pos + 1] = ord(_HEX_DIGITS[int(byte & 0xf)])
292+
data[pos + 0] = ord(_HEX_DIGITS[int((byte >> 4) & 0xF)])
293+
data[pos + 1] = ord(_HEX_DIGITS[int(byte & 0xF)])

0 commit comments

Comments
 (0)