plugin.video.viervijfzes/test/xbmc.py

248 lines
8.2 KiB
Python
Raw Normal View History

2020-03-19 16:45:31 +01:00
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <dag@wieers.com>
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2020-03-26 11:31:28 +01:00
""" This file implements the Kodi xbmc module, either using stubs or alternative functionality """
2020-03-19 16:45:31 +01:00
# pylint: disable=invalid-name,no-self-use,unused-argument
from __future__ import absolute_import, division, print_function, unicode_literals
import json
import os
import time
from xbmcextra import global_settings, import_language
LOGDEBUG = 0
LOGERROR = 4
LOGFATAL = 6
LOGINFO = 1
LOGNONE = 7
LOGNOTICE = 2
LOGSEVERE = 5
LOGWARNING = 3
LOG_MAPPING = {
LOGDEBUG: 'Debug',
LOGERROR: 'Error',
LOGFATAL: 'Fatal',
LOGINFO: 'Info',
LOGNONE: 'None',
LOGNOTICE: 'Notice',
LOGSEVERE: 'Severe',
LOGWARNING: 'Warning',
}
INFO_LABELS = {
'System.BuildVersion': '18.2',
}
REGIONS = {
'datelong': '%A, %e %B %Y',
'dateshort': '%Y-%m-%d',
}
GLOBAL_SETTINGS = global_settings()
PO = import_language(language=GLOBAL_SETTINGS.get('locale.language'))
def to_unicode(text, encoding='utf-8'):
""" Force text to unicode """
return text.decode(encoding) if isinstance(text, bytes) else text
def from_unicode(text, encoding='utf-8'):
""" Force unicode to text """
import sys
if sys.version_info.major == 2 and isinstance(text, unicode): # noqa: F821; pylint: disable=undefined-variable
return text.encode(encoding)
return text
class Keyboard:
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc Keyboard class """
2020-03-19 16:45:31 +01:00
def __init__(self, line='', heading=''):
2020-03-26 11:31:28 +01:00
""" A stub constructor for the xbmc Keyboard class """
2020-03-19 16:45:31 +01:00
def doModal(self, autoclose=0):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Keyboard class doModal() method """
2020-03-19 16:45:31 +01:00
def isConfirmed(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Keyboard class isConfirmed() method """
2020-03-19 16:45:31 +01:00
return True
def getText(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Keyboard class getText() method """
2020-03-19 16:45:31 +01:00
return 'test'
class Monitor:
2020-03-26 11:31:28 +01:00
"""A stub implementation of the xbmc Monitor class"""
2020-03-19 16:45:31 +01:00
def __init__(self, line='', heading=''):
2020-03-26 11:31:28 +01:00
"""A stub constructor for the xbmc Monitor class"""
self._deadline = time.time() + 10 # 10 seconds
2020-03-19 16:45:31 +01:00
def abortRequested(self):
2020-03-26 11:31:28 +01:00
"""A stub implementation for the xbmc Keyboard class abortRequested() method"""
return time.time() > self._deadline
2020-03-19 16:45:31 +01:00
def waitForAbort(self, timeout=None):
2020-03-26 11:31:28 +01:00
"""A stub implementation for the xbmc Keyboard class waitForAbort() method"""
time.sleep(0.5)
2020-03-19 16:45:31 +01:00
return False
class Player:
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc Player class """
2020-03-19 16:45:31 +01:00
def __init__(self):
self._count = 0
def play(self, item='', listitem=None, windowed=False, startpos=-1):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class play() method """
2020-03-19 16:45:31 +01:00
return
def isPlaying(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class isPlaying() method """
2020-03-19 16:45:31 +01:00
# Return True four times out of five
self._count += 1
return bool(self._count % 5 != 0)
def setSubtitles(self, subtitleFile):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class setSubtitles() method """
2020-03-19 16:45:31 +01:00
return
def showSubtitles(self, visible):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class showSubtitles() method """
2020-03-19 16:45:31 +01:00
return
def getTotalTime(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class getTotalTime() method """
2020-03-19 16:45:31 +01:00
return 0
def getTime(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class getTime() method """
2020-03-19 16:45:31 +01:00
return 0
def getVideoInfoTag(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class getVideoInfoTag() method """
2020-03-19 16:45:31 +01:00
return VideoInfoTag()
def getPlayingFile(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc Player class getPlayingFile() method """
2020-03-19 16:45:31 +01:00
return ''
2020-03-20 13:53:21 +01:00
2020-03-19 16:45:31 +01:00
class VideoInfoTag:
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc VideoInfoTag class """
2020-03-19 16:45:31 +01:00
def __init__(self):
2020-03-26 11:31:28 +01:00
""" A stub constructor for the xbmc VideoInfoTag class """
2020-03-19 16:45:31 +01:00
def getSeason(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc VideoInfoTag class getSeason() method """
2020-03-19 16:45:31 +01:00
return 0
def getEpisode(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc VideoInfoTag class getEpisode() method """
2020-03-19 16:45:31 +01:00
return 0
def getTVShowTitle(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc VideoInfoTag class getTVShowTitle() method """
2020-03-19 16:45:31 +01:00
return ''
def getPlayCount(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc VideoInfoTag class getPlayCount() method """
2020-03-19 16:45:31 +01:00
return 0
def getRating(self):
2020-03-26 11:31:28 +01:00
""" A stub implementation for the xbmc VideoInfoTag class getRating() method """
2020-03-19 16:45:31 +01:00
return 0
def executebuiltin(string, wait=False): # pylint: disable=unused-argument
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc executebuiltin() function """
2020-03-19 16:45:31 +01:00
return
def executeJSONRPC(jsonrpccommand):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc executeJSONRPC() function """
2020-03-19 16:45:31 +01:00
command = json.loads(jsonrpccommand)
if command.get('method') == 'Settings.GetSettingValue':
key = command.get('params').get('setting')
return json.dumps(dict(id=1, jsonrpc='2.0', result=dict(value=GLOBAL_SETTINGS.get(key))))
if command.get('method') == 'Addons.GetAddonDetails':
if command.get('params', {}).get('addonid') == 'script.module.inputstreamhelper':
return json.dumps(dict(id=1, jsonrpc='2.0', result=dict(addon=dict(enabled='true', version='0.3.5'))))
return json.dumps(dict(id=1, jsonrpc='2.0', result=dict(addon=dict(enabled='true', version='1.2.3'))))
if command.get('method') == 'Textures.GetTextures':
return json.dumps(dict(id=1, jsonrpc='2.0', result=dict(textures=[dict(cachedurl="", imagehash="", lasthashcheck="", textureid=4837, url="")])))
if command.get('method') == 'Textures.RemoveTexture':
return json.dumps(dict(id=1, jsonrpc='2.0', result="OK"))
log("executeJSONRPC does not implement method '{method}'".format(**command), 'Error')
return json.dumps(dict(error=dict(code=-1, message='Not implemented'), id=1, jsonrpc='2.0'))
def getCondVisibility(string):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc getCondVisibility() function """
2020-03-19 16:45:31 +01:00
if string == 'system.platform.android':
return False
return True
def getInfoLabel(key):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc getInfoLabel() function """
2020-03-19 16:45:31 +01:00
return INFO_LABELS.get(key)
def getLocalizedString(msgctxt):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc getLocalizedString() function """
2020-03-19 16:45:31 +01:00
for entry in PO:
if entry.msgctxt == '#%s' % msgctxt:
return entry.msgstr or entry.msgid
if int(msgctxt) >= 30000:
log('Unable to translate #{msgctxt}'.format(msgctxt=msgctxt), LOGERROR)
return '<Untranslated>'
def getRegion(key):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc getRegion() function """
2020-03-19 16:45:31 +01:00
return REGIONS.get(key)
def log(msg, level=LOGINFO):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc log() function """
2020-03-19 16:45:31 +01:00
if level in (LOGERROR, LOGFATAL):
print('\033[31;1m%s: \033[32;0m%s\033[0;39m' % (LOG_MAPPING.get(level), to_unicode(msg)))
if level == LOGFATAL:
raise Exception(msg)
elif level in (LOGWARNING, LOGNOTICE):
print('\033[33;1m%s: \033[32;0m%s\033[0;39m' % (LOG_MAPPING.get(level), to_unicode(msg)))
else:
print('\033[32;1m%s: \033[32;0m%s\033[0;39m' % (LOG_MAPPING.get(level), to_unicode(msg)))
def setContent(self, content):
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc setContent() function """
2020-03-19 16:45:31 +01:00
return
def sleep(seconds):
2020-03-26 11:31:28 +01:00
""" A reimplementation of the xbmc sleep() function """
2020-03-19 16:45:31 +01:00
time.sleep(seconds)
def translatePath(path):
2020-03-26 11:31:28 +01:00
""" A stub implementation of the xbmc translatePath() function """
2020-03-19 16:45:31 +01:00
if path.startswith('special://home'):
return path.replace('special://home', os.path.join(os.getcwd(), 'test/'))
if path.startswith('special://masterprofile'):
return path.replace('special://masterprofile', os.path.join(os.getcwd(), 'test/userdata/'))
if path.startswith('special://profile'):
return path.replace('special://profile', os.path.join(os.getcwd(), 'test/userdata/'))
if path.startswith('special://userdata'):
return path.replace('special://userdata', os.path.join(os.getcwd(), 'test/userdata/'))
return path