Beispiel #1
0
    def __init__(self, centralwidget, **kwargs):
        """MainWebView loads the page, and attaches interfaces to it.
        
        Keyword arguments:
          - url
          - hostname
          - use_repl

        NOTE: all QWebSettings must be set before setUrl() is called!
        """
        QtWebKit.QWebView.__init__(self, centralwidget)
        self.setObjectName("webView0")

        # store keyword arguments to options
        self.options = kwargs

        use_repl = self.options.get('use_repl')

        # if IIVARI_CACHE_PATH is None, caching is disabled
        cache_path = __builtin__.IIVARI_CACHE_PATH

        # set custom WebPage to log JavaScript messages
        self.setPage(MainWebPage())

        # initialize REPL here to attach to console log early
        if use_repl is True:
            self.repl = Repl(page=self.page())

        # get token for network request authentication
        hostname = self.options.get('hostname')
        token = self.get_token(hostname)

        # set custom NetworkAccessManager for cookie management and network error logging
        self.page().setNetworkAccessManager(MainNetworkAccessManager(token=token))

        # attach a Display object to JavaScript window.display after page has loaded
        self.page().loadFinished[bool].connect(self.create_display)

        # use QWebSettings for this webView
        qsettings = self.settings()
        #qsettings = QWebSettings.globalSettings()

        # enable Javascript
        qsettings.setAttribute(QWebSettings.JavascriptEnabled, True)

        """
        Enable application to work in offline mode.
        Use HTML5 cache manifest for static content,
        and jquery.offline for dynamic JSON content.
        
        @see http://diveintohtml5.info/offline.html
        """
        if cache_path is not None:
            qsettings.enablePersistentStorage(cache_path)
            # uncertain whether LocalContentCanAccessRemoteUrls is needed even when using offline cache
            # FIXME: check, and disable this if unneeded.
            qsettings.setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)

        #qsettings.setAttribute(
        #    QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)
        
        # write qsettings to log
        logger.debug("\n * ".join([
            ' --- QWebSettings ---',
            'OfflineWebApplicationCache: %s' % (
                qsettings.testAttribute(
                    QWebSettings.OfflineWebApplicationCacheEnabled)),
            'LocalStorage: %s' % (
                qsettings.testAttribute(
                    QWebSettings.LocalStorageEnabled)),
            'offlineWebApplicationCachePath: %s' % (
                qsettings.offlineWebApplicationCachePath()),
            'offlineWebApplicationCacheQuota: %i' % (
                qsettings.offlineWebApplicationCacheQuota()),
            'LocalContentCanAccessRemoteUrls: %s' % (
                qsettings.testAttribute(
                    QWebSettings.LocalContentCanAccessRemoteUrls)),
            
            ]))

        # set URL and launch the request
        url = self.options.get('url')
        logger.info("url: %s" % url)
        self.setUrl(QtCore.QUrl(url))
Beispiel #2
0
    def __init__(self, centralwidget, **kwargs):
        """MainWebView loads the page, and attaches interfaces to it.
        
        Keyword arguments:
          - url
          - hostname
          - use_repl

        NOTE: all QWebSettings must be set before setUrl() is called!
        """
        QtWebKit.QWebView.__init__(self, centralwidget)
        self.setObjectName("webView0")

        # store keyword arguments to options
        self.options = kwargs

        use_repl = self.options.get('use_repl')

        # if IIVARI_CACHE_PATH is None, caching is disabled
        cache_path = __builtin__.IIVARI_CACHE_PATH

        # set custom WebPage to log JavaScript messages
        self.setPage(MainWebPage())

        # initialize REPL here to attach to console log early
        if use_repl is True:
            self.repl = Repl(page=self.page())

        # get token for network request authentication
        hostname = self.options.get('hostname')
        token = self.get_token(hostname)

        # set custom NetworkAccessManager for cookie management and network error logging
        self.page().setNetworkAccessManager(
            MainNetworkAccessManager(token=token))

        # attach a Display object to JavaScript window.display after page has loaded
        self.page().loadFinished[bool].connect(self.create_display)

        # use QWebSettings for this webView
        qsettings = self.settings()
        #qsettings = QWebSettings.globalSettings()

        # enable Javascript
        qsettings.setAttribute(QWebSettings.JavascriptEnabled, True)
        """
        Enable application to work in offline mode.
        Use HTML5 cache manifest for static content,
        and jquery.offline for dynamic JSON content.
        
        @see http://diveintohtml5.info/offline.html
        """
        if cache_path is not None:
            qsettings.enablePersistentStorage(cache_path)
            # uncertain whether LocalContentCanAccessRemoteUrls is needed even when using offline cache
            # FIXME: check, and disable this if unneeded.
            qsettings.setAttribute(
                QWebSettings.LocalContentCanAccessRemoteUrls, True)

        #qsettings.setAttribute(
        #    QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)

        # write qsettings to log
        logger.debug("\n * ".join([
            ' --- QWebSettings ---',
            'OfflineWebApplicationCache: %s' % (qsettings.testAttribute(
                QWebSettings.OfflineWebApplicationCacheEnabled)),
            'LocalStorage: %s' %
            (qsettings.testAttribute(QWebSettings.LocalStorageEnabled)),
            'offlineWebApplicationCachePath: %s' %
            (qsettings.offlineWebApplicationCachePath()),
            'offlineWebApplicationCacheQuota: %i' %
            (qsettings.offlineWebApplicationCacheQuota()),
            'LocalContentCanAccessRemoteUrls: %s' % (qsettings.testAttribute(
                QWebSettings.LocalContentCanAccessRemoteUrls)),
        ]))

        # set URL and launch the request
        url = self.options.get('url')
        logger.info("url: %s" % url)
        self.setUrl(QtCore.QUrl(url))
Beispiel #3
0
class MainWebView(QtWebKit.QWebView):
    
    display = None
    repl = None
    options = None
    
    def __init__(self, centralwidget, **kwargs):
        """MainWebView loads the page, and attaches interfaces to it.
        
        Keyword arguments:
          - url
          - hostname
          - use_repl

        NOTE: all QWebSettings must be set before setUrl() is called!
        """
        QtWebKit.QWebView.__init__(self, centralwidget)
        self.setObjectName("webView0")

        # store keyword arguments to options
        self.options = kwargs

        use_repl = self.options.get('use_repl')

        # if IIVARI_CACHE_PATH is None, caching is disabled
        cache_path = __builtin__.IIVARI_CACHE_PATH

        # set custom WebPage to log JavaScript messages
        self.setPage(MainWebPage())

        # initialize REPL here to attach to console log early
        if use_repl is True:
            self.repl = Repl(page=self.page())

        # get token for network request authentication
        hostname = self.options.get('hostname')
        token = self.get_token(hostname)

        # set custom NetworkAccessManager for cookie management and network error logging
        self.page().setNetworkAccessManager(MainNetworkAccessManager(token=token))

        # attach a Display object to JavaScript window.display after page has loaded
        self.page().loadFinished[bool].connect(self.create_display)

        # use QWebSettings for this webView
        qsettings = self.settings()
        #qsettings = QWebSettings.globalSettings()

        # enable Javascript
        qsettings.setAttribute(QWebSettings.JavascriptEnabled, True)

        """
        Enable application to work in offline mode.
        Use HTML5 cache manifest for static content,
        and jquery.offline for dynamic JSON content.
        
        @see http://diveintohtml5.info/offline.html
        """
        if cache_path is not None:
            qsettings.enablePersistentStorage(cache_path)
            # uncertain whether LocalContentCanAccessRemoteUrls is needed even when using offline cache
            # FIXME: check, and disable this if unneeded.
            qsettings.setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)

        #qsettings.setAttribute(
        #    QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)
        
        # write qsettings to log
        logger.debug("\n * ".join([
            ' --- QWebSettings ---',
            'OfflineWebApplicationCache: %s' % (
                qsettings.testAttribute(
                    QWebSettings.OfflineWebApplicationCacheEnabled)),
            'LocalStorage: %s' % (
                qsettings.testAttribute(
                    QWebSettings.LocalStorageEnabled)),
            'offlineWebApplicationCachePath: %s' % (
                qsettings.offlineWebApplicationCachePath()),
            'offlineWebApplicationCacheQuota: %i' % (
                qsettings.offlineWebApplicationCacheQuota()),
            'LocalContentCanAccessRemoteUrls: %s' % (
                qsettings.testAttribute(
                    QWebSettings.LocalContentCanAccessRemoteUrls)),
            
            ]))

        # set URL and launch the request
        url = self.options.get('url')
        logger.info("url: %s" % url)
        self.setUrl(QtCore.QUrl(url))


    def create_display(self, ok):
        """Display is a JavaScript object proxy.

        It connects window.display JavaScript signals 
        to Python methods. 

        Python can also emit signals to JavaScript "slots"
        through this object.

        CoffeeScript-generated DisplayCtrl is another object,
        that is referred as window.displayCtrl. This Display
        object connects signals between these.
        """
        self.display = Display(self.page(), **self.options)
        # disconnect the signal once the display has first been created
        self.page().loadFinished.disconnect(self.create_display)
        # start REPL thread?
        if self.repl is not None:
            self.repl.run()


    def get_token(self, hostname):
        """Reads authentication key and calculates token."""
        try:
            keyfile = settings.AUTHKEY_FILE
            f = open(keyfile, 'r')
            key = f.read().strip()
            f.close()
            token = hostname+":"+hashlib.sha1(hostname+":"+key).hexdigest()
            return token
        except Exception, e:
            # suppress warning to debug level, as this feature is not yet used
            logger.debug("Failed to read authentication key: " + str(e))
            return None
Beispiel #4
0
class MainWebView(QtWebKit.QWebView):

    display = None
    repl = None
    options = None

    def __init__(self, centralwidget, **kwargs):
        """MainWebView loads the page, and attaches interfaces to it.
        
        Keyword arguments:
          - url
          - hostname
          - use_repl

        NOTE: all QWebSettings must be set before setUrl() is called!
        """
        QtWebKit.QWebView.__init__(self, centralwidget)
        self.setObjectName("webView0")

        # store keyword arguments to options
        self.options = kwargs

        use_repl = self.options.get('use_repl')

        # if IIVARI_CACHE_PATH is None, caching is disabled
        cache_path = __builtin__.IIVARI_CACHE_PATH

        # set custom WebPage to log JavaScript messages
        self.setPage(MainWebPage())

        # initialize REPL here to attach to console log early
        if use_repl is True:
            self.repl = Repl(page=self.page())

        # get token for network request authentication
        hostname = self.options.get('hostname')
        token = self.get_token(hostname)

        # set custom NetworkAccessManager for cookie management and network error logging
        self.page().setNetworkAccessManager(
            MainNetworkAccessManager(token=token))

        # attach a Display object to JavaScript window.display after page has loaded
        self.page().loadFinished[bool].connect(self.create_display)

        # use QWebSettings for this webView
        qsettings = self.settings()
        #qsettings = QWebSettings.globalSettings()

        # enable Javascript
        qsettings.setAttribute(QWebSettings.JavascriptEnabled, True)
        """
        Enable application to work in offline mode.
        Use HTML5 cache manifest for static content,
        and jquery.offline for dynamic JSON content.
        
        @see http://diveintohtml5.info/offline.html
        """
        if cache_path is not None:
            qsettings.enablePersistentStorage(cache_path)
            # uncertain whether LocalContentCanAccessRemoteUrls is needed even when using offline cache
            # FIXME: check, and disable this if unneeded.
            qsettings.setAttribute(
                QWebSettings.LocalContentCanAccessRemoteUrls, True)

        #qsettings.setAttribute(
        #    QtWebKit.QWebSettings.DeveloperExtrasEnabled, True)

        # write qsettings to log
        logger.debug("\n * ".join([
            ' --- QWebSettings ---',
            'OfflineWebApplicationCache: %s' % (qsettings.testAttribute(
                QWebSettings.OfflineWebApplicationCacheEnabled)),
            'LocalStorage: %s' %
            (qsettings.testAttribute(QWebSettings.LocalStorageEnabled)),
            'offlineWebApplicationCachePath: %s' %
            (qsettings.offlineWebApplicationCachePath()),
            'offlineWebApplicationCacheQuota: %i' %
            (qsettings.offlineWebApplicationCacheQuota()),
            'LocalContentCanAccessRemoteUrls: %s' % (qsettings.testAttribute(
                QWebSettings.LocalContentCanAccessRemoteUrls)),
        ]))

        # set URL and launch the request
        url = self.options.get('url')
        logger.info("url: %s" % url)
        self.setUrl(QtCore.QUrl(url))

    def create_display(self, ok):
        """Display is a JavaScript object proxy.

        It connects window.display JavaScript signals 
        to Python methods. 

        Python can also emit signals to JavaScript "slots"
        through this object.

        CoffeeScript-generated DisplayCtrl is another object,
        that is referred as window.displayCtrl. This Display
        object connects signals between these.
        """
        self.display = Display(self.page(), **self.options)
        # disconnect the signal once the display has first been created
        self.page().loadFinished.disconnect(self.create_display)
        # start REPL thread?
        if self.repl is not None:
            self.repl.run()

    def get_token(self, hostname):
        """Reads authentication key and calculates token."""
        try:
            keyfile = settings.AUTHKEY_FILE
            f = open(keyfile, 'r')
            key = f.read().strip()
            f.close()
            token = hostname + ":" + hashlib.sha1(hostname + ":" +
                                                  key).hexdigest()
            return token
        except Exception, e:
            # suppress warning to debug level, as this feature is not yet used
            logger.debug("Failed to read authentication key: " + str(e))
            return None