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:
- apacherequest -- The Apache request object (if JOTWeb is interfaced with Apache).
- currentdir -- The directory that the current document is in.
- filetypemagic -- (Defaults to 1) If true, JOTWeb will read the
first line of the file to help detect if the file is HTML or XML.
The file type is set based on what the first line starts with:
- XML:
<?xmlor<!--jotwebtype=xml - HTML:
<!--jotwebtype=html - XHTML:
<!--jotwebtype=xhtml - XHTML:
<!DOCTYPEand findingXHTMLin the line, i.e. a standard XHTML DOCTYPE declaration.
If you are counting on user agents changing their rendering behaviour based on DOCTYPE, remember that IE6 will not recognize a DOCTYPE prefaced by an xml preamble ( <?xml ... ). - XML:
- talglobalvars -- A dictionary object containing the global variables. Note that the values associated with this are TAL variables, and you'll probably need to call the .value() method to get the raw data. You'll probably want to use jotwebutils.getGlobal() instead of directly accessing this via the config, as it calls the value() method for you.
- tallocalvars -- Similar to "talglobalvars", but contains the TAL local variables. See the notes for that. You'll probably want to use jotwebutils.getLocal() instead.
- rootdir -- The JOTWeb document root directory.
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.