class Node(model.Model): """ Stores the lightweight data associated with a node, and an index of heavy data (which has to be downloaded separately). This includes: - ``id``: self-descriptive. The ID of a node cannot be changed. - ``parent``: the node parent's ID. - ``pos_start`` and ``pos_end``: for nodes where it makes sense (mostly chunka), the range of positions occupied by this object. ``pos_start`` is inclusive, while ``pos_end`` is exclusive. - ``tags``: an unordered set of tags, which are simple strings. - ``attr``: a string-keyed dict of attributes with arbitrary data. - ``data``: an unordered set of keys with non-null heavyweight data. The data has to be downloaded separately. - ``bindata``: a dict with an index of available bindata. The keys in this dict correspond to bindata keys, and the values are size of the corresponding binary data in bytes. The actual bindata has to be downloaded separately. - ``triggers``: a dict containing active triggers for the given node. The keys are trigger names, and they are mapped to the state of the given trigger. """ id = fields.NodeID() parent = fields.NodeID(default=NodeID.root_id) pos_start = fields.Integer(optional=True) pos_end = fields.Integer(optional=True) tags = fields.Set(fields.String()) attr = fields.Map(fields.String(), fields.Any()) data = fields.Set(fields.String()) bindata = fields.Map(fields.String(), fields.SmallUnsignedInteger()) triggers = fields.Map(fields.String(), fields.Enum(TriggerState))
class MsgConnected(MsgpackMsg): """ Sent by the server in reply to MsgConnect. """ object_type = 'connected' proto_version = fields.SmallInteger(minimum=1) server_name = fields.String() server_version = fields.String()
class OperationCreate(Operation): object_type = 'create' parent = fields.NodeID(default=NodeID.root_id) pos_start = fields.Integer(optional=True) pos_end = fields.Integer(optional=True) tags = fields.Set(fields.String()) attr = fields.Map(fields.String(), fields.Any()) data = fields.Map(fields.String(), fields.Any()) bindata = fields.Map(fields.String(), fields.Binary()) triggers = fields.Set(fields.String())
class MsgPluginTriggerRegister(MsgpackMsg): """ Sent by the client to register a trigger handler. Has no reply. ``phid`` is an arbitrary number chosen by the client to identify this method handler. """ object_type = 'plugin_trigger_register' phid = fields.SmallUnsignedInteger() name = fields.String() tags = fields.Set(fields.String())
class MsgConnect(MsgpackMsg): """ Sent by the client immediately upon connection. Server replies with MsgConnected or MsgConnectionError. """ object_type = 'connect' proto_version = fields.SmallInteger(minimum=1) client_name = fields.String(optional=True) client_version = fields.String(optional=True) client_description = fields.String(optional=True) client_type = fields.String(optional=True) quit_on_close = fields.Boolean(default=False)
class CheckList(Check): object_type = 'list' parent = fields.NodeID() tags = fields.Set(fields.String()) pos_filter = fields.Object(PosFilter) nodes = fields.Set(fields.NodeID())
class OperationSetBinData(Operation): object_type = 'set_bindata' key = fields.String() start = fields.SmallUnsignedInteger(default=0) data = fields.Binary() truncate = fields.Boolean(default=False)
class MsgGetList(MsgpackMsg): """ Sent by client, requests a list of children of the given node matching the given filters. Children can be filtered by tags (the set of tags in the query must be a subset of the node's tags for it to match) and by a position filter. If sub is False, server will reply with a single MsgGetListReply with gone=[], or with a single MsgQueryError. If sub is True, the first reply will be like for sub=False, but subsequent replies will only mention changed nodes - nodes that are new on the list, along with changed nodes remaining on the list are sent in the objs field of the reply, while the IDs of nodes no longer on the list are sent in the gone field of the reply. The list may fail with ObjectGoneError if the parent node is gone. If it reappears in the future, a MsgGetListReply will be sent with a complete list of children. """ object_type = 'get_list' qid = fields.SmallUnsignedInteger() parent = fields.NodeID(default=NodeID.root_id) tags = fields.Set(fields.String()) pos_filter = fields.Object(PosFilter, default=PosFilter()) sub = fields.Boolean(default=False)
class CheckBinData(Check): object_type = 'bindata' node = fields.NodeID() key = fields.String() start = fields.SmallUnsignedInteger() end = fields.SmallUnsignedInteger(optional=True) data = fields.Binary()
class ChunkDataItemField(ChunkDataItem): pos_start = fields.Integer() pos_end = fields.Integer() name = fields.String() repack = fields.Object(Repacker) num_elements = fields.SmallUnsignedInteger() type = fields.Object(FieldType) raw_value = fields.BinData()
class MsgGetQuery(MsgpackMsg): object_type = 'get_query' qid = fields.SmallUnsignedInteger() node = fields.NodeID() query = fields.String() params = fields.Any(optional=True) trace = fields.Boolean(default=False) sub = fields.Boolean(default=False)
class MsgPluginBroadcastRegister(MsgpackMsg): """ Sent by the client to register a broadcast handler. Has no reply. ``phid`` is an arbitrary number chosen by the client to identify this broadcast handler. """ object_type = 'plugin_broadcast_register' phid = fields.SmallUnsignedInteger() name = fields.String()
class MsgCreate(MsgpackMsg): """ Creates a node on the server. It is an error if a node with given id already exists. Server replies with MsgRequestAck or MsgRequestError. Replies are matched to requests by ``rid``, which is assigned by the client. """ object_type = 'create' rid = fields.SmallUnsignedInteger() id = fields.NodeID() parent = fields.NodeID(default=NodeID.root_id) pos_start = fields.Integer(optional=True) pos_end = fields.Integer(optional=True) tags = fields.Set(fields.String()) attr = fields.Map(fields.String(), fields.Any()) data = fields.Map(fields.String(), fields.Any()) bindata = fields.Map(fields.String(), fields.Binary()) triggers = fields.Set(fields.String())
class MsgDelTag(MsgpackMsg): """ Removes a tag from a given node. Server replies with MsgRequestAck or MsgRequestError. If the node doesn't have the given tag, nothing happens. """ object_type = 'del_tag' rid = fields.SmallUnsignedInteger() id = fields.NodeID() tag = fields.String()
class MsgAddTag(MsgpackMsg): """ Adds a tag to a given node. Server replies with MsgRequestAck or MsgRequestError. If the node already has the given tag, nothing happens. """ object_type = 'add_tag' rid = fields.SmallUnsignedInteger() id = fields.NodeID() tag = fields.String()
class MsgBroadcastRun(MsgpackMsg): """ Sent by the client to request running a given broadcast. ``bid`` is an arbitrary number selected by the client to associate replies to this request. """ object_type = 'broadcast_run' bid = fields.SmallUnsignedInteger() broadcast = fields.String() params = fields.Any(optional=True)
class MsgMethodRun(MsgpackMsg): """ Sent by the client to request running a given method on a given object. ``mid`` is an arbitrary number selected by the client to associate replies to this request. """ object_type = 'method_run' mid = fields.SmallUnsignedInteger() node = fields.NodeID() method = fields.String() params = fields.Any(optional=True)
def test_string(self): a = fields.String() a.validate('abcd') with self.assertRaises(SchemaError): a.validate(b'abcd') with self.assertRaises(SchemaError): a.validate(1234) self.assertEqual(a.dump('abcd'), 'abcd') self.assertEqual(a.load('abcd'), 'abcd') with self.assertRaises(SchemaError): a.load(b'abcd') with self.assertRaises(SchemaError): a.load(1234)
class MsgGetData(MsgpackMsg): """ Sent by client, requests the data associated with the given node and key. Works like MsgGet, but replies with MsgGetData. It is not an error if a key doesn't exist - None will be returned in this case. """ object_type = 'get_data' qid = fields.SmallUnsignedInteger() id = fields.NodeID() key = fields.String() sub = fields.Boolean(default=False)
class MsgSetData(MsgpackMsg): """ Sets a data value on a given node. Server replies with MsgRequestAck or MsgRequestError. Setting a data value to None is equivalent to deleting it. """ object_type = 'set_data' rid = fields.SmallUnsignedInteger() id = fields.NodeID() key = fields.String() data = fields.Any(optional=True)
class MsgGetBinData(MsgpackMsg): """ Sent by client, requests a range of bindata associated with the given node and key. Works like MsgGet, but replies with MsgGetBinData. The range is left-inclusive and right-exclusive. It is not an error if the key doesn't exist - empty bytestring will be returned in this case. It is also not an error if the range is out of bounds for the bindata - it will be truncated if it's partially in range, or an empty bytestring will be returned if it's completely out of range. """ object_type = 'get_bindata' qid = fields.SmallUnsignedInteger() id = fields.NodeID() key = fields.String() start = fields.SmallUnsignedInteger() end = fields.SmallUnsignedInteger(optional=True) sub = fields.Boolean(default=False)
class MsgSetBinData(MsgpackMsg): """ Sets a range of bindata on a given node. Server replies with MsgRequestAck or MsgRequestError. The bindata is modified starting from a given start position - it is an error if the position is after the current end of bindata (but not if it's equal). The bindata is expanded if necessary to hold the new data. If truncate is set, the bindata is truncated after the end of the new data. If bindata would become 0-length, it is deleted. """ object_type = 'set_bindata' rid = fields.SmallUnsignedInteger() id = fields.NodeID() key = fields.String() start = fields.SmallUnsignedInteger(default=0) data = fields.Binary() truncate = fields.Boolean(default=False)
def test_map(self): a = fields.Map(fields.Object(Piwo), fields.Object(Zlew)) a.validate({}) a.validate({Piwo(): Zlew()}) with self.assertRaises(SchemaError): a.validate(Piwo()) with self.assertRaises(SchemaError): a.validate(Zlew()) with self.assertRaises(SchemaError): a.validate([]) with self.assertRaises(SchemaError): a.validate(None) with self.assertRaises(SchemaError): a.validate({Piwo(): Piwo()}) with self.assertRaises(SchemaError): a.validate({Zlew(): Zlew()}) with self.assertRaises(SchemaError): a.validate({Zlew(): Piwo()}) with self.assertRaises(SchemaError): a.validate({Piwo(): None}) with self.assertRaises(SchemaError): a.validate({None: Zlew()}) self.assertEqual(a.load({}), {}) self.assertEqual(a.load({'piwo': 'zlew'}), {Piwo(): Zlew()}) with self.assertRaises(SchemaError): a.validate(a.load([])) with self.assertRaises(SchemaError): a.validate(a.load(None)) with self.assertRaises(SchemaError): a.validate(a.load('piwo')) with self.assertRaises(SchemaError): a.validate(a.load({'piwo': 'piwo'})) with self.assertRaises(SchemaError): a.validate(a.load({'piwo', 'zlew'})) self.assertEqual(a.dump({}), {}) self.assertEqual(a.dump({Piwo(): Zlew()}), {'piwo': 'zlew'}) fields.Map(fields.Integer(), fields.String(), default={1: 'a'})
class CheckTrigger(Check): object_type = 'trigger' node = fields.NodeID() key = fields.String() state = fields.Enum(TriggerState, optional=True)
class OperationDelTrigger(Operation): object_type = 'del_trigger' trigger = fields.String()
class CheckBinDataSize(Check): object_type = 'bindata_size' node = fields.NodeID() key = fields.String() size = fields.SmallUnsignedInteger()
class CheckData(Check): object_type = 'data' node = fields.NodeID() key = fields.String() data = fields.Any(optional=True)
class CheckAttr(Check): object_type = 'attr' node = fields.NodeID() key = fields.String() data = fields.Any(optional=True)
class CheckTag(Check): object_type = 'tag' node = fields.NodeID() tag = fields.String() present = fields.Boolean()
class CheckTags(Check): object_type = 'tags' node = fields.NodeID() tags = fields.Set(fields.String())