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