source: wokkel/formats.py @ 209:1f9f603586ad

Last change on this file since 209:1f9f603586ad was 209:1f9f603586ad, checked in by Ralph Meijer <ralphm@…>, 6 years ago

imported patch py3-imports.patch

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