UCC Code Repository

Contents of /bunnyblog/modules/cgiutils.pyc

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2 - (show annotations) (download) (as text)
Tue Jan 29 14:46:58 2008 UTC (12 years, 7 months ago) by theodore
File MIME type: application/octet-stream
File size: 15711 byte(s)
Edit stuff, add css dir

2 +:ŸGc@s‚dkZdkZddddddddd d
3 d d d dddddddddgZdZdZdZdZded„Z ded„Z
4 ed„Z d„Z d „Z d!„Zddd"d#ddedd$„Zddeed%„Zdd&„Zd'„Zd(„Zdd)„Zd*d+d,„Zd-„Zd.„Zded/d0„Zded1d/d2„Zd3„Zed4jo d5GHndS(6Nt
5 serverlinetSENDMAILt
6 validcharst alphanumst
7 getrequesttgetformtgetalltisblankt
8 formencodet
9 formdecodetmailmet
10 sendmailmetcreatehtmlmailt environdatat
11 validemailtcgiprintt ucgiprinttreplaceterrort makeindexlinetistruet randomstringsContent-Type: text/htmls/usr/sbin/sendmails(abcdefghijklmnopqrstuvwxyz0123456789!-_*t$abcdefghijklmnopqrstuvwxyz0123456789cCsMdk}|iƒ}|dj ot||d|ƒSnt|d|ƒSdS(s<
12 Initialise the ``FieldStorage`` and return the specified list of values as
13 a dictionary.
15 If you don't specify a list of values, then *all* values will be returned.
17 If you set ``nolist`` to ``True`` then any parameters supplied as lists
18 will only have their first entry returned.
19 Ntnolist(tcgit FieldStoragetformt valuelisttNoneRRR(RRRR((t./modules/cgiutils.pyR9s    tcCs®h}x¡|D]™}|i|ƒp|||<q t||tƒp||i||<q |p0g}||D]}||iqp~||<q ||di||<q W|S(sD
20 This function, given a CGI form, extracts the data from it, based on
21 valuelist passed in. Any non-present values are set to '' - although this
22 can be changed.
24 It also takes a keyword argument 'nolist'. If this is True list values only
25 return their first value.
27 Returns a dictionary.
28 iN( tdataRtfieldttheformthas_keyt
29 notpresentt
30 isinstancetlisttvalueRt_[1]tx(RR!R#RR'R R(R((RRJs
31 0cCs–h}x‰|iƒD]{}t||tƒp||i||<q|p0g}||D]}||iqX~||<q||di||<qW|S(sÚ
32 Passed a form (FieldStorage instance) return all the values.
33 This doesn't take into account file uploads.
35 Also accepts the 'nolist' keyword argument as ``getform``.
37 Returns a dictionary.
38 iN(
39 RR!tkeysR R$R%R&RR'R((R!RR'R R(R((RRes 0cCs1g}|iƒD]}|o ||qq~ S(s
40 Passed an indict of values it checks if any of the values are set.
42 Returns ``True`` if every member of the indict is empty (evaluates as False).
44 I use it on a form processed with getform to tell if my CGI has been
45 activated without any values.
46 N(R'tindicttvaluestval(R*R'R,((RR{scCs)dkl}l}||t|ƒƒƒS(sé
47 A version that turns a cgi form into a single string.
48 It only handles single and list values, not multipart.
49 This allows the contents of a form requested to be encoded into a single value as part of another request.
50 (s urlencodes
51 quote_plusN(turllibt urlencodet
52 quote_plusRR!(R!R.R/((RR†scCs-dkl}dkl}|||ƒtƒS(s8Decode a single string back into a form like dictionary.(sparse_qs(s unquote_plusN(Rtparse_qsR-t unquote_plust thestringtTrue(R2R0R1((RR s  t localhostic
53 Cs| dj ot|||||| ƒSnt|t ƒp |g}ndk
54 } ddi |ƒ} |dj o| d|7} n| o|dj o| d|7} n| |}| i ||ƒ}
55 |o|
56 i||ƒn|
57 i|||ƒ|
58 iƒdS(s=
59 This function will send an email using ``sendmail`` or ``smtplib``, depending
60 on what parameters you pass it.
62 If you want to use ``sendmail`` to send the email then set
63 ``sendmail='/path/to/sendmail'``. (The ``SENDMAIL`` value from Constants_ often
64 works).
66 If you aren't using sendmail then you will need to set ``host`` and ``port`` to
67 the correct values. If your server requires authentication then you'll need to
68 supply the correct ``username`` and ``password``.
70 ``to_email`` can be a single email address, *or* a list of addresses.
72 ``mailme`` *assumes* you are sending an html email created by
73 ``createhtmlmail``. If this isn't the case then set ``html=False``.
75 Some servers won't let you send a message without supplying a ``from_email``.
76 NsTo: %s
77 t,s
78 From: %s
79 sSubject: %s
81 (tsendmailRR tto_emailtmsgt email_subjectt
82 from_emailthtmlR$R%tsmtplibtjointheadtSMTPthosttporttservertusernametlogintpasswordtquit( R7R8R9R:[email protected]RARCRER;R6RBR<R>((RR
83 œs$    
84 cCs¹t|tƒp |g}ntid|dƒ}|iddi|ƒƒ|o|id|ƒn| o|o|id|ƒn|idƒ|id|ƒ|i ƒd S(
85 s
86 Quick and dirty, pipe a message to sendmail. Can only work on UNIX type systems
87 with sendmail.
89 Will need the path to sendmail - defaults to the 'SENDMAIL' constant.
91 ``to_email`` can be a single email address, *or* a list of addresses.
93 *Assumes* you are sending an html email created by ``createhtmlmail``. If this
94 isn't the case then set ``html=False``.
95 s%s -ttwsTo: %s
96 R5s
97 From: %s
98 s Subject: %s
99 s
100 s%s
101 N(R$R7R%tostpopenR6totwriteR=R:R;R9R8tclose(R7R8R9R:R;R6RJ((RR Ës   cCs½dk}dk}dk}|djoudk}
102 dk}|iƒ} |i|i | ƒƒ}|
103 i |ƒ}|i |ƒ|iƒ| iƒ}~ ~~n|iƒ}|i|ƒ} |i|ƒ}|i|ƒ}|id|ƒ|iddƒ|idƒ|iƒ|iƒ}|idd gƒ} | i|iƒƒ|iƒ|iƒ}|id d
104 ƒ|id dgƒ} |i| | d
105 ƒ| iƒ|i ƒ|iƒ} |iƒ| S(sö
106 Create a mime-message that will render as HTML or text as appropriate.
107 If no text is supplied we use htmllib to guess a text rendering.
108 (so html needs to be well formed)
110 Adapted from recipe 13.5 from Python Cookbook 2
111 NtSubjects MIME-Versions1.0t alternatives
112 text/plaintcharsets
113 iso-8859-lsContent-Transfer-Encodingsquoted-printables text/htmlsus-ascii(scharsets
114 iso-8859-l(scharsetsus-ascii("t
115 MimeWritert mimetoolstStringIOttextRthtmllibt formatterttextouttAbstractFormattert
116 DumbWritertformtextt
117 HTMLParsertparsertfeedR;RLtgetvaluetoutthtmlinttxtintwritert addheadertsubjecttstartmultipartbodyt flushheaderstnextparttsubpartt startbodytpoutRKtreadtencodetlastpartR8(RcR;RSRQRaRgRRR`R[R_RTRiRVR8RPRYRUR^((RR äsB     
123 c Csddddddddd d
124 d d g }g}|id ƒxC|D];}tii|ƒo"|id|ti|fƒqDqDW|idƒdi|ƒS(sIReturns some data about the CGI environment, in a way that can be mailed.t REQUEST_URItHTTP_USER_AGENTt REMOTE_ADDRt HTTP_FROMt REMOTE_HOSTt REMOTE_PORTtSERVER_SOFTWAREt HTTP_REFERERt REMOTE_IDENTt REMOTE_USERt QUERY_STRINGt
127 ---------------------------------------
128 s%s: %s
129 s(---------------------------------------
130 RN(t ENVIRONLISTtenvironstappendR(RHtenvironR"R=(R(RzRy((RR s* & cCsfd|jotSn|idƒ}|idƒ}|djp|djp ||jotSn|S(sb
131 A quick function to do a basic email validation.
132 Returns False or the email address.
133 t t.t@iÿÿÿÿiN(temailtFalsetrfindtdottfindtat(R€RƒR…((RR s 'cCs2tGHHdGH|o d|GHndGHtiƒdS(sThe generic error function.s~<html><head><title>An Error Has Occurred</title>
134 <body><center>
135 <h1>Very Sorry</h1>
136 <h2>An Error Has Occurred</h2>s <h3>%s</h3>s</center></body></html>N(Rterrorvaltsystexit(R†((RR/s i
137 ic Cs”d}||}g}
138 d}d} ||}||o|d7}n||djoN|
139 i
140 |d
141 ƒ|
142 i
143 dƒ|
144 i
145 |||ddfƒ|
146 i
147 dƒnt ||dƒ}t |||ƒ} xU|| joG||jo|
148 i
149 ||ƒn|
150 i
151 |||fƒ|d7}qÍW|
152 i
153 dƒ|||joG|
154 i
155 |||ddfƒ|
156 i
157 dƒ|
158 i
159 ||dfƒndi|
160 ƒS( s
162 Make a menu line for a given number of inputs, with a certain number per page.
163 Will look something like : ::
165 First Previous 22 23 24 25 26 27 28 29 30 31 32 Next Last
167 Each number or word will be a link to the relevant page.
169 url should be in the format : ``'<a href="script.py?startpage=%s">%s</a>'`` -
170 it will have the two ``%s`` values filled in by the function.
172 The url will automatically be put between ``<strong></strong>`` tags. Your
173 script needs to accepts a parameter ``start`` telling it which page to display.
175 ``startpage`` is the page actually being viewed - which won't be a link.
177 ``total`` is the number of total inputs.
179 ``numonpage`` is the number of inputs per page - this tells makeindexline how
180 many pages to divide the total into.
182 The links shown will be some before startpage and some after. The amount of
183 pages links are shown for is ``pagesonscreen``. (The actual total number shown
184 will be *2 \* pagesonscreen + 1*).
186 The indexes generated are *a bit* like the ones created by google. Unlike
187 google however, next and previous jump you into the *middle* of the next set of
188 links. i.e. If you are on page 27 next will take you to 33 and previous to 21.
189 (assuming pagesonscreen is 5). This makes it possible to jump more quickly
190 through a lot of links. Also - the current page will always be in the center of
191 the index. (So you never *need* Next just to get to the next page).
192 s<strong>%s</strong>RitFirsts&nbsp;tPrevioustNexttLastN(iR‰(tbturltoutlisttlasttnextttotalt numonpagetnumpagest startpaget pagesonscreenR{tmaxtindextmintendR=( RŽR•R’R“R–R”R˜RRR‘RRš((RR>s8 
194       cCsVhdt<dt<dt<dt<dt<dt<dt<dt<|iƒS(
195 sF
196 Accepts a string as input.
198 If the string is one of ``True``, ``On``, ``Yes``, or ``1`` it returns
199 ``True``.
201 If the string is one of ``False``, ``Off``, ``No``, or ``0`` it returns
202 ``False``.
204 ``istrue`` is not case sensitive.
206 Any other input will raise a ``KeyError``.
207 tyestnotontofft1t0ttruetfalseN(R3RR&tlower(R&((RR~s cCs\dk}g}x=|djo/|d8}|itt|iƒdƒƒqWdi|ƒS(s|
208 Return a random string of length 'length'.
210 The string is comprised only of numbers and lowercase letters.
211 Niii$R(trandomt outstringtlengthR{RtintR=(R¦R¤R¥((RR“s  
212 &s
213 cCs<tii|ƒtii|ƒ|otiiƒndS(s 
214 Print to the ``stdout``.
216 Set ``unbuff=True`` to flush the buffer after every write.
218 It prints the inline you send it, followed by the ``line_end``. By default this
219 is ``
220 `` - which is the standard specified by the RFC for http headers.
221 N(R‡tstdoutRKtinlinetline_endtunbufftflush(R©R«Rª((RR¢s
222 sUTF-8cCsV|o|i|ƒ}ntii|ƒtii|ƒ|otiiƒndS(s¤
223 A unicode version of ``cgiprint``. It allows you to store everything in your
224 script as unicode and just do your encoding in one place.
226 Print to the ``stdout``.
228 Set ``unbuff=True`` to flush the buffer after every write.
230 It prints the inline you send it, followed by the ``line_end``. By default this
231 is ``
232 `` - which is the standard specified by the RFC for http headers.
234 ``inline`` should be a unicode string.
236 ``encoding`` is the encoding used to encode ``inline`` to a byte-string. It
237 defaults to ``UTF-8``, set it to ``None`` if you pass in ``inline`` as a byte
238 string rather than a unicode string.
239 N( tencodingR©RkR‡R¨RKRªR«R¬(R©R«R­Rª((RR°scs’tˆƒ‰tˆƒdjoHtiddittiˆiƒƒƒƒ}|i
240 ‡d†|ƒSnx$ˆD]}|i |ˆ|ƒ}qnW|S(sê
241 This function provides a simple but effective template system for your html
242 pages. Effectively it is a convenient way of doing multiple replaces in a
243 single string.
245 Takes a string and a dictionary of replacements.
247 This function goes through the string and replaces every occurrence of every
248 dicitionary key with it's value.
250 ``indict`` can also be a list of tuples instead of a dictionary (or anything
251 accepted by the dict function).
252 i(s(%s)t|csˆ|i|iƒ|iƒ!S(N(R*tmotstringtstartRš(R¯(R*(Rt<lambda>ÜsN(tdictR*tlentretcompileR=tmaptescapeR)tregextsubtinstringtkeyR(R»R*R¹R¼((R*RRÊs  . t__main__sNo tests yet - sorry(RHR‡t__all__RRRRRRRRRRRR R3R
254 R RRRR‡RRRR RR¾R RRRHR((Rt?s4  H  !/ 1  @    


Name Value
svn:mime-type application/octet-stream

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26