LDP/LDP/lampadas/pylib/HTML.py

417 lines
16 KiB
Python
Raw Normal View History

#!/usr/bin/python
2002-06-19 20:30:18 +00:00
#
# This file is part of the Lampadas Documentation System.
#
# Copyright (c) 2000, 2001, 2002 David Merrill <david@lupercalia.net>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Lampadas HTML Primitives Module
This module generates HTML primitives and web pages for the WWW front-end
to the Lampadas system.
"""
# Modules ##################################################################
from Globals import *
2002-06-19 20:30:18 +00:00
from Config import config
from Log import log
from URLParse import URI
2002-07-07 21:32:26 +00:00
from DataLayer import *
from SourceFiles import sourcefiles
2002-06-20 16:06:38 +00:00
from WebLayer import lampadasweb
from Tables import tables, tablemap
from Widgets import widgets
from Lintadas import lintadas
from Sessions import sessions
import commands
import string
2002-05-21 22:55:11 +00:00
import sys
2002-06-19 20:30:18 +00:00
import os
import time
import fpformat
import re
# Globals
# PageFactory
class PageFactory:
elapsed_time = 0
2002-06-19 20:30:18 +00:00
def page_exists(self, key):
uri = URI(key)
if uri.path=='' and lampadasweb.pages[uri.page_code]:
return 1
return
def page(self, uri):
if sessions.session:
log(3, 'user: ' + sessions.session.username)
page = lampadasweb.pages[uri.page_code]
if page==None:
page = lampadasweb.pages['404']
assert not page==None
html = self.build_page(page, uri)
2002-06-19 20:30:18 +00:00
2002-06-20 16:06:38 +00:00
return html
2002-06-19 20:30:18 +00:00
def build_page(self, page, uri):
start_time = time.time()
2002-06-20 16:06:38 +00:00
template = lampadasweb.templates[page.template_code]
assert not template==None
2002-06-20 16:06:38 +00:00
html = template.template
2002-06-19 20:30:18 +00:00
html = self.replace_tokens(page, uri, html)
end_time = time.time()
self.elapsed_time = end_time - start_time
html = html.replace('DCM_ELAPSED_TIME', fpformat.fix(self.elapsed_time, 3))
html = html.replace('DCM_PIPE', '|')
return html
def replace_tokens(self, page, uri, html):
temp = html.replace('\|', 'DCM_PIPE')
pos = temp.find('|')
2002-06-19 20:30:18 +00:00
while pos <> -1 :
temp = temp.replace('\|', 'DCM_PIPE')
pos2 = temp.find('|', pos+1)
if pos2==-1:
2002-06-19 20:30:18 +00:00
pos = -1
else:
token = temp[pos+1:pos2]
2002-07-13 02:41:25 +00:00
newstring = None
2002-06-19 20:30:18 +00:00
# System diagnostic tokens
if token=='elapsed_time':
2002-07-29 16:20:11 +00:00
newstring = 'DCM_ELAPSED_TIME'
# Session Tokens
elif token=='session_id':
if sessions.session:
newstring = sessions.session.user.session_id
else:
newstring = ''
elif token=='session_username':
if sessions.session:
newstring = sessions.session.username
else:
newstring = ''
elif token=='session_name':
if sessions.session:
newstring = sessions.session.user.name
else:
newstring = ''
elif token=='session_user_docs':
if sessions.session:
newstring = tables.userdocs(uri, username=sessions.session.username)
else:
newstring = '|nopermission|'
2002-07-04 14:24:54 +00:00
# Page Meta-data
elif token=='title':
2002-07-04 05:31:59 +00:00
newstring = page.title[uri.lang]
elif token=='body':
if page.only_registered==1 and sessions.session==None:
newstring = '|blknopermission|'
elif page.only_admin==1 and (sessions.session==None or sessions.session.user.admin==0):
newstring = '|blknopermission|'
elif page.only_sysadmin==1 and (sessions.session==None or sessions.session.user.sysadmin==0):
newstring = '|blknopermission|'
else:
newstring = page.page[uri.lang]
elif token=='base':
newstring = 'http://' + config.hostname
if config.port > '':
newstring = newstring + ':' + config.port
newstring = newstring + config.root_dir
2002-07-13 02:41:25 +00:00
# URI Meta-data
elif token=='uri.lang_ext':
2002-07-13 02:41:25 +00:00
newstring = uri.lang_ext
elif token=='uri.base':
2002-07-04 05:31:59 +00:00
newstring = uri.base
elif token=='uri.page_code':
newstring = uri.page_code
elif token=='uri.id':
newstring = str(uri.id)
elif token=='uri.code':
newstring = uri.code
elif token=='uri.filename':
newstring = uri.filename
2002-07-04 05:31:59 +00:00
2002-07-04 14:24:54 +00:00
# Configuration Tokens
elif token=='hostname':
2002-07-04 14:24:54 +00:00
newstring = config.hostname
elif token=='rootdir':
2002-07-04 14:24:54 +00:00
newstring = config.root_dir
elif token=='port':
2002-07-04 14:24:54 +00:00
newstring = str(config.port)
elif token=='stylesheet':
if sessions.session:
newstring = sessions.session.user.stylesheet
2002-07-05 07:00:49 +00:00
else:
newstring='default'
elif token=='version':
2002-07-04 14:24:54 +00:00
newstring = VERSION
###########################################
# Tokens for when a page embeds an object #
###########################################
# Embedded User
elif token=='user.username':
newstring = uri.username
elif token=='user.name':
user = lampadas.users[uri.username]
if user:
newstring = user.name
else:
newstring = ''
elif token=='user.docs':
newstring = tables.userdocs(uri, uri.username)
# Embedded Type
elif token=='type.name':
2002-07-04 14:24:54 +00:00
type = lampadas.types[uri.code]
if not type:
newstring = '|blknotfound|'
else:
newstring = type.name[uri.lang]
# Embedded Topic
elif token=='topic.name':
2002-07-04 14:24:54 +00:00
topic = lampadas.topics[uri.code]
if not topic:
newstring = '|blknotfound|'
else:
newstring = topic.name[uri.lang]
elif token=='topic.description':
2002-07-04 14:24:54 +00:00
topic = lampadas.topics[uri.code]
if not topic:
newstring = '|blknotfound|'
else:
newstring = topic.description[uri.lang]
# Embedded Collection
elif token=='collection.name':
collection = lampadas.collections[uri.code]
if not collection:
newstring = '|blknotfound|'
else:
newstring = collection.name[uri.lang]
elif token=='collection.description':
collection = lampadas.collections[uri.code]
if not collection:
newstring = '|blknotfound|'
else:
newstring = collection.description[uri.lang]
# Embedded Document
elif token=='doc.title':
doc = lampadas.docs[uri.id]
if not doc:
newstring = '|blknotfound|'
else:
newstring = doc.title
elif token=='doc.abstract':
doc = lampadas.docs[uri.id]
if not doc:
newstring = '|blknotfound|'
else:
newstring = doc.abstract
# Navigation Boxes
elif token=='navlogin':
2002-08-25 15:51:15 +00:00
newstring = tables.navlogin(uri)
elif token=='navmenus':
newstring = tables.section_menus(uri)
elif token=='navtopics':
newstring = tables.navtopics(uri)
elif token=='navtypes':
2002-08-25 15:51:15 +00:00
newstring = tables.navtypes(uri)
elif token=='navcollections':
newstring = tables.navcollections(uri)
elif token=='navsessions':
newstring = tables.navsessions(uri)
elif token=='navlanguages':
2002-08-25 15:51:15 +00:00
newstring = tables.navlanguages(uri)
# Tables
elif token=='tabcollections':
newstring = tables.tabcollections(uri)
elif token=='tabcollection':
newstring = tables.tabcollection(uri)
elif token=='tabtopics':
newstring = tables.tabtopics(uri)
elif token=='tabtopic':
newstring = tables.tabtopic(uri)
elif token=='tabmaint_wanted':
newstring = tables.doctable(uri, maintainer_wanted='1', lang=uri.lang)
elif token=='tabunmaintained':
newstring = tables.doctable(uri, maintained='0', lang=uri.lang)
elif token=='tabpending':
newstring = tables.doctable(uri, pub_status_code='P', lang=uri.lang)
elif token=='tabwishlist':
newstring = tables.doctable(uri, pub_status_code='W', lang=uri.lang)
elif token=='tabeditdoc':
newstring = tables.doc(uri)
elif token=='tabdocfiles':
newstring = tables.docfiles(uri)
elif token=='tabdocusers':
newstring = tables.docusers(uri)
elif token=='tabdocversions':
newstring = tables.docversions(uri)
elif token=='tabdoctopics':
newstring = tables.doctopics(uri)
elif token=='tabdocerrors':
newstring = tables.docerrors(uri)
elif token=='tabfile_reports':
newstring = tables.filereports(uri)
elif token=='tabfile_report':
newstring = tables.filereport(uri)
elif token=='tabdocfileerrors':
newstring = tables.docfileerrors(uri)
elif token=='tabdocnotes':
newstring = tables.docnotes(uri)
elif token=='tabdoctranslations':
newstring = tables.doctranslations(uri)
elif token=='tabcvslog':
newstring = tables.cvslog(uri)
elif token=='tabletters':
newstring = tables.letters(uri)
elif token=='tabusers':
newstring = tables.users(uri)
elif token=='tabuser':
newstring = tables.user(uri)
elif token=='tabrecentnews':
newstring = tables.recent_news(uri)
elif token=='tabtypedocs':
newstring = tables.doctable(uri, type_code=uri.code, lang=uri.lang)
elif token=='tabtopicdocs':
newstring = tables.doctable(uri, topic_code=uri.code, lang=uri.lang)
elif token=='tabsitemap':
newstring = tables.sitemap(uri)
elif token=='tabsessions':
newstring = tables.tabsessions(uri)
elif token=='tabmailpass':
newstring = tables.tabmailpass(uri)
elif token=='taberrors':
newstring = tables.errors(uri)
elif token=='tabsearch':
newstring = tables.tabsearch(uri, lang=uri.lang)
elif token=='tabsplashlanguages':
newstring = tables.tabsplashlanguages(uri)
elif token=='tablint_time_stats':
newstring = tables.tablint_time_stats(uri)
elif token=='tabmirror_time_stats':
newstring = tables.tabmirror_time_stats(uri)
elif token=='tabpub_time_stats':
newstring = tables.tabpub_time_stats(uri)
elif token=='tabpub_status_stats':
newstring = tables.tabpub_status_stats(uri)
2002-08-23 01:45:26 +00:00
elif token=='tabdoc_error_stats':
newstring = tables.tabdoc_error_stats(uri)
elif token=='tabdoc_format_stats':
newstring = tables.tabdoc_format_stats(uri)
elif token=='tabdoc_dtd_stats':
newstring = tables.tabdoc_dtd_stats(uri)
elif token=='tabdocument_tabs':
newstring = tables.tabdocument_tabs(uri)
elif token=='tabdocument_icon_box':
newstring = tables.tabdocument_icon_box(uri)
2002-06-19 20:30:18 +00:00
# Tables, Blocks and Strings
2002-07-13 02:41:25 +00:00
if newstring==None:
tablegen = tablemap[token]
if tablegen==None:
block = lampadasweb.blocks[token]
if block==None:
string = lampadasweb.strings[token]
if string==None:
log(1, 'Could not replace token ' + token)
else:
newstring = string.string[uri.lang]
2002-06-19 20:30:18 +00:00
else:
newstring = block.block
2002-06-19 20:30:18 +00:00
else:
newstring = tablegen(uri)
2002-06-19 20:30:18 +00:00
# Just use the token if the token was not found
2002-07-13 02:41:25 +00:00
if newstring==None:
2002-06-20 16:06:38 +00:00
log(1, 'Could not replace token ' + token)
newstring = token
2002-07-01 18:33:12 +00:00
# Call myself recursively before replacing, so we do replacement on it only once,
# while it is still small. Replacing text in large pages is more costly on large
# strings.
#
# Routines that build potentially very large tables should be caching their
# static portions during page build.
temp = temp.replace(temp[pos:pos2+1], self.replace_tokens(page, uri, newstring))
2002-06-19 20:30:18 +00:00
temp = temp.replace('\|', 'DCM_PIPE')
pos = temp.find('|')
return temp
2002-06-19 20:30:18 +00:00
2002-06-20 16:06:38 +00:00
page_factory = PageFactory()
2002-07-05 03:54:50 +00:00
def benchmark(url, reps):
2002-07-05 07:00:49 +00:00
from DataLayer import Lampadas
2002-07-05 03:54:50 +00:00
for x in range(0, reps):
page = page_factory.page(url)
2002-05-21 22:55:11 +00:00
def main():
2002-07-05 03:54:50 +00:00
import profile
2002-07-05 07:00:49 +00:00
import pstats
2002-06-30 17:45:57 +00:00
if len(sys.argv[1:]):
2002-07-05 03:54:50 +00:00
profile_it = 0
reps_flag = 0
2002-07-05 07:00:49 +00:00
profile_reps = 100
2002-06-30 17:45:57 +00:00
for arg in sys.argv[1:]:
2002-07-05 03:54:50 +00:00
if reps_flag:
profile_reps = int(arg)
reps_flag = 0
elif arg=='-p' or arg=='--profile':
profile_it = 1
elif arg=='-r' or arg=='--reps':
reps_flag = 1
elif profile_it > 0:
print 'Profiling, ' + str(profile_reps) + ' repetitions...'
page = page_factory.page(arg)
2002-07-05 07:00:49 +00:00
profile.run('benchmark("' + arg + '", ' + str(profile_reps) + ')', 'profile_stats')
p = pstats.Stats('profile_stats')
p.sort_stats('time').print_stats()
2002-07-05 03:54:50 +00:00
else:
print page_factory.page(URI(arg))
2002-06-30 17:45:57 +00:00
else:
profile()
2002-05-21 22:55:11 +00:00
2002-05-23 15:28:03 +00:00
if __name__=="__main__":
2002-06-19 20:30:18 +00:00
main()
2002-07-05 07:00:49 +00:00