mirror of
https://github.com/microsoft/debugpy.git
synced 2025-12-23 08:48:12 +00:00
Add Readonly and WithRepr.
This commit is contained in:
parent
56b4a8e68c
commit
2fc452e3d8
2 changed files with 50 additions and 0 deletions
28
debugger_protocol/_base.py
Normal file
28
debugger_protocol/_base.py
Normal 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)
|
||||
22
debugger_protocol/messages/_base.py
Normal file
22
debugger_protocol/messages/_base.py
Normal 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 {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue