Skip to content

Commit

Permalink
refactor: update _LockFileEncoder to Python3 syntax
Browse files Browse the repository at this point in the history
Also, added the encode method in addition to the iterencode
method.
This way we can later import this class in Pipenv instead
of redefining it there.
  • Loading branch information
oz123 committed Jun 12, 2024
1 parent 894def8 commit 93efebf
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/plette/lockfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,36 @@ class _LockFileEncoder(json.JSONEncoder):
This adds a few characteristics to the encoder:
* The JSON is always prettified with indents and spaces.
* The output is always UTF-8-encoded text, never binary, even on Python 2.
* TOMLKit's container elements are seamlessly encodable.
* The output is always UTF-8-encoded text, never binary.
"""
def __init__(self):
super(_LockFileEncoder, self).__init__(
indent=4, separators=(",", ": "), sort_keys=True,
super().__init__(
indent=4,
separators=(",", ": "),
sort_keys=True,
)

def encode(self, obj):
content = super(_LockFileEncoder, self).encode(obj)
content = super().encode(obj)
if not isinstance(content, str):
content = content.decode("utf-8")
content += "\n"
return content

def iterencode(self, obj):
for chunk in super(_LockFileEncoder, self).iterencode(obj):
for chunk in super().iterencode(obj):
if not isinstance(chunk, str):
chunk = chunk.decode("utf-8")
yield chunk
yield "\n"

def encode(self, obj):
content = super().encode(obj)
if not isinstance(content, str):
content = content.decode("utf-8")
return content


PIPFILE_SPEC_CURRENT = 6

Expand Down

0 comments on commit 93efebf

Please sign in to comment.