input.py :  » Template-Engines » Ophelia » Ophelia » tags » 0.3.3 » 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 » 0.3.3 » ophelia » input.py
# Copyright (c) 2006-2008 Thomas Lotze
# See also LICENSE.txt

import re

import zope.interface

import ophelia.interfaces


XML_DECLARATION = re.compile("(<\?xml([^<>]*)\?>)")
CODING_PATTERN = re.compile("coding[=:]\s*\"?\s*([-\w.]+)\s*\"?")


class Splitter(object):
    """Splitter decomposing a file into Python script and template.

    Instantiate as Splitter(**options).
    """

    zope.interface.implements(ophelia.interfaces.ISplitterAPI)

    script_encoding = None
    template_encoding = None

    def __init__(self, **options):
        self.script_encoding = options.get("script_encoding", "ascii")
        self.template_encoding = options.get("template_encoding", "ascii")

    def __call__(self, content):
        """Split file content into Python script and template.

        content: str

        returns (unicode, unicode): Python script and template

        may raise ValueError if <?xml ... ?> is not closed
        """
        parts = XML_DECLARATION.split(content, 1)
        if len(parts) == 1:
            script, xml_options, template = "", "", content
            line_offset = row_offset = 0
        else:
            script, xml_declaration, xml_options, template = parts
            pre_lines = (script + xml_declaration).splitlines()
            line_offset = len(pre_lines) - 1
            row_offset = len(pre_lines[-1])

        # XXX This is a bad hack in order to keep the splitter API backwards
        # compatible during the 0.3 series:
        self._last_template_offset = (line_offset, row_offset)

        template_encoding = self.template_encoding
        coding_match = CODING_PATTERN.search(xml_options)
        if coding_match:
            template_encoding = coding_match.group(1)

        template = template.decode(template_encoding)

        script_encoding = self.script_encoding
        script = script.strip()
        if script.startswith("#"):
            lines = script.splitlines(True)
            coding_match = CODING_PATTERN.search(lines[0])
            if coding_match:
                script_encoding = coding_match.group(1)
                del lines[0]
            script = "".join(lines)

        script = script.decode(script_encoding)

        return script, template
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.