PkgInfo: simplify 'linuxmint' with new-style wrap

Also, update copyright year.
This commit is contained in:
James Lu 2016-02-05 20:30:19 -08:00
parent 5861d5b15d
commit 826f55f5a8

View File

@ -1,5 +1,5 @@
### ###
# Copyright (c) 2014-2015, James Lu # Copyright (c) 2014-2016, James Lu
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -367,52 +367,60 @@ class PkgInfo(callbacks.Plugin):
pkgsearch = wrap(pkgsearch, ['somethingWithoutSpaces', pkgsearch = wrap(pkgsearch, ['somethingWithoutSpaces',
'somethingWithoutSpaces']) 'somethingWithoutSpaces'])
@wrap(['somethingWithoutSpaces',
'somethingWithoutSpaces',
getopts({'exact': ''})])
def linuxmint(self, irc, msg, args, release, query, opts): def linuxmint(self, irc, msg, args, release, query, opts):
"""<release> <package> [--exact] """<release> <package> [--exact]
Looks up <package> in Linux Mint's repositories. If --exact is given, Looks up <package> in Linux Mint's repositories. If --exact is given,
look up packages by the exact package name. Otherwise, look it up look up packages by the exact package name. Otherwise, look it up
as a simple glob pattern.""" as a simple glob pattern."""
addr = 'http://packages.linuxmint.com/list.php?release=' + \ addr = 'http://packages.linuxmint.com/list.php?release=' + \
quote(release) quote(release)
try: try:
fd = utils.web.getUrl(addr).decode("utf-8") fd = utils.web.getUrl(addr).decode("utf-8")
except utils.web.Error as e: except utils.web.Error as e:
irc.error(str(e), Raise=True) irc.error(str(e), Raise=True)
soup = BeautifulSoup(fd) soup = BeautifulSoup(fd)
# Linux Mint puts their package lists in tables # Linux Mint puts their package lists in tables
results = soup.find_all("td") results = soup.find_all("td")
found = OrderedDict()
packages = []
query = query.lower() query = query.lower()
exact = 'exact' in dict(opts) exact = 'exact' in dict(opts)
for result in results: for result in results:
name = result.contents[0].string # Package name name = result.contents[0].string # Package name
if query == name or (query in name and not exact): if query == name or (query in name and not exact):
# This feels like really messy code, but we have to find tags # This feels like really messy code, but we have to find tags
# relative to our results. # relative to our results.
# Ascend to find the section name (in <h2>): # Ascend to find the section name (in <h2>):
section = result.parent.parent.parent.previous_sibling.\ section = result.parent.parent.parent.previous_sibling.\
previous_sibling.string previous_sibling.string
# Find the package version in the next <td>; for some reason we # Find the package version in the next <td>; for some reason we
# have to go two siblings further, as the first .next_sibling # have to go two siblings further, as the first .next_sibling
# returns '\n'. This is mentioned briefly in Beautiful Soup 4's # returns '\n'. This is mentioned briefly in Beautiful Soup 4's
# documentation... # documentation...
version = result.next_sibling.next_sibling.string version = result.next_sibling.next_sibling.string
# We format our found dictionary this way because the same # We format our found dictionary this way because the same
# package can exist multiple times in different sections of # package can exist multiple times in different sections of
# the repository (e.g. one in Main, one in Backports, etc.) # the repository (e.g. one in Main, one in Backports, etc.)
found['%s [\x02%s\x02]' % (name, section)] = version packages.append('%s \x02(%s)\x02 [\x02%s\x02]' % (name, version, section))
if found:
items = [format('%s \x02(%s)\x02', pkg, found[pkg]) for pkg in if packages: # If we have results
found] s = format('Found %n: %L, %s %u', (len(packages), 'result'), packages,
s = format('Found %n: %L, %s %u', (len(found), 'result'), items,
_('View more at: '), addr) _('View more at: '), addr)
irc.reply(s) irc.reply(s)
else: else:
irc.error('No results found.') irc.error('No results found.')
linuxmint = wrap(linuxmint, ['somethingWithoutSpaces',
'somethingWithoutSpaces',
getopts({'exact': ''})])
@wrap(['positiveInt', 'somethingWithoutSpaces']) @wrap(['positiveInt', 'somethingWithoutSpaces'])
def fedora(self, irc, msg, args, release, query): def fedora(self, irc, msg, args, release, query):