Esempio n. 1
0
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))
Esempio n. 2
0
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()
Esempio n. 3
0
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())
Esempio n. 4
0
class MsgSetPos(MsgpackMsg):
    """
    Changes the position of a given node.  Server replies with MsgRequestAck
    or MsgRequestError.
    """

    object_type = 'set_pos'

    rid = fields.SmallUnsignedInteger()
    id = fields.NodeID()
    pos_start = fields.Integer(optional=True)
    pos_end = fields.Integer(optional=True)
Esempio n. 5
0
 def test_set(self):
     a = fields.Set(fields.Object(Piwo))
     a.validate(set())
     a.validate({Piwo()})
     with self.assertRaises(SchemaError):
         a.validate(Piwo())
     with self.assertRaises(SchemaError):
         a.validate([])
     with self.assertRaises(SchemaError):
         a.validate(None)
     with self.assertRaises(SchemaError):
         a.validate({Piwo(), Zlew()})
     with self.assertRaises(SchemaError):
         a.validate({Piwo(), None})
     self.assertEqual(a.load([]), set())
     self.assertEqual(a.load(['piwo']), {Piwo()})
     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(['zlew']))
     with self.assertRaises(SchemaError):
         a.validate(a.load(['piwo', None]))
     self.assertEqual(a.dump({}), [])
     self.assertEqual(a.dump({Piwo()}), ['piwo'])
     fields.Set(fields.Integer(), default={1, 2, 3})
Esempio n. 6
0
 def test_list(self):
     a = fields.List(fields.Object(Piwo))
     a.validate([])
     a.validate([Piwo()])
     a.validate([Piwo(), Piwo(), Piwo()])
     with self.assertRaises(SchemaError):
         a.validate(Piwo())
     with self.assertRaises(SchemaError):
         a.validate(set())
     with self.assertRaises(SchemaError):
         a.validate(None)
     with self.assertRaises(SchemaError):
         a.validate([Piwo(), Zlew(), Piwo()])
     with self.assertRaises(SchemaError):
         a.validate([Piwo(), None])
     self.assertEqual(a.load([]), [])
     self.assertEqual(a.load(['piwo', 'piwo']), [Piwo(), Piwo()])
     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(['zlew']))
     with self.assertRaises(SchemaError):
         a.validate(a.load(['piwo', None]))
     self.assertEqual(a.dump([]), [])
     self.assertEqual(a.dump([Piwo(), Piwo()]), ['piwo', 'piwo'])
     fields.List(fields.Integer(), default=[1, 2, 3])
Esempio n. 7
0
class PosFilter(model.Model):
    """
    A position range filter for use in list queries to the database.

    Ranges can be independently specified for start and end.  This allows
    for searching for chunks that start/end in given ranges, as well
    as searching for chunks that overlap a given range of positions.

    *_from and *_to are both *inclusive*.

    If *_from is None, it is treated as -inf.  If *_to is None, it is
    treated as +inf.  Also, for purposes of this filter, a pos_start
    of None is treated as -inf, and pos_end of None is treated as +inf.
    """

    start_from = fields.Integer(optional=True)
    start_to = fields.Integer(optional=True)
    end_from = fields.Integer(optional=True)
    end_to = fields.Integer(optional=True)

    @classmethod
    def intersecting_with(cls, pos_from, pos_to):
        """
        Constructs a filter that returns chunks overlapping with a given
        range of positions (left-inclusive, right-exclusive, like pos_start
        and pos_end).
        """
        return cls(
            start_to=pos_to-1,
            end_from=pos_from+1,
        )

    def matches(self, node):
        if self.start_from is not None:
            if node.pos_start is None or node.pos_start < self.start_from:
                return False
        if self.start_to is not None:
            if node.pos_start is not None and node.pos_start > self.start_to:
                return False
        if self.end_from is not None:
            if node.pos_end is not None and node.pos_end < self.end_from:
                return False
        if self.end_to is not None:
            if node.pos_end is None or node.pos_end > self.end_to:
                return False
        return True
Esempio n. 8
0
    def test_integer(self):
        a = fields.Integer()
        a.validate(0)
        a.validate(1)
        a.validate(-1)
        a.validate(0x123456789abcdef123456789abcdef)
        a.validate(-0x123456789abcdef123456789abcdef)
        with self.assertRaises(SchemaError):
            a.validate(False)
        with self.assertRaises(SchemaError):
            a.validate(True)

        a = fields.Integer(minimum=-123, maximum=456)
        a.validate(-123)
        a.validate(123)
        a.validate(234)
        a.validate(456)
        with self.assertRaises(SchemaError):
            a.validate(-0x123456789abcdef123456789abcdef)
        with self.assertRaises(SchemaError):
            a.validate(-124)
        with self.assertRaises(SchemaError):
            a.validate(457)
        with self.assertRaises(SchemaError):
            a.validate(0x123456789abcdef123456789abcdef)

        a = fields.Integer(minimum=123)
        a.validate(123)
        a.validate(234)
        a.validate(456)
        a.validate(0x123456789abcdef123456789abcdef)
        with self.assertRaises(SchemaError):
            a.validate(0)
        with self.assertRaises(SchemaError):
            a.validate(-123)
        with self.assertRaises(SchemaError):
            a.validate(122)

        with self.assertRaises(TypeError):
            fields.Integer(minimum='zlew')
        with self.assertRaises(TypeError):
            fields.Integer(maximum='zlew')
        fields.Integer(minimum=3, maximum=3)
        fields.Integer(minimum=3)
        fields.Integer(maximum=3)
        with self.assertRaises(ValueError):
            fields.Integer(minimum=3, maximum=2)
Esempio n. 9
0
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())
Esempio n. 10
0
 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'})
Esempio n. 11
0
class OperationSetPos(Operation):
    object_type = 'set_pos'

    pos_start = fields.Integer(optional=True)
    pos_end = fields.Integer(optional=True)
Esempio n. 12
0
class IntegerOptional(model.Model):
    a = fields.Integer(optional=True)
Esempio n. 13
0
class Integer(model.Model):
    a = fields.Integer(default=-42)
Esempio n. 14
0
class WieloZlew(BaseZlew):
    przeplyw = fields.Integer(default=13)
Esempio n. 15
0
class TurboZlew(Zlew):
    c = fields.Set(fields.Binary())
    d = fields.Object(Piwo)
    e = fields.Integer(default=3)
Esempio n. 16
0
class ChunkDataItemPad(ChunkDataItem):
    pos_start = fields.Integer()
    pos_end = fields.Integer()
Esempio n. 17
0
class CheckPos(Check):
    object_type = 'pos'

    node = fields.NodeID()
    pos_start = fields.Integer(optional=True)
    pos_end = fields.Integer(optional=True)
Esempio n. 18
0
class ChunkDataItemSubchunk(ChunkDataItem):
    name = fields.String()
    pos_start = fields.Integer()
    pos_end = fields.Integer()
    ref = fields.NodeID()