pybee / toga-demo

A demonstration of the capabilities of the Toga widget toolkit.
BSD 3-Clause "New" or "Revised" License
16 stars 9 forks source link

Fedora 24 only has the 4.0 Webkit2 bindings, causing toga-demo to fail #5

Closed ncoghlan closed 8 years ago

ncoghlan commented 8 years ago

Thanks for the Gtk support! Unfortunately, the demo is failing to find the WebKit bindings on Fedora, and the number of different projects and version numbers interacting, as well as the major naming differences between Debian and Fedora in this area, makes it hard to figure out what it's actually looking for:

$ toga-demo
Traceback (most recent call last):
  File "/home/ncoghlan/.local/bin/toga-demo", line 7, in <module>
    from toga_demo.__main__ import main
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga_demo/__main__.py", line 6, in <module>
    from .app import TogaDemo
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga_demo/app.py", line 4, in <module>
    import toga
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga/__init__.py", line 91, in <module>
    set_platform()
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga/__init__.py", line 73, in set_platform
    locals['platform'] = importlib.import_module(module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga_gtk/__init__.py", line 20, in <module>
    from .widgets.webview import *
  File "/home/ncoghlan/.local/lib/python2.7/site-packages/toga_gtk/widgets/webview.py", line 8, in <module>
    gi.require_version('WebKit2', '3.0')
  File "/usr/lib64/python2.7/site-packages/gi/__init__.py", line 106, in require_version
    (namespace, version))
ValueError: Namespace WebKit2 not available for version 3.0

gir1.2-webkit2-3.0 seems to be the relevant Debian package (which is mentioned on toga's Gtk support page), and looking at https://packages.debian.org/sid/gir1.2-webkit2-4.0 shows that's the Gobject introspection bindings for WebKit2 3.0

However, I can't find anything similar for Fedora, which suggests this may work quite differently on non-Debian derived Linux systems.

ncoghlan commented 8 years ago

Looking at http://lazka.github.io/pgi-docs/#WebKit2-4.0 suggests only 4.0 may be available, and sure enough, if I try that at the Python 3 prompt it works:

Python 3.5.1 (default, Sep 14 2016, 11:27:59) 
[GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('WebKit2', '3.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.5/site-packages/gi/__init__.py", line 106, in require_version
    (namespace, version))
ValueError: Namespace WebKit2 not available for version 3.0
>>> gi.require_version('WebKit2', '4.0')
>>>

Ditto for Python 2:

Python 2.7.12 (default, Sep  2 2016, 14:46:00) 
[GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('WebKit2', '3.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/gi/__init__.py", line 106, in require_version
    (namespace, version))
ValueError: Namespace WebKit2 not available for version 3.0
>>> gi.require_version('WebKit2', '4.0')
>>> 
freakboy3742 commented 8 years ago

Thanks for the report, @ncoghlan. It looks like Ubuntu 16.04 has Webkit2-4.0 bindings available. I'll need to take a closer look to see if there's a better way to specify this version string that won't be as problematic across platforms (suggestions welcome!).

The error handling also needs some work, apparently, because webkit should be optional - it isn't used by the demo, so the fact it isn't installed shouldn't be an issue.

ncoghlan commented 8 years ago

I had a quick look for examples, but didn't see anything other than folks just writing "require_version" with a specific version number (as in https://developer.fedoraproject.org/tech/languages/python/pygobject.html ).

Following some of the links from there and searching a bit more, it looks like there is a C level API to list the versions of available namespaces, but that isn't visible to Python: https://developer.gnome.org/gi/stable/GIRepository.html#g-irepository-enumerate-versions

The one introspection API that does seem to be available is gi.get_required_version which at least lets you detect if a namespace is already loaded, and what version that was:

>>> gi.get_required_version("WebKit2")
>>> gi.require_version("WebKit2", "4.0")
>>> gi.get_required_version("WebKit2")
'4.0'
ncoghlan commented 8 years ago

I guess one thing you could try if toga can handle multiple versions of the WebKit2 namespace is something like:

acceptable_versions = [" 4.0", "3.0"]
for namespace_version in acceptable_versions:
    try:
        gi.require_version("WebKit2", namespace_version)
    except ValueError:
        continue
    break
else:
    # Whatever you need to do to indicate internally that WebKit2 is unavailable

(I don't know how different the 3.0 and 4.0 WebKit2 bindings actually are, though)