CAT-SOOP API: catsoop.loader

Functions for loading page specifications into dictionaries

This file contains functions that do a lot of the "heavy lifting" associated with loading pages, including handling preload, managing parsing/evaluation of code in content files, and evaluation of plugins.

View Source

Members

  • catsoop.loader.available_plugins (function, lines 83-105)

    Determine all the plugins that can be loaded

    Parameters:

    • context: the context associated with this request
    • course: the course from which plugins should be loaded (or None if no course).

    Returns: a list of the full paths to all available plugins' directories

 

  • catsoop.loader.clean_builtins (function, lines 40-53)

    Removes the '__builtins__' key from a dictionary to make it serializable

    Parameters:

    • d: the dictionary to clean

    Returns: None

 

  • catsoop.loader.cs_compile (function, lines 354-398)

    Return a code object representing the code in the specified file, after making a few CAT-SOOP-specific modifications.

    As a side-effect, store on disk a file containing the updated code, and another containing information about how many new lines were added to the top of the given file, for use in error reporting. These pieces are only updated if the contents of the given file have changed (based on the modification time).

    Parameters:

    • fname: the name of the file to be compiled

    Optional Parameters:

    • pre_code (default ''): a string containing code to be inserted at the start of the file
    • post_code (default ''): a string containing code to be inserted at the end of the file

    Returns: a bytestring containing the compiled code

 

  • catsoop.loader.do_preload (function, lines 274-337)

    Load data from preload.py files in the appropriate directories for this request.

    The preload.py file from the course will be executed first, followed by the next level down the path, and so on until the file from this request's path has been run. The preload files will also be run from this page's children, though they will be executed into separate directories, and stored in the 'children' key of the supplied dictionary.

    This function is run before loading user data, so the code in preload.py cannot make use of user information, though it can make use of any variables specified in the base context or in preload files from higher up the tree.

    Parameters:

    • context: the context associated with this request
    • course: the course associated with this request
    • path: the path associated with this request, as a list of strings not including the course
    • into: the dictionary in which the code should be executed

    Optional Parameters:

    • content_file (default None): the name of the content file associated with this page load. We need to know this because the behavior is slightly different depending on whether the associated content file is indeed a content.xx file (in which case we can run a preload.py for every element in the given path) or whether it is an arbitrary file (in which case we cannot run a preload.py for the last element in the list).

    Returns: None on success, or the string 'missing' on failure

 

  • catsoop.loader.generate_context (function, lines 229-252)

    Generate a new context, loading the global data and running the preload.py files for the specified path.

    This function is particularly useful in scripts, as many of the functions in CAT-SOOP require a "context" in which to run.

    Parameters:

    • path: a list of strings (starting with a course name) representing the path whose preload context should be spoofed

    Returns: a context dictionary containing the global values and those defined in the preload.py files along the specified path

 

  • catsoop.loader.get_course_fs_location (function, lines 193-226)

    Returns the base location of the specified course on disk, including "special" courses (_util, _qtype, etc).

    Parameters:

    • context: the context associated with this request
    • course: the name of the course

    Optional Parameters:

    • join (default True): controls the return type. If True, the elements in the path will be joined together and the return value will be a string. If False, the return value will be a list of directory names.

    Returns: depends on the value of join (see above).

 

  • catsoop.loader.get_directory_name (function, lines 401-428)

    Return the actual name of a subdirectory of the given path (including sorting numbers) given the shortname of the resource it represents.

    Directories for pages can optionally begin with a series of digits and a period, in which case the name of the associated page is the piece following that period, and the numbers that come before it are used for sorting.

    Parameters:

    • context: the context associated with this request
    • course: the course associated with this request
    • path: the path associated with this request, as a list of strings not including the course
    • name: the name of the page being requested (a known child of path)

    Returns: the appropriate directory name if name is indeed a child of path, or None otherwise

 

  • catsoop.loader.get_plugin_code_file (function, lines 108-123)

    Return the filename of a particular hook from the given plugin

    Parameters:

    • plugin: a string containing the name of a directory containing a plugin
    • type_: the name of a plugin hook as a string (e.g., 'post_load')

    Returns: a string containing the full path to the given hook for the given plugin if it exists, or None otherwise

 

  • catsoop.loader.get_subdirs (function, lines 431-457)

    Return all subdirectories of the given path that represent pages.

    Parameters:

    • context: the context associated with this request
    • course: the course associated with this request
    • path: the path associated with this request, as a list of strings not including the course

    Returns: a list of all directory names under path that represent pages.

 

  • catsoop.loader.load_content (function, lines 472-571)

    Load data from the Python file specified by the content file in the appropriate directory for this request.

    This function is run after loading user data, so the code in the content file can make use of that information, which includes user permissions.

    This function also populates the cs_children variable by executing the preload.py files of this page's children into the given context.

    Parameters:

    • context: the context associated with this request
    • course: the course associated with this request
    • path: the path associated with this request, as a list of strings not including the course
    • into: the dictionary in which the code should be executed

    Optional Parameters:

    • content_file (default None): the name of the content file associated with this page load. We need to know this because the behavior is slightly different depending on whether the associated content file is indeed a content.xx file or whether it is an arbitrary file.

    Returns: None

 

  • catsoop.loader.load_global_data (function, lines 148-190)

    Load global data into the specified dictionary

    Includes anything specified in base_context.py and config.py, as well as all of the modules in the catsoop directory.

    Parameters:

    • into: a dictionary into which the built-in values should be loaded

    Optional Parameters:

    • check_values (default True): whether to error on invalid configuration values

    Returns: None on success, or a string containing an error message on failure

 

  • catsoop.loader.plugin_locations (function, lines 56-80)

    Look up the directories from which plugins should be loaded

    Parameters:

    • context: the context associated with this request
    • course: the course from which plugins should be loaded (or None if no course).

    Returns: a list of directories from which plugins should be loaded.

 

  • catsoop.loader.run_plugins (function, lines 126-145)

    Run the given hook for all plugins

    Parameters:

    • context: the context associated with this request
    • course: the course from which plugins should be loaded (or None if no course).
    • type_: the name of a plugin hook as a string (e.g., 'post_load')
    • into: the context in which the plugins should be run

    Returns: None