Add Readonly and WithRepr.

This commit is contained in:
Eric Snow 2018-01-18 17:35:36 +00:00
parent 56b4a8e68c
commit 2fc452e3d8
2 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,28 @@
class Readonly(object):
"""For read-only instances."""
def __setattr__(self, name, value):
raise AttributeError(
'{} objects are read-only'.format(type(self).__name__))
def __delattr__(self, name):
raise AttributeError(
'{} objects are read-only'.format(type(self).__name__))
def _bind_attrs(self, **attrs):
for name, value in attrs.items():
object.__setattr__(self, name, value)
class WithRepr(object):
def _init_args(self):
# XXX Extract from __init__()...
return ()
def __repr__(self):
args = ', '.join('{}={!r}'.format(arg, value)
for arg, value in self._init_args())
return '{}({})'.format(type(self).__name__, args)

View file

@ -0,0 +1,22 @@
from debugger_protocol._base import Readonly, WithRepr
class Base(Readonly, WithRepr):
"""Base class for message-related types."""
_INIT_ARGS = None
@classmethod
def from_data(cls, **kwargs):
"""Return an instance based on the given raw data."""
return cls(**kwargs)
def __init__(self):
self._validate()
def _validate(self):
pass
def as_data(self):
"""Return serializable data for the instance."""
return {}