Debian Codegraph @

tx_xmpp/formats.py

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