wine: revert a patch that breaks icon rendering in sugared wine
[sugaredwine.git] / filenames.py
blob9e2c28dfeb912ef1a5dbfb10e82c4c059cf05174
1 # Copyright (C) 2008, Vincent Povirk for CodeWeavers
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
18 # Sugar's metadata does not typically provide a file extension, and the
19 # filename tends to be unusable. Windows depends on extensions so we need
20 # a conversion of metadata -> usable filenames.
22 from sugar.activity import activity
23 import os
25 settings = {}
26 f = open(os.path.join(activity.get_bundle_path(), 'activity', 'wine.info'), 'U')
27 for line in f.readlines():
28 if '=' in line:
29 key, value = line.rstrip('\n').split('=')
30 settings[key] = value
31 f.close()
33 def guess_extension(metadata):
34 #try wine.info settings
35 for key in settings:
36 if key.startswith('.'):
37 if metadata['mime_type'] in settings[key].split(';'):
38 return key
40 #then try python's standard library
41 import mimetypes
42 guess = mimetypes.guess_extension(metadata['mime_type'])
43 if guess: return guess
45 #ok, maybe it's an application/x-extension- type?
46 if metadata['mime_type'].startswith(['application/x-extension-']):
47 return '.%s' % metadata['mime_type'][len('application/x-extension-'):]
49 #no? let's call it quits then
50 return ''
52 def guess_filename(metadata):
53 def sanitize_filename(string):
54 for character in r'\/:*?"<>|':
55 string = string.replace(character, '_')
56 return string
58 try:
59 # trivial case: someone told us what filename to use
60 return sanitize_filename(metadata['suggested_filename'])
61 except KeyError:
62 ext = guess_extension(metadata)
63 if ext:
64 # if there is a word that ends with the extension, use it
65 # this does the right thing for downloads in English, at least
66 for word in metadata['title'].split():
67 if word.endswith(ext):
68 return sanitize_filename(word)
69 # otherwise, just use the title and extension
70 return '%s%s' % (sanitize_filename(metadata['title']), ext)
71 else:
72 return sanitize_filename(metadata['title'])
74 def create_dsobject_file(metadata, tmpdir=None):
75 import tempfile
77 if tmpdir is None:
78 tmpdir = tempfile.gettempdir()
80 filename = guess_filename(metadata)
82 try:
83 path = os.path.join(tmpdir, filename)
84 fd = os.open(path, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0770)
85 except OSError:
86 fd, path = tempfile.mkstemp(suffix=filename, dir=tmpdir)
88 return fd, path