Better format Geo and GeoEntry. Add test for Geo Extension
This commit is contained in:
parent
c0a1acc18c
commit
ae5759d133
3 changed files with 72 additions and 12 deletions
|
@ -1,15 +1,20 @@
|
||||||
from lxml import etree
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
feedgen.ext.geo
|
||||||
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Extends the FeedGenerator to produce Simple GeoRSS feeds.
|
||||||
|
|
||||||
|
:copyright: 2017, Bob Breznak <bob.breznak@gmail.com>
|
||||||
|
|
||||||
|
:license: FreeBSD and LGPL, see license.* for more details.
|
||||||
|
'''
|
||||||
|
|
||||||
from feedgen.ext.base import BaseExtension
|
from feedgen.ext.base import BaseExtension
|
||||||
|
|
||||||
class GeoExtension(BaseExtension):
|
class GeoExtension(BaseExtension):
|
||||||
def __init__(self):
|
'''FeedGenerator extension for Simple GeoRSS.
|
||||||
self.__point__ = None
|
'''
|
||||||
|
|
||||||
def extend_ns(self):
|
def extend_ns(self):
|
||||||
return { 'georss' : 'http://www.georss.org/georss' }
|
return { 'georss' : 'http://www.georss.org/georss' }
|
||||||
|
|
||||||
def extend_rss(self, rss_feed):
|
|
||||||
return rss_feed
|
|
||||||
|
|
||||||
def extend_atom(self, atom_feed):
|
|
||||||
return atom_feed
|
|
||||||
|
|
|
@ -1,11 +1,32 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
feedgen.ext.geo_entry
|
||||||
|
~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Extends the FeedGenerator to produce Simple GeoRSS feeds.
|
||||||
|
|
||||||
|
:copyright: 2017, Bob Breznak <bob.breznak@gmail.com>
|
||||||
|
|
||||||
|
:license: FreeBSD and LGPL, see license.* for more details.
|
||||||
|
'''
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from feedgen.ext.base import BaseEntryExtension
|
from feedgen.ext.base import BaseEntryExtension
|
||||||
|
|
||||||
class GeoEntryExtension(BaseEntryExtension):
|
class GeoEntryExtension(BaseEntryExtension):
|
||||||
|
'''FeedEntry extension for Simple GeoRSS.
|
||||||
|
'''
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# Simple GeoRSS tag
|
||||||
self.__point = None
|
self.__point = None
|
||||||
|
|
||||||
def extend_rss(self, entry):
|
def extend_file(self, entry):
|
||||||
|
'''Add additional fields to an RSS item.
|
||||||
|
|
||||||
|
:param feed: The RSS item XML element to use.
|
||||||
|
'''
|
||||||
|
|
||||||
GEO_NS = 'http://www.georss.org/georss'
|
GEO_NS = 'http://www.georss.org/georss'
|
||||||
|
|
||||||
if self.__point:
|
if self.__point:
|
||||||
|
@ -14,9 +35,20 @@ class GeoEntryExtension(BaseEntryExtension):
|
||||||
|
|
||||||
return entry
|
return entry
|
||||||
|
|
||||||
|
def extend_rss(self, entry):
|
||||||
|
return self.extend_file(entry)
|
||||||
|
|
||||||
def extend_atom(self, entry):
|
def extend_atom(self, entry):
|
||||||
return self.extend_rss(self, entry)
|
return self.extend_file(entry)
|
||||||
|
|
||||||
def point(self, point=None):
|
def point(self, point=None):
|
||||||
self.__point = point or '0.0 0.0'
|
'''Get or set the georss:point of the entry.
|
||||||
|
|
||||||
|
:param point: The GeoRSS formatted point (i.e. "42.36 -71.05")
|
||||||
|
:returns: The author of the podcast.
|
||||||
|
'''
|
||||||
|
|
||||||
|
if point is not None:
|
||||||
|
self.__point = point
|
||||||
|
|
||||||
return self.__point
|
return self.__point
|
||||||
|
|
|
@ -134,6 +134,29 @@ class TestExtensionPodcast(unittest.TestCase):
|
||||||
namespaces=ns)
|
namespaces=ns)
|
||||||
assert author == ['Lars Kiesow']
|
assert author == ['Lars Kiesow']
|
||||||
|
|
||||||
|
class TestExtensionGeo(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.fg = FeedGenerator()
|
||||||
|
self.fg.load_extension('geo')
|
||||||
|
self.fg.title('title')
|
||||||
|
self.fg.link(href='http://example.com', rel='self')
|
||||||
|
self.fg.description('description')
|
||||||
|
|
||||||
|
def test_geoEntryItems(self):
|
||||||
|
fe = self.fg.add_item()
|
||||||
|
fe.title('y')
|
||||||
|
fe.geo.point('42.36 -71.05')
|
||||||
|
|
||||||
|
assert fe.geo.point() == '42.36 -71.05'
|
||||||
|
|
||||||
|
# Check that we have the item in the resulting XML
|
||||||
|
ns = {'georss': 'http://www.georss.org/georss'}
|
||||||
|
root = etree.fromstring(self.fg.rss_str())
|
||||||
|
point = root.xpath('/rss/channel/item/georss:point/text()',
|
||||||
|
namespaces=ns)
|
||||||
|
assert point == ['42.36 -71.05']
|
||||||
|
|
||||||
|
|
||||||
class TestExtensionDc(unittest.TestCase):
|
class TestExtensionDc(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue