]> git.sesse.net Git - vlc/blob - bindings/python/vlcwrapper.py
python/bindings/vlc_object.c: complete vlc_object_find parameters
[vlc] / bindings / python / vlcwrapper.py
1 """Wrapper around vlc module in order to ease the use of vlc.Object
2 class (completion in ipython, access variable as attributes, etc).
3 """
4 import vlc
5
6 class VLCObject(object):
7     def __init__(self, id):
8         object.__setattr__(self, '_o', vlc.Object(id))
9
10     def find(self, typ):
11         """Returns a VLCObject for the given child.
12
13         See vlc.Object.find_object.__doc__ for the different values of typ.
14         """
15         t=self._o.find_object(typ)
16         if t is not None:
17             return VLCObject(t.info()['object-id'])
18         else:
19             return None
20
21     def __str__(self):
22         """Returns a string representation of the object.
23         """
24         i=self._o.info()
25         return "VLCObject %d (%s) : %s" % (i['object-id'],
26                                            i['object-type'],
27                                            i['object-name'])
28
29     def tree(self, prefix=" "):
30         """Displays all children as a tree of VLCObject
31         """
32         res=prefix + str(self) + "\n"
33         for i in self._o.children():
34             t=VLCObject(i)
35             res += t.tree(prefix=prefix + " ")
36         return res
37
38     def __getattribute__(self, attr):
39         """Converts attribute access to access to variables.
40         """
41         if attr == '__members__':
42             # Return the list of variables
43             o=object.__getattribute__(self, '_o')
44             l=dir(o)
45             l.extend([ n.replace('-','_') for n in o.list() ])
46             return l
47         try:
48             return object.__getattribute__ (self, attr)
49         except AttributeError, e:
50             try:
51                 return self._o.__getattribute__ (attr)
52             except AttributeError, e:
53                 attr=attr.replace('_', '-')
54                 if attr in self._o.list():
55                     return self._o.get(attr)
56                 else:
57                     raise e
58
59     def __setattr__(self, name, value):
60         """Handle attribute assignment.
61         """
62         n=name.replace('_', '-')
63         if n in self._o.list():
64             self._o.set(n, value)
65         else:
66             object.__setattr__(self, name, value)
67
68 def test(f='/tmp/k.mpg'):
69     global mc,o
70     mc=vlc.MediaControl()
71     mc.playlist_add_item(f)
72     mc.start(0)
73     mc.pause(0)
74     o=VLCObject(0)
75     v=o.find('vout')
76