summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-21 21:50:46 +0200
committerDavid Robillard <d@drobilla.net>2019-10-21 21:50:46 +0200
commit7a7bff205975ac1668f71da1a7af172ead100849 (patch)
tree96e24e75ca904d744de2e1ef4f6a18cda560788c
parent06e30c3fc93a2cc59e8d0642e257f73b59c38974 (diff)
downloadlilv-7a7bff205975ac1668f71da1a7af172ead100849.tar.gz
lilv-7a7bff205975ac1668f71da1a7af172ead100849.tar.bz2
lilv-7a7bff205975ac1668f71da1a7af172ead100849.zip
Make Python bindings more Pythonic
-rw-r--r--NEWS3
-rw-r--r--bindings/python/lilv.py36
-rw-r--r--bindings/test/python/test_api.py12
3 files changed, 44 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index 41f286a..f1a93be 100644
--- a/NEWS
+++ b/NEWS
@@ -8,8 +8,9 @@ lilv (0.24.5) unstable;
* Fix unit tests on Windows
* Add more strict error detection when storing plugin state properties
* Fix memory errors and Python 3.4+ compatibility in Python bindings
+ * Make Python bindings more Pythonic
- -- David Robillard <d@drobilla.net> Mon, 21 Oct 2019 17:19:31 +0000
+ -- David Robillard <d@drobilla.net> Mon, 21 Oct 2019 19:50:13 +0000
lilv (0.24.4) stable;
diff --git a/bindings/python/lilv.py b/bindings/python/lilv.py
index 6fdd790..bfff997 100644
--- a/bindings/python/lilv.py
+++ b/bindings/python/lilv.py
@@ -946,9 +946,17 @@ class Plugins(Collection):
def __getitem__(self, key):
if type(key) == int:
return super(Plugins, self).__getitem__(key)
- return self.get_by_uri(key)
+
+ plugin = self.get_by_uri(key)
+ if plugin is None:
+ raise KeyError
+
+ return plugin
def get_by_uri(self, uri):
+ if type(uri) == str:
+ uri = self.world.new_uri(uri)
+
return Plugin.wrap(
self.world, c.plugins_get_by_uri(self.collection, uri.node)
)
@@ -985,9 +993,17 @@ class PluginClasses(Collection):
def __getitem__(self, key):
if type(key) == int:
return super(PluginClasses, self).__getitem__(key)
- return self.get_by_uri(key)
+
+ klass = self.get_by_uri(key)
+ if klass is None:
+ raise KeyError
+
+ return klass
def get_by_uri(self, uri):
+ if type(uri) == str:
+ uri = self.world.new_uri(uri)
+
plugin_class = c.plugin_classes_get_by_uri(self.collection, uri.node)
return PluginClass(self.world, plugin_class) if plugin_class else None
@@ -1027,9 +1043,17 @@ class UIs(Collection):
def __getitem__(self, key):
if type(key) == int:
return super(UIs, self).__getitem__(key)
- return self.get_by_uri(key)
+
+ ui = self.get_by_uri(key)
+ if ui is None:
+ raise KeyError
+
+ return ui
def get_by_uri(self, uri):
+ if type(uri) == str:
+ uri = self.world.new_uri(uri)
+
ui = c.uis_get_by_uri(self.collection, uri.node)
return UI(self.world, ui) if ui else None
@@ -1324,7 +1348,11 @@ class World(Structure):
def new_uri(self, uri):
"""Create a new URI node."""
- return Node.wrap(self, c.new_uri(self.world, uri))
+ c_node = c.new_uri(self.world, uri)
+ if not c_node:
+ raise ValueError("Invalid URI '%s'" % uri)
+
+ return Node.wrap(self, c_node)
def new_file_uri(self, host, path):
"""Create a new file URI node. The host may be None."""
diff --git a/bindings/test/python/test_api.py b/bindings/test/python/test_api.py
index c3e0752..59fc201 100644
--- a/bindings/test/python/test_api.py
+++ b/bindings/test/python/test_api.py
@@ -63,8 +63,8 @@ class UriTests(unittest.TestCase):
self.world.load_all()
def testInvalidURI(self):
- self.plugin_uri = self.world.new_uri("invalid_uri")
- self.assertIsNone(self.plugin_uri)
+ with self.assertRaises(ValueError):
+ self.plugin_uri = self.world.new_uri("invalid_uri")
def testNonExistentURI(self):
self.plugin_uri = self.world.new_uri("exist:does_not")
@@ -116,6 +116,8 @@ class PluginClassesTests(unittest.TestCase):
self.assertGreater(len(classes), 1)
self.assertIsNotNone(classes[0])
self.assertIsNotNone(classes[pclass.get_uri()])
+ with self.assertRaises(KeyError):
+ classes["http://example.org/notaclass"].get_uri()
class LoadTests(unittest.TestCase):
@@ -151,6 +153,9 @@ class PluginTests(unittest.TestCase):
self.assertTrue(self.plugin in self.plugins)
self.assertTrue(self.plugin.get_uri() in self.plugins)
self.assertEqual(self.plugins[self.plugin.get_uri()], self.plugin)
+ with self.assertRaises(KeyError):
+ self.plugins["http://example.org/notaplugin"].get_uri()
+
self.assertIsNotNone(
self.plugin,
msg="Test plugin not found at location: '" + location + "'",
@@ -386,6 +391,9 @@ class UITests(unittest.TestCase):
)
self.assertEqual(1, len(uis))
self.assertEqual(str(uis[0]), str(ui_uri))
+ with self.assertRaises(KeyError):
+ uis["http://example.org/notaui"].get_uri()
+
self.assertEqual(uis[0], str(ui_uri))
self.assertEqual(uis[0].get_uri(), ui_uri)
self.assertEqual(uis[0].get_bundle_uri(), self.bundle_uri)