Key Configurations and Some Apis
Total Page:16
File Type:pdf, Size:1020Kb
APPENDIX A Key Configurations and Some APIs THIS APPENDIX CONTAINS some of the key development configurations for Zope, Plone, and Python. This appendix provides information for developers of sites, and it also lists some ofthe most usefulApplication Programming Interfaces (APIs). Setting Up Your Environment The following sections relate to configuring your development or production environment for optimal usage. If you're developing a lot with Plone, I rec ommend these settings. Setting Up PYTHONPATH Setting up PYTHONPATH is extremely useful because it allows you to easily access all the Zope functionality from a Python prompt. You can easily test if you have this set up-you can just attempt to import the PageTemplate module from Products. If this isn't set up, you should see the following: $ python -c "import Products.PageTemplates" Traceback (most recent call last): File "<string>", line 1, in ? ImportError: No module named Products.PageTemplates Unix} Linux} and Mac OS X First, find the Products directory of your Zope installation (not the instance horne). On the standard installation, this is at /opt/Zope- 2.7 /lib/python; on Windows it's at c: \Program Files\Plone\Zope\lib\python. When Python starts, it reads an envi ronmentvariable called PYTHONPATH and puts all those directories in thatvariable into the search path for new modules. So you need to add your directory to that variable. 465 AppendixA You do this using the export command, to see if PYTHONPATH contains anything initially, run the following: $ export I grep PYTHONPATH declare -x PYTHONPATH="/home/andy/modules" In my case, I already have an environment variable called PYTHONPATH, although chances are your computer won't have this set up. In my case, it contains a path to some internal modules, so you need to add Zope 2 into that path, like so: $ export PYTHONPATH="loptlZope-2.7Ilib/python:$PYTHONPATH" You can check that this works by repeating the following command and ensuring no error is raised. python -c "import Products.PageTemplates" Windows The key directory on Windows is at c: \Program Files\Plone\Zope\lib\python. When Python starts, it reads an environment variable called PYTHONPATH and puts all those directories in that variable into the search path for new modules. So you need to add your directory to that variable. On Windows, to add an environment variable, right-dick the My Computer icon and select Properties. In the System Properties dialog box, dick the Advanced tab and then, underneath System Variables, dick New, as shown in Figure A-l. Environment Variables IJ EI ~v~f~~ ------------------~ YI!ri.IbIe v... ~ ... I ~dot... ~ SYstemv8riables-- -- , v PROCESSOIUD... x86 faMy 6 Model 7 Steppr.g 3, GetU ••• PROCESSOR_LE ••• 6 PROCESSOR_RE ••• 0703 J lEM' C:\WItM\lEM' 1M' C:\WiN'IT\lEM' ;:J Net!•.. ~ ... De!ete I' Figure A-l. The Environment Variables dialog box 466 Key Canfiguratians and Same APIs This will open the System Variable dialog box. To edit a variable, select a variable from the list and edit the value, as shown in Figure A-2. The variable should be PYTHONPATH, and the value should be the place your Plone is located, for example, c: \Program Files \Plone 2 \Zope \lib \python. Edlt 5y,tem V.n.ble 613 Yar1/lble (:iorne: II'YTHC)fA>ATH y~ ~okJe: J MUi ""'6411_ ()I( C.....". Figure A-2. Adding the variable Now you should be able to run the import statement as usual. You can do that from the command line or fire up PythonWin and trythe import as Figure A-3 demonstrates. '. PythonWin IIII~ EI PythonWin 2.3.3 (#51, Oec 182003,20.22.39) [MSC v.1200 32 bit (Intel)] on win32 . PoTbons Copyright 1994-2004 Mark Hammond ([email protected]) - see 'HelplAbout PythonWin' for further copyright Information »> imuort Produces. PageTemplaees »> ~rint Produces. PageTemplaees <module ' Produc~s.PageTempla~e~ ' !ro~ ' d : \ Zop",\ lib\ pychon' Produc",,\ PageTempl ..te:!t\ 1n1t_. py' ~ »> Figure A-3. Importing in Python Win 467 AppendixA Not only can you now import products, but you can also import Zope from the Python command line, which is key for many scripts and tools in the more advanced chapters of the book. Setting Up to Run Unit Tests Plone comes with some several hundred unit tests, which are excellent ways of ensuring that your Plone is functioning correctly. You can find Plone unit tests in CMFPlone/tests. Ityou develop in Plone, then it'd be a good idea to set these up so you can add your own tests. To run these tests, you need ZopeTestCase. In the future, this will probably be packaged with Plone, but for the moment you can download it from http://zope . org/Members/ shh/Zope TestCase. The installation is a little unusual-you need to untar the package and then place it in lib/python/Testing directory ofyour Zope software horne. On Unix this is usually in /opt/Zope- 2.7. On Windows this is c: \Program Files\Plone 2\Zope. Next you need a script that will run the tests. I usuallywrite a little shell script to do so and place it in the bin directory of my Plone instance. I can then just run the shell script to run the tests. On Unix where my instance is at /var /test, my script looks like the following: export SOFTWARE_HOME=/opt/Zope-2.7/1ib/python export INSTANCE_HOME=/var/test123 echo Testing CMFPlone ... cd $INSTANCE_HOME/Products/CMFPlone/tests python2.3 runalltests.py On Windows the equivalent is as follows: set SOFTWARE_HOME=c:\Program Files\Plone 2\Zope set INSTANCE_HOME=c:\Program Files\Plone 2\Data cd "c:\Program Files\Plone 2\Data\Products\CMFPlone\tests" "c:\Program Files\Plone 2\Python\python.exe" runalltests.py The output should be something like the following: 468 Key Canfiguratians and Same APIs [root@basil bin]# ./testAll.sh Testing CMFPlone ... SOFTWARE_HOME: /opt/Zope-2.7/lib/python INSTANCE_HOME: /opt/Zope-2.7/lib/python/Testing Loading Zope, please stand by ... done (7.8995) Installing CMFCore ... done (1.3635) Installing CMFDefault ... done (1.1795) Installing CMFCalendar ... done (0.1955) And so on ... The unit testing framework is a great basis for allowing you to unit test your own products. Ifyou do write products or software development in Plone, then ideallyyou'd write unit tests for them. The unit tests in Plone provide an excellent set of examples for you. Zope Configuration File The Zope configuration file is written in a format called ZConfig. You can find more information on ZConfig at http://www . zope. org/Members/fdrake/zcanfig/. The configuration file is called zope. canf and is contained in the etc directory of your Zope installation. Some installers also create a plane. conf file that contains Plone-specific information. The recommended platform for all Plone 2 installations is Zope 2.7. This was the first version ofZope to feature this file; in earlierversions ofZope, parameters were passed to Zope in one command line. All comments in the file begin with #. In ZConfig you define variables that can then be used later in the configuration file; you do this with a line in the fol lowing format: %define variable-name variable-value The following are example definitions in my config file: %define INSTANCE /var/test %define ZOPE /opt/Zope-2.7 You can then reference the variables using a $prefix (for example, $INSTANCE). Table A-l shows the full Zope configuration for 2.0.1. The Directive column shows the name of the variable in the configuration file. The Description column details what the directive does, and the Default column teIls you what will happen if no value is set. In the Example column, I've detailed some examples to help explain the directive. 469 Key Canfiguratians and Same APIs Table A -1. Directives in the Zope Configuration File Directive Description Default Example instancehome The path to the data files, local product files, import None Ivar Izope or the $INSTANCE directory, and Extensions directory. Each instance of Zope should have one of these. client home The directory in which a running Zope's process INSTANCE/var $INSTANCE_HOME/var identifier files are placed, such as the process ID file, Z2. pid. Not recommended for use; look at pid-filename instead. path Name of a directory that should be inserted into the None path Ihome/pythonModules beginning of Python's module search path. This directive may be specified as many times as needed to insert additional directories. Since this may be too late, altering the PYTHONPATH, described earlier in this appendix, is preferred. products Name of a directory that contains additional Product None products Ihome/chrisml packages. This directive may be used as many times projects/myproducts as needed to add additional collections of products. Each directory identified will be added to the _path _ ofthe Products package. I don't recommend using this; normally I find using an instance horne is enough. Using this has also caused some bug reports withPlone. 470 Key Canfiguratians and Same APIs Table A -1. Directives in the Zope Configuration File (Continued) Directive Description Default Example environment A section that can be used to defme arbitrary key/value None <environment>MY - PRODUCT - ENV pairs for use as environment variables during Zope's VAR foobar</environment> run cyde. It isn't recommended to set system-related environment variables such as PYTHON PATH within this seetion. Most startup scripts will defme system -related environment variables for you, so this isn't recommend for use. debug-mode A switch that controls several aspects of Zope operation On debug-mode off useful for developing under Zope. I recommend running all development servers in this mode.