From ee85ff894a36d3f3bd870e06738f75f71b89a8d0 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 18 May 2012 20:23:02 +0000 Subject: [PATCH] Add Mime classes git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@71484 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- TODO.txt | 1 - etg/_core.py | 1 + etg/mimetype.py | 181 +++++++++++++++++++++++++++++++++++++ unittests/test_mimetype.py | 45 +++++++++ 4 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 etg/mimetype.py create mode 100644 unittests/test_mimetype.py diff --git a/TODO.txt b/TODO.txt index a1af9845..625477f4 100644 --- a/TODO.txt +++ b/TODO.txt @@ -127,7 +127,6 @@ other dev stuff * axbase (ActiveX. Need to figure out best ways to do MSW-only items...) * check that all items in _functions.i and _misc.i have been wrapped * joystick - * mimetype * power * overlay * PseudoDC diff --git a/etg/_core.py b/etg/_core.py index 9b0861bd..bb646bcb 100644 --- a/etg/_core.py +++ b/etg/_core.py @@ -190,6 +190,7 @@ INCLUDES = [ # core 'dragimag', 'printfw', 'printdlg', + 'mimetype', ] diff --git a/etg/mimetype.py b/etg/mimetype.py new file mode 100644 index 00000000..cc15b2e0 --- /dev/null +++ b/etg/mimetype.py @@ -0,0 +1,181 @@ +#--------------------------------------------------------------------------- +# Name: etg/mimetype.py +# Author: Robin Dunn +# +# Created: 16-May-2012 +# Copyright: (c) 2012 by Total Control Software +# License: wxWindows License +#--------------------------------------------------------------------------- + +import etgtools +import etgtools.tweaker_tools as tools + +PACKAGE = "wx" +MODULE = "_core" +NAME = "mimetype" # Base name of the file to generate to for this script +DOCSTRING = "" + +# The classes and/or the basename of the Doxygen XML files to be processed by +# this script. +ITEMS = [ "wxFileType", + "wxFileTypeInfo", + "wxMimeTypesManager", + ] + +#--------------------------------------------------------------------------- + +def run(): + # Parse the XML file(s) building a collection of Extractor objects + module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING) + etgtools.parseDoxyXML(module, ITEMS) + + #----------------------------------------------------------------- + # Tweak the parsed meta objects in the module object as needed for + # customizing the generated code and docstrings. + + c = module.find('wxFileType') + assert isinstance(c, etgtools.ClassDef) + c.addPrivateCopyCtor() + c.addPublic() + + # Change semantics for some methods to return values instead of using + # output parameters. This is for Classic compatibility as well as being a + # bit more pythonic. + c.find('GetDescription').ignore() + c.addCppMethod('wxString', 'GetDescription', '()', + doc="""\ + Returns a brief description for this file type: for example, "text document" for + the "text/plain" MIME type.""", + body="""\ + wxString rv; + self->GetDescription(&rv); + return new wxString(rv); + """) + + c.find('GetExtensions').ignore() + c.addCppMethod('wxArrayString*', 'GetExtensions', '()', + factory=True, + doc="""\ + Returns all extensions associated with this file type: for + example, it may contain the following two elements for the MIME + type "text/html" (notice the absence of the leading dot): "html" + and "htm". + + This function is not implemented on Windows, there is no (efficient) + way to retrieve associated extensions from the given MIME type on + this platform. """, + body="""\ + wxArrayString* arr = new wxArrayString; + self->GetExtensions(*arr); return arr; + """) + + c.find('GetMimeType').ignore() + c.addCppMethod('wxString', 'GetMimeType', '()', + doc='Returns full MIME type specification for this file type: for example, "text/plain".', + body="""\ + wxString rv; + self->GetMimeType(&rv); + return new wxString(rv); + """) + + c.find('GetMimeTypes').ignore() + c.addCppMethod('wxArrayString*', 'GetMimeTypes', '()', + factory=True, + doc="""\ + Same as GetMimeType but returns a list of types. This will usually contain + only one item, but sometimes, such as on Unix with KDE more than one type + if there are differences between KDE< mailcap and mime.types.""", + body="""\ + wxArrayString* arr = new wxArrayString; + self->GetMimeTypes(*arr); + return arr; + """) + + + c.find('GetIcon').ignore() + c.addCppMethod('wxIcon*', 'GetIcon', '()', + factory = True, + doc="Return the icon associated with this mime type, if any.", + body="""\ + wxIconLocation loc; + if (self->GetIcon(&loc)) + return new wxIcon(loc); + else + return NULL; + """) + c.addCppMethod('wxIconLocation*', 'GetIconLocation', '()', + factory = True, + doc="Returns a wx.IconLocation that can be used to fetch the icon for this mime type.", + body="""\ + wxIconLocation loc; + if (self->GetIcon(&loc)) + return new wxIconLocation(loc); + else + return NULL; + """) + + + c.find('GetOpenCommand').findOverload('command').ignore() + c.addCppMethod('wxString', 'GetOpenCommand', '(const wxFileType::MessageParameters& params)', + doc="""\ + Returns the command which must be executed (see wx.Execute()) in order + to open the file of the given type. The name of the file as well as + any other parameters is retrieved from MessageParameters() class.""", + body="""\ + wxString rv; + self->GetOpenCommand(&rv, *params); + return new wxString(rv); + """) + + c.find('GetPrintCommand').ignore() + c.addCppMethod('wxString', 'GetPrintCommand', '(const wxFileType::MessageParameters& params)', + doc="""\ + Returns the command which must be executed (see wxExecute()) in order to + print the file of the given type. The name of the file is retrieved from + the MessageParameters class.""", + body="""\ + wxString rv; + self->GetPrintCommand(&rv, *params); + return new wxString(rv); + """) + + c.find('GetAllCommands.verbs').out = True + c.find('GetAllCommands.commands').out = True + + + #----------------------------------------------------------------- + c = module.find('wxFileTypeInfo') + + # Ignore the last two args for this ctor as the varargs is causing some + # trouble. TODO: Figure this out... + ctor = c.find('wxFileTypeInfo').findOverload('extension') + ctor.items[-1].ignore() + ctor.items[-2].ignore() + + + #----------------------------------------------------------------- + c = module.find('wxMimeTypesManager') + c.addPrivateCopyCtor() + + c.find('Associate').factory = True + + c.find('EnumAllFileTypes').ignore() + c.addCppMethod('wxArrayString*', 'EnumAllFileTypes', '()', + factory=True, + doc="Returns a list of all known file types.", + body="""\ + wxArrayString* arr = new wxArrayString; + self->EnumAllFileTypes(*arr); + return arr; + """) + + + #----------------------------------------------------------------- + tools.doCommonTweaks(module) + tools.runGenerators(module) + + +#--------------------------------------------------------------------------- +if __name__ == '__main__': + run() + diff --git a/unittests/test_mimetype.py b/unittests/test_mimetype.py new file mode 100644 index 00000000..35b1f616 --- /dev/null +++ b/unittests/test_mimetype.py @@ -0,0 +1,45 @@ +import imp_unittest, unittest +import wtc +import wx + +#--------------------------------------------------------------------------- + +class mimetype_Tests(wtc.WidgetTestCase): + + def test_mimetype1(self): + ft = wx.TheMimeTypesManager.GetFileTypeFromExtension('*.pdf') + ft.GetExtensions() + ft.GetMimeType() + ft.GetIcon() + + def test_mimetype2(self): + ft = wx.TheMimeTypesManager.GetFileTypeFromMimeType('image/png') + ft.GetExtensions() + ft.GetMimeType() + ft.GetIcon() + + def test_mimetype3(self): + fti = wx.FileTypeInfo('mime', 'open', 'print', 'desc') + fti.AddExtension('ext1') + fti.AddExtension('ext2') + fti.AddExtension('ext3') + self.assertEqual(fti.GetMimeType(), 'mime') + self.assertEqual(fti.GetOpenCommand(), 'open') + self.assertEqual(fti.GetPrintCommand(), 'print') + self.assertEqual(fti.GetDescription(), 'desc') + self.assertEqual(fti.GetExtensions(), ['ext1', 'ext2', 'ext3']) + self.assertEqual(fti.GetExtensionsCount(), 3) + + def test_mimetype4(self): + fti = wx.FileTypeInfo(['mime', 'open', 'print', 'desc', 'ext1', 'ext2', 'ext3']) + self.assertEqual(fti.GetMimeType(), 'mime') + self.assertEqual(fti.GetOpenCommand(), 'open') + self.assertEqual(fti.GetPrintCommand(), 'print') + self.assertEqual(fti.GetDescription(), 'desc') + self.assertEqual(fti.GetExtensions(), ['ext1', 'ext2', 'ext3']) + self.assertEqual(fti.GetExtensionsCount(), 3) + +#--------------------------------------------------------------------------- + +if __name__ == '__main__': + unittest.main()