mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-05-03 01:22:59 +00:00
Use the existing Tahoe JSON encoder.
This commit is contained in:
parent
3ddfb92484
commit
83fa028925
@ -125,5 +125,5 @@ if sys.platform == "win32":
|
|||||||
initialize()
|
initialize()
|
||||||
|
|
||||||
from eliot import to_file
|
from eliot import to_file
|
||||||
from allmydata.util.eliotutil import BytesEliotJSONEncoder
|
from allmydata.util.jsonbytes import AnyBytesJSONEncoder
|
||||||
to_file(open("eliot.log", "wb"), encoder=BytesEliotJSONEncoder)
|
to_file(open("eliot.log", "wb"), encoder=AnyBytesJSONEncoder)
|
||||||
|
@ -38,8 +38,8 @@ from twisted.python.monkey import (
|
|||||||
MonkeyPatcher,
|
MonkeyPatcher,
|
||||||
)
|
)
|
||||||
|
|
||||||
from ..util.eliotutil import (
|
from ..util.jsonbytes import (
|
||||||
BytesEliotJSONEncoder
|
AnyBytesJSONEncoder
|
||||||
)
|
)
|
||||||
|
|
||||||
_NAME = Field.for_types(
|
_NAME = Field.for_types(
|
||||||
@ -147,7 +147,7 @@ def with_logging(
|
|||||||
"""
|
"""
|
||||||
@wraps(test_method)
|
@wraps(test_method)
|
||||||
def run_with_logging(*args, **kwargs):
|
def run_with_logging(*args, **kwargs):
|
||||||
validating_logger = MemoryLogger(encoder=BytesEliotJSONEncoder)
|
validating_logger = MemoryLogger(encoder=AnyBytesJSONEncoder)
|
||||||
original = swap_logger(None)
|
original = swap_logger(None)
|
||||||
try:
|
try:
|
||||||
swap_logger(_TwoLoggers(original, validating_logger))
|
swap_logger(_TwoLoggers(original, validating_logger))
|
||||||
|
@ -67,7 +67,7 @@ from allmydata.util import (
|
|||||||
configutil,
|
configutil,
|
||||||
jsonbytes as json,
|
jsonbytes as json,
|
||||||
)
|
)
|
||||||
from allmydata.util.eliotutil import capture_logging, BytesEliotJSONEncoder
|
from allmydata.util.eliotutil import capture_logging
|
||||||
from allmydata.util.fileutil import abspath_expanduser_unicode
|
from allmydata.util.fileutil import abspath_expanduser_unicode
|
||||||
from allmydata.interfaces import IFilesystemNode, IFileNode, \
|
from allmydata.interfaces import IFilesystemNode, IFileNode, \
|
||||||
IImmutableFileNode, IMutableFileNode, IDirectoryNode
|
IImmutableFileNode, IMutableFileNode, IDirectoryNode
|
||||||
@ -850,7 +850,7 @@ class StorageClients(SyncTestCase):
|
|||||||
actionType=u"storage-client:broker:set-static-servers",
|
actionType=u"storage-client:broker:set-static-servers",
|
||||||
succeeded=True,
|
succeeded=True,
|
||||||
),
|
),
|
||||||
encoder_=BytesEliotJSONEncoder
|
encoder_=json.AnyBytesJSONEncoder
|
||||||
)
|
)
|
||||||
def test_static_servers(self, logger):
|
def test_static_servers(self, logger):
|
||||||
"""
|
"""
|
||||||
@ -885,7 +885,7 @@ class StorageClients(SyncTestCase):
|
|||||||
actionType=u"storage-client:broker:make-storage-server",
|
actionType=u"storage-client:broker:make-storage-server",
|
||||||
succeeded=False,
|
succeeded=False,
|
||||||
),
|
),
|
||||||
encoder_=BytesEliotJSONEncoder
|
encoder_=json.AnyBytesJSONEncoder
|
||||||
)
|
)
|
||||||
def test_invalid_static_server(self, logger):
|
def test_invalid_static_server(self, logger):
|
||||||
"""
|
"""
|
||||||
|
@ -63,7 +63,6 @@ from twisted.internet.task import deferLater
|
|||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
|
|
||||||
from ..util.eliotutil import (
|
from ..util.eliotutil import (
|
||||||
BytesEliotJSONEncoder,
|
|
||||||
log_call_deferred,
|
log_call_deferred,
|
||||||
_parse_destination_description,
|
_parse_destination_description,
|
||||||
_EliotLogging,
|
_EliotLogging,
|
||||||
@ -87,21 +86,6 @@ def passes():
|
|||||||
return AfterPreprocessing(run, Equals(True))
|
return AfterPreprocessing(run, Equals(True))
|
||||||
|
|
||||||
|
|
||||||
class BytesEliotJSONEncoderTests(TestCase):
|
|
||||||
"""Tests for ``BytesEliotJSONEncoder``."""
|
|
||||||
|
|
||||||
def test_encoding_bytes(self):
|
|
||||||
"""``BytesEliotJSONEncoder`` can encode bytes."""
|
|
||||||
encoder = BytesEliotJSONEncoder()
|
|
||||||
self.assertEqual(encoder.default(b"xxx"), "xxx")
|
|
||||||
self.assertEqual(encoder.default(bytes([12])), "\x0c")
|
|
||||||
|
|
||||||
def test_encoding_sets(self):
|
|
||||||
"""``BytesEliotJSONEncoder`` can encode sets."""
|
|
||||||
encoder = BytesEliotJSONEncoder()
|
|
||||||
self.assertIn(encoder.default({1, 2}), ([1, 2], [2, 1]))
|
|
||||||
|
|
||||||
|
|
||||||
class EliotLoggedTestTests(TestCase):
|
class EliotLoggedTestTests(TestCase):
|
||||||
"""
|
"""
|
||||||
Tests for the automatic log-related provided by ``AsyncTestCase``.
|
Tests for the automatic log-related provided by ``AsyncTestCase``.
|
||||||
|
@ -117,7 +117,7 @@ class TestStreamingLogs(AsyncTestCase):
|
|||||||
proto.transport.loseConnection()
|
proto.transport.loseConnection()
|
||||||
yield proto.is_closed
|
yield proto.is_closed
|
||||||
|
|
||||||
self.assertThat(len(messages), Equals(3))
|
self.assertThat(len(messages), Equals(3), messages)
|
||||||
self.assertThat(messages[0]["action_type"], Equals("test:cli:some-exciting-action"))
|
self.assertThat(messages[0]["action_type"], Equals("test:cli:some-exciting-action"))
|
||||||
self.assertThat(messages[0]["arguments"],
|
self.assertThat(messages[0]["arguments"],
|
||||||
Equals(["hello", "good-\\xff-day", 123, {"a": 35}, [None]]))
|
Equals(["hello", "good-\\xff-day", 123, {"a": 35}, [None]]))
|
||||||
|
@ -50,7 +50,6 @@ from eliot.testing import (
|
|||||||
MemoryLogger,
|
MemoryLogger,
|
||||||
capture_logging,
|
capture_logging,
|
||||||
)
|
)
|
||||||
from eliot.json import EliotJSONEncoder
|
|
||||||
|
|
||||||
from eliot._validation import (
|
from eliot._validation import (
|
||||||
ValidationError,
|
ValidationError,
|
||||||
@ -78,16 +77,7 @@ from twisted.internet.defer import (
|
|||||||
)
|
)
|
||||||
from twisted.application.service import Service
|
from twisted.application.service import Service
|
||||||
|
|
||||||
|
from .jsonbytes import AnyBytesJSONEncoder
|
||||||
class BytesEliotJSONEncoder(EliotJSONEncoder):
|
|
||||||
"""Support encoding bytes."""
|
|
||||||
|
|
||||||
def default(self, o):
|
|
||||||
if isinstance(o, bytes):
|
|
||||||
return o.decode("utf-8", "backslashreplace")
|
|
||||||
if isinstance(o, set):
|
|
||||||
return list(o)
|
|
||||||
return EliotJSONEncoder.default(self, o)
|
|
||||||
|
|
||||||
|
|
||||||
def validateInstanceOf(t):
|
def validateInstanceOf(t):
|
||||||
@ -306,7 +296,7 @@ class _DestinationParser(object):
|
|||||||
rotateLength=rotate_length,
|
rotateLength=rotate_length,
|
||||||
maxRotatedFiles=max_rotated_files,
|
maxRotatedFiles=max_rotated_files,
|
||||||
)
|
)
|
||||||
return lambda reactor: FileDestination(get_file(), encoder=BytesEliotJSONEncoder)
|
return lambda reactor: FileDestination(get_file(), encoder=AnyBytesJSONEncoder)
|
||||||
|
|
||||||
|
|
||||||
_parse_destination_description = _DestinationParser().parse
|
_parse_destination_description = _DestinationParser().parse
|
||||||
|
@ -61,6 +61,9 @@ class UTF8BytesJSONEncoder(json.JSONEncoder):
|
|||||||
"""
|
"""
|
||||||
A JSON encoder than can also encode UTF-8 encoded strings.
|
A JSON encoder than can also encode UTF-8 encoded strings.
|
||||||
"""
|
"""
|
||||||
|
def default(self, o):
|
||||||
|
return bytes_to_unicode(False, o)
|
||||||
|
|
||||||
def encode(self, o, **kwargs):
|
def encode(self, o, **kwargs):
|
||||||
return json.JSONEncoder.encode(
|
return json.JSONEncoder.encode(
|
||||||
self, bytes_to_unicode(False, o), **kwargs)
|
self, bytes_to_unicode(False, o), **kwargs)
|
||||||
@ -77,6 +80,9 @@ class AnyBytesJSONEncoder(json.JSONEncoder):
|
|||||||
Bytes are decoded to strings using UTF-8, if that fails to decode then the
|
Bytes are decoded to strings using UTF-8, if that fails to decode then the
|
||||||
bytes are quoted.
|
bytes are quoted.
|
||||||
"""
|
"""
|
||||||
|
def default(self, o):
|
||||||
|
return bytes_to_unicode(True, o)
|
||||||
|
|
||||||
def encode(self, o, **kwargs):
|
def encode(self, o, **kwargs):
|
||||||
return json.JSONEncoder.encode(
|
return json.JSONEncoder.encode(
|
||||||
self, bytes_to_unicode(True, o), **kwargs)
|
self, bytes_to_unicode(True, o), **kwargs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user