Skip to content

Commit

Permalink
add kodiutils and kodilogging from generator-kodi-addon project
Browse files Browse the repository at this point in the history
  • Loading branch information
aqw committed Aug 24, 2018
1 parent 2a16ca8 commit 81c9e15
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
15 changes: 15 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,21 @@ License: CC BY-NC 2.0
You. This License may not be modified without the mutual written agreement of
the Licensor and You.

Files: resources/lib/kodiutils.py, resources/lib/kodilogging.py
Copyright 2016 Kolja Lampe <[email protected]>
License: Apache License, Version 2
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
.
http://www.apache.org/licenses/LICENSE-2.0
.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Files: resources/lib/pigskin/pigskin.py
Copyright: 2012-2018, see git log for respective authors
License: MIT
Expand Down
2 changes: 2 additions & 0 deletions default.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import xbmcvfs

from resources.lib.pigskin.pigskin import pigskin
from resources.lib import kodiutils
from resources.lib import kodilogging

addon = xbmcaddon.Addon()
language = addon.getLocalizedString
Expand Down
43 changes: 43 additions & 0 deletions resources/lib/kodilogging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from resources.lib.kodiutils import get_setting_as_bool

import logging
import xbmc
import xbmcaddon


class KodiLogHandler(logging.StreamHandler):

def __init__(self):
logging.StreamHandler.__init__(self)
addon_id = xbmcaddon.Addon().getAddonInfo('id')
prefix = b"[%s] " % addon_id
formatter = logging.Formatter(prefix + b'%(name)s: %(message)s')
self.setFormatter(formatter)

def emit(self, record):
levels = {
logging.CRITICAL: xbmc.LOGFATAL,
logging.ERROR: xbmc.LOGERROR,
logging.WARNING: xbmc.LOGWARNING,
logging.INFO: xbmc.LOGINFO,
logging.DEBUG: xbmc.LOGDEBUG,
logging.NOTSET: xbmc.LOGNONE,
}
if get_setting_as_bool('debug'):
try:
xbmc.log(self.format(record), levels[record.levelno])
except UnicodeEncodeError:
xbmc.log(self.format(record).encode(
'utf-8', 'ignore'), levels[record.levelno])

def flush(self):
pass


def config():
logger = logging.getLogger()
logger.addHandler(KodiLogHandler())
logger.setLevel(logging.DEBUG)
70 changes: 70 additions & 0 deletions resources/lib/kodiutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-

import xbmc
import xbmcaddon
import xbmcgui
import sys
import logging
import json as json

# read settings
ADDON = xbmcaddon.Addon()

logger = logging.getLogger(__name__)


def notification(header, message, time=5000, icon=ADDON.getAddonInfo('icon'), sound=True):
xbmcgui.Dialog().notification(header, message, icon, time, sound)


def show_settings():
ADDON.openSettings()


def get_setting(setting):
return ADDON.getSetting(setting).strip().decode('utf-8')


def set_setting(setting, value):
ADDON.setSetting(setting, str(value))


def get_setting_as_bool(setting):
return get_setting(setting).lower() == "true"


def get_setting_as_float(setting):
try:
return float(get_setting(setting))
except ValueError:
return 0


def get_setting_as_int(setting):
try:
return int(get_setting_as_float(setting))
except ValueError:
return 0


def get_string(string_id):
return ADDON.getLocalizedString(string_id).encode('utf-8', 'ignore')


def kodi_json_request(params):
data = json.dumps(params)
request = xbmc.executeJSONRPC(data)

try:
response = json.loads(request)
except UnicodeDecodeError:
response = json.loads(request.decode('utf-8', 'ignore'))

try:
if 'result' in response:
return response['result']
return None
except KeyError:
logger.warn("[%s] %s" %
(params['method'], response['error']['message']))
return None

0 comments on commit 81c9e15

Please sign in to comment.