1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>9. Using several modules</title>
<META NAME="description" CONTENT="9. Using several modules">
<META NAME="keywords" CONTENT="tut">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="tut.css" type='text/css'>
<link rel="first" href="tut.html">
<link rel="contents" href="contents.html" title="Contents">
<LINK REL="next" HREF="node12.html">
<LINK REL="previous" HREF="node10.html">
<LINK REL="up" HREF="tut.html">
<LINK REL="next" HREF="node12.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node10.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node12.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node10.html">8. Using OOP to</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node12.html">10. HTTP and cookie-based</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0011000000000000000000">
9. Using several modules</A>
</H1>
As you'll program bigger websites, you'll soon feel the need to split your source code in several modules.
There are two ways to do this:
<UL>
<LI>Either your modules are completely independant. In this case, just create your files (for instance:
<span class="file">Hello1.cpy</span>, <span class="file">Hello2.cpy</span> and <span class="file">Hello3.cpy</span>) and compile them
using:
<div class="verbatim"><pre>
python ../cherrypy.py Hello1.cpy Hello2.cpy Hello3.cpy
</pre></div>
Note that the executable that CherryPy will generate will be called <span class="file">Hello1Server.py</span>
</LI>
<LI>Or one module is needed by the other (for instance, one is a library used by the other one). In this case, all you
need to do is type the keyword <var>use module</var> on the very first line of the file. This works like an <var>import</var>
statement in Python. For instance, you can have:
<div class="verbatim"><pre>
***** File BoldTime.cpy: *****
import time
CherryClass BoldTime:
view:
def getBoldTime(self):
# Display the time in bold
return "<b>%s</b>"%time.time()
***** File Hello.cpy: *****
use BoldTime
CherryClass Root:
view:
def index(self):
return "<html><body>Hello, time is %s</body></hello>"%boldTime.getBoldTime()
</pre></div>
To compile this, just use:
<div class="verbatim"><pre>
python ../cherrypy.py Hello.cpy
</pre></div>
<P>
Five things to note:
<UL>
<LI>The <var>use</var> statement MUST be on the very first line of the file (don't put any comment before).
</LI>
<LI>CherryPy will automatically create a list of dependencies, and thus read the files in order and generate the
executable accordingly. If you create a loop in the dependencies, CherryPy will raise an error.
</LI>
<LI>The name of the CherryClass is <var>BoldTime</var> (with an upper case B). So is the name of the file and the name
you use in the <var>use</var> statement. But when you call <var>boldTime.getBoldTime</var>, a lower b is used, because it
refers to the instance of the class that is automatically created.
</LI>
<LI>You can still use regular Python <var>import</var> statements. (Either <code>import ...</code> or <code>from ... import ...</code>)
</LI>
<LI>If you have lots of modules to include with <var>use</var>, then you can split the "use" statement on several lines (but they have
to be the first lines of the file). For instance, you can use:
<div class="verbatim"><pre>
****** File Root.cpy: *****
use HttpAuthenticate, CookieAuthenticate
use Mail, MaskTools
CherryClass Root:
mask:
def index(self):
OK
</pre></div>
</LI>
</UL>
</LI>
</UL>
<P>
What if the modules are not in the same directory ?
<P>
Well, all you have to do is to use the <var>-I</var> option to compile the files. This allows you to specify the directories
where CherryPy will look for input files. For instance, if you have the following files:
<div class="verbatim"><pre>
/dir1/Module1.cpy
/dir2/Module2.cpy
Hello.cpy (uses Module1 and Module2)
</pre></div>
Then you would compile <span class="file">Hello.cpy</span> using:
<div class="verbatim"><pre>
python ../cherrypy.py -I /dir1 -I /dir2 Hello.cpy
</pre></div>
By default, CherryPy will look in <span class="file">.</span>, <span class="file">../lib</span> and <span class="file">../src</span>
<P>
You can also set an environment variable called <var>CHERRYPY_HOME</var> that contains the
name of the directory where CherryPy is installed. In this case, CherryPy will also look in <span class="file">CHERRYPY_HOME/lib</span>
and <span class="file">CHERRYPY_HOME/src</span> to find the modules.
<P>
In the next chapters, we'll learn how to use a few of CherryPy's standard library modules.
<P>
<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node10.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="tut.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node12.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy Tutorial</td>
<td><A HREF="node2.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node10.html">8. Using OOP to</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="tut.html">CherryPy Tutorial</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node12.html">10. HTTP and cookie-based</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
|