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

« back to all changes in this revision

Viewing changes to lib/common/util.py

  • Committer: mattgiuca
  • Date: 2008-03-15 07:49:26 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:701
Moved "open_exercise_file" from tutorialservice to util.
    Will be used by tutorial app as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
    else:
111
111
        return tuple(splitpath)
112
112
 
 
113
def open_exercise_file(exercisename):
 
114
    """Given an exercise name, opens the corresponding XML file for reading.
 
115
    Returns None if the exercise file was not found.
 
116
    (For tutorials / worksheets).
 
117
    """
 
118
    # First normalise the path
 
119
    exercisename = os.path.normpath(exercisename)
 
120
    # Now if it begins with ".." or separator, then it's illegal
 
121
    if exercisename.startswith("..") or exercisename.startswith(os.sep):
 
122
        exercisefile = None
 
123
    else:
 
124
        exercisefile = os.path.join(conf.exercises_base, exercisename)
 
125
 
 
126
    try:
 
127
        return open(exercisefile)
 
128
    except (TypeError, IOError):    # TypeError if exercisefile == None
 
129
        return None
 
130
 
113
131
# Initialise mime types library
114
132
mimetypes.init()
115
133
for (ext, mimetype) in conf.mimetypes.additional_mime_types.items():