[vlc-commits] python bindings: implement vlc. get_default_instance method and allow to instanciate vlc.Media

Olivier Aubert git at videolan.org
Wed May 18 11:01:49 CEST 2011


vlc/python | branch: master | Olivier Aubert <olivier.aubert at liris.cnrs.fr> | Sun May 15 17:48:03 2011 +0200| [8c055d1c92b83b69b977a14e03ca944142673eff] | committer: Olivier Aubert

python bindings: implement vlc.get_default_instance method and allow to instanciate vlc.Media

This can potentially introduce inconsistencies (different Instances for associated objects such as Media and MediaPlayer). Users are assumed to know what they are doing if they use their own Instance and the lower-level methods.

> http://git.videolan.org/gitweb.cgi/vlc/python.git/?a=commit;h=8c055d1c92b83b69b977a14e03ca944142673eff
---

 generated/vlc.py |   41 ++++++++++++++++++++++++++++++++++-------
 header.py        |   12 ++++++++++++
 override.py      |   24 ++++++++++++++++++++++--
 test.py          |    5 +++++
 4 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/generated/vlc.py b/generated/vlc.py
index 611dbe0..24d2de9 100755
--- a/generated/vlc.py
+++ b/generated/vlc.py
@@ -47,7 +47,7 @@ import sys
 from inspect import getargspec
 
 __version__ = "N/A"
-build_date  = "Sun May 15 17:22:02 2011"
+build_date  = "Sun May 15 17:44:33 2011"
 
  # Used on win32 and MacOS in override.py
 plugin_path = None
@@ -123,6 +123,18 @@ try:
 except NameError:  # no long in Python 3+
     _Ints =  int
 
+# Default instance. It is used to instanciate classes directly in the
+# OO-wrapper.
+_default_instance = None
+
+def get_default_instance():
+    """Return the default VLC.Instance.
+    """
+    global _default_instance
+    if _default_instance is None:
+        _default_instance = Instance()
+    return _default_instance
+
 _Seqs = (list, tuple)
 
 _Cfunctions = {}  # from LibVLC __version__
@@ -893,6 +905,7 @@ class Instance(_Ctype):
         m = libvlc_media_new_location(self, mrl)
         for o in options:
             libvlc_media_add_option(m, o)
+        m._instance = self
         return m
 
     def audio_output_enumerate_devices(self):
@@ -1371,13 +1384,27 @@ class LogIterator(_Ctype):
 class Media(_Ctype):
     '''Create a new Media instance.
     
+    Usage: Media(MRL, *options)
+
+    See vlc.Instance.media_new documentation for details.
+    
     '''
 
-    def __new__(cls, ptr=None):
-        '''(INTERNAL) ctypes wrapper constructor.
-        '''
-        return _Constructor(cls, ptr)
+    def __new__(cls, *args):
+        if args:
+            i = args[0]
+            if i == 0:
+                return None
+            if isinstance(i, _Ints):
+                return _Cobject(cls, ctypes.c_void_p(i))
+            if isinstance(i, Instance):
+                return i.media_new()
 
+        o = get_default_instance().media_new(*args)
+        return o
+
+    def get_instance(self):
+        return getattr(self, '_instance', None)
 
     def add_options(self, *options):
         """Add a list of options to the media.
@@ -1776,7 +1803,7 @@ class MediaListPlayer(_Ctype):
         if args and isinstance(args[0], Instance):
             i = args[0]
         else:
-            i = Instance()
+            i = get_default_instance()
         return i.media_list_player_new()
 
     def get_instance(self):
@@ -1893,7 +1920,7 @@ class MediaPlayer(_Ctype):
             if isinstance(i, Instance):
                 return i.media_player_new()
 
-        i = Instance()
+        i = get_default_instance()
         o = i.media_player_new()
         if args:
             o.set_media(i.media_new(*args))  # args[0]
diff --git a/header.py b/header.py
index 3f0d303..774ae62 100755
--- a/header.py
+++ b/header.py
@@ -122,6 +122,18 @@ try:
 except NameError:  # no long in Python 3+
     _Ints =  int
 
+# Default instance. It is used to instanciate classes directly in the
+# OO-wrapper.
+_default_instance = None
+
+def get_default_instance():
+    """Return the default VLC.Instance.
+    """
+    global _default_instance
+    if _default_instance is None:
+        _default_instance = Instance()
+    return _default_instance
+
 _Seqs = (list, tuple)
 
 _Cfunctions = {}  # from LibVLC __version__
diff --git a/override.py b/override.py
index 42b3c90..0bbd94b 100644
--- a/override.py
+++ b/override.py
@@ -62,6 +62,7 @@ class Instance:
         m = libvlc_media_new_location(self, mrl)
         for o in options:
             libvlc_media_add_option(m, o)
+        m._instance = self
         return m
 
     def audio_output_enumerate_devices(self):
@@ -103,7 +104,26 @@ class Instance:
 
 class Media:
     """Create a new Media instance.
+    
+    Usage: Media(MRL, *options)
+
+    See vlc.Instance.media_new documentation for details.
     """
+    def __new__(cls, *args):
+        if args:
+            i = args[0]
+            if i == 0:
+                return None
+            if isinstance(i, _Ints):
+                return _Cobject(cls, ctypes.c_void_p(i))
+            if isinstance(i, Instance):
+                return i.media_new()
+
+        o = get_default_instance().media_new(*args)
+        return o
+
+    def get_instance(self):
+        return getattr(self, '_instance', None)
 
     def add_options(self, *options):
         """Add a list of options to the media.
@@ -138,7 +158,7 @@ class MediaPlayer:  #PYCHOK expected (comment is lost)
             if isinstance(i, Instance):
                 return i.media_player_new()
 
-        i = Instance()
+        i = get_default_instance()
         o = i.media_player_new()
         if args:
             o.set_media(i.media_new(*args))  # args[0]
@@ -269,7 +289,7 @@ class MediaListPlayer:
         if args and isinstance(args[0], Instance):
             i = args[0]
         else:
-            i = Instance()
+            i = get_default_instance()
         return i.media_list_player_new()
 
     def get_instance(self):
diff --git a/test.py b/test.py
index 05aee5b..826952d 100755
--- a/test.py
+++ b/test.py
@@ -69,6 +69,11 @@ class TestVLCAPI(unittest.TestCase):
         m=i.media_new(mrl)
         self.assertEqual(m.get_mrl(), mrl)
 
+    def test_wrapper_media(self):
+        mrl = '/tmp/foo.avi'
+        m = vlc.Media(mrl)
+        self.assertEqual(m.get_mrl(), mrl)
+
     def test_libvlc_player(self):
         mrl='/tmp/foo.avi'
         i=vlc.Instance()



More information about the vlc-commits mailing list