diff --git a/hackertray/__init__.py b/hackertray/__init__.py index 5045809..d7facff 100644 --- a/hackertray/__init__.py +++ b/hackertray/__init__.py @@ -23,20 +23,14 @@ import argparse from os.path import expanduser import signal -##This is to get --version to work -try: - import pkg_resources - - __version = pkg_resources.require("hackertray")[0].version -except: - __version = "Can't read version number." - from hackernews import HackerNews from chrome import Chrome +from version import Version class HackerNewsApp: HN_URL_PREFIX = "https://news.ycombinator.com/item?id=" - + UPDATE_URL = "https://github.com/captn3m0/hackertray#upgrade" + ABOUT_URL = "https://github.com/captn3m0/hackertray" def __init__(self, args): #Load the database home = expanduser("~") @@ -81,6 +75,12 @@ class HackerNewsApp: btnRefresh.connect("activate", self.refresh, True, args.chrome) self.menu.append(btnRefresh) + if Version.new_available(): + btnUpdate = gtk.MenuItem("New Update Available") + btnUpdate.show() + btnUpdate.connect('activate',self.showUpdate) + self.menu.append(btnUpdate) + btnQuit = gtk.MenuItem("Quit") btnQuit.show() btnQuit.connect("activate", self.quit) @@ -95,9 +95,16 @@ class HackerNewsApp: """Whether comments page is opened or not""" self.commentState = not self.commentState + def showUpdate(self,widget): + """Handle the update button""" + webbrowser.open(HackerNewsApp.UPDATE_URL) + # Remove the update button once clicked + self.menu.remove(widget) + + def showAbout(self, widget): """Handle the about btn""" - webbrowser.open("https://github.com/captn3m0/hackertray/") + webbrowser.open(HackerNewsApp.ABOUT_URL) #ToDo: Handle keyboard interrupt properly def quit(self, widget, data=None): @@ -173,7 +180,7 @@ class HackerNewsApp: self.addItem(item) # Catch network errors except requests.exceptions.RequestException as e: - print "There was an error in fetching news items" + print "[+] There was an error in fetching news items" finally: # Call every 10 minutes if not no_timer: @@ -183,7 +190,7 @@ class HackerNewsApp: def main(): parser = argparse.ArgumentParser(description='Hacker News in your System Tray') - parser.add_argument('-v','--version', action='version', version=__version) + parser.add_argument('-v','--version', action='version', version=Version.current()) parser.add_argument('-c','--comments', dest='comments',action='store_true', help="Load the HN comments link for the article as well") parser.add_argument('--chrome', dest='chrome', help="Specify a Google Chrome Profile directory to use for matching chrome history") parser.set_defaults(comments=False) diff --git a/hackertray/version.py b/hackertray/version.py new file mode 100644 index 0000000..d462364 --- /dev/null +++ b/hackertray/version.py @@ -0,0 +1,28 @@ +import requests +import pkg_resources + +class Version: + PYPI_URL = "https://pypi.python.org/pypi/hackertray/json" + @staticmethod + def latest(): + res = requests.get(Version.PYPI_URL).json() + return res['info']['version'] + + @staticmethod + def current(): + return pkg_resources.require("hackertray")[0].version + + @staticmethod + def new_available(): + latest = Version.latest() + current = Version.current() + print [latest, current] + try: + if pkg_resources.parse_version(latest) > pkg_resources.parse_version(current): + print "[+] New version " + latest + " is available" + return True + else: + return False + except requests.exceptions.RequestException as e: + print "[+] There was an error in trying to fetch updates" + return False \ No newline at end of file diff --git a/test/version_test.py b/test/version_test.py new file mode 100644 index 0000000..0ff4292 --- /dev/null +++ b/test/version_test.py @@ -0,0 +1,10 @@ +import unittest +from hackertray import Version + +class VersionTest(unittest.TestCase): + def runTest(self): + version = Version.latest() + assert version + +if __name__ == '__main__': + unittest.main() \ No newline at end of file