source: wokkel/formats.py

Last change on this file was 213:7b89b9208b88, checked in by Ralph Meijer <ralphm@…>, 6 years ago

Add tests for formats, fix py3 incompatibility.

  • Property exe set to *
File size: 3.6 KB
RevLine 
[213]1# -*- test-case-name: wokkel.test.test_formats -*-
2#
[96]3# Copyright (c) Ralph Meijer.
[12]4# See LICENSE for details.
5
[213]6"""
7Generic payload formats.
8"""
9
[209]10from __future__ import division, absolute_import
11
[213]12from twisted.python.compat import unicode
13
[12]14NS_MOOD = 'http://jabber.org/protocol/mood'
15NS_TUNE = 'http://jabber.org/protocol/tune'
16
17class Mood:
18    """
19    User mood.
20
21    This represents a user's mood, as defined in
[165]22    U{XEP-0107<http://xmpp.org/extensions/xep-0107.html>}.
[12]23
24    @ivar value: The mood value.
25    @ivar text: The optional natural-language description of, or reason
26                for the mood.
27    """
28
29    def __init__(self, value, text=None):
30        self.value = value
31        self.text = text
32
[213]33
[12]34    def fromXml(self, element):
35        """
36        Get a Mood instance from an XML representation.
37
38        This class method parses the given XML document into a L{Mood}
39        instances.
40
41        @param element: The XML mood document.
42        @type element: object providing
43                       L{IElement<twisted.words.xish.domish.IElement>}
44        @return: A L{Mood} instance or C{None} if C{element} was not a mood
45                 document or if there was no mood value element.
46        """
47        if element.uri != NS_MOOD or element.name != 'mood':
48            return None
49
50        value = None
51        text = None
52
53        for child in element.elements():
54            if child.uri != NS_MOOD:
55                continue
56
57            if child.name == 'text':
58                text = unicode(child)
59            else:
60                value = child.name
61
62        if value:
63            return Mood(value, text)
64        else:
65            return None
66
67    fromXml = classmethod(fromXml)
68
[213]69
70
[12]71class Tune:
72    """
73    User tune.
74
75    This represents a user's mood, as defined in
[165]76    U{XEP-0118<http://xmpp.org/extensions/xep-0118.html>}.
[12]77
78    @ivar artist: The artist or performer of the song or piece.
79    @type artist: C{unicode}
80    @ivar length: The duration of the song or piece in seconds.
81    @type length: C{int}
82    @ivar source: The collection (e.g. album) or other source.
83    @type source: C{unicode}
84    @ivar title: The title of the song or piece
85    @type title: C{unicode}
86    @ivar track: A unique identifier for the tune; e.g. the track number within
87                 the collection or the specific URI for the object.
88    @type track: C{unicode}
89    @ivar uri: A URI pointing to information about the song, collection, or
90               artist.
91    @type uri: C{str}
92
93    """
94
95    artist = None
96    length = None
97    source = None
98    title = None
99    track = None
100    uri = None
101
102    def fromXml(self, element):
103        """
104        Get a Tune instance from an XML representation.
105
106        This class method parses the given XML document into a L{Tune}
107        instances.
108
109        @param element: The XML tune document.
110        @type element: object providing
111                       L{IElement<twisted.words.xish.domish.IElement>}
112        @return: A L{Tune} instance or C{None} if C{element} was not a tune
113                 document.
114        """
115        if element.uri != NS_TUNE or element.name != 'tune':
116            return None
117
118        tune = Tune()
119
120        for child in element.elements():
121            if child.uri != NS_TUNE:
122                continue
123
124            if child.name in ('artist', 'source', 'title', 'track', 'uri'):
125                setattr(tune, child.name, unicode(child))
126            elif child.name == 'length':
127                try:
128                    tune.length = int(unicode(child))
129                except ValueError:
130                    pass
131
132        return tune
133
134    fromXml = classmethod(fromXml)
Note: See TracBrowser for help on using the repository browser.