context_processors.py 3.4 KB
Newer Older
1

2 3
import ConfigParser
from django.conf import settings
4
import logging
5

6 7 8 9 10

log = logging.getLogger(__name__)


# Open and parse the configuration file when the module is initialized
11 12 13
config_file = open(settings.REPO_ROOT / "docs" / "config.ini")
config = ConfigParser.ConfigParser()
config.readfp(config_file)
14 15


16
def doc_url(request=None):  # pylint: disable=unused-argument
17 18 19 20 21 22 23 24 25 26 27
    """
    This function is added in the list of TEMPLATE_CONTEXT_PROCESSORS, which is a django setting for
    a tuple of callables that take a request object as their argument and return a dictionary of items
    to be merged into the RequestContext.

    This function returns a dict with get_online_help_info, making it directly available to all mako templates.

    Args:
        request: Currently not used, but is passed by django to context processors.
            May be used in the future for determining the language of choice.
    """
28

29
    def get_online_help_info(page_token=None):
30 31 32 33 34
        """
        Args:
            page_token: A string that identifies the page for which the help information is requested.
                It should correspond to an option in the docs/config.ini file.  If it doesn't, the "default"
                option is used instead.
35

36 37 38 39 40 41
        Returns:
            A dict mapping the following items
                * "doc_url" - a string with the url corresponding to the online help location for the given page_token.
                * "pdf_url" - a string with the url corresponding to the location of the PDF help file.
        """

42 43 44 45 46 47 48 49
        def get_config_value_with_default(section_name, option, default_option="default"):
            """
            Args:
                section_name: name of the section in the configuration from which the option should be found
                option: name of the configuration option
                default_option: name of the default configuration option whose value should be returned if the
                    requested option is not found
            """
50
            try:
51
                return config.get(section_name, option)
52
            except (ConfigParser.NoOptionError, AttributeError):
53 54
                log.debug("Didn't find a configuration option for '%s' section and '%s' option", section_name, option)
                return config.get(section_name, default_option)
55

56
        def get_doc_url():
57 58 59 60
            """
            Returns:
                The URL for the documentation
            """
61 62
            return "{url_base}/{language}/{version}/{page_path}".format(
                url_base=config.get("help_settings", "url_base"),
63
                language=get_config_value_with_default("locales", settings.LANGUAGE_CODE),
64
                version=config.get("help_settings", "version"),
65
                page_path=get_config_value_with_default("pages", page_token),
66 67
            )

68
        def get_pdf_url():
69 70 71 72
            """
            Returns:
                The URL for the PDF document using the pdf_settings and the help_settings (version) in the configuration
            """
73 74 75 76 77
            return "{pdf_base}/{version}/{pdf_file}".format(
                pdf_base=config.get("pdf_settings", "pdf_base"),
                version=config.get("help_settings", "version"),
                pdf_file=config.get("pdf_settings", "pdf_file"),
            )
78

79
        return {
80 81
            "doc_url": get_doc_url(),
            "pdf_url": get_pdf_url(),
82
        }
83

84
    return {'get_online_help_info': get_online_help_info}