Commit a7a9cc09 authored by Olivier Aubert's avatar Olivier Aubert

python-ctypes: improve enum conversion

parent e826f642
...@@ -402,7 +402,6 @@ class PythonGenerator(object): ...@@ -402,7 +402,6 @@ class PythonGenerator(object):
self.fd=open(filename, 'w') self.fd=open(filename, 'w')
self.insert_code('header.py') self.insert_code('header.py')
self.generate_enums(self.parser.enums)
wrapped_methods=self.generate_wrappers(self.parser.methods) wrapped_methods=self.generate_wrappers(self.parser.methods)
for l in self.parser.methods: for l in self.parser.methods:
self.output_ctypes(*l) self.output_ctypes(*l)
...@@ -437,10 +436,13 @@ class PythonGenerator(object): ...@@ -437,10 +436,13 @@ class PythonGenerator(object):
""" """
f=open(filename, 'r') f=open(filename, 'r')
for l in f: for l in f:
if 'build_date' in l: if l.startswith('build_date'):
self.output('build_date="%s"' % time.ctime()) self.output('build_date="%s"' % time.ctime())
elif l.startswith('# GENERATED_ENUMS'):
self.generate_enums(self.parser.enums)
else: else:
self.output(l.rstrip()) self.output(l.rstrip())
f.close() f.close()
def convert_enum_names(self, enums): def convert_enum_names(self, enums):
...@@ -462,7 +464,7 @@ class PythonGenerator(object): ...@@ -462,7 +464,7 @@ class PythonGenerator(object):
raise Exception('This method only handles enums') raise Exception('This method only handles enums')
pyname=self.type2class[name] 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) self.output(' """%s\n """' % comment)
conv={} conv={}
...@@ -477,9 +479,6 @@ class PythonGenerator(object): ...@@ -477,9 +479,6 @@ class PythonGenerator(object):
n='_'+n n='_'+n
conv[k]=n conv[k]=n
for k, v in values:
self.output(" %s=ctypes.c_uint(%s)" % (conv[k], v))
self.output(" _names={") self.output(" _names={")
for k, v in values: for k, v in values:
self.output(" %s: '%s'," % (v, conv[k])) self.output(" %s: '%s'," % (v, conv[k]))
...@@ -490,12 +489,19 @@ class PythonGenerator(object): ...@@ -490,12 +489,19 @@ class PythonGenerator(object):
return ".".join((self.__class__.__module__, self.__class__.__name__, self._names[self.value])) return ".".join((self.__class__.__module__, self.__class__.__name__, self._names[self.value]))
def __eq__(self, other): 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): def __ne__(self, other):
return not self.__eq__(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): def output_ctypes(self, rtype, method, params, comment):
"""Output ctypes decorator for the given method. """Output ctypes decorator for the given method.
......
...@@ -76,6 +76,16 @@ elif sys.platform == 'darwin': ...@@ -76,6 +76,16 @@ elif sys.platform == 'darwin':
# FIXME: should find a means to configure path # FIXME: should find a means to configure path
dll=ctypes.CDLL('/Applications/VLC.app/Contents/MacOS/lib/libvlc.2.dylib') 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): class ListPOINTER(object):
'''Just like a POINTER but accept a list of ctype as an argument. '''Just like a POINTER but accept a list of ctype as an argument.
''' '''
...@@ -157,24 +167,35 @@ class LogMessage(ctypes.Structure): ...@@ -157,24 +167,35 @@ class LogMessage(ctypes.Structure):
class MediaControlPosition(ctypes.Structure): class MediaControlPosition(ctypes.Structure):
_fields_= [ _fields_= [
('origin', ctypes.c_int), ('origin', PositionOrigin),
('key', ctypes.c_int), ('key', PositionKey),
('value', ctypes.c_longlong), ('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): def __str__(self):
return "MediaControlPosition %ld (%s, %s)" % ( return "MediaControlPosition %ld (%s, %s)" % (
self.value, self.value,
str(PositionOrigin(self.origin)), str(self.origin),
str(PositionKey(self.key)) str(self.key)
) )
@staticmethod @staticmethod
def from_param(arg): def from_param(arg):
if isinstance(arg, (int, long)): if isinstance(arg, (int, long)):
p=MediaControlPosition() p=MediaControlPosition(arg)
p.value=arg
p.key=2
return p return p
else: else:
return arg return arg
...@@ -192,15 +213,15 @@ class MediaControlException(ctypes.Structure): ...@@ -192,15 +213,15 @@ class MediaControlException(ctypes.Structure):
class MediaControlStreamInformation(ctypes.Structure): class MediaControlStreamInformation(ctypes.Structure):
_fields_= [ _fields_= [
('status', ctypes.c_int), ('status', PlayerStatus),
('url', ctypes.c_char_p), ('url', ctypes.c_char_p),
('position', ctypes.c_longlong), ('position', ctypes.c_longlong),
('length', ctypes.c_longlong), ('length', ctypes.c_longlong),
] ]
def __str__(self): def __str__(self):
return "%s (%s) : %ld / %ld" % (self.url, return "%s (%s) : %ld / %ld" % (self.url or "<No defined URL>",
str(PlayerStatus(self.status)), str(self.status),
self.position, self.position,
self.length) self.length)
......
...@@ -91,7 +91,7 @@ class MediaControl: ...@@ -91,7 +91,7 @@ class MediaControl:
@param pos: a MediaControlPosition or an integer (in ms) @param pos: a MediaControlPosition or an integer (in ms)
""" """
if not isinstance(pos, MediaControlPosition): if not isinstance(pos, MediaControlPosition):
pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos)) pos=MediaControlPosition(long(pos))
e=MediaControlException() e=MediaControlException()
mediacontrol_set_media_position(self, pos, e) mediacontrol_set_media_position(self, pos, e)
...@@ -101,7 +101,7 @@ class MediaControl: ...@@ -101,7 +101,7 @@ class MediaControl:
@param pos: a MediaControlPosition or an integer (in ms) @param pos: a MediaControlPosition or an integer (in ms)
""" """
if not isinstance(pos, MediaControlPosition): if not isinstance(pos, MediaControlPosition):
pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos)) pos=MediaControlPosition(long(pos))
e=MediaControlException() e=MediaControlException()
mediacontrol_start(self, pos, e) mediacontrol_start(self, pos, e)
...@@ -115,11 +115,16 @@ class MediaControl: ...@@ -115,11 +115,16 @@ class MediaControl:
@param pos: a MediaControlPosition or an integer (in ms) @param pos: a MediaControlPosition or an integer (in ms)
""" """
if not isinstance(pos, MediaControlPosition): if not isinstance(pos, MediaControlPosition):
pos=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(pos)) pos=MediaControlPosition(long(pos))
e=MediaControlException() e=MediaControlException()
p=mediacontrol_snapshot(self, pos, e) p=mediacontrol_snapshot(self, pos, e)
if p: 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: else:
return None return None
...@@ -131,9 +136,9 @@ class MediaControl: ...@@ -131,9 +136,9 @@ class MediaControl:
@param end: the end position @param end: the end position
""" """
if not isinstance(begin, MediaControlPosition): 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): if not isinstance(end, MediaControlPosition):
begin=MediaControlPosition(origin=PositionOrigin.AbsolutePosition, key=PositionKey.MediaTime, value=long(end)) end=self.value2position(end)
e=MediaControlException() e=MediaControlException()
mediacontrol_display_text(self, message, begin, end, e) mediacontrol_display_text(self, message, begin, end, e)
......
...@@ -84,6 +84,29 @@ class TestVLCAPI(unittest.TestCase): ...@@ -84,6 +84,29 @@ class TestVLCAPI(unittest.TestCase):
mc.set_mrl(mrl) mc.set_mrl(mrl)
self.assertEqual(mc.get_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 # Basic libvlc tests
def test_instance_creation(self): def test_instance_creation(self):
i=vlc.Instance() i=vlc.Instance()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment