Browse Source

use a configuration file for board setup...

main
John-Mark Gurney 4 years ago
parent
commit
262548d4b5
4 changed files with 50 additions and 21 deletions
  1. +33
    -21
      bitelab/__init__.py
  2. +1
    -0
      bitelab/config.py
  3. +15
    -0
      fixtures/board_conf.ucl
  4. +1
    -0
      setup.py

+ 33
- 21
bitelab/__init__.py View File

@@ -58,6 +58,7 @@ import sqlite3
import subprocess
import sys
import tempfile
import ucl
import unittest
import urllib

@@ -127,26 +128,36 @@ class BITEError(Exception):
status_code: int

class BoardManager(object):
board_class_info = {
'cora-z7s': {
'clsname': 'cora-z7s',
'arch': 'arm-armv7',
},
}

# Naming scheme:
# <abbreviated class>-<num>
#
board_gen = [
dict(name='cora-1', brdclass='cora-z7s', options=[
SNMPPower(host='poe', port=2),
]),
]

def __init__(self, settings):
self._settings = settings
_option_map = dict(
snmppower=SNMPPower,
)

def __init__(self, cls_info, boards):
# add the name to the classes
classes = { k: dict(clsname=k, **cls_info[k]) for k in cls_info }
self.board_class_info = classes

self.boards = dict(**{ x.name: x for x in
(BoardImpl(**y) for y in self.board_gen)})
(BoardImpl(**y) for y in boards)})

@classmethod
def from_settings(cls, settings):
return cls.from_ucl(settings.board_conf)

@classmethod
def from_ucl(cls, fname):
with open(fname) as fp:
conf = ucl.load(fp.read())

classes = conf['classes']

brds = conf['boards']
makeopt = lambda x: cls._option_map[x['cls']](**{ k: v for k, v in x.items() if k != 'cls' })
for i in brds:
opt = i['options']
opt[:] = [ makeopt(x) for x in opt ]

return cls(classes, brds)

def classes(self):
return self.board_class_info
@@ -202,7 +213,7 @@ def get_data(settings: config.Settings = Depends(get_settings)):
return d

async def real_get_boardmanager(settings, data):
brdmgr = BoardManager(settings)
brdmgr = BoardManager.from_settings(settings)

# Clean up the database
# XXX - This isn't a complete fix, we need a better solution.
@@ -496,9 +507,10 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
# setup settings
self.settings = config.Settings(db_file=self.dbtempfile.name,
setup_script='somesetupscript',
board_conf = os.path.join('fixtures', 'board_conf.ucl')
)

self.brdmgr = BoardManager(self.settings)
self.brdmgr = BoardManager.from_settings(self.settings)

self.app.dependency_overrides[get_settings] = \
self.get_settings_override


+ 1
- 0
bitelab/config.py View File

@@ -38,6 +38,7 @@ __all__ = [ 'Settings' ]
class Settings(BaseSettings):
db_file: str = Field(description='path to SQLite3 database file')
setup_script: str = Field(description='script that will initalize an environment')
board_conf: str = Field(description='UCL configuration for the boards')

class Config:
env_file = ".env"

+ 15
- 0
fixtures/board_conf.ucl View File

@@ -0,0 +1,15 @@
classes {
cora-z7s = {
arch = arm-armv7;
}
}

boards [
{
name = cora-1;
brdclass = cora-z7s;
options = [
{ cls = snmppower, host = poe, port = 2 },
]
},
]

+ 1
- 0
setup.py View File

@@ -24,6 +24,7 @@ setup(
'pydantic[dotenv]',
'aiokq @ git+https://www.funkthat.com/gitea/jmg/aiokq.git',
'orm',
'ucl',
'databases[sqlite]',
],
extras_require = {


Loading…
Cancel
Save