Initial import

This commit is contained in:
Raoul Snyman 2017-05-17 14:35:21 -07:00
commit 5e2ed052c6
5 changed files with 202 additions and 0 deletions

2
.bzrignore Normal file
View File

@ -0,0 +1,2 @@
dist
*.egg-info

15
README.rst Normal file
View File

@ -0,0 +1,15 @@
WebAppify
=========
WebAppify is a simple module to easily create your own desktop apps of websites.
To create your own desktop web app, import and set up the WebApp class.
.. code:: python
from webappify import WebApp
app = WebApp('OpenStreetMap', 'https://www.openstreetmap.org', 'osm.png')
app.run()
This will create a window with the website, using the icon provided.

0
setup.cfg Normal file
View File

43
setup.py Normal file
View File

@ -0,0 +1,43 @@
"""
The webappify package
"""
import os
from codecs import open
from setuptools import setup, find_packages
HERE = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(HERE, 'README.rst'), encoding='utf8') as f:
LONG_DESCRIPTION = f.read()
setup(
name='WebAppify',
version='0.1',
description='Create desktop apps of your favourite websites',
long_description=LONG_DESCRIPTION,
url='https://launchpad.net/webappify',
author='Raoul Snyman',
author_email='raoul@snyman.info',
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: MacOS X',
'Environment :: Win32 (MS Windows)',
'Environment :: X11 Applications',
'Environment :: X11 Applications :: Qt',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Desktop Environment',
'Topic :: Internet :: WWW/HTTP :: Browsers',
'Topic :: Software Development :: Libraries :: Python Modules'
],
keywords='Qt website',
packages=find_packages(),
install_requires=['PyQt5']
)

142
webappify/__init__.py Normal file
View File

@ -0,0 +1,142 @@
"""
The :mod:`webapp` module contains a WebApp class which can be used to create simple "app windows"
for websites. To use it, do this::
from webapp import WebApp
app = WebApp('GMail', 'https://mail.google.com', 'gmail.png')
app.run()
.. note:
If your site needs Flash Player, you'll need the appropriate Flash Player plugin installed
system-wide. For WebKit, this is the NPAPI plugin, and for WebEngine, this is the PPAPI plugin.
"""
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from PyQt5 import QtWebEngineWidgets
HAS_WEBENGINE = True
except ImportError:
HAS_WEBENGINE = False
try:
from PyQt5 import QtWebKit, QtWebKitWidgets
HAS_WEBKIT = True
except ImportError:
HAS_WEBKIT = False
if HAS_WEBENGINE:
SETTINGS = [
QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled,
QtWebEngineWidgets.QWebEngineSettings.JavascriptCanAccessClipboard,
QtWebEngineWidgets.QWebEngineSettings.LocalContentCanAccessRemoteUrls
]
WebView = QtWebEngineWidgets.QWebEngineView
elif HAS_WEBKIT:
SETTINGS = [
QtWebKit.QWebSettings.AutoLoadImages,
QtWebKit.QWebSettings.JavascriptEnabled,
QtWebKit.QWebSettings.JavaEnabled,
QtWebKit.QWebSettings.PluginsEnabled,
QtWebKit.QWebSettings.JavascriptCanOpenWindows,
QtWebKit.QWebSettings.JavascriptCanCloseWindows,
QtWebKit.QWebSettings.JavascriptCanAccessClipboard,
QtWebKit.QWebSettings.DeveloperExtrasEnabled,
QtWebKit.QWebSettings.OfflineStorageDatabaseEnabled,
QtWebKit.QWebSettings.OfflineWebApplicationCacheEnabled,
QtWebKit.QWebSettings.LocalStorageEnabled,
QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls,
QtWebKit.QWebSettings.LocalContentCanAccessFileUrls,
QtWebKit.QWebSettings.AcceleratedCompositingEnabled
]
WebView = QtWebKitWidgets.QWebView
class WebPage(QtWebKitWidgets.QWebPage):
"""Custom class for overriding the user agent to make WebKit look like Chrome"""
def userAgentForUrl(self, url):
return 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/28.0.1500.52 Safari/537.36'
else:
print('Cannot detect either QtWebEngine or QtWebKit!')
sys.exit(1)
class WebWindow(QtWidgets.QWidget):
"""
Window
"""
def __init__(self, title, url, icon, parent=None):
"""
Create the window
"""
super().__init__(parent)
self.setWindowTitle(title)
self.setWindowIcon(QtGui.QIcon(icon))
self.setContentsMargins(0, 0, 0, 0)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.webview = WebView(self)
if HAS_WEBKIT:
self.webview.setPage(WebPage(self.webview))
for setting in SETTINGS:
self.webview.settings().setAttribute(setting, True)
self.webview.titleChanged.connect(self.onTitleChanged)
self.webview.setUrl(QtCore.QUrl(url))
self.layout.addWidget(self.webview)
def onTitleChanged(self, title):
"""
React to title changes
"""
if title:
self.setWindowTitle(title)
class WebApp(QtWidgets.QApplication):
"""
A generic application to open a web page in a desktop app
"""
def __init__(self, title, url, icon, hasTray=False, canMinimizeToTray=False):
"""
Create an application which loads a URL into a window
"""
super().__init__(sys.argv)
self.beforeHooks = []
self.afterHooks = []
self.window = None
self.title = title
self.url = url
self.icon = icon
self.hasTray = hasTray
self.canMinimizeToTray = canMinimizeToTray
self.setWindowIcon(QtGui.QIcon(self.icon))
def setupTrayIcon(self):
"""
Set up the tray icon
"""
if not QtWidgets.QSystemTrayIcon.isSystemTrayAvailable():
# No reason to continue if the OS doesn't support system tray icons
return
self.trayIcon = QtWidgets.QSystemTrayIcon(QtGui.QIcon(self.icon), self.window)
self.trayIcon.show()
def addBeforeHook(self, hook):
"""
Add a function to run before setting everything up
"""
if hook:
self.beforeHooks.append(hook)
def run(self):
"""
Run the app
"""
self.window = WebWindow(self.title, self.url, self.icon)
if self.hasTray:
self.setupTrayIcon()
self.window.showMaximized()
return self.exec()