modulecomponents.py :  » Template-Engines » Myghty » Myghty-1.1 » examples » common » 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 » Myghty 
Myghty » Myghty 1.1 » examples » common » modulecomponents.py
import myghty.component
import myghty.request as request
import highlight
import os
import posixpath as unixpath

"""module components for source code viewing, path translation dhandler.

this package illustrates how to create small template-based components 
within a module component, and also how to write a dhandler as a module 
component."""

class ViewSource(myghty.component.ModuleComponent):
    def do_component_init(self, **params):
        comp = """
            <%global>
                import posixpath as unixpath
            </%global>
            <%args>
                content
                name
                path
                uri
            </%args>

            <%method title>
                <%args scope="subrequest">
                name
                </%args>
                Source of <% name %>
            </%method>
            
            <span class="smallheader">Source of <a href="<% uri %>"><% path %>/</a><% name %></span>
<a href="<% name %>?type=plain">(download)</a>

<pre class="source">
<% content %>
</pre>
        """
        
        dirlist = """
        <%args>
            directories
            files
            name
            binfiles
            parent
            path
        </%args>

            <%method title>
                <%args scope="subrequest">
                name
                </%args>
                Listing of <% name %>
            </%method>

            <div class="smallheader">Source Code Listing of <a href="<% parent %>"><% path %>/</a><% name %>/</div>
            
            <br/>
    <div class="filelist">
    <a href="<% parent %>">(parent directory)</a><br/>
%    for directory in directories:
    <a href="<% directory %>/index"><% directory %>/</a><br/>
%

%    for file in files:
    <a href="<% file %>"><% file %></a><br/>
%

%    for file in binfiles:
    <% file %><br/>
%

    </div>
"""
        self.viewsource = request.instance().interpreter.make_component(comp)
        self.dirlisting = request.instance().interpreter.make_component(dirlist)
        
    def do_run_component(self, m, r, ARGS, **params):
        filename = r.filename

        source_uri = m.interpreter.attributes['source_uri']

        if filename.endswith('/index'):
            (filename, index) = unixpath.split(filename)
        
        fileuri = filename[len(unixpath.commonprefix([m.interpreter.attributes['source_root'], filename])):]

        if os.path.isdir(filename):
            listing = os.listdir(filename)
            files = filter(lambda f: not f.endswith(".pyc") and os.path.isfile(os.path.join(filename, f)), listing)
            binfiles = filter(lambda f: f.endswith(".pyc") and os.path.isfile(os.path.join(filename, f)), listing)
            directories = filter(lambda f: os.path.isdir(os.path.join(filename, f)), listing)
            (path, name) = unixpath.split(fileuri)
            (parent, n2) = unixpath.split(m.get_request_path())
            parent = unixpath.join(parent, '..', 'index')    
            m.subexec(self.dirlisting, directories = directories, files = files, binfiles = binfiles, path=path, name=name, parent = parent)
        else:
            try:
                f = file(filename)
            except IOError:
                m.abort(404)
    
            r.content_type = 'text/html'        
            s = f.read()
   
            if ARGS.get('type', None) == 'plain':
                 r.content_type = 'text/plain'
                 m.write(s)
                 return
 
            s = highlight.highlight(s, filename = fileuri)
    
            (path, name) = unixpath.split(fileuri)
            (uri, n2) = unixpath.split(m.get_request_path())
            uri = unixpath.join(uri, 'index')    
            m.subexec(self.viewsource, content = s, uri = uri, path = path, name=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.