[vlc-devel] commit: python-ctypes: improve enum conversion (Olivier Aubert )

git version control git at videolan.org
Fri Sep 4 15:43:36 CEST 2009


vlc | branch: master | Olivier Aubert <olivier.aubert at liris.cnrs.fr> | Fri Sep  4 15:40:09 2009 +0200| [a7a9cc09d42472ffbf099aab66bacb4fb999b175] | committer: Olivier Aubert 

python-ctypes: improve enum conversion

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

 bindings/python-ctypes/generate.py |   22 ++++++++++++-------
 bindings/python-ctypes/header.py   |   41 +++++++++++++++++++++++++++--------
 bindings/python-ctypes/override.py |   17 +++++++++-----
 bindings/python-ctypes/test.py     |   23 ++++++++++++++++++++
 4 files changed, 79 insertions(+), 24 deletions(-)

diff --git a/bindings/python-ctypes/generate.py b/bindings/python-ctypes/generate.py
index abeadf8..f1fdf05 100755
--- a/bindings/python-ctypes/generate.py
+++ b/bindings/python-ctypes/generate.py
@@ -402,7 +402,6 @@ class PythonGenerator(object):
             self.fd=open(filename, 'w')
 
         self.insert_code('header.py')
-        self.generate_enums(self.parser.enums)
         wrapped_methods=self.generate_wrappers(self.parser.methods)
         for l in self.parser.methods:
             self.output_ctypes(*l)
@@ -437,10 +436,13 @@ class PythonGenerator(object):
         """
         f=open(filename, 'r')
         for l in f:
-            if 'build_date' in l:
+            if l.startswith('build_date'):
                 self.output('build_date="%s"' % time.ctime())
+            elif l.startswith('# GENERATED_ENUMS'):
+                self.generate_enums(self.parser.enums)
             else:
                 self.output(l.rstrip())
+
         f.close()
 
     def convert_enum_names(self, enums):
@@ -462,7 +464,7 @@ class PythonGenerator(object):
                 raise Exception('This method only handles enums')
             pyname=self.type2class[name]
 
-            self.output("class %s(ctypes.c_uint):" % pyname)
+            self.output("class %s(ctypes.c_ulong):" % pyname)
             self.output('    """%s\n    """' % comment)
 
             conv={}
@@ -477,9 +479,6 @@ class PythonGenerator(object):
                     n='_'+n
                 conv[k]=n
 
-            for k, v in values:
-                self.output("    %s=ctypes.c_uint(%s)" % (conv[k], v))
-
             self.output("    _names={")
             for k, v in values:
                 self.output("        %s: '%s'," % (v, conv[k]))
@@ -490,12 +489,19 @@ class PythonGenerator(object):
         return ".".join((self.__class__.__module__, self.__class__.__name__, self._names[self.value]))
 
     def __eq__(self, other):
-        return (isinstance(other, ctypes.c_uint) and self.value == other.value)
+        return ( (isinstance(other, ctypes.c_ulong) and self.value == other.value)
+                 or (isinstance(other, (int, long)) and self.value == other ) )
 
     def __ne__(self, other):
         return not self.__eq__(other)
-
     """)
+            for k, v in values:
+                self.output("%(class)s.%(attribute)s=%(class)s(%(value)s)" % {
+                        'class': pyname,
+                        'attribute': conv[k],
+                        'value': v
+                        })
+            self.output("")
 
     def output_ctypes(self, rtype, method, params, comment):
         """Output ctypes decorator for the given method.
diff --git a/bindings/python-ctypes/header.py b/bindings/python-ctypes/header.py
index fcaaaae..cc95119 100755
--- a/bindings/python-ctypes/header.py
+++ b/bindings/python-ctypes/header.py
@@ -76,6 +76,16 @@ elif sys.platform == 'darwin':
     # FIXME: should find a means to configure path
     dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib')
 
+#
+# Generated enum types.
+#
+
+# GENERATED_ENUMS
+
+#
+# End of generated enum types.
+#
+
 class ListPOINTER(object):
     '''Just like a POINTER but accept a list of ctype as an argument.
     '''
@@ -157,24 +167,35 @@ class LogMessage(ctypes.Structure):
 
 class MediaControlPosition(ctypes.Structure):
     _fields_= [
-                ('origin', ctypes.c_int),
-                ('key', ctypes.c_int),
+                ('origin', PositionOrigin),
+                ('key', PositionKey),
                 ('value', ctypes.c_longlong),
                 ]
 
+    def __init__(self, value=0, origin=None, key=None):
+        # We override the __init__ method so that instanciating the
+        # class with an int as parameter will create the appropriate
+        # default position (absolute position, media time, with the
+        # int as value).
+        self.value=value
+        if origin is None:
+            origin=PositionOrigin.AbsolutePosition
+        if key is None:
+            key=PositionKey.MediaTime
+        self.origin=origin
+        self.key=key
+
     def __str__(self):
         return "MediaControlPosition %ld (%s, %s)" % (
             self.value,
-            str(PositionOrigin(self.origin)),
-            str(PositionKey(self.key))
+            str(self.origin),
+            str(self.key)
             )
 
     @staticmethod
     def from_param(arg):
         if isinstance(arg, (int, long)):
-            p=MediaControlPosition()
-            p.value=arg
-            p.key=2
+            p=MediaControlPosition(arg)
             return p
         else:
             return arg
@@ -192,15 +213,15 @@ class MediaControlException(ctypes.Structure):
 
 class MediaControlStreamInformation(ctypes.Structure):
     _fields_= [
-                ('status', ctypes.c_int),
+                ('status', PlayerStatus),
                 ('url', ctypes.c_char_p),
                 ('position', ctypes.c_longlong),
                 ('length', ctypes.c_longlong),
                 ]
 
     def __str__(self):
-        return "%s (%s) : %ld / %ld" % (self.url,
-                                        str(PlayerStatus(self.status)),
+        return "%s (%s) : %ld / %ld" % (self.url or "<No defined URL>",
+                                        str(self.status),
                                         self.position,
                                         self.length)
 
diff --git a/bindings/python-ctypes/override.py b/bindings/python-ctypes/override.py
index 61ddd1e..729f500 100644
--- a/bindings/python-ctypes/override.py
+++ b/bindings/python-ctypes/override.py
@@ -91,7 +91,7 @@ class MediaControl:
         @param pos: a MediaControlPosition or an integer (in ms)
         """
         if not isinstance(pos, MediaControlPosition):
-            pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos))
+            pos=MediaControlPosition(long(pos))
         e=MediaControlException()
         mediacontrol_set_media_position(self, pos, e)
 
@@ -101,7 +101,7 @@ class MediaControl:
         @param pos: a MediaControlPosition or an integer (in ms)
         """
         if not isinstance(pos, MediaControlPosition):
-            pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos))
+            pos=MediaControlPosition(long(pos))
         e=MediaControlException()
         mediacontrol_start(self, pos, e)
 
@@ -115,11 +115,16 @@ class MediaControl:
         @param pos: a MediaControlPosition or an integer (in ms)
         """
         if not isinstance(pos, MediaControlPosition):
-            pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos))
+            pos=MediaControlPosition(long(pos))
         e=MediaControlException()
         p=mediacontrol_snapshot(self, pos, e)
         if p:
-            return p.contents
+            snap=p.contents
+            # FIXME: there is a bug in the current mediacontrol_snapshot
+            # implementation, which sets an incorrect date.
+            # Workaround here:
+            snap.date=self.get_media_position().value
+            return snap
         else:
             return None
 
@@ -131,9 +136,9 @@ class MediaControl:
         @param end: the end position
         """
         if not isinstance(begin, MediaControlPosition):
-            begin=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(begin))
+            begin=self.value2position(pos)
         if not isinstance(end, MediaControlPosition):
-            begin=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(end))
+            end=self.value2position(end)
         e=MediaControlException()
         mediacontrol_display_text(self, message, begin, end, e)
 
diff --git a/bindings/python-ctypes/test.py b/bindings/python-ctypes/test.py
index 02622f3..7675fed 100755
--- a/bindings/python-ctypes/test.py
+++ b/bindings/python-ctypes/test.py
@@ -84,6 +84,29 @@ class TestVLCAPI(unittest.TestCase):
         mc.set_mrl(mrl)
         self.assertEqual(mc.get_mrl(), mrl)
 
+    def test_mediacontrol_position(self):
+        p=vlc.MediaControlPosition(value=2,
+                                   origin=vlc.PositionOrigin.RelativePosition,
+                                   key=vlc.PositionKey.MediaTime)
+        self.assertEqual(p.value, 2)
+
+    def test_mediacontrol_position_shortcut(self):
+        p=vlc.MediaControlPosition(2)
+        self.assertEqual(p.value, 2)
+        self.assertEqual(p.key, vlc.PositionKey.MediaTime)
+        self.assertEqual(p.origin, vlc.PositionOrigin.AbsolutePosition)
+
+    def test_mediacontrol_get_media_position(self):
+        mc=vlc.MediaControl()
+        p=mc.get_media_position()
+        self.assertEqual(p.value, -1)
+
+    def test_mediacontrol_get_stream_information(self):
+        mc=vlc.MediaControl()
+        s=mc.get_stream_information()
+        self.assertEqual(s.position, 0)
+        self.assertEqual(s.length, 0)
+
     # Basic libvlc tests
     def test_instance_creation(self):
         i=vlc.Instance()




More information about the vlc-devel mailing list