~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/common/util.py

  • Committer: stevenbird
  • Date: 2008-02-01 03:51:56 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:368
First version of a DTD for XML problem files

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
    root = os.path.normpath(root_dir)
53
53
 
54
54
    if path.startswith(root):
55
 
        # +1 to take out the slash as well
56
 
        return path[len(root)+1:]
57
 
    else:
58
 
        return path
 
55
        path = path[len(root):]
 
56
        # Take out the slash as well
 
57
        if len(path) > 0 and path[0] == os.sep:
 
58
            path = path[1:]
 
59
 
 
60
    return path
59
61
 
60
62
def split_path(path):
61
63
    """Given a path, returns a tuple consisting of the top-level directory in
97
99
for (ext, mimetype) in conf.mimetypes.additional_mime_types.items():
98
100
    mimetypes.add_type(mimetype, ext)
99
101
 
 
102
def nice_filetype(filename):
 
103
    """Given a filename or basename, returns a "friendly" name for that
 
104
    file's type.
 
105
    eg. nice_mimetype("file.py") == "Python source code".
 
106
        nice_filetype("file.bzg") == "BZG file".
 
107
        nice_filetype("directory/") == "Directory".
 
108
    """
 
109
    if filename[-1] == os.sep:
 
110
        return "Directory"
 
111
    else:
 
112
        try:
 
113
            return conf.mimetypes.nice_mimetypes[
 
114
                mimetypes.guess_type(filename)[0]]
 
115
        except KeyError:
 
116
            filename = os.path.basename(filename)
 
117
            try:
 
118
                return filename[filename.rindex('.')+1:].upper() + " file"
 
119
            except ValueError:
 
120
                return "File"