Commit 02ddaf29 authored by Thibault Saunier's avatar Thibault Saunier

structures: Override __new__ to make it more pythonic

parent 24156b0b
......@@ -224,6 +224,27 @@ Pipeline = override(Pipeline)
__all__.append('Pipeline')
class Structure(Gst.Structure):
def __new__(cls, *args, **kwargs):
if not args:
if kwargs:
raise TypeError("wrong arguments when creating GstStructure, first argument"
" must be the structure name.")
return Structure.new_empty()
elif len(args) > 1:
raise TypeError("wrong arguments when creating GstStructure object")
elif isinstance(args[0], str):
if not kwargs:
return Structure.from_string(args[0])[0]
struct = Structure.new_empty(args[0])
for k, v in kwargs.items():
struct[k] = v
return struct
elif isinstance(args[0], Structure):
return args[0].copy()
raise TypeError("wrong arguments when creating GstStructure object")
def __getitem__(self, key):
return self.get_value(key)
......
......@@ -76,5 +76,15 @@ class TestNotInitialized(TestCase):
Gst.ElementFactory.make("identity", None)
class TestStructure(TestCase):
def test_new(self):
Gst.init(None)
test = Gst.Structure('test', test=1)
self.assertEqual(test['test'], 1)
test = Gst.Structure('test,test=1')
self.assertEqual(test['test'], 1)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment