From 3afba6f3ebac4186d34b38a6f8f8a3b5fee85ea2 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sun, 24 Dec 2017 18:55:20 +0530 Subject: [PATCH] pep8 changes --- hackertray/__init__.py | 39 +++++++++++++------------- hackertray/analytics.py | 10 +++++-- hackertray/appindicator_replacement.py | 1 + hackertray/chrome.py | 5 +++- hackertray/firefox.py | 7 +++-- hackertray/hackernews.py | 3 +- hackertray/version.py | 6 ++-- 7 files changed, 43 insertions(+), 28 deletions(-) diff --git a/hackertray/__init__.py b/hackertray/__init__.py index 3df0522..e33c1bb 100644 --- a/hackertray/__init__.py +++ b/hackertray/__init__.py @@ -5,7 +5,7 @@ import requests import platform import subprocess -if(os.environ.get('TRAVIS')!='true'): +if(os.environ.get('TRAVIS') != 'true'): import pygtk pygtk.require('2.0') @@ -31,13 +31,15 @@ from .firefox import Firefox from .version import Version from .analytics import Analytics + 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" MIXPANEL_TOKEN = "51a04e37dad59393c7371407e84a8050" + def __init__(self, args): - #Load the database + # Load the database home = expanduser("~") with open(home + '/.hackertray.json', 'a+') as content_file: content_file.seek(0) @@ -58,7 +60,7 @@ class HackerNewsApp: # create a menu self.menu = gtk.Menu() - #The default state is false, and it toggles when you click on it + # The default state is false, and it toggles when you click on it self.commentState = args.comments # create items for the menu - refresh, quit and a separator @@ -79,14 +81,14 @@ class HackerNewsApp: btnRefresh = gtk.MenuItem("Refresh") btnRefresh.show() - #the last parameter is for not running the timer + # the last parameter is for not running the timer 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) + btnUpdate.connect('activate', self.showUpdate) self.menu.append(btnUpdate) btnQuit = gtk.MenuItem("Quit") @@ -106,7 +108,7 @@ class HackerNewsApp: launch_data['version'] = Version.current() launch_data['platform'] = platform.linux_distribution() try: - launch_data['browser'] = subprocess.check_output(["xdg-settings","get","default-web-browser"]).strip() + launch_data['browser'] = subprocess.check_output(["xdg-settings", "get", "default-web-browser"]).strip() except subprocess.CalledProcessError as e: launch_data['browser'] = "unknown" self.tracker.track('launch', launch_data) @@ -115,26 +117,25 @@ class HackerNewsApp: """Whether comments page is opened or not""" self.commentState = not self.commentState - def showUpdate(self,widget): + def showUpdate(self, widget): """Handle the update button""" webbrowser.open(HackerNewsApp.UPDATE_URL) # Remove the update button once clicked self.menu.remove(widget) self.tracker.visit(HackerNewsApp.UPDATE_URL) - def showAbout(self, widget): """Handle the about btn""" webbrowser.open(HackerNewsApp.ABOUT_URL) self.tracker.visit(HackerNewsApp.ABOUT_URL) - #ToDo: Handle keyboard interrupt properly + # ToDo: Handle keyboard interrupt properly def quit(self, widget, data=None): """ Handler for the quit button""" l = list(self.db) home = expanduser("~") - #truncate the file + # truncate the file with open(home + '/.hackertray.json', 'w+') as file: file.write(json.dumps(l)) @@ -148,8 +149,8 @@ class HackerNewsApp: def open(self, widget, event=None, data=None): """Opens the link in the web browser""" - #We disconnect and reconnect the event in case we have - #to set it to active and we don't want the signal to be processed + # We disconnect and reconnect the event in case we have + # to set it to active and we don't want the signal to be processed if not widget.get_active(): widget.disconnect(widget.signal_id) widget.set_active(True) @@ -164,7 +165,7 @@ class HackerNewsApp: def addItem(self, item): """Adds an item to the menu""" - #This is in the case of YC Job Postings, which we skip + # This is in the case of YC Job Postings, which we skip if item['points'] == 0 or item['points'] is None: return @@ -184,7 +185,6 @@ class HackerNewsApp: i.show() def refresh(self, widget=None, no_timer=False, chrome_data_directory=None, firefox_data_directory=None): - """Refreshes the menu """ try: # Create an array of 20 false to denote matches in History @@ -197,12 +197,12 @@ class HackerNewsApp: if(firefox_data_directory): searchResults = self.mergeBoolArray(searchResults, Firefox.search(urls, firefox_data_directory)) - #Remove all the current stories + # Remove all the current stories for i in self.menu.get_children(): if hasattr(i, 'url'): self.menu.remove(i) - #Add back all the refreshed news + # Add back all the refreshed news for index, item in enumerate(data): item['history'] = searchResults[index] if item['url'].startswith('item?id='): @@ -223,13 +223,14 @@ class HackerNewsApp: original[index] = original[index] or patch[index] return original + def main(): parser = argparse.ArgumentParser(description='Hacker News in your System Tray') - 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('-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.add_argument('--firefox', dest='firefox', help="Specify a Firefox Profile directory to use for matching firefox history") - parser.add_argument('--dnt', dest='dnt',action='store_true', help="Disable all analytics (Do Not Track)") + parser.add_argument('--dnt', dest='dnt', action='store_true', help="Disable all analytics (Do Not Track)") parser.set_defaults(comments=False) parser.set_defaults(dnt=False) args = parser.parse_args() diff --git a/hackertray/analytics.py b/hackertray/analytics.py index ffa4352..8498b6c 100644 --- a/hackertray/analytics.py +++ b/hackertray/analytics.py @@ -1,9 +1,11 @@ from mixpanel import Mixpanel + class Analytics: # Setup analytics. # dnt - do not track. Disables tracking if True # token - The mixpanel token + def __init__(self, dnt, token): self.dnt = dnt self.tracker = Mixpanel(token) @@ -12,12 +14,14 @@ class Analytics: # Track an event # event - string containing the event name # data - data related to the event, defaults to {} - def track(self, event, data = {}): + + def track(self, event, data={}): if(self.dnt == False): # All events are tracked anonymously self.tracker.track("anonymous", event, data) # Track a visit to a URL - # The url maybe an HN submission or + # The url maybe an HN submission or # some meta-url pertaining to hackertray + def visit(self, url): - self.track('visit', {"link":url}) \ No newline at end of file + self.track('visit', {"link": url}) diff --git a/hackertray/appindicator_replacement.py b/hackertray/appindicator_replacement.py index 8ea0171..34b8722 100644 --- a/hackertray/appindicator_replacement.py +++ b/hackertray/appindicator_replacement.py @@ -35,6 +35,7 @@ def get_icon_filename(icon_name): # The main class class Indicator: # Constructor + def __init__(self, unknown, icon, category): # Store the settings self.inactive_icon = get_icon_filename(icon) diff --git a/hackertray/chrome.py b/hackertray/chrome.py index 4d6d3c0..c3f0008 100644 --- a/hackertray/chrome.py +++ b/hackertray/chrome.py @@ -4,8 +4,10 @@ import shutil import os import sys + class Chrome: HISTORY_TMP_LOCATION = '/tmp/hackertray.chrome' + @staticmethod def search(urls, config_folder_path): Chrome.setup(config_folder_path) @@ -13,13 +15,14 @@ class Chrome: db = conn.cursor() result = [] for url in urls: - db_result = db.execute('SELECT url from urls WHERE url=:url',{"url":url}) + db_result = db.execute('SELECT url from urls WHERE url=:url', {"url": url}) if(db.fetchone() == None): result.append(False) else: result.append(True) os.remove(Chrome.HISTORY_TMP_LOCATION) return result + @staticmethod def setup(config_folder_path): file_name = os.path.abspath(config_folder_path+'/History') diff --git a/hackertray/firefox.py b/hackertray/firefox.py index 7cb4a57..1f7b78c 100644 --- a/hackertray/firefox.py +++ b/hackertray/firefox.py @@ -4,9 +4,11 @@ import shutil import os import sys + class Firefox: HISTORY_TMP_LOCATION = '/tmp/hackertray.firefox' HISTORY_FILE_NAME = '/places.sqlite' + @staticmethod def search(urls, config_folder_path): Firefox.setup(config_folder_path) @@ -14,17 +16,18 @@ class Firefox: db = conn.cursor() result = [] for url in urls: - db_result = db.execute('SELECT url from moz_places WHERE url=:url',{"url":url}) + db_result = db.execute('SELECT url from moz_places WHERE url=:url', {"url": url}) if(db.fetchone() == None): result.append(False) else: result.append(True) os.remove(Firefox.HISTORY_TMP_LOCATION) return result + @staticmethod def setup(config_folder_path): file_name = os.path.abspath(config_folder_path+Firefox.HISTORY_FILE_NAME) if not os.path.isfile(file_name): print("ERROR: ", "Could not find Firefox history file", file=sys.stderr) sys.exit(1) - shutil.copyfile(file_name, Firefox.HISTORY_TMP_LOCATION) \ No newline at end of file + shutil.copyfile(file_name, Firefox.HISTORY_TMP_LOCATION) diff --git a/hackertray/hackernews.py b/hackertray/hackernews.py index 9857e45..d21ff37 100644 --- a/hackertray/hackernews.py +++ b/hackertray/hackernews.py @@ -7,6 +7,7 @@ urls = [ class HackerNews: + @staticmethod def getHomePage(): random.shuffle(urls) @@ -15,4 +16,4 @@ class HackerNews: try: return r.json() except ValueError: - continue \ No newline at end of file + continue diff --git a/hackertray/version.py b/hackertray/version.py index d102da5..82c8114 100644 --- a/hackertray/version.py +++ b/hackertray/version.py @@ -1,8 +1,10 @@ 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() @@ -14,7 +16,7 @@ class Version: @staticmethod def new_available(): - latest = Version.latest() + latest = Version.latest() current = Version.current() try: if pkg_resources.parse_version(latest) > pkg_resources.parse_version(current): @@ -24,4 +26,4 @@ class Version: 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 + return False