__init__.py :  » Web-Frameworks » Zope » Zope-2.6.0 » lib » python » Products » ZopeTutorial » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » lib » python » Products » ZopeTutorial » __init__.py
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import TutorialTopic
import App.Common
import os.path
import os
import stat
from DateTime import DateTime
from urllib import quote_plus
from cgi import escape
import re
from HelpSys import APIHelpTopic


def initialize(context):
    # abuse registerClass to get a tutorial constructor
    # in the product add list
    context.registerClass(
        None,
        meta_type='Zope Tutorial',
        constructors=(TutorialTopic.addTutorialForm, TutorialTopic.addTutorial),
        )

    from App.Product import doInstall

    if not doInstall():
        return

    # create tutorial help topics
    lesson_path=os.path.join(App.Common.package_home(globals()), 'tutorial.stx')
    glossary_path=os.path.join(App.Common.package_home(globals()), 'glossary.stx')
    help=context.getProductHelp()

    # test to see if nothing has changed since last registration
    if help.lastRegistered is not None and \
            help.lastRegistered >= DateTime(os.stat(lesson_path)[stat.ST_MTIME]):
        return
    help.lastRegistered=DateTime()

    # delete old help topics
    for id in help.objectIds('Help Topic'):
        help._delObject(id)

    # create glossary
    text=open(glossary_path).read()
    text=term_pat.sub(defineTerm, text)

    glossary=TutorialTopic.GlossaryTopic('tutorialGlossary',
                                         'Zope Tutorial Glossary', text)
    context.registerHelpTopic('tutorialGlossary', glossary)

    # create lessons
    f=open(lesson_path)
    lines=[]
    id=0

    while 1:
        line = f.readline()
        if (line.strip() and line.find(' ') != 0) or line=='':
            # new topic
            if lines:
                id = id + 1
                topic_id = 'topic_%02d' % id
                text=''.join(lines[1:])
                text=term_pat.sub(glossaryTerm, text)
                topic=TutorialTopic.TutorialTopic(topic_id, lines[0].strip(), spacestrip(text))
                context.registerHelpTopic(topic_id, topic)
            lines=[line]
        else:
            lines.append(line)
        if line == '':
            break
    f.close()


def spacestrip(txt):
    """ dedent text by 2 spaces !

    We need this to workaround a nasty bug in STXNG.
    STXNG creates empty <pre>..</pre> when then text start
    if a level > 1. This fix is lame. The problem should be fixed
    inside STXNG
    """

    l = []
    for x in txt.split("\n"):
        if len(x)>2 and x[:2]=='  ':
            l.append(x[2:])
        else: l.append(x)

    return '\n'.join(l)


# Glossary functions
# ----------------

term_pat=re.compile('\[(.*?)\]')
terms=[]

def glossaryTerm(match):
    """
    A linked glossary term
    """
    name=match.group(1)
    if name in terms:
        return """<a href="../tutorialGlossary#%s">%s</a>""" % \
                (quote_plus(name), escape(name))
    return """[%s]""" % name

def defineTerm(match):
    """
    Define a glossary term
    """
    name=match.group(1)
    terms.append(name)
    return """<a name="%s"></a>\n\n<strong>%s</strong>""" % \
            (quote_plus(name), escape(name))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.