1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (C) 2011-2013 Martijn Kaijser
Copyright (C) 2013-2014 Team-XBMC
Copyright (C) 2014-2019 Team Kodi
This file is part of service.xbmc.versioncheck
SPDX-License-Identifier: GPL-3.0-or-later
See LICENSES/GPL-3.0-or-later.txt for more information.
"""
from contextlib import closing
import os
import sys
import xbmc # pylint: disable=import-error
import xbmcaddon # pylint: disable=import-error
import xbmcgui # pylint: disable=import-error
import xbmcvfs # pylint: disable=import-error
_ADDON = xbmcaddon.Addon('service.xbmc.versioncheck')
_ADDON_NAME = _ADDON.getAddonInfo('name')
if sys.version_info[0] >= 3:
_ADDON_PATH = _ADDON.getAddonInfo('path')
else:
_ADDON_PATH = _ADDON.getAddonInfo('path').decode('utf-8')
_ICON = _ADDON.getAddonInfo('icon')
class Viewer:
""" Show user a text viewer (WINDOW_DIALOG_TEXT_VIEWER)
Include the text file for the viewers body in the resources/ directory
usage:
script_path = os.path.join(_ADDON_PATH, 'resources', 'lib', 'version_check', 'viewer.py')
xbmc.executebuiltin('RunScript(%s,%s,%s)' % (script_path, 'Heading', 'notice.txt'))
:param heading: text viewer heading
:type heading: str
:param filename: filename to use for text viewers body
:type filename: str
"""
WINDOW = 10147
CONTROL_LABEL = 1
CONTROL_TEXTBOX = 5
def __init__(self, heading, filename):
self.heading = heading
self.filename = filename
# activate the text viewer window
xbmc.executebuiltin('ActivateWindow(%d)' % (self.WINDOW,))
# get window
self.window = xbmcgui.Window(self.WINDOW)
# give window time to initialize
xbmc.sleep(100)
# set controls
self.set_controls()
def set_controls(self):
""" Set the window controls
"""
# get text viewer body text
text = self.get_text()
# set heading
self.window.getControl(self.CONTROL_LABEL).setLabel('%s : %s' % (_ADDON_NAME,
self.heading,))
# set text
self.window.getControl(self.CONTROL_TEXTBOX).setText(text)
xbmc.sleep(2000)
def get_text(self):
""" Get the text viewers body text from self.filename
:return: contents of self.filename
:rtype: str
"""
try:
return self.read_file(self.filename)
except Exception as error: # pylint: disable=broad-except
xbmc.log(_ADDON_NAME + ': ' + str(error), xbmc.LOGERROR)
return ''
@staticmethod
def read_file(filename):
""" Read the contents of the provided file, from
os.path.join(_ADDON_PATH, 'resources', filename)
:param filename: name of file to read
:type filename: str
:return: contents of the provided file
:rtype: str
"""
filename = os.path.join(_ADDON_PATH, 'resources', filename)
with closing(xbmcvfs.File(filename)) as open_file:
contents = open_file.read()
return contents
class WebBrowser:
""" Display url using the default browser
usage:
script_path = os.path.join(_ADDON_PATH, 'resources', 'lib', 'version_check', 'viewer.py')
xbmc.executebuiltin('RunScript(%s,%s,%s)' % (script_path, 'webbrowser', 'https://kodi.tv/'))
:param url: url to open
:type url: str
"""
def __init__(self, url):
self.url = url
try:
# notify user
self.notification(_ADDON_NAME, self.url)
xbmc.sleep(100)
# launch url
self.launch_url()
except Exception as error: # pylint: disable=broad-except
xbmc.log(_ADDON_NAME + ': ' + str(error), xbmc.LOGERROR)
@staticmethod
def notification(heading, message, icon=None, time=15000, sound=True):
""" Create a notification
:param heading: notification heading
:type heading: str
:param message: notification message
:type message: str
:param icon: path and filename for the notification icon
:type icon: str
:param time: time to display notification
:type time: int
:param sound: is notification audible
:type sound: bool
"""
if not icon:
icon = _ICON
xbmcgui.Dialog().notification(heading, message, icon, time, sound)
def launch_url(self):
""" Open self.url in the default web browser
"""
import webbrowser # pylint: disable=import-outside-toplevel
webbrowser.open(self.url)
if __name__ == '__main__':
try:
if sys.argv[1] == 'webbrowser':
WebBrowser(sys.argv[2])
else:
Viewer(sys.argv[1], sys.argv[2])
except Exception as err: # pylint: disable=broad-except
xbmc.log(_ADDON_NAME + ': ' + str(err), xbmc.LOGERROR)
|