Including modules
Clashing module names
To avoid clashes of included modules between sites the following functionality is available. For Python two dynamic included modules from defines import * look the same, so they get mixed up when asked to import a module with the same name in another site. To solve this create a file ./_py/_packagename containing the name of the site (e.g. “mysite”). Then modules inside the ./_py folder can be imported as from mysite.defines import *.Including single methods
It is possible to include (dynamically) local modules as part of the Python application code. The gethandler, before running the page render, will add two paths to the Python path. The first path is the global /_root/_lib2/py. This directory contains an __init__.py file so it will function as a standard Python module. The second is derived from an unbased path ./_py which then expands into the a based file system path. It find this ./_py on the same level as the ./_xsl/_base file. This ./_py directory and all of it’s child directories should contain an __init__.py file.The two appended paths are automatically removed by the gethandler when rendering of the page is finished. Now all Python modules in these directories can be included in the Xierpa Python application, allowing values, functions and classes to be shared by all application files in a site.
An example is the shared values of a website, as in a file located on ./_py/includes/defines.py.
# -*- coding: UTF-8 -*- # # ./_py/defines.py # UNIT = 8 GUTTER = MARGIN = UNIT COLUMNWIDTH = 8 * UNIT def px(n): return str(n) + 'px' def col(n): # Calcultate column position --> pixels return px(MARGIN + n * (COLUMNWIDTH + GUTTER)) def colw(n): # Calcultate column width --> pixels return px(n * COLUMNWIDTH + (n-1) * GUTTER)
from mysite.include.defines import *
Reloading modules
When the sources are under development it is not practicle to restart the server every time one of the local modules changes (required because Python does not check on modification date of loaded modules). A solution to this is to reload the modules every time the page is loaded. This is a bit expensive, but if we make this conditional to the global DEBUGSERVER flag, this will not happen when the server is in production mode (note, however, that when update the Xierpa version of a production server, restarting or forcing the DEBUGSERVER flag to True once, is still necessary.The code for the reload could look like this in the heading of a Python application
from xpyth.defines import DEBUGSERVER if DEBUGSERVER: import mysite.include reload(mysite.include) import mysite.include.defines reload(mysite.include.defines) from mysite.include.defines import *
Reloading database definition
An agent reads the field description of a database only once from the graffle datamodel. Any change to the datamodel file will not show up when the server is running. To force the server to re-read the table definition from the datamodel add the following lines to the initialization of an application.from xpyth.defines import DEBUGSERVER from xpyth.xierpa.db.agent import Agent if DEBUGSERVER: try: del Agent._descriptors[DATABASE] except KeyError: pass
