Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage:
status:
project: false
comment: off
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
*.pyc
*.pyo
.coverage
.pytest_cache/
.tox/
kodi-addon-checker-report.log
kodi-addon-checker.log
File renamed without changes.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ install:

script:
- pylint lib/*.py
- pytest lib/tests.py
- coverage run -m pytest lib/tests.py
#- if [[ $TRAVIS_PYTHON_VERSION != 2.7 ]]; then (kodi-addon-checker --branch=krypton .); fi

after_success:
- codecov
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![GitHub release](https://img.shields.io/github/release/tamland/kodi-plugin-routing.svg)](https://github.com/tamland/kodi-plugin-routing/releases)
[![Build Status](https://travis-ci.org/tamland/kodi-plugin-routing.svg?branch=master)](https://travis-ci.org/tamland/kodi-plugin-routing)
[![Codecov status](https://img.shields.io/codecov/c/github/tamland/kodi-plugin-routing/master)](https://codecov.io/gh/tamland/kodi-plugin-routing/branch/master)
[![License: GPLv3](https://img.shields.io/badge/License-GPLv3-yellow.svg)](https://opensource.org/licenses/GPL-3.0)
[![Contributors](https://img.shields.io/github/contributors/tamland/kodi-plugin-routing.svg)](https://github.com/tamland/kodi-plugin-routing/graphs/contributors)

Expand Down
26 changes: 12 additions & 14 deletions lib/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
from urllib.parse import urlencode

try:
import xbmc
import xbmcaddon
_addon_id = xbmcaddon.Addon().getAddonInfo('id')
from xbmc import LOGDEBUG, log
from xbmcaddon import Addon
_addon_id = Addon().getAddonInfo('id')

def log(msg):
msg = "[%s][routing] %s" % (_addon_id, msg)
xbmc.log(msg, level=xbmc.LOGDEBUG)
def _log(msg):
log('[%s][routing] %s' % (_addon_id, msg), level=LOGDEBUG)
except ImportError:
def log(msg):
_addon_id = 'foo.bar'

def _log(msg):
print(msg)


Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(self, base_url=None):
self.args = {}
self.base_url = base_url
if self.base_url is None:
self.base_url = "plugin://" + xbmcaddon.Addon().getAddonInfo('id')
self.base_url = "plugin://" + _addon_id

def route_for(self, path):
"""
Expand Down Expand Up @@ -137,7 +138,7 @@ def _dispatch(self, path):
for rule in rules:
kwargs = rule.match(path)
if kwargs is not None:
log("Dispatching to '%s', args: %s" % (view_func.__name__, kwargs))
_log("Dispatching to '%s', args: %s" % (view_func.__name__, kwargs))
view_func(**kwargs)
return
raise RoutingError('No route to path "%s"' % path)
Expand Down Expand Up @@ -183,10 +184,7 @@ def make_path(self, *args, **kwargs):
qs_kwargs = dict(((k, v) for k, v in list(kwargs.items()) if k not in self._keywords))

query = '?' + urlencode(qs_kwargs) if qs_kwargs else ''
try:
return self._pattern.format(**url_kwargs) + query
except KeyError:
return None
return self._pattern.format(**url_kwargs) + query

def __str__(self):
return b"Rule(pattern=%s, keywords=%s)" % (self._pattern, self._keywords)
return "Rule(pattern=%s, keywords=%s)" % (self._pattern, self._keywords)
36 changes: 35 additions & 1 deletion lib/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

from __future__ import absolute_import, division, print_function, unicode_literals

import sys
import pytest
import mock
from routing import Plugin, UrlRule, RoutingError
from routing import Plugin, UrlRule, RoutingError, _addon_id


@pytest.fixture()
Expand All @@ -37,6 +38,9 @@ def test_make_path():
assert rule.make_path(1, 2) == "/p/1/2"
assert rule.make_path(baz=3, foo=1, bar=2) == "/p/1/2?baz=3"
assert rule.make_path(1) is None
assert rule.make_path(1, bar=2) is None
assert rule.make_path(1, 2, 3) is None
assert str(rule) # Cover __str__


def test_make_path_should_urlencode_args():
Expand Down Expand Up @@ -96,6 +100,13 @@ def test_path(plugin):
assert plugin.path == '/foo/bar/baz'


def test_redirect(plugin):
f = mock.create_autospec(lambda: None)
plugin.route("/foo")(f)
plugin.redirect('/foo')
f.assert_called_with()


def test_no_route(plugin):
f = lambda a: None
plugin.route("/foo/<a>/<b>")(f)
Expand All @@ -114,20 +125,23 @@ def test_arg_parsing(plugin):
plugin.run(['plugin://py.test/foo', '0', '?bar=baz'])
assert plugin.args['bar'][0] == 'baz'


def test_trailing_slash_in_route_definition(plugin):
""" Should call registered route with trailing slash. """
f = mock.create_autospec(lambda: None)
plugin.route("/foo/")(f)
plugin.run(['plugin://py.test/foo', '0'])
assert f.call_count == 1


def test_trailing_slashes_in_run(plugin):
""" Should call registered route without trailing slash. """
f = mock.create_autospec(lambda: None)
plugin.route("/foo")(f)
plugin.run(['plugin://py.test/foo/', '0'])
assert f.call_count == 1


def test_trailing_slash_handling_for_root(plugin):
f = mock.create_autospec(lambda: None)
plugin.route("/<a>")(lambda: None)
Expand All @@ -137,3 +151,23 @@ def test_trailing_slash_handling_for_root(plugin):
assert f.call_count == 2
with pytest.raises(RoutingError):
plugin.run(['plugin://py.test/a/b', '0'])


def test_empty_run():
f = lambda: None
sys.argv = ['plugin://py.test/foo', '666', '?bar=baz']
p = Plugin('plugin://py.test')
p.route('/foo')(f)
p.run()
assert p.args['bar'][0] == 'baz'


def test_argv():
sys.argv = ['plugin://py.test/foo', '666', '?bar=baz']
p = Plugin('plugin://py.test')
assert p.handle == 666


def test_baseurl():
p = Plugin()
assert p.base_url == 'plugin://' + _addon_id
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
codecov
kodi-addon-checker
mock
pylint
Expand Down