gnip / gnip-python

Python library for utilizing Gnip services.

gnip-python / gnip / publisher.py
100644 61 lines (44 sloc) 1.858 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from xml.dom.minidom import parseString
 
class Publisher(object):
    """Gnip Publisher container class
 
This class provides an abstraction from the Gnip Publisher XML.
 
"""
    
    def __init__(self, name=None, rule_types=None):
        """Initialize the class.
 
@type name string
@param name the name of the publisher
@type rule_types list of strings
@param rule_types The rule types this publisher supports
 
Initializes the class with the proper variables.
 
"""
        
        self.name = name
        self.rule_types=rule_types
    
    def to_xml(self):
        """ Return a XML representation of the Publisher as a string. """
        xml = '<publisher name="' + self.name + '">'
        xml += '<supportedRuleTypes>'
        
        for rule_type in self.rule_types:
            xml += '<type>' + rule_type + '</type>'
        
        xml += '</supportedRuleTypes></publisher>'
        
        return xml
    
    def from_xml(self, xml):
        """ Populates the Publisher object based on Publsiher XML
@type xml string
@param xml the Publisher XML
"""
        root = parseString(xml).documentElement
        self.name = root.getAttribute("name")
        
        self.rule_types = []
        for node in root.childNodes:
            if node.tagName == 'supportedRuleTypes':
                for subnode in node.childNodes:
                    if subnode.tagName == 'type':
                        self.rule_types.append(subnode.childNodes[0].nodeValue)
 
    def __cmp__(self, other):
        if isinstance(other, Publisher):
            ret = cmp(self.name, other.name)
            if ret is 0:
                ret = cmp(sorted(self.rule_types), sorted(other.rule_types))
        else:
            ret = 1
        return ret