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

42 by mattgiuca
Added common/util.py for common functionality.
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 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
# Module: common.util
19
# Author: Matt Giuca
20
# Date: 12/12/2007
21
22
# Contains common utility functions.
23
24
import os
25
26
import conf
27
28
root_dir = conf.root_dir
29
30
def make_path(path):
31
    """Given a path relative to the IVLE root, makes the path relative to the
32
    site root using conf.root_dir. This path can be used in URLs sent to the
33
    client."""
34
    return os.path.join(root_dir, path)
35
74 by mattgiuca
apps/help: Full help file handling (now this module works).
36
def make_local_path(path):
37
    """Given a path relative to the IVLE root, on the local file system, makes
38
    the path relative to the root using conf.ivlepath. This path can be used
39
    in reading files from the local file system."""
40
    return os.path.join(conf.ivlepath, path)
41
43 by mattgiuca
src/common/util.py: Added functions unmake_path and split_path.
42
def unmake_path(path):
43
    """Given a path relative to the site root, makes the path relative to the
44
    IVLE root by removing conf.root_dir if it appears at the beginning. If it
45
    does not appear at the beginning, returns path unchanged. Also normalises
46
    the path."""
47
    path = os.path.normpath(path)
48
    root = os.path.normpath(root_dir)
49
50
    if path.startswith(root):
51
        # +1 to take out the slash as well
52
        return path[len(root)+1:]
53
    else:
54
        return path
55
56
def split_path(path):
57
    """Given a path, returns a tuple consisting of the top-level directory in
58
    the path, and the rest of the path. Note that both items in the tuple will
59
    NOT begin with a slash, regardless of whether the original path did. Also
60
    normalises the path.
61
62
    Always returns a pair of strings, except for one special case, in which
63
    the path is completely empty (or just a single slash). In this case the
64
    return value will be (None, ''). But still always returns a pair.
65
66
    Examples:
67
68
    >>> split_path("")
69
    (None, '')
70
    >>> split_path("/")
71
    (None, '')
72
    >>> split_path("home")
73
    ('home', '')
74
    >>> split_path("home/docs/files")
75
    ('home', 'docs/files')
76
    >>> split_path("//home/docs/files")
77
    ('', 'home/docs/files')
78
    """
79
    path = os.path.normpath(path)
80
    # Ignore the opening slash
81
    if path.startswith(os.sep):
82
        path = path[len(os.sep):]
83
    if path == '' or path == '.':
84
        return (None, '')
85
    splitpath = path.split(os.sep, 1)
86
    if len(splitpath) == 1:
87
        return (splitpath[0], '')
88
    else:
89
        return tuple(splitpath)