Skip to content

Commit

Permalink
Noted write method in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiker committed Apr 21, 2023
1 parent e6d2822 commit 2da0925
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Encoding(name='text', version=UUID('e21c7f3c-8a33-41c5-9977-a76d3a32aa0d'))

>>> bt_config.format
Format(name='generic', version=UUID('7412167c-06e9-4698-aff2-e63eb59037e7'))

# To write it back
>>> kv3.write(bt_config, "tests/documents/bt_config.kv3", use_original_encoding=True)
```

## Install
Expand Down
19 changes: 12 additions & 7 deletions keyvalues3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def write(kv3: KV3File | ValueType, path_or_stream: str | os.PathLike | typing.I
Args:
kv3: The KV3File or KV3 value to write.
path: The path to write to.
path_or_stream: The file path to write to. Or a text/binary stream.
encoding: The encoding to use.
format: If kv3 passed is not a file, this is the format to build it with.
format: If a raw kv3 value is passed, this is the format to build it with. Default is 'generic'.
use_original_encoding: If a kv3 file is passed, use its original encoding.
"""

if not isinstance(kv3, KV3File):
Expand Down Expand Up @@ -115,12 +117,15 @@ def write(kv3: KV3File | ValueType, path_or_stream: str | os.PathLike | typing.I
fp.write(text_result)
else:
fp.write(text_result.encode("utf-8"))
elif encoding == ENCODING_BINARY_UNCOMPRESSED:
binarywriter.BinaryV1UncompressedWriter(kv3).write(fp)
elif encoding == ENCODING_BINARY_BLOCK_LZ4:
binarywriter.BinaryLZ4(kv3).write(fp)
else:
raise NotImplementedError(f"Encoding type {encoding} not implemented.")
if isinstance(fp, io.TextIOBase):
raise TypeError("Cannot write binary KV3 to a text stream. If this is a file, please open it in binary mode ('wb').")
if encoding == ENCODING_BINARY_UNCOMPRESSED:
binarywriter.BinaryV1UncompressedWriter(kv3).write(fp)
elif encoding == ENCODING_BINARY_BLOCK_LZ4:
binarywriter.BinaryLZ4(kv3).write(fp)
else:
raise NotImplementedError(f"Encoding type {encoding} not implemented.")

if is_file:
fp.close()
Expand Down
4 changes: 2 additions & 2 deletions keyvalues3/keyvalues3.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def check_valid(value: ValueType):
if nested_value is value:
raise ValueError("dict contains itself")
if not isinstance(key, str):
raise ValueError("dict key is not a string")
raise ValueError(f"dict key is not a string type, but {type(key)}")
if not key.isidentifier():
raise ValueError("dict key is not a valid identifier") # I think
raise ValueError(f"dict key '{key}' is not accepted (not a valid identifier)") # TODO: spaces and . are allowed
check_valid(nested_value)
case array.array() | bytes() | bytearray():
pass
Expand Down

0 comments on commit 2da0925

Please sign in to comment.