structures: Override __new__ to make it more pythonic

This commit is contained in:
Thibault Saunier 2017-07-24 12:13:13 -04:00
parent 24156b0b0d
commit 02ddaf29f9
2 changed files with 31 additions and 0 deletions

View file

@ -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)

View file

@ -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()