Exemplo n.º 1
0
class TestDoc(DocumentElement):

    __tag__ = 'test'
    attr_attr = Attribute('attr')
    attr_decoder = Attribute('attr-decoder', decoder=lambda s: s.lower())
    attr_decoder_decorator = Attribute('attr-decoder-decorator')
    title_attr = Child('title', TextElement, required=True)
    content_attr = Child('content', TextElement, required=True)
    multi_attr = Child('multi', TextElement, multiple=True)
    sorted_children = Child('s-multi', TextElement,
                            multiple=True, sort_key=lambda e: e.value)
    text_content_attr = Text('text-content')
    text_multi_attr = Text('text-multi', multiple=True)
    sorted_texts = Text('s-text-multi',
                        multiple=True, sort_key=lambda t: t, sort_reverse=True)
    text_decoder = Text('text-decoder', decoder=float, encoder=str)
    text_decoder_decorator = Text('text-decoder-decorator')
    text_combined_decoder = Text('text-combined-decoder',
                                 decoder=int, encoder=lambda i: i and i / 100)
    ns_element_attr = Child('ns-element', TextElement,
                            xmlns='http://earthreader.github.io/')
    ns_text_attr = Text('ns-text', xmlns='http://earthreader.github.io/')
    content_decoder = Child('content-decoder', TextDecoderElement)

    @attr_decoder_decorator.decoder
    def attr_decoder_decorator(self, value):
        return value[::-1]

    @attr_decoder_decorator.encoder
    def attr_decoder_decorator(self, value):
        return value and value[::-1]

    @text_decoder_decorator.decoder
    def text_decoder_decorator(self, text):
        return int(text[::-1])

    @text_decoder_decorator.encoder
    def text_decoder_decorator(self, value):
        if value is not None:
            return str(value)[::-1]

    @text_combined_decoder.decoder
    def text_combined_decoder(self, value):
        return value * 100

    @text_combined_decoder.decoder
    def text_combined_decoder(self, value):
        return -value

    @text_combined_decoder.encoder
    def text_combined_decoder(self, value):
        if value is not None:
            return -value

    @text_combined_decoder.encoder
    def text_combined_decoder(self, value):
        if value is not None:
            if value % 1 <= 0:
                value = int(value)
            return str(value)
Exemplo n.º 2
0
class DefaultAttrTestDoc(DocumentElement):

    __tag__ = 'default-attr-test'
    attr = Attribute('attr', Integer)
    default_attr = Attribute('default-attr',
                             IntPair,
                             default=lambda e: (e.attr, e.attr * 2)
                             if e.attr else (0, 0))
Exemplo n.º 3
0
class VTElement(Element):

    req_attr = Attribute('a', required=True)
    attr = Attribute('b')
    req_child = Child('c', TextElement, required=True)
    child = Child('d', TextElement)

    def __repr__(self):
        return 'VTElement(req_attr={0!r}, req_child={1!r})'.format(
            self.req_attr, self.req_child)
Exemplo n.º 4
0
class TestRevisedEntity(Element):

    ident = Text('ident', required=True)
    rev = Attribute('rev', Integer)
    value = Attribute('value')

    def __entity_id__(self):
        return self.ident

    def __merge_entities__(self, other):
        return max(self, other, key=operator.attrgetter('rev'))
Exemplo n.º 5
0
class VTElement(Element):

    req_attr = Attribute('a', required=True)
    attr = Attribute('b')
    req_child = Child('c', TextElement, required=True)
    child = Child('d', TextElement)
    req_text = Text('e', required=True)
    text = Text('f')

    def __repr__(self):
        fmt = 'VTElement(req_attr={0!r}, req_child={1!r}, req_text={2!r})'
        return fmt.format(self.req_attr, self.req_child, self.req_text)
Exemplo n.º 6
0
class VTDoc(DocumentElement):

    __tag__ = 'vtest'
    req_attr = Attribute('a', required=True)
    attr = Attribute('b')
    req_child = Child('c', VTElement, required=True)
    child = Child('d', VTElement)
    multi = Child('e', VTElement, multiple=True)

    def __repr__(self):
        return 'VTDoc(req_attr={0!r}, req_child={1!r})'.format(
            self.req_attr, self.req_child)
Exemplo n.º 7
0
class VTDoc(DocumentElement):

    __tag__ = 'vtest'
    req_attr = Attribute('a', required=True)
    attr = Attribute('b')
    req_child = Child('c', VTElement, required=True)
    child = Child('d', VTElement)
    multi = Child('e', VTElement, multiple=True)
    req_text = Text('f', required=True)
    text = Text('g')

    def __repr__(self):
        fmt = 'VTDoc(req_attr={0!r}, req_child={1!r}, req_text={2!r})'
        return fmt.format(self.req_attr, self.req_child, self.req_text)
Exemplo n.º 8
0
class TestUniqueEntity(Element):

    ident = Text('ident', required=True)
    value = Attribute('value')

    def __entity_id__(self):
        return self.ident
Exemplo n.º 9
0
class TextElement(Element):

    ns_attr_attr = Attribute('ns-attr', xmlns='http://earthreader.github.io/')
    value = Content()

    @classmethod
    def __coerce_from__(cls, value):
        if isinstance(value, string_type):
            return TextElement(value=value)
        raise TypeError
Exemplo n.º 10
0
class TestMergeableDoc(MergeableDocumentElement):

    __tag__ = 'merge-test'
    multi_text = Text('multi-text', multiple=True)
    text = Text('text')
    attr = Attribute('attr')
    unique_entities = Child('unique-entity', TestUniqueEntity, multiple=True)
    rev_entities = Child('rev-multi-entity', TestRevisedEntity, multiple=True)
    rev_entity = Child('rev-single-entity', TestRevisedEntity)
    nullable = Child('nullable-entity', TestUniqueEntity)
Exemplo n.º 11
0
class CodecTestDoc(DocumentElement):

    __tag__ = 'codec-test'
    attr = Attribute('attr', IntPair)
    text = Text('text', IntPair)
Exemplo n.º 12
0
class EncodeErrorDoc(DocumentElement):

    __tag__ = 'encode-error-test'
    attr = Attribute('attr', encoder=lambda s: s and 123)
    text = Text('text', encoder=lambda s: s and object())
Exemplo n.º 13
0
class AttrDescriptorConflictElement(Element):

    attr = Attribute('same-attr')
    attr2 = Attribute('same-attr')
Exemplo n.º 14
0
 class AdhocElement(Element):
     format_version = Attribute('version')
     name = Text('name-e')
     url = Child('url-e', AdhocTextElement, multiple=True)
     dob = Child('dob-e', AdhocTextElement, xmlns='http://example.com/')