mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-27 04:01:08 +00:00
structures: Override __new__ to make it more pythonic
This commit is contained in:
parent
24156b0b0d
commit
02ddaf29f9
2 changed files with 31 additions and 0 deletions
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue