config

  1. Configuration Introduction
  2. General Usage
  3. Pre-defined Entries
  4. Methods
  5. Example: Access from TAL
  6. Example: Access from Python
  7. Example

Configuration Introduction

The config module defines one method: "getconfig", which returns a dictionary-like object that can be used to access and override configuration values.

These values include values set by the JOTWeb system, values pulled from the "jotweb.conf" file in the JOTWeb document root (as specified in the httpd.conf file "JOTWebDocRoot" option), and values set by any Python code referenced from pages.

General Usage

The "getconfig()" method returns a dictionary-like object. This object is also available as the name "jotwebnsroot/config" in the TAL global name-space. This can be used to reference configuration values from HTML/XML files. The keys of this dictionary are the names of the configuration entries.

JOTWeb will load the file "jotweb.conf" in the JOTWeb document root before rendering your document. This file may contain arbitrary Python code, but any names set in the local name-space will be included in the configuration name-space. For example:

def getDbconnectstr():
   import os
   if os.uname()[1] == 'test.example.com':
      return('::testdbname')
   return(::mydbname')
dbconnectstr = getDbconnectstr()
baseurl = 'http://jotweb.tummy.com/'

The above sets a "dbconnectstr" name based on wether the code is running on the test or production environment. It also sets a "baseurl" name. Note that I put the complicated code into a function, so as to not pollute the configuration name-space with the "os" name, for example. However, this does also create the name "getDbconnectstr", set to the above function.

Pre-defined Entries

The following names are set by JOTWeb in when it initializes the configuration:

Methods

getconfig()

Arguments: None

Returns: Configuration dictionary-like object or None if no configuration available.

Exceptions: None.

Description: This function data for use by the page. If True is returned, session data has been retrieved and user/session information is available.

Example: Access from TAL

The config object can be accessed from TAL using the global name "jotwebnsroot/config". For example, if the name "baseurl" is set in the config, you can display it from TAL by doing:

Base URL: <span tal:replace="jotwebnsroot/config/baseurl"></span>

Example: Access from Python

The config object can be accessed using the "getconfig" method of in the "config" module. For example, to access the "baseurl" config value, to see if ht URL starts with "http://dev.":

def isdevsite():
   from jotweb.config import getconfig
   config = getconfig()
   if config.get('baseurl', '')[:11] == 'http://dev.':
      return(True)
   else:
      return(False)

Example

See the "sessionex" files in the JOTWeb "examples" directory in the source distribution for an example of using this code.

Index