Skip to content

Commit

Permalink
Add default values for missing settings (#298)
Browse files Browse the repository at this point in the history
The xbmcaddon getSetting() function returns an empty string for
non-existing settings. Which makes it unclear if a setting is missing,
or simply set to an empty string on purpose.

But for boolean values, this is not a real issue.

This implements providing a default in case the setting does not exist,
or is empty.

This PR includes:
- Default values for get_settings()
- Improve kodi xbmcaddon stub
- Fix pylint issues related to long lines
  • Loading branch information
dagwieers authored Jun 7, 2019
1 parent b83b8b3 commit 7104fd9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 26 deletions.
23 changes: 13 additions & 10 deletions resources/lib/kodiwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def __init__(self, handle, url):
self._url = url
self._addon = xbmcaddon.Addon()
self._addon_id = self._addon.getAddonInfo('id')
self._max_log_level = log_levels.get(self.get_setting('max_log_level'), 3)
self._usemenucaching = self.get_setting('usemenucaching') == 'true'
self._max_log_level = log_levels.get(self.get_setting('max_log_level', 'Debug'), 3)
self._usemenucaching = self.get_setting('usemenucaching', 'true') == 'true'
self._cache_path = self.get_userdata_path() + 'cache/'
self._system_locale_works = None

Expand Down Expand Up @@ -211,7 +211,7 @@ def play(self, video):
play_item.setProperty('inputstream.adaptive.license_type', 'com.widevine.alpha')
play_item.setProperty('inputstream.adaptive.license_key', video.license_key)

subtitles_visible = self.get_setting('showsubtitles') == 'true'
subtitles_visible = self.get_setting('showsubtitles', 'true') == 'true'
# Separate subtitle url for hls-streams
if subtitles_visible and video.subtitle_url is not None:
self.log_notice('Subtitle URL: ' + unquote(video.subtitle_url), 'Verbose')
Expand Down Expand Up @@ -294,9 +294,12 @@ def localize_datelong(self, date):
''' Return a localized long date string '''
return self.localize_date(date, xbmc.getRegion('datelong'))

def get_setting(self, setting_id):
def get_setting(self, setting_id, default=None):
''' Get an add-on setting '''
return self._addon.getSetting(setting_id)
value = self._addon.getSetting(setting_id)
if value == '' and default is not None:
return default
return value

def set_setting(self, setting_id, setting_value):
''' Set an add-on setting '''
Expand All @@ -314,7 +317,7 @@ def get_global_setting(self, setting):

def get_max_bandwidth(self):
''' Get the max bandwidth based on Kodi and VRT NU add-on settings '''
vrtnu_max_bandwidth = int(self.get_setting('max_bandwidth'))
vrtnu_max_bandwidth = int(self.get_setting('max_bandwidth', '0'))
global_max_bandwidth = int(self.get_global_setting('network.bandwidth'))
if vrtnu_max_bandwidth != 0 and global_max_bandwidth != 0:
return min(vrtnu_max_bandwidth, global_max_bandwidth)
Expand Down Expand Up @@ -365,7 +368,7 @@ def get_proxies(self):

def has_inputstream_adaptive(self):
''' Whether InputStream Adaptive is installed and enabled in add-on settings '''
return self.get_setting('useinputstreamadaptive') == 'true' and xbmc.getCondVisibility('System.HasAddon(inputstream.adaptive)') == 1
return self.get_setting('useinputstreamadaptive', 'true') == 'true' and xbmc.getCondVisibility('System.HasAddon(inputstream.adaptive)') == 1

def has_credentials(self):
''' Whether the add-on has credentials configured '''
Expand All @@ -377,7 +380,7 @@ def kodi_version(self):

def can_play_drm(self):
''' Whether this Kodi can do DRM using InputStream Adaptive '''
return self.get_setting('useinputstreamadaptive') == 'true' and self.kodi_version() > 17
return self.get_setting('useinputstreamadaptive', 'true') == 'true' and self.kodi_version() > 17

def get_userdata_path(self):
''' Return the profile's userdata path '''
Expand Down Expand Up @@ -457,7 +460,7 @@ def human_delta(self, seconds):

def get_cache(self, path, ttl=None):
''' Get the content from cache, if it's still fresh '''
if self.get_setting('usehttpcaching') == 'false':
if self.get_setting('usehttpcaching', 'true') == 'false':
return None

fullpath = self._cache_path + path
Expand All @@ -484,7 +487,7 @@ def get_cache(self, path, ttl=None):

def update_cache(self, path, data):
''' Update the cache, if necessary '''
if self.get_setting('usehttpcaching') == 'false':
if self.get_setting('usehttpcaching', 'true') == 'false':
return

import hashlib
Expand Down
4 changes: 2 additions & 2 deletions resources/lib/streamservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def get_stream(self, video, roaming=False, api_data=None):
# Select streaming protocol
if not drm_stream and self._kodi.has_inputstream_adaptive():
protocol = 'mpeg_dash'
elif drm_stream and self._can_play_drm and self._kodi.get_setting('usedrm') == 'true':
elif drm_stream and self._can_play_drm and self._kodi.get_setting('usedrm', 'true') == 'true':
protocol = 'mpeg_dash'
elif vudrm_token:
protocol = 'hls_aes'
Expand Down Expand Up @@ -307,7 +307,7 @@ def _select_hls_substreams(self, master_hls_url):
hls_variant_url = hls_base_url + match_audio.group('AUDIO_URI') + '-' + hls_variant_url.split('-')[-1]

# Get subtitle url, works only for on demand streams
if self._kodi.get_setting('showsubtitles') == 'true' and '/live/' not in master_hls_url and hls_subtitle_id:
if self._kodi.get_setting('showsubtitles', 'true') == 'true' and '/live/' not in master_hls_url and hls_subtitle_id:
subtitle_regex = re.compile(r'#EXT-X-MEDIA:TYPE=SUBTITLES[\w\-=,\.\"\/]+?GROUP-ID=\"' + hls_subtitle_id + ''
r'\"[\w\-=,\.\"\/]+URI=\"(?P<SUBTITLE_URI>[\w\-=]+)\.m3u8\"')
match_subtitle = re.search(subtitle_regex, hls_playlist)
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/tvguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, _kodi):
self._kodi = _kodi
self._proxies = _kodi.get_proxies()
install_opener(build_opener(ProxyHandler(self._proxies)))
self._showfanart = _kodi.get_setting('showfanart') == 'true'
self._showfanart = _kodi.get_setting('showfanart', 'true') == 'true'

def show_tvguide(self, params):
''' Offer a menu depending on the information provided '''
Expand Down
20 changes: 10 additions & 10 deletions resources/lib/vrtapihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def __init__(self, _kodi, _favorites):
self._kodi = _kodi
self._proxies = _kodi.get_proxies()
install_opener(build_opener(ProxyHandler(self._proxies)))
self._showfanart = _kodi.get_setting('showfanart') == 'true'
self._showpermalink = _kodi.get_setting('showpermalink') == 'true'
self._showfanart = _kodi.get_setting('showfanart', 'true') == 'true'
self._showpermalink = _kodi.get_setting('showpermalink', 'false') == 'true'
self._favorites = _favorites
self._channel_filter = [channel.get('name') for channel in CHANNELS if _kodi.get_setting(channel.get('name')) == 'true']
self._channel_filter = [channel.get('name') for channel in CHANNELS if _kodi.get_setting(channel.get('name'), 'true') == 'true']

def get_tvshow_items(self, category=None, channel=None, use_favorites=False):
''' Get all TV shows for a given category or channel, optionally filtered by favorites '''
Expand Down Expand Up @@ -131,6 +131,7 @@ def get_episode_items(self, path=None, page=None, show_seasons=False, use_favori
sort = 'episode'
ascending = True
content = 'episodes'
use_favorites = statichelper.boolean(use_favorites)

# Recent items
if variety in ('offline', 'recent'):
Expand Down Expand Up @@ -163,9 +164,9 @@ def get_episode_items(self, path=None, page=None, show_seasons=False, use_favori
self._kodi.log_notice('URL get: ' + unquote(api_url), 'Verbose')
api_json = json.load(urlopen(api_url))
self._kodi.update_cache(cache_file, api_json)
episode_items, sort, ascending, content = self._map_to_episode_items(api_json.get('results', []), titletype=variety, use_favorites=statichelper.boolean(use_favorites))
return self._map_to_episode_items(api_json.get('results', []), titletype=variety, use_favorites=use_favorites)

elif path:
if path:
if '.relevant/' in path:
params = {
'facets[programUrl]': '//www.vrt.be' + path.replace('.relevant/', '/'),
Expand All @@ -183,9 +184,9 @@ def get_episode_items(self, path=None, page=None, show_seasons=False, use_favori
results, episodes = self._get_season_episode_data(api_url, show_seasons=show_seasons, all_items=all_items)

if results.get('episodes'):
episode_items, sort, ascending, content = self._map_to_episode_items(results.get('episodes'), titletype=titletype, season_key=season_key, use_favorites=use_favorites)
elif results.get('seasons'):
episode_items, sort, ascending, content = self._map_to_season_items(api_url, results.get('seasons'), episodes)
return self._map_to_episode_items(results.get('episodes', []), titletype=titletype, season_key=season_key, use_favorites=use_favorites)
if results.get('seasons'):
return self._map_to_season_items(api_url, results.get('seasons'), episodes)

return episode_items, sort, ascending, content

Expand Down Expand Up @@ -416,8 +417,7 @@ def search(self, search_string, page=0):
api_json = json.load(urlopen(api_url))

episodes = api_json.get('results', [{}])
episode_items, sort, ascending, content = self._map_to_episode_items(episodes, titletype='recent')
return episode_items, sort, ascending, content
return self._map_to_episode_items(episodes, titletype='recent')

def get_live_screenshot(self, channel):
''' Get a live screenshot for a given channel, only supports Eén, Canvas and Ketnet '''
Expand Down
8 changes: 6 additions & 2 deletions resources/lib/vrtplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def show_main_menu_items(self):
TitleItem(title=self._kodi.localize(30020), # Recent items
url_dict=dict(action=actions.LISTING_RECENT),
is_playable=False,
art_dict=dict(thumb='DefaultRecentlyAddedEpisodes.png', icon='DefaultRecentlyAddedEpisodes.png', fanart='DefaultRecentlyAddedEpisodes.png'),
art_dict=dict(thumb='DefaultRecentlyAddedEpisodes.png',
icon='DefaultRecentlyAddedEpisodes.png',
fanart='DefaultRecentlyAddedEpisodes.png'),
video_dict=dict(plot=self._kodi.localize(30021))),
TitleItem(title=self._kodi.localize(30022), # Soon offline
url_dict=dict(action=actions.LISTING_OFFLINE),
Expand Down Expand Up @@ -88,7 +90,9 @@ def show_favorites_menu_items(self):
TitleItem(title=self._kodi.localize(30042), # My recent items
url_dict=dict(action=actions.LISTING_RECENT, use_favorites=True),
is_playable=False,
art_dict=dict(thumb='DefaultRecentlyAddedEpisodes.png', icon='DefaultRecentlyAddedEpisodes.png', fanart='DefaultRecentlyAddedEpisodes.png'),
art_dict=dict(thumb='DefaultRecentlyAddedEpisodes.png',
icon='DefaultRecentlyAddedEpisodes.png',
fanart='DefaultRecentlyAddedEpisodes.png'),
video_dict=dict(plot=self._kodi.localize(30043))),
TitleItem(title=self._kodi.localize(30044), # My soon offline
url_dict=dict(action=actions.LISTING_OFFLINE, use_favorites=True),
Expand Down
2 changes: 1 addition & 1 deletion test/xbmcaddon.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def getLocalizedString(msgctxt):
@staticmethod
def getSetting(key):
''' A working implementation for the xbmcaddon Addon class getSetting() method '''
return ADDON_SETTINGS.get(key)
return ADDON_SETTINGS.get(key, '')

@staticmethod
def setSetting(key, value):
Expand Down

0 comments on commit 7104fd9

Please sign in to comment.