pep8 changes

This commit is contained in:
Nemo 2017-12-24 18:55:20 +05:30
parent e34d767ef0
commit 3afba6f3eb
7 changed files with 43 additions and 28 deletions

View File

@ -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()

View File

@ -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
# some meta-url pertaining to hackertray
def visit(self, url):
self.track('visit', {"link":url})
self.track('visit', {"link": url})

View File

@ -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)

View File

@ -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')

View File

@ -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,13 +16,14 @@ 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)

View File

@ -7,6 +7,7 @@ urls = [
class HackerNews:
@staticmethod
def getHomePage():
random.shuffle(urls)

View File

@ -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):