File: MimeTypesManager.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (357 lines) | stat: -rw-r--r-- 13,608 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#----------------------------------------------------------------------
# Name:        wxMimeTypesManager
# Purpose:     Demonstrate use of wx.MimeTypesManager, wx.FileType
#
# Author:      Jeff Grimmett (grimmtoo@softhome.net), adapted from original
#              .wdr-derived demo
#
# Created:     12/31/03
# RCS-ID:      $Id$
# Copyright:   
# Licence:     wxWindows license
#----------------------------------------------------------------------
#


import  pprint
import  wx
import  images


# helper function to make sure we don't convert unicode objects to strings
# or vice versa when converting lists and None values to text.
convert = str
if 'unicode' in wx.PlatformInfo:
   convert = unicode

#----------------------------------------------------------------------------

class MimeTypesDemoPanel(wx.Panel):
    def __init__(self, parent, log):
        
        self.log = log
        
        wx.Panel.__init__(self, parent, -1)

        # This will be used for all of the labels that follow (bold label)
        bfont = self.GetFont()
        bfont.SetWeight(wx.BOLD)
        
        # Contains everything
        tsizer = wx.BoxSizer(wx.VERTICAL)
        
        # Contains upper controls
        usizer = wx.BoxSizer(wx.HORIZONTAL)

        # Text control for ext / type entry plus label.
        t = wx.StaticText(self, -1, 'Extension / MIME type: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        usizer.Add(t, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 2)

        self.ext = wx.TextCtrl(self, -1, value="wav", style = wx.TE_PROCESS_ENTER )
        usizer.Add(self.ext, 0, wx.ALL | wx.ALIGN_TOP, 4)
        self.Bind(wx.EVT_TEXT_ENTER, self.OnLookup, self.ext)

        # Select how to look it up
        self.useExt = wx.RadioButton(self, -1, "By extension", style = wx.RB_GROUP)
        self.useMime = wx.RadioButton(self, -1, "By MIME type")

        usizer.Add(self.useExt, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4)
        usizer.Add(self.useMime, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4)
        self.useExt.SetValue(1)

        # Trigger a lookup (hitting ENTER in the text ctrl will do the same thing)
        self.go = wx.Button(self, -1, "Go get it!")
        usizer.Add(self.go, 0, wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL, 4)
        self.Bind(wx.EVT_BUTTON, self.OnLookup, self.go)

        # StaticBox with larger label than usual
        lbox = wx.StaticBox(self, -1, 'wx.FileType')

        f = self.GetFont()
        f.SetPointSize(f.GetPointSize() * 2)
        f.SetWeight(wx.BOLD)
        lbox.SetFont(f)

        lsizer = wx.StaticBoxSizer(lbox, wx.HORIZONTAL)

        # Contains the wx.FileType info
        llsizer = wx.GridBagSizer(2, 2)

        #------- Icon info

        t = wx.StaticText(self, -1, 'GetIconInfo: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (0, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.icon = wx.StaticBitmap(self, -1, images.NoIcon.GetBitmap())
        llsizer.Add(self.icon, (0, 1), (1, 1), wx.ALL | wx.ALIGN_CENTER, 2)

        self.iconsource = wx.TextCtrl(self, -1, value="", size=(125, -1), style = wx.TE_READONLY )
        llsizer.Add(self.iconsource, (0, 2), (1, 1), wx.ALL | wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 2)

        self.iconoffset = wx.TextCtrl(self, -1, value="", size=(25,-1), style = wx.TE_READONLY )
        llsizer.Add(self.iconoffset, (0, 3), (1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 2)

        #------- MIME Type

        t = wx.StaticText(self, -1, 'GetMimeType: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (1, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.mimetype = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY )
        llsizer.Add(self.mimetype, (1, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- MIME Types

        t = wx.StaticText(self, -1, 'GetMimeTypes: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (2, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.mimetypes = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY )
        llsizer.Add(self.mimetypes, (2, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- Extensions

        t = wx.StaticText(self, -1, 'GetExtensions: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (3, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.extensions = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY )
        llsizer.Add(self.extensions, (3, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- Description

        t = wx.StaticText(self, -1, 'GetDescription: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (4, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.description = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY)
        llsizer.Add(self.description, (4, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- Open command

        t = wx.StaticText(self, -1, 'GetOpenCommand: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (5, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.opencommand = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY )
        llsizer.Add(self.opencommand, (5, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- Print command

        t = wx.StaticText(self, -1, 'GetPrintCommand: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (6, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.printcommand = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY )
        llsizer.Add(self.printcommand, (6, 1), (1, 3), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        #------- All commands

        t = wx.StaticText(self, -1, 'GetAllCommands: ', style = wx.ALIGN_RIGHT )
        t.SetFont(bfont)
        llsizer.Add(t, (7, 0), (1, 1), wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 2)

        self.allcommands = wx.TextCtrl(self, -1, value="", style = wx.TE_READONLY | wx.TE_DONTWRAP | wx.TE_MULTILINE )

        # Set the default height to be smaller than normal (for
        # multi-line) so the sizer can then expand it to whatever
        # space is available
        self.allcommands.SetSize((-1, 20))
        
        llsizer.Add(self.allcommands, (7, 1), (1, 3), wx.ALL | wx.GROW | wx.ALIGN_CENTER, 2)

        # Tell the sizer to expand this row as needed
        llsizer.AddGrowableRow(7)
        llsizer.AddGrowableCol(2)

        #----------------------------------------------------------------------------

        lrsizer = wx.BoxSizer(wx.VERTICAL)
        
        #------- List box with known MIME types

        t = wx.StaticText(self, -1, 'Known MIME types')
        t.SetFont(bfont)
        lrsizer.Add(t, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4)

        self.mimelist = wx.ListBox(self, -1, choices=[], style = wx.LB_SINGLE)# | wx.LB_SORT)
        lrsizer.Add(self.mimelist, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER | wx.FIXED_MINSIZE, 4)
        self.Bind(wx.EVT_LISTBOX, self.OnListbox, self.mimelist)

        #----------------------------------------------------------------------------

        lsizer.Add(llsizer, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4)
        lsizer.Add(lrsizer, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4)

        #----------------------------------------------------------------------------

        tsizer.Add(usizer, 0, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4)
        tsizer.Add(lsizer, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER, 4)

        #----------------------------------------------------------------------------

        self.SetSizer(tsizer)
        tsizer.Fit(self)
        
        # Populate the Known MIME types list with what is in the database
        try:
            mtypes = wx.TheMimeTypesManager.EnumAllFileTypes()
        except wx.PyAssertionError:
            mtypes = []
        
        # TODO: On wxMac, EnumAllFileTypes produces tons of dupes, which
        # causes quirky behavior because the list control doesn't expect
        # dupes, and simply wastes space. So remove the dupes for now,
        # then remove this hack when we fix EnumAllFileTypes on Mac.
        mimes = []
        for mt in mtypes:
            if mt not in mimes:
                #self.mimelist.Append(mt)
                mimes.append(mt)
        if mimes:
            mimes.sort()
            self.mimelist.AppendItems(mimes)
            
        # Do a lookup of *.wav for a starting position
        self.OnLookup()

    # Grab the selection from the listbox, push that into
    # the text box at top, select 'MIME', and then look it up.
    def OnListbox(self, event):
        mimetype = event.GetString()
        self.ext.SetValue(mimetype)
        self.useMime.SetValue(1)
        self.OnLookup()

    # Look up a given file extension or MIME type.
    def OnLookup(self, event=None):
        txt = self.ext.GetValue()

        # For MIME lookups
        if self.useMime.GetValue() == 1:
            fileType = wx.TheMimeTypesManager.GetFileTypeFromMimeType(txt)
            msg = "Mime type"

            # Select the entered value in the list
            if fileType:
                if self.mimelist.FindString(txt) != -1:
                    self.mimelist.SetSelection(self.mimelist.FindString(txt))
        
        # Must be an extension lookup
        else:
            fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(txt)
            msg = "File extension"

            # Select the entered value in the list
            if fileType:
                if self.mimelist.FindString(convert(fileType.GetMimeType())) != -1:
                    # Using CallAfter to ensure that GUI is ready before trying to
                    # select it (otherwise, it's selected but not visible)
                    wx.CallAfter(self.mimelist.SetSelection, self.mimelist.FindString(convert(fileType.GetMimeType())))


        if fileType is None:
            wx.MessageBox(msg + " not found.", "Oops!")
        else:
            self.DoUpdate(fileType)

    # Populate the wx.FileType fields with actual values.
    def DoUpdate(self, ft):

        #------- Icon info
        info = ft.GetIconInfo()

        if info is None:
            bmp = images.NoIcon.GetBitmap()
            self.icon.SetBitmap(bmp)
            self.iconsource.SetValue("")
            self.iconoffset.SetValue("")
        else:
            icon, file, idx = info
            if icon.Ok():
                self.icon.SetIcon(icon)
            else:
                bmp = images.NoIcon.GetBitmap()
                self.icon.SetBitmap(bmp)                
            self.iconsource.SetValue(file)
            self.iconoffset.SetValue(convert(idx))

        #------- MIME type
        self.mimetype.SetValue(convert(ft.GetMimeType()))
        #------- MIME types
        self.mimetypes.SetValue(convert(ft.GetMimeTypes()))
        #------- Associated extensions
        self.extensions.SetValue(convert(ft.GetExtensions()))
        #------- Description of file type
        self.description.SetValue(convert(ft.GetDescription()))

        #------- Prep a fake command line command
        extList = ft.GetExtensions()

        if extList:
            ext = extList[0]
            if len(ext) > 0 and ext[0] == ".": ext = ext[1:]
        else:
            ext = ""

        filename = "SPAM" + "." + ext
        mime = ft.GetMimeType() or ""

        #------- OPEN command
        cmd = ft.GetOpenCommand(filename, mime)
        self.opencommand.SetValue(convert(cmd))

        #------- PRINT command
        cmd = ft.GetPrintCommand(filename, mime)
        self.printcommand.SetValue(convert(cmd))

        #------- All commands
        all = ft.GetAllCommands(filename, mime)
        
        if all is None:
            self.allcommands.SetValue("")
        else:
            verbs, commands = all
            text = pprint.pformat(map(None, verbs, commands))
            self.allcommands.SetValue(text)
            

#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = MimeTypesDemoPanel(nb, log)
    return win

#----------------------------------------------------------------------

overview = """\

The <b>wx.MimeTypesManager</b> class allows the application to retrieve the 
information about all known MIME types from a system-specific location and the 
filename extensions to the MIME types and vice versa. After initialization the 
methods <b>GetFileTypeFromMimeType()</b> and <b>GetFileTypeFromExtension()</b> 
may be called: they will return a <b>wx.FileType</b> object which may be further 
queried for file description, icon and other attributes.

A global instance of <b>wx.MimeTypesManager</b> is always available as
<b>wx.TheMimeTypesManager</b>. It is recommended to use this instance instead 
of creating your own because gathering MIME information may take quite a long 
on Unix systems.

This demo shows how to use wx.TheMimeTypesManager to list all known MIME types
and retrieve that information as a wx.FileType from either an extension or
MIME type.

For further information please consult the wxWindows documentation for
<b>wx.MimeTypesManager</b> and <b>wx.FileType</b>.

"""

#----------------------------------------------------------------------

if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])