spiegel-keyman/linux/keyman-config/keyman_config/handle_install.py
Eberhard Beilharz 8fbdebc9df
feat(linux): keyman:// protocol handler
This change implements the custom keyman:// protocol handler which
will cause the browser to launch km-config when it encounters a
URL using the keyman protocol.

Related to #3271.
2020-07-23 17:35:26 +02:00

63 lines
2 KiB
Python

import logging
import os
from urllib.parse import parse_qs, urlparse
from keyman_config import KeymanComUrl, __tier__
from keyman_config.install_window import InstallKmpWindow
from keyman_config.get_kmp import get_download_folder, download_kmp_file
def download_and_install_package(url):
"""
Handle the download and installation of the given package. This can either be a .kmp
file that got downloaded previously, or a keyman:// URL which will be downloaded and
installed.
Args:
url: a .kmp file, a keyman:// URL, or a file:// URL pointing to a .kmp file,
possibly with a bcp47=<language> specified
"""
parsedUrl = urlparse(url)
bcp47 = _extract_bcp47(parsedUrl.query)
if parsedUrl.scheme == 'keyman':
logging.info("downloading " + url)
if not url.startswith('keyman://download/keyboard/'):
logging.error("Don't know what to do with URL " + url)
return
packageId = parsedUrl.path[len('/keyboard/'):]
if not packageId:
logging.error("Missing package id")
return
downloadFile = os.path.join(get_download_folder(), packageId)
downloadUrl = KeymanComUrl + '/go/package/download/' + packageId + '?platform=linux&tier=' + __tier__
packageFile = download_kmp_file(downloadUrl, downloadFile)
elif parsedUrl.scheme == '' or parsedUrl.scheme == 'file':
packageFile = parsedUrl.path
else:
logging.error("Invalid URL: " + url)
return
if not _install_package(packageFile, bcp47):
logging.error("Can't find file " + url)
def _extract_bcp47(query):
if query:
queryStrings = parse_qs(query)
values = queryStrings['bcp47']
if len(values) > 0:
return values[0]
return ''
def _install_package(packageFile, bcp47):
if not os.path.isfile(packageFile):
return False
w = InstallKmpWindow(packageFile, language=bcp47)
w.run()
w.destroy()
return True