1207
by William Grant
Move ivle.conf.mimetypes to ivle.mimetypes, and rename things in it. |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2009 The University of Melbourne
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17 |
||
18 |
"""MIME type utilities and constants."""
|
|
19 |
||
20 |
# We are named 'mimetypes', so we'd otherwise shadow the real mimetypes.
|
|
21 |
from __future__ import absolute_import |
|
22 |
||
23 |
import mimetypes |
|
24 |
import os |
|
25 |
import os.path |
|
26 |
||
27 |
DEFAULT_MIMETYPE = "text/plain" |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
28 |
|
29 |
# Mapping mime types to friendly names
|
|
1207
by William Grant
Move ivle.conf.mimetypes to ivle.mimetypes, and rename things in it. |
30 |
NICE_MIMETYPES = { |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
31 |
"text/x-python" : "Python source code", |
32 |
"text/plain" : "Text file", |
|
33 |
"text/html" : "HTML file", |
|
34 |
"image/png" : "PNG image", |
|
35 |
}
|
|
36 |
||
1207
by William Grant
Move ivle.conf.mimetypes to ivle.mimetypes, and rename things in it. |
37 |
|
38 |
def nice_filetype(filename): |
|
39 |
"""Given a filename or basename, returns a "friendly" name for that
|
|
40 |
file's type.
|
|
1281
by William Grant
Test ivle.mimetypes.nice_filetype. |
41 |
|
42 |
>>> nice_filetype("file.py")
|
|
43 |
'Python source code'
|
|
44 |
>>> nice_filetype("file.bzg")
|
|
45 |
'BZG file'
|
|
46 |
>>> nice_filetype("directory/")
|
|
47 |
'Directory'
|
|
48 |
>>> nice_filetype("file")
|
|
49 |
'File'
|
|
1207
by William Grant
Move ivle.conf.mimetypes to ivle.mimetypes, and rename things in it. |
50 |
"""
|
51 |
if filename[-1] == os.sep: |
|
52 |
return "Directory" |
|
53 |
else: |
|
54 |
try: |
|
55 |
return NICE_MIMETYPES[mimetypes.guess_type(filename)[0]] |
|
56 |
except KeyError: |
|
57 |
filename = os.path.basename(filename) |
|
58 |
try: |
|
59 |
return filename[filename.rindex('.')+1:].upper() + " file" |
|
60 |
except ValueError: |
|
61 |
return "File" |
|
62 |