harvest.py

This commit is contained in:
Dimitri John Ledkov 2014-12-16 02:48:52 +00:00
parent a7dedd9296
commit 65ab539516

View File

@ -14,7 +14,12 @@
import json import json
import os.path import os.path
import sys import sys
import urllib2 try:
from urllib.request import urlopen
from urllib.error import URLError
except ImportError:
from urllib2 import urlopen
from urllib2 import URLError
from ubuntutools.logger import Logger from ubuntutools.logger import Logger
@ -32,11 +37,11 @@ class Harvest(object):
def _get_data(self): def _get_data(self):
try: try:
sock = urllib2.urlopen(self.data_url) sock = urlopen(self.data_url)
except IOError: except IOError:
try: try:
urllib2.urlopen(BASE_URL) urlopen(BASE_URL)
except urllib2.URLError: except URLError:
Logger.error("Harvest is down.") Logger.error("Harvest is down.")
sys.exit(1) sys.exit(1)
return None return None
@ -45,9 +50,7 @@ class Harvest(object):
return json.loads(response) return json.loads(response)
def opportunity_summary(self): def opportunity_summary(self):
l = [] l = ["%s (%s)" % (k,v) for (k,v) in self.data.items() if k != "total"]
for key in filter(lambda a: a != "total", self.data.keys()):
l += ["%s (%s)" % (key, self.data[key])]
return ", ".join(l) return ", ".join(l)
def report(self): def report(self):