Skip to content

Commit 702cfbd

Browse files
authored
Merge pull request #4 from bauman/nopy2
fixup py3, remove py2
2 parents 3d4528c + 2648653 commit 702cfbd

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

bson2json.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from bsonstream import BSONInput
55
from sys import argv, stdout
66
import gzip
7-
import six
8-
97

108
if "gz" in argv[1] or "dz" in argv[1]:
119
f = gzip.open(argv[1], 'rb')
@@ -15,8 +13,5 @@
1513
stream = BSONInput(fh=f)
1614
for doc in stream:
1715
json_str = dumps(doc)
18-
if six.PY3:
19-
stdout.write("{}\n".format(json_str))
20-
else: # six.PY2
21-
stdout.write("%s\n" %json_str)
16+
stdout.write(f"{json_str}\n")
2217

bsonstream/__init__.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bson import InvalidBSON, BSON, codec_options
2-
import six
32
import sys
43
import struct
54

@@ -15,9 +14,14 @@ class BSONInput(object):
1514
https://github.com/klbostee/typedbytes
1615
"""
1716

18-
def __init__(self, fh=sys.stdin, unicode_errors='strict', fast_string_prematch="", decode=True):
17+
def __init__(self, fh=sys.stdin,
18+
unicode_errors: str = 'strict',
19+
fast_string_prematch: bytes = b"",
20+
decode: bool = True):
1921
self.fh = fh
2022
self.unicode_errors = unicode_errors
23+
if not isinstance(fast_string_prematch, bytes):
24+
raise ValueError("fast_string_prematch must be a bytes object")
2125
self.fast_string_prematch=fast_string_prematch
2226
self.eof = False
2327
self.decode = decode
@@ -26,26 +30,22 @@ def __init__(self, fh=sys.stdin, unicode_errors='strict', fast_string_prematch="
2630
def _read(self):
2731
try:
2832
size_bits = self.fh.read(4)
29-
size = struct.unpack("<i", size_bits)[0] - 4 # BSON size byte includes itself
33+
size = struct.unpack("<i", size_bits)[0] - 4 # BSON size byte includes itself
3034
data = size_bits + self.fh.read(size)
3135
if len(data) != size + 4:
3236
raise struct.error("Unable to read expected BSON Chunk; " +
3337
"EOF, underful buffer or invalid object size.")
34-
if six.PY3:
35-
eoo = 0x00
36-
else: # six.PY2
37-
eoo = "\x00"
38+
eoo = 0x00
3839
if data[size + 4 - 1] != eoo:
3940
raise InvalidBSON("Bad EOO in BSON Data")
40-
if self.fast_string_prematch.encode("utf-8") in data:
41+
if self.fast_string_prematch in data:
4142
if self.decode:
4243
try:
4344
return BSON(data).decode(self.codec)
4445
except TypeError:
4546
return BSON(data).decode()
4647
else:
4748
return data
48-
raise ValueError("Unknown Error")
4949
except struct.error as e:
5050
self.eof = True
5151
raise StopIteration(e)
@@ -60,13 +60,19 @@ def read(self):
6060
def _reads(self):
6161
r = self._read
6262
while 1:
63-
yield r()
63+
try:
64+
data = r()
65+
if data:
66+
yield data
67+
except StopIteration:
68+
break
6469

6570
def close(self):
6671
self.fh.close()
6772

6873
__iter__ = reads = _reads
6974

75+
7076
class KeyValueBSONInput(BSONInput):
7177
def read(self):
7278
try:
@@ -78,12 +84,12 @@ def read(self):
7884

7985
def reads(self):
8086
it = self._reads()
81-
if six.PY3:
82-
n = it.__next__
83-
else: # six.PY2
84-
n = it.next
87+
n = it.__next__
8588
while 1:
86-
doc = n()
87-
yield doc
89+
try:
90+
doc = n()
91+
yield doc
92+
except StopIteration:
93+
break
8894

8995
__iter__ = reads

0 commit comments

Comments
 (0)