Commits

thijsdezoete committed 0bdcfa5

Working stuff, with py2app

  • Participants
  • Parent commits 3b60642

Comments (0)

Files changed (5)

api/testing.py

-from PyQt4 import QtCore, QtGui  # , QtSvg
-import os
-
-FOLDER = os.path.dirname(os.path.abspath(__file__) + '../')
-print FOLDER
-IMAGE = FOLDER + '/ImageCache.png'
-
-IMAGES = (
-        FOLDER + '/ImageCache.png',
-        FOLDER + '/apple.png',
-        )
-
-
-def some_call():
-    print 'some method in api'
-
-def get_entries():
-    def test():
-        print 'function from get_entries'
-    return [('From api', test),
-            ('Another', some_call)]
 import os
 from PyQt4 import QtCore
 from PyQt4 import QtGui
+import requests
+from urlparse import urlparse
+
 
 # if 'linux' in platform:
 #     import appindicator
 
 class Favicon(object):
     def __init__(self):
-        self.storage_cache = os.path.abspath(os.path.dirname(__file__))
+        self.storage_cache = os.path.abspath(os.path.dirname(__file__) + '/img_cache')
+        # print 'storage cache:'
+        # print self.storage_cache
+        self.icons = {}
+        self.icon_pm = {}
 
-        pass
+    def _get_direct(self, url, name=None):
+        if not name:
+            raise
+        icon = requests.get(url)
+
+        return icon.content
+
+    def _get_local(self, name):
+        try:
+            if self.icons[name]:
+                return self.icons[name]
+        except KeyError:
+            return None
+
+    def _get_remote(self, name):
+        url = urlparse(name).path
+        # print url
+        # url = name
+
+        # if 'http://' in url:
+        #     url = url[6:]
+        # print 'Get favicon from %s' % url
+        icon_url = "http://www.google.com/s2/favicons?domain="
+
+        #icon_url = "http://g.etfv.co/"
+        icon = requests.get(icon_url + url)
+        # print icon_url + url
+        # print icon.headers
+
+        icon_file = self.storage_cache + '/favicon-%s.ico' % name
+
+        return icon.content
+
+    def get_image_url(self, url, name=None):
+        icon = self._get_direct(url, name)
+        dataBuffer = QtCore.QByteArray(icon)
+        pm = QtGui.QPixmap(32, 32)
+        pm.loadFromData(dataBuffer)
+        self.icon_pm[name] = pm
+        # print self.icon_pm[name].getSize()
+        self.icons[name] = QtGui.QIcon(self.icon_pm[name])
+        return name
 
     def get(self, name):
+        icon = self._get_local(name)
+        if not icon:
+            icon = self._get_remote(name)
+
+            dataBuffer = QtCore.QByteArray(icon)
+            pm = QtGui.QPixmap(32, 32)
+            pm.loadFromData(dataBuffer)
+            self._icon_pixmap = pm
+            if not pm:
+                print '---- NO PIXMAP!!!!! ----'
+            self.icons[name] = QtGui.QIcon(pm)
+
         return name
 
     def put(self, name):
 
     def hello(self, icon_name):
         print 'Hello world!'
-        self.parent.change_top_icon(icon_name)
+        self.parent.set_top_icon(icon_name)
 
     def download_favicon(self):
         import requests
         return self.icon
 
 
-class MyWindow(QtGui.QDialog):    # any super class is okay
-    def __init__(self, parent=None):
-        super(MyWindow, self).__init__(parent)
-        self.button = QtGui.QPushButton('Press')
-        layout = QtGui.QHBoxLayout()
-        layout.addWidget(self.button)
-        self.setLayout(layout)
-        self.button.clicked.connect(self.create_child)
-
-    def create_child(self):
-        # here put the code that creates the new window and shows it.
-        child = MyWindow(self)
-        child.show()
-
-
 class Indicatr(object):
     def __init__(self):
         self.app = QtGui.QApplication([])
         self.app.setQuitOnLastWindowClosed(False)
         self.top_icon = QtGui.QSystemTrayIcon()
         self.menu = QtGui.QMenu()
+        # Add quit btn straight away
+        self.add_quit_btn()
+
         self.icons = {}
         self.new_tray = SetupTrayWindow(parent=self)
+        self.favicon = Favicon()
 
     def add_icon(self, file, name='filename'):
-        try:
-            if name == 'filename':
-                name = os.path.splitext(file)[0]
-            self.icons[name] = QtGui.QIcon(file)
-        except:
             pass
 
     def change_icon(self, element, icon):
         except:
             pass
 
-    def change_top_icon(self, icon):
+    def set_top_icon(self, icon):
         if not icon in self.icons:
             self.add_icon(icon)
 
-        self.change_icon(self.top_icon, self.icons[icon])
+        self.change_icon(self.top_icon, self.favicon.icons[icon])
 
     def add_icon_to_menu(self, icon_name, text, callback):
-        self.menu.addAction(self.icons[icon_name], text, callback)
+        return self.menu.addAction(self.favicon.icons[icon_name], text, callback)
+
+    def add_to_menu(self, text, callback):
+        return self.menu.addAction(text, callback)
 
-    def add_to_menu(self, qt_action):
-        self.menu.addAction(qt_action)
+    # def _show_tray_add(self):
+    #     self.new_tray.raise_()
+    #     self.new_tray.activateWindow()
+    #     self.app.setActiveWindow(self.new_tray)
+    #     self.app.restoreOverrideCursor()
+    #     self.new_tray.show()
 
-    def _show_tray_add(self):
-        self.new_tray.raise_()
-        self.new_tray.activateWindow()
-        self.app.setActiveWindow(self.new_tray)
-        self.app.restoreOverrideCursor()
-        self.new_tray.show()
+    def add_separator(self):
+        self.menu.addSeparator()
 
     def add_quit_btn(self):
         # self.menu.addAction('Initialise API', self.init_api)
-        self.menu.addAction('New Tray App', self._show_tray_add)
+        # self.menu.addAction('New Tray App', self._show_tray_add)
         self.menu.addAction('Quit', self._quit)
+        self.menu.addSeparator()
 
     def _quit(self):
-        for icon in self.icons:
-            print 'Should remove %s' % icon
+        # for icon in self.favicon.icons:
+        #     print 'Should remove %s' % icon
         QtGui.QApplication.quit()
 
     def run(self):
         self.top_icon.setContextMenu(self.menu)
         self.app.exec_()
 
-    # def hide_mac_dock_icon(self):
-    #     try:
-    #         import AppKit
-    #         # https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html
-    #         NSApplicationActivationPolicyRegular = 0
-    #         NSApplicationActivationPolicyAccessory = 1
-    #         NSApplicationActivationPolicyProhibited = 2
-    #         AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)
-    #     except:
-    #         # Don't do anything if we can't remove dock icon...
-    #         print 'Cant remove icon from dock. Install pyobjc to fix this'
-    #         pass
+    def hide_mac_dock_icon(self):
+        try:
+            import AppKit
+            # https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html
+            NSApplicationActivationPolicyRegular = 0
+            NSApplicationActivationPolicyAccessory = 1
+            NSApplicationActivationPolicyProhibited = 2
+            AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)
+        except:
+            # Don't do anything if we can't remove dock icon...
+            print 'Cant remove icon from dock. Install pyobjc to fix this'
+            pass
 
 if __name__ == '__main__':
     x = Indicatr()
     x.add_icon('apple.png')
-    x.change_top_icon('apple')
+    x.set_top_icon('apple')
     x.add_quit_btn()
+    # x.cls_factory('Reddit')
     #x._show_tray_add()
     x.run()
+"""
+This is a setup.py script generated by py2applet
+
+Usage:
+    python setup.py py2app
+"""
+
+from setuptools import setup
+
+APP = ['testing.py']
+DATA_FILES = []
+OPTIONS = {'argv_emulation': False, 'includes': ['indicate']}
+
+setup(
+    app=APP,
+    data_files=DATA_FILES,
+    options={'py2app': OPTIONS},
+    setup_requires=['py2app'],
+)
-import objc 
-from Foundation import *
-from AppKit import *
-from PyObjCTools import AppHelper
+import requests
+import indicate
+# from indicate import Favicon
+from urlparse import urlparse
+# import sys
+import os
+
+
+class Reddit(object):
+    def __init__(self, indicatr):
+        self.url = 'http://www.reddit.com/'
+        self.parent = indicatr
+        self.parent.favicon.get('www.reddit.com')
+        self.parent.set_top_icon('www.reddit.com')
+        self.items = {}
+
+    def click_link(self, link):
+        os.system('open %s' % link)
+
+    def process_item(self, item, url):
+        base_url = str(urlparse(url).netloc)
+        self.parent.favicon.get(base_url)
+        self.items[item] = url
+        self.parent.add_icon_to_menu(base_url,
+            item,
+            lambda: self.click_link(self.items[item])
+        )
+
+    def get_subreddit(self, sub):
+        self.parent.add_to_menu((sub + ' subreddit').title(), lambda: 'hello')
+        url = '{0}r/{1}/{2}.json?sort={2}&limit={3}'.format(self.url, sub, 'hot', '10')
+        posts = requests.get(url).json()
+        links = [(x['data']['title'][:12] + '...', str(x['data']['url'])) for x in posts['data']['children'] if x['kind'] == u't3']
+
+        [self.process_item(item, url) for (item, url) in links]
+
+    def run(self):
+        self.parent.add_quit_btn()
+        self.get_subreddit('python')
+        self.parent.run()
+
+
+if __name__ == '__main__':
+    x = indicate.Indicatr()
+    r = Reddit(indicatr=x)
+    r.run()
+import requests
+import indicate
+# from indicate import Favicon
+from urlparse import urlparse
+# import sys
+import os
+# from PyQt4 import QtGui
+
+
+class Reddit(object):
+    def __init__(self, indicatr):
+        self.url = 'http://www.reddit.com/'
+        self.parent = indicatr
+        self.parent.favicon.get('www.reddit.com')
+        self.parent.set_top_icon('www.reddit.com')
+        self.items = {}
+
+    def click_link(self, link):
+        os.system('open %s' % link)
+
+    def process_item(self, item, url):
+        base_url = str(urlparse(url).netloc)
+        self.parent.favicon.get(base_url)
+        self.items[item] = url
+        self.parent.add_icon_to_menu(base_url,
+            item,
+            lambda: self.click_link(self.items[item])
+        )
+
+    def get_subreddit(self, sub):
+        url = '{0}r/{1}/{2}.json?sort={2}&limit={3}'.format(self.url, sub, 'hot', '10')
+        posts = requests.get(url).json()
+        links = [(x['data']['title'][:20] + '...', str(x['data']['url'])) for x in posts['data']['children'] if x['kind'] == u't3']
+
+        [self.process_item(item, url) for (item, url) in links]
+
+    def run(self, subreddit="python"):
+        self.get_subreddit(subreddit)
+        self.parent.add_separator()
+        self.get_subreddit('php')
+
+        self.parent.run()
+
+
+if __name__ == '__main__':
+    x = indicate.Indicatr()
+    x.hide_mac_dock_icon()
+    r = Reddit(indicatr=x)
+    r.run()