Browse Source

add in simple ro attrib definition..

main
John-Mark Gurney 4 years ago
parent
commit
dbf53bbac3
3 changed files with 28 additions and 1 deletions
  1. +13
    -0
      bitelab/__init__.py
  2. +14
    -0
      bitelab/abstract.py
  3. +1
    -1
      bitelab/testing.py

+ 13
- 0
bitelab/__init__.py View File

@@ -72,6 +72,9 @@ def new_parse_socket_addr(domain, addr):

tcp_server.parse_socket_addr = new_parse_socket_addr

class SerialConsole(DefROAttribute):
defattrname = 'console'

class BoardImpl:
def __init__(self, name, brdclass, options):
self.name = name
@@ -825,3 +828,13 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
'attrs': { 'power': True },
}
self.assertEqual(res.json(), info)

class TestAttrs(unittest.IsolatedAsyncioTestCase):
async def test_serialconsole(self):
data = 'somepath'
sc = SerialConsole(data)

self.assertEqual(data, await sc.getvalue())

with self.assertRaises(TypeError):
await sc.setvalue(data)

+ 14
- 0
bitelab/abstract.py View File

@@ -41,5 +41,19 @@ class Attribute:
async def setvalue(self, v): # pragma: no cover
raise NotImplementedError

class ROAttribute(Attribute):
'''A read-only attribute class. This implements setvalue, but
raises a TypeError when setvalue is called.'''

async def setvalue(self, v):
raise TypeError('%s is read-only.' % self.__class__.__name__)

class DefROAttribute(ROAttribute):
def __init__(self, val):
self._value = val

async def getvalue(self):
return self._value

class Power(Attribute):
defattrname = 'power'

+ 1
- 1
bitelab/testing.py View File

@@ -30,5 +30,5 @@

from .snmp import TestSNMPPower, TestSNMPWrapper
from .data import TestDatabase
from . import TestBiteLab, TestUnhashLRU
from . import TestBiteLab, TestUnhashLRU, TestAttrs
from .__main__ import TestClient

Loading…
Cancel
Save