oapi.py :  » Template-Engines » Ophelia » Ophelia » tags » Ophelia-0.1 » ophelia » 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 » Template Engines » Ophelia 
Ophelia » Ophelia » tags » Ophelia 0.1 » ophelia » oapi.py
# Python
import os
import inspect

# Zope
from zope.tales.engine import Engine
from zope.tal.htmltalparser import HTMLTALParser
from zope.tal.talgenerator import TALGenerator

# project
from ophelia import publisher,template


########################
# exceptions and classes

StopTraversal = publisher.StopTraversal
NotFound = publisher.NotFound

Namespace = publisher.Namespace


###########
# functions

def getScriptGlobals():
    for frame_record in inspect.stack():
        candidate = frame_record[0].f_globals
        if type(candidate) == publisher._ScriptGlobals:
            return candidate
    else:
        raise LookupError("Could not find script globals.")


def getLogError():
    return getScriptGlobals()["log_error"]

def getContext():
    return getScriptGlobals()["context"]

def getMacros():
    return getScriptGlobals()["macros"]

def getRequest():
    return getScriptGlobals()["request"]

def getTraversal():
    return getScriptGlobals()["traversal"]

def getTalesNames():
    return getScriptGlobals()["tales_names"]


def loadMacros(*args):
    traversal = getTraversal()
    macros = getMacros()
    log_error = getLogError()
    for name in args:
        file_path = os.path.join(os.path.dirname(traversal.file_path), name)
        try:
            content = file(file_path).read()
        except:
            log_error("Can't read macro file at " + file_path)
            raise

        script, template_ = template.split(content, traversal)
        if script:
            log_error("Macro file contains a script at " + file_path)
            raise ValueError("Macro file contains a script at " + file_path)

        generator = TALGenerator(TALESEngine, xml=False,
                                 source_file=file_path)
        parser = HTMLTALParser(generator)

        try:
            parser.parseString(template_)
        except:
            log_error("Can't compile template at " + file_path)
            raise

        program, macros_ = parser.getCode()
        macros.__dict__.update(macros_)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.