class Channel: """A channel of communications.""" id: s.str() backend: s.dataclass(Backend) = field(default_factory=Backend) head: s.str() = "From {from}: " users: s.list(s.dataclass(User)) = field(default_factory=list) phones: s.list(s.dataclass(Phone)) = field(default_factory=list) aliases: s.dataclass(Aliases) = field(default_factory=Aliases) rcpt: vs.nick() = "ops"
def init(path=None): global config file = path if path else f"{app_dir}/config.toml" try: with open(file, "r") as f: config = s.dataclass(Config).json_decode(toml.load(f)) except FileNotFoundError: if path: # explict path missing raises exception raise FileNotFoundError(f"config file {path} not found") config = Config()
def test_dataclass_required(): @dataclasses.dataclass class DC: fjx: s.str() fjy: s.str() _required = "fjx fjy" schema = s.dataclass(DC) _error(schema.validate, DC(fjx=None, fjy=None)) _error(schema.validate, DC(fjx="foo", fjy=None)) _error(schema.validate, DC(fjx=None, fjy="foo")) schema.validate(DC(fjx="foo", fjy="foo"))
def test_binary(database): @dataclass class Bin: id: s.uuid() bin: s.bytes(format="binary") schema = s.dataclass(Bin) row = Bin(uuid4(), b"12345") table = db.Table("bin", schema, "id") database.create_table(table) try: resource = db.TableResource(table) resource.database = database resource.create(row.id, row) assert resource.read(row.id) == row row.bin = b"bacon" resource.update(row.id, row) assert resource.read(row.id).bin == b"bacon" finally: database.drop_table(table)
"Identifies the organization the monitoring task managed under.") start_time: s.datetime( description= "Date and time (inclusive) that the task is scheduled to begin.") end_time: s.datetime( description= "Date and time (inclusive) that the task is scheduled to end.") frequency: s.int(description="Dial frequency to monitor, in hertz.") modulation: ks.modulation( description="Modulation of signals to be processed.") bandwidth: s.int(description="Monitoring bandwidth, in hertz.") _required = "team_id start_time end_time frequency modulation bandwidth" schema = s.dataclass(Task) class Tasks(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
"""Detected signal, computed from station reception reports.""" id: s.uuid(description="Identifies signal.") task_id: s.uuid(description="Identifies task that signal is for.") report_ids: s.set(description="Station receiption reports of signal.", items=s.uuid()) time: s.datetime(description="Date and time of signal.") duration: s.int(description="Duration of signal, in seconds.", min=1) location: roax.geo.Point( description="Computed location of transmitting station.") cep: s.int(description="Circle error probable of location, in metres.") _required = "task_id report_ids time duration location cep" schema = s.dataclass(Signal) class Signals(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
class Team: """Team of users and monitoring tasks within a geographic region.""" id: s.uuid(description="Identifies the team.") name: s.str(description="Name of the team.") description: s.str(description="Description of the team.") area: roax.geo.Polygon( description="Area from which signal reports will be accepted.", max_rings=1) visibility: s.str(description="Organization visibility.", enum={"public", "private"}) _required = "name description area visibility" schema = s.dataclass(Team) class Teams(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
description="Hash of the password for user to authenticate with server." ) name: s.str(description="Full name of the user.") call_sign: s.str(description="Call sign of the user.") status: s.str(enum={"active", "suspended"}, description="Status of the user.") created: s.datetime(description="Date and time user record was created.") failures: s.list( items=s.datetime(), description="Date and time of recent consecutive login failures.", ) _required = "email name status created" schema = s.dataclass(User) class Users(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
def test_dataclass_json_encode_error(): DC = make_dataclass("DC", [("ejh", s.int())]) _error(s.dataclass(DC).json_encode, DC(ejh="not an int"))
def test_dataclass_json_encode_success(): DC = make_dataclass("DC", [("eja", s.str()), ("ejb", s.int())]) assert s.dataclass(DC).json_encode(DC(eja="foo", ejb=123)) == { "eja": "foo", "ejb": 123, }
def test_dataclass_validate_optional_success(): DC = make_dataclass("DC", [("k", s.str()), ("l", s.str(nullable=True), field(default=None))]) s.dataclass(DC).validate(DC(k="m"))
def test_dataclass_validate_required_error(): DC = make_dataclass("DC", [("f", s.str())]) _error(s.dataclass(DC).validate, DC(f=None))
def test_dataclass_validate_required_success(): DC = make_dataclass("DC", [("e", s.float())]) s.dataclass(DC).validate(DC(e=1.2))
def test_dataclass_validate_error(): DC = make_dataclass("DC", [("c", s.int())]) _error(s.dataclass(DC).validate, DC(c="does not validate"))
def test_dataclass_validate_success(): DC = make_dataclass("DC", [("a", s.str())]) s.dataclass(DC).validate(DC(a="b"))
id: s.uuid(description="Identifies the membership.") team_id: s.uuid(description="Identifies the team.") user_id: s.uuid(description="Identifies the user.") status: s.str( description="Status of user's group membership.", enum={"active", "suspended", "requested", "denied"}, ) roles: s.set( description="User role(s) in team.", items=s.str(enum={"read", "submit", "admin", "owner"}), ) _required = "team_id user_id status roles" schema = s.dataclass(Member) class Members(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
@dataclasses.dataclass class Token: """Token for accessing Kaptos server.""" id: s.uuid(description="Identifies the token.") type: s.str(description="Token type.", enum={"password", "session", "reset"}) value: s.str(description="Token value.") subject: s.uuid(description="Reference to subject of token.") created: s.datetime(description="Date and time token was created.") expires: s.datetime(description="Date and time token expires.") _required = "type value subject created" schema = s.dataclass(Token) class Tokens(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
def test_dataclass_json_decode_success(): DC = make_dataclass("DC", [("dja", s.float()), ("djb", s.bool())]) assert s.dataclass(DC).json_decode({ "dja": 802.11, "djb": True }) == DC(dja=802.11, djb=True)
@dataclass class Channel: """A channel of communications.""" id: s.str() backend: s.dataclass(Backend) = field(default_factory=Backend) head: s.str() = "From {from}: " users: s.list(s.dataclass(User)) = field(default_factory=list) phones: s.list(s.dataclass(Phone)) = field(default_factory=list) aliases: s.dataclass(Aliases) = field(default_factory=Aliases) rcpt: vs.nick() = "ops" _schema = s.dataclass(Channel) class Channels(roax.file.FileResource): """Vokiz channels resource.""" schema = _schema extension = ".json" def __init__(self): self.dir = vokiz.config.config.channel_dir super().__init__() def read(self, id): """Read a channel resource item.""" result = super().read(id)
def test_dataclass_json_decode_optional_success(): DC = make_dataclass("DC", [("djc", s.int()), ("djd", s.str(), field(default=None))]) assert s.dataclass(DC).json_decode({"djc": 12345}) == DC(djc=12345, djd=None)
) bearing: ks.bearing(description="Bearing toward received signal, in degrees true.") duration: s.int( description="Duration of received signal, in seconds rounded-up.", min=1 ) strength: s.int(description="Strength of the received signal, in dBm.") nmea: s.str( description="$GPRMC NMEA sentence received from GPS at the time the signal was detected." ) _required = ( "task_id station_id time frequency modulation location bearing duration strength nmea", ) schema = s.dataclass(Report) class Reports(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
def test_dataclass_json_decode_error(): DC = make_dataclass("DC", [("djx", s.str())]) _error(s.dataclass(DC).json_decode, {"djx": False})
@dataclasses.dataclass class DC: id: s.uuid() str: s.str(nullable=True) dict: s.dict({"a": s.int()}, nullable=True) list: s.list(s.int(), nullable=True) _set: s.set(s.str(), nullable=True) int: s.int(nullable=True) float: s.float(nullable=True) bool: s.bool(nullable=True) bytes: s.bytes(format="byte", nullable=True) date: s.date(nullable=True) datetime: s.datetime(nullable=True) DC._schema = s.dataclass(DC) class TR(db.TableResource): def __init__(self, database): super().__init__(db.Table("foo", DC._schema, "id"), database=database) @pytest.fixture(scope="module") def database(): with tempfile.TemporaryDirectory() as dir: database = sqlite.Database(f"{dir}/test.db") foo = db.Table("foo", DC._schema, "id") database.create_table(foo) yield database database.drop_table(foo)
def test_dataclass_disallow_none(): DC = make_dataclass("DC", [("foo", s.str())]) _error(s.dataclass(DC).json_encode, None)
from roax.resource import operation @dataclasses.dataclass class Station: """Monitoring station.""" id: s.uuid(description="Identifies the station.") name: s.str(description="Station name.") description: s.str(description="Description of the station.") owner_id: s.uuid(description="Identifies the user who controls the station.") _required = "name description owner_id" schema = s.dataclass(Station) class Stations(kaptos.db.TableResource): schema = schema @operation def create(self, _body: schema) -> s.dict({"id": schema.attrs.id}): return super().create(_body) @operation def read(self, id: schema.attrs.id) -> schema: return super().read(id) @operation
def test_dataclass_allow_none(): DC = make_dataclass("DC", [("foo", s.str())]) assert s.dataclass(DC, nullable=True).json_encode(None) == None