[Tutor] reafline reads file, not just 1 line
Kirk Bailey
deliberatus at verizon.net
Fri Mar 30 17:37:56 CEST 2007
#!C:\Python25\pythonw.exe
#
import os, os.path, random, sys, string, time, cgitb; cgitb.enable()
#
print "Content-Type: text/html\n"
#
try:
pagename=os.environ['QUERY_STRING'] # try to retreive the page name requested
except exception, e:
pagename="FrontPage"
#
if len(pagename)>3:
pass
else:
pagename="FrontPage" # but default to the FrontPage file.
#
path=os.getcwd()
if os.path.exists('WW.key'):
pass
else:
f1=open('WW.key','w')
f1.write('unregistered')
f1.close()
path=path+'\\text'
os.chdir(path) # make the pages dir current
#
"""
if os.path.exists('ConfigureMe'):
f1=open('ConfigureMe','r')
tablebgcolor=string.strip(f1.readline())
papercolor=string.strip(f1.readline())
inkcolor=string.strip(f1.readline())
linkcolor=string.strip(f1.readline())
f1.close()
else:
tablebgcolor='FFC0A0'
papercolor='White'
inkcolor='Black'
linkcolor='Blue'
f1=open('ConfigureMe','w')
f1.write(tablebgcolor+'\n')
f1.write(papercolor+'\n')
f1.write(inkcolor+'\n')
f1.write(linkcolor+'\n')
f1.write('\nThe lines above are:\n')
f1.write('tablecolor\n')
f1.write('papercolor\n')
f1.write('inkcolor\n')
f1.write('linkcolor\n\n')
f1.write('[=@! Navigation Toolbar !@=]\n')
f1.write('@! CategoryWiki !@\n')
f1.close()
"""
tablebgcolor='FFC0A0'
papercolor='White'
inkcolor='Black'
linkcolor='Blue'
#
if os.path.exists(pagename): # if the page asked for exists,
pass # do nothing here;
else: # BUT if it does NOT-
f1=open(pagename,'w') # CREATE it!
f1.write("""Please contribute something to this webpage.
[=@! Navigation toolbar !@=]
@! FrontPage CategoryCategory !@""")
f1.close()
#
# page header follows
print "<html><head><title>WindowsWiki - " + pagename + "</title>"
print '<style TYPE="text/css">'
print "A:link, A:visited, A:active { text-decoration:none; }"
print "A:hover { text-decoration:underline; }"
print 'body { margin-left: 5%; margin-right: 5%; }'
print "</style></head>"
print '<body bgcolor="' + papercolor + '" text="' + inkcolor + '" links="' + linkcolor + '">'
print '<table width=100% bgcolor="' + tablebgcolor + '" border="0" cellpadding="10" cellspacing="0">'
print '<tr>'
print '<td width="120" ><B><font size="4" face="Georgia" color="FF0000">WindowsWiki™</font></B></td>'
print '<td width="*" align="center" >(click title for backsearch)</td>'
print '<td rowspan="2"> </td>'
print '</tr><tr>'
print '<td> </td>'
print '<td align="center"><font size="6"><a href="./MWbacksearch.py?' + pagename + '">' + pagename + '</a></font></td>'
# put a cell here if need be
print '</tr><tr>'
print '<td> </td>'
print '<td align="center">'
# Begin nagware suite
birthday=os.stat('..\WW.key')[-1]
now=int(time.time())
month=60*60*24*30
age=now-birthday
if age > month:
print 'your 1 month trial period has expired.'
timebomb=1
else:
naglist=[ 'Register and get free upgrades!','This software will expire-register today!','Shareware, not freeware. Please register!','Please register your shareware','Paying for shareware FEELS GOOD!','Your shareware dollar rewards creativity','Do YOU get
paid for what you do?']
print random.choice(naglist)
timebomb=0
# end nagware suite
print '</td>'
print '<td width="120" align="right">Personal Edition</td>'
print '</tr></table>'
# begin suicide switch
if timebomb==1:
print 'Shutting down. Your trial has expired. Please register your shareware.<P></body></html>'
print 'It is time to register your shareware if you wish to continue to use it.<br>'
print 'You can do so by going <a href="http://www.freeholdmarketing.com/WW/">HERE</a> and reegistering your copy for a mere $10.<P>'
sys.exit()
# end suicide device
print '<div align="justify"><P>'
f1=open(pagename,'r')
page=f1.readlines()
f1.close()
#
def isin( searchthis, forthis ): # return a 1 or 0 to control IF statements
value = 1 + string.find(searchthis, forthis)
if value > 0:
return 1
else:
return 0
#
#
# Now we try to hash out wiether or not a word is a wikiword...
# a WikiWord is in CamelCaps. it has a 'hump' in it.
# ThisIs a WikiWord,
# but ThisISNOT,
# ANDNEITHERISTHIS,
# NorisTHIS. Fun?
#
# these functions build wikiwords.
def buildwikilink(word): # turns a word into a hyperlink wikiword
word = '<a href="./MW.py?' + word + '">' + word + '</a>' # it's a hyperlink anchor, normal html.
return word
#
def potentialword(word): # processes word, seperates into prefix, word, and suffix
newword=''
prefix=''
suffix=''
index=0
while 1: # loop processes the word 1 char at a time- return breaks loop.
if word[index] in string.ascii_letters:
prefix, newword, suffix = mainbody(prefix, word[index:])
return prefix, newword, suffix
else:
prefix=prefix+word[index]
index=index+1
#
def mainbody(prefix, word,):
newword=''
suffix=''
index=0
for char in word:
if char in string.ascii_letters:
newword=newword+char
else:
suffix=suffix+char # the remainder of the word is the suffix
index=index+1
return prefix, newword, suffix
#
def makewikiword(word): # combines processed link, prefizes, and suffix into a complete word.
prefix=''
suffix=''
prefix, word, suffix=potentialword(word)
return prefix + buildwikilink(word) + suffix+' '
#
#
# these next 2 words determine if a word is a wikiword.
def processword(word):
if word: # it is possible to exause a word and not find another capital letter
if word[0] in string.ascii_lowercase: # wikiwords CAN have several lowercase letters before the next capital, after all...
value=processword(word[1:]) # so keep invoking this word until exaustion, or a capital is found
return value
else: # MIGHT be a capital!
if word[0] in string.ascii_uppercase:
if len(word)>1:
if word[1] in string.ascii_lowercase:
return 1
else:
return 0
else:
return 0
else:
return 0
else:
return 0
# def iswikiword(word):
# return bool(re.match('^([A-Z][a-z]+){2,}$', word))
#
def iswikiword(word): # tests to see if a word is a wikiword.
word=string.strip(word)
if word:
if word[0]in string.ascii_uppercase: # ALL wikiwords start with a capital.
if (len(word)>3): # guard against small words ('A','I') blowing out the program
if word[1] in string.ascii_lowercase: # next letter MUST be lowercase
if (len(word)>3): # is there any more to process?
if processword(word[2:]): # is there another dipthong?
return 1 # YES! it's a wikiword!
return 0 # it's not a wikiword.
#
#
#
#
# this group processes the raw page to convert it to html code- but not wikiwords or links.
index=0
for line in page:
line=string.replace(line,'<','<') # kills html tag opener
line=string.replace(line,'>','>') # kills html tag closer
if line == "": # convert nulllines with P tag
line=' <P>' # process the line for substrings
line=string.replace(line,'\r',' <br> \r') # end lines with a BR
line=string.replace(line,'----','<hr>') # create standard <hr>
line=string.replace(line,'@!','<center>') # open centering
line=string.replace(line,'!@','</center>') # close centering
line=string.replace(line,"```","<b>") # open BOLD
line=string.replace(line,"'''","</b>") # close BOLD
line=string.replace(line,"``","<i>") # open ITALIC
line=string.replace(line,"''","</i>") # Close Italic
line=string.replace(line,"{{{","<pre>") # open PREFORMATTED TEXT
line=string.replace(line,"}}}","</pre>") # close PREFORMATTED TEXT
line=string.replace(line,'[=',' <table border="0" cellpadding="5" width=100% bgcolor="'+tablebgcolor+'"><tr><td><B><big>') # create header bar
line=string.replace(line,'=]','</big></b></td></tr></table>') # end header bar
line=string.replace(line,'{{{','<pre>') # start preformastted text
line=string.replace(line,'}}}','<pre>') # end it.
line=string.replace(line,'#! ','<img src="../images/') # start an image tag
line=string.replace(line,' !#','">') # end an image tag
line=string.replace(line,'[-','<ul> ') # start an unordered list
line=string.replace(line,'-]',' </ul> ') # end an unordered list
line=string.replace(line,'[#','<ol> ') # start an ordered list
line=string.replace(line,'#]','</ol> ') # end an ordered list
line=string.replace(line,'* ',' <li> ') # declare a list item
page[index]=line # save resulting line
index=index+1 # and increase the line pointer.
#
# wordscanner routine
linecounter=0 # reset the linepointer we will use it again...
for line in page: # see?
wordcounter=0 # start the word pointer over at 0 again
wordlist = string.split(line,' ') # split the line into a list of words.
for word in wordlist: # massage every word
if ((isin(word,'http://')) or (isin(word,'mailto:'))): # if this seems to be a hyperlink:
if isin(word,'"'): # DO NOT process a "http- it's imbedded code!
pass # DO NOT process; leave the word alone!
else: # otherwise, make a hyperlink for them to click.
wordlist[wordcounter]='<a href="' + word + '">' + word + '</a> '
else:
if iswikiword(word):
wordlist[wordcounter]=makewikiword(word)
else:
pass
wordcounter=wordcounter+1
line=string.join(wordlist,' ')
page[linecounter]=line
linecounter=linecounter+1
# print out the final highly modified page.
for line in page:
print line,
#
# Page footer follows
print '\r<p></div><table border="0" width=100% cellpadding="10" bgcolor="'+tablebgcolor+'">'
print '<tr>'
print '<td width=30% >'
if not os.access(pagename,os.W_OK):
print 'This page is Read-Only</td>'
else:
print '<a href="./MWed1.py?'+pagename+'">Edit '+pagename+'</a></td>'
print '<td align="center" width=40% ><form method="get" action="./MWbacksearch.py?">'
print '<input type="text" size="20" maxlength="20" name=""> <input type=submit value="SEARCH">'
print '</form></td>'
print '<td align="right" width=30% ><a href="./MWlistall.py">LIST ALL PAGES</a></td></tr>'
print '<tr><td><a href="http://www.tinylist.org/WW/">WindowsWiki V:1.4.0</a></td>'
print '<td align="center"><a href="/">Site home page</a></td>'
print '<td align="right"><a href="./MW.py?FrontPage">FrontPage</a></td></tr></table>'
print '</body></html>'
--
Salute!
-Kirk Bailey
Think
+-----+
| BOX |
+-----+
knihT
Fnord.
More information about the Tutor
mailing list