summaryrefslogtreecommitdiff
path: root/sample-scripts/relchk.py
diff options
context:
space:
mode:
Diffstat (limited to 'sample-scripts/relchk.py')
-rwxr-xr-xsample-scripts/relchk.py52
1 files changed, 49 insertions, 3 deletions
diff --git a/sample-scripts/relchk.py b/sample-scripts/relchk.py
index 2a337d3..19e6424 100755
--- a/sample-scripts/relchk.py
+++ b/sample-scripts/relchk.py
@@ -1,25 +1,71 @@
#!/usr/bin/env python3
+from collections import OrderedDict
from urllib.request import urlopen
+from humanize import naturalsize # not stdlib
try:
from lxml import etree
has_lxml = True
except ImportError:
import xml.etree.ElementTree as etree
has_lxml = False
+ from io import StringIO
# Normally this'd be passed via calling the class, but this is mostly for
# demonstration purposes only so we're not too worried about it.
-feed_url = 'https://git.square-r00t.net/RelSpec/tree/samplefeed.xml'
+feed_url = 'https://git.square-r00t.net/RelSpec/plain/samplefeed.xml'
class Release(object):
def __init__(self, feed):
self.xml = etree.fromstring(feed)
+ # The namespace map.
+ # I pine for the day when the xml module is smart enough to get this from the
+ # XML declaration itself.
+ # Seriously, Python should just adopt lxml as the core XML module.
+ # AAAANNYYYWAYS, this ugly (but working!) hack is due to much thanks to:
+ # https://stackoverflow.com/a/37409050
+ if not has_lxml:
+ self.nsmap = dict(
+ [node for _, node in etree.iterparse(StringIO(feed.decode('utf-8')),
+ events = ['start-ns'])])
+ else:
+ self.nsmap = self.xml.nsmap
+ self.channels = [i for i in self.xml.findall('channel')]
- def getChannels(self):
- self.channels = []
+ def Releases(self, channel):
+ releases = OrderedDict()
+ # Returns an ordered dict (from newest to oldest) of releases,
+ # containing all specs of the item ("release") component.
+ for c in channel.findall('item'):
+ # This just saves some typing.
+ _title = c.find('title').text
+ _ver = c.find('relspec:ver', self.nsmap).text
+ _releaseDate = c.find('pubDate')
+ _description = c.find('description').text
+ _download = c.find('enclosure').attrib['url']
+ _size = naturalsize(int(c.find('enclosure').attrib['length']))
+ _mime = c.find('enclosure').attrib['type']
+ _sha512 = c.find('guid').text
+ _sig = c.find('relspec:sig', self.nsmap).text
+ _notes = c.find('link')
+ _bugs = c.find('comments')
+ releases[_ver] = {'title': _title,
+ 'released': _releaseDate,
+ 'desc': _description,
+ 'donwload': _download,
+ 'size': _size,
+ 'mime-type': _mime,
+ 'SHA512': _sha512,
+ 'signature': _sig,
+ 'release-notes': _notes,
+ 'bug-reports': _bugs}
+ return(releases)
if __name__ == '__main__':
with urlopen(feed_url) as http:
feed = Release(http.read())
+ for c in feed.channels:
+ print('\nChannel: {0}'.format(c.find('title').text))
+ for r in feed.Releases(c):
+ print(r)