File: test.py

package info (click to toggle)
python-pyrss2gen 1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 92 kB
  • sloc: python: 478; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 5,504 bytes parent folder | download | duplicates (4)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# unittest?  What's that?
#
# You'll need Mark Pilgrim's feed_parser, from
#  http://diveintomark.org/projects/feed_parser/
# The test takes a structure, converts it to XML,
# reads it back using feed_parser, and compares
# the two stuctures.  feed_parser doesn't handle
# everything, so I needed to filter some items.
# I also haven't tested everything.

import datetime
import PyRSS2Gen
import feedparser

rss = PyRSS2Gen.RSS2(
    "This is a test",
    "http://www.dalkescientific.com/",
    "To be or not to be.  That is the question.",

    language = "en-US",
    copyright = "Copyright (c) 2003, by me",
    managingEditor = "here@there (everywhere)",
    webMaster = "Spider Man",
    pubDate = datetime.datetime(2000, 11, 20, 23, 45, 19),
    lastBuildDate = datetime.datetime(2001, 12, 25, 22, 51, 49),
    categories = ["live", "love", "larf", "loaf"],

    cloud = PyRSS2Gen.Cloud("rpc.sys.com", 80, "/RPC2", "pingMe", "soap"),
                       
    ttl = 10,
    image = PyRSS2Gen.Image("http://there/", "THERE!", "link?",
                            25, 94, "some description"),
    rating = "For all audiences",

    textInput = PyRSS2Gen.TextInput("Qwerty", "Shrdlu", "Etaoin",
                                    "http://some/link"),
    skipDays = PyRSS2Gen.SkipDays(["Monday", "Thursday"]),
    skipHours = PyRSS2Gen.SkipHours([0, 5, 22]),

    items = [PyRSS2Gen.RSSItem(
                "Chapter 1", "http://xe.com/",
                "How to convert money.",
                author = "x@xe",
                # categories
                comments = "http://slashdot.org",
                # enclosure
                guid = "http://asdfghjk/",
                pubDate = datetime.datetime(1999, 1, 30),
                source = PyRSS2Gen.Source("ftp://x.y/",
                                          "Quotes of the Day"),
                          
                ),
             PyRSS2Gen.RSSItem("Chapter 2", "http://acm.org/",
                               "AT&T is <Ma Bell>.",
                               guid = PyRSS2Gen.Guid(guid = "12345", isPermaLink = False),
                               ),
             ])

def _convert_to_liberal(obj):
    if isinstance(obj, basestring):
        return obj
    elif isinstance(obj, int):
        return str(obj)
    elif isinstance(obj, datetime.datetime):
        return PyRSS2Gen._format_date(obj)
    else:
        d = {}
        for k, v in obj.__dict__.items():
            if v is None:
                continue
            if k == "element_attrs":
                d.update(v)
            elif k in ("categories", "days", "hours", "author",
                       "comments", "source"):
                # feedparser doesn't handle these
                continue
            elif k == "guid" and not isinstance(v, str):
                d[k] = v.guid
            else:
                if k == "pubDate":
                    k = "date"
                d[k] = _convert_to_liberal(v)
        return d
        

def to_liberal(rss):
    d = {"encoding": "iso-8859-1"}  # a bit of a hack
    channel = d["channel"] = {}
    items = rss.__dict__.items() # enforce an (arbitrary) order
    items.sort()                 # (feedparser result depends on the
    for k, v in items:           # order of the elements.)
        if v is None:
            continue
        if k in ("categories", "docs", "generator", "cloud", "ttl",
                 "image", "rating", "textInput", "skipDays", "skipHours"):
            # feedparser doesn't handle these
            pass
        elif k != "items":
            # Why the changes?
            if k == "copyright":
                k = "rights"
            elif k == "lastBuildDate":
                k = "date"
            elif k in ("webMaster", "managingEditor"):
                k = "creator"   # order dependent!
            elif k in ("pubDate", "lastBuildDate"):
                if "date" in channel:
                    # lastBuildDate has priority
                    if k == "pubDate":
                        continue
                k = "date"  # also order dependent
            channel[k] = _convert_to_liberal(v)
        
    items = [_convert_to_liberal(item) for item in rss.items]
    d["items"] = items

    return d

s = rss.to_xml()
from PyRSS2Gen import StringIO
f = StringIO(s)
result = feedparser.parse(f)
##print result
##print "=========="
##print to_liberal(rss)

result2 = to_liberal(rss)
assert result == result2

execfile("example.py")

# Check a few things new to 1.0

def EQ(x, y):
    if not (x == y):
        raise AssertionError( (x, y) )

class RecordingHandler:
    def __init__(self):
        self.events = []
    def startElement(self, tag, d):
        self.events.append( ("SE", tag, d) )
    def characters(self, text):
        self.events.append( ("C", text) )
    def endElement(self, tag):
        self.events.append( ("EE", tag) )

def publish_it(obj):
    h = RecordingHandler()
    obj.publish(h)
    return h.events

obj = PyRSS2Gen.Enclosure("http://example.com", 5, "text/plain")
EQ(publish_it(obj), [("SE", "enclosure", {"url": "http://example.com",
                                          "length": "5",
                                          "type": "text/plain"}),
                     ("EE", "enclosure"),
                     ])

obj = PyRSS2Gen.Guid("ABCDEF", False)
EQ(publish_it(obj), [("SE", "guid", {"isPermaLink": "false"}),
                     ("C", "ABCDEF"),
                     ("EE", "guid"),
                     ])