Source

psilib / processor.py

Lynn Rees 95fadba 









Lynn Rees d33aa84 
Lynn Rees acdff55 

Lynn Rees 7e57475 
Lynn Rees 95fadba 
Lynn Rees 149dda5 
Lynn Rees 0ee9637 
Lynn Rees d33aa84 
Lynn Rees 0ee9637 


Lynn Rees acdff55 

Lynn Rees 0ee9637 
Lynn Rees 7e57475 
Lynn Rees 0ee9637 
Lynn Rees acdff55 
Lynn Rees 0ee9637 







Lynn Rees 7e57475 
Lynn Rees 0ee9637 
Lynn Rees acdff55 

Lynn Rees 0ee9637 










Lynn Rees acdff55 
Lynn Rees 0ee9637 
Lynn Rees acdff55 
Lynn Rees 0ee9637 





Lynn Rees acdff55 
Lynn Rees 0ee9637 









Lynn Rees acdff55 

Lynn Rees 0ee9637 
Lynn Rees 7e57475 
Lynn Rees 0ee9637 
Lynn Rees acdff55 
Lynn Rees 0ee9637 

Lynn Rees acdff55 
Lynn Rees 0ee9637 













Lynn Rees 7e57475 
Lynn Rees 0ee9637 
Lynn Rees acdff55 

Lynn Rees 0ee9637 



Lynn Rees acdff55 
Lynn Rees 0ee9637 


Lynn Rees acdff55 
Lynn Rees 0ee9637 


Lynn Rees acdff55 
Lynn Rees 0ee9637 
Lynn Rees acdff55 
Lynn Rees 0ee9637 










Lynn Rees 7e57475 
Lynn Rees 0ee9637 


Lynn Rees acdff55 
Lynn Rees 0ee9637 





Lynn Rees d33aa84 
Lynn Rees 0ee9637 





Lynn Rees 594ee6f 
Lynn Rees acdff55 
Lynn Rees 0ee9637 


Lynn Rees acdff55 


Lynn Rees 7e57475 
Lynn Rees acdff55 
Lynn Rees 7e57475 
Lynn Rees acdff55 

Lynn Rees 0ee9637 





Lynn Rees acdff55 


Lynn Rees 7e57475 
Lynn Rees acdff55 
Lynn Rees 7e57475 
Lynn Rees acdff55 

Lynn Rees 0ee9637 

#! /usr/bin/env python

## Copyright (c) 1999 - 2003 L. C. Rees.  All rights reserved.
## See COPYRIGHT file for license terms.

'''Turns PSI maps into something usuable'''

__version__ = '0.01-test'
__author__  = 'L. C. Rees (xanimal@users.sf.net)'

import os
from psiutils import attset, attdel
from Ft.Xml.Domlette import NonvalidatingReader as nr
from psiutils import toxmlfile, tags, getname, getclass, stripws, psins

rsrc, fld, fmnt, cls, nm = u'resource', u'field', u'fragment', u'class', u'name'


class Processor:

    def _expandFragments(self, source, target):
        fields = [f for f in tags(source, psins, fld)]
        fragments = [f for f in tags(target, psins, fmnt)]
        for fragment in fragments:
            fclass = getclass(fragment)
            for field in fields:
                if fclass == getname(field):
                    for child in fragment.childNodes:
                        clone = child.cloneNode(1)
                        field.parentNode.insertBefore(clone, field)
        for field in fields: field.parentNode.removeChild(field)
    
    def _expand(self, lo, fragment, end):
        for resource in lo:
            for master in self._templates:
                if getclass(resource) == getname(master):
                    child = master.cloneNode(1)
                    attset(child, nm, getname(resource))
                    attdel(child, cls)
                    fragment(child, resource)                
                    resource.parentNode.appendChild(child)
                    resource.parentNode.removeChild(resource)
                    end(child, resource)
    
    def _cleanTemplates(self, child, resource):
        self._templates.append(child)
        self._templates.remove(resource)
    
    def _cleanUnexpanded(self):
        for resource in self._unexpanded:
            name = getname(resource)
            for template in self._templates:
                if name == getname(template): self._unexpanded.remove(resource)
            
    def _expandTemplates(self):
        self._expand(self._templates, self._expandFragments, self._cleanTemplates)
        self._cleanUnexpanded()
    
    def _exportPack(self, child, resource):
        self._exports[getname(resource)] = child.firstChild
            
    def _expandResources(self):    
        self._expand(self._unexpanded, self._expandFragments, self._exportPack)
    
    def _exportResources(self):
        for i in self._exports: toxmlfile(self._exports.get(i), i)

    def _expandFragmentsDW(self, source, target):
        tbe = 'InstanceBeginEditable name="%s"'
        tee = 'InstanceEndEditable'
        fields = [f for f in tags(source, psins, fld)]
        fragments = [f for f in tags(target, psins, fmnt)]
        for fragment in fragments:
            fclass = getclass(fragment)
            for field in fields:
                if fclass == getname(field):
                    pn = field.parentNode
                    ppn = pn.parentNode.insertBefore
                    ttbe = self._doc.createComment(tbe % getname(field))
                    ttee = self._doc.createComment(tee)
                    ppn(ttbe, field.parentNode)
                    for child in fragment.childNodes:
                        clone = child.cloneNode(1)
                        pn.insertBefore(clone, field)
                    ppn(ttee, pn.nextSibling)
        for field in fields: field.parentNode.removeChild(field)
    
    def _expandTemplatesDW(self):
        tbe = 'TemplateBeginEditable name="%s"'
        tee = 'TemplateEndEditable'
        dwtemplates = [d.cloneNode(1) for d in self._templates]
        for i in dwtemplates:
            for master in self._templates:
                if getclass(i) == getname(master):
                    child = master.cloneNode(1)
                    attset(child, nm, getname(i))
                    attdel(child, cls)
                    self._expandFragmentsDW(child, i)                
                    dwtemplates.remove(i)
                    dwtemplates.append(child)
                    i = child
                fields = [f for f in tags(i, psins, fld)]           
                for field in fields:
                    pn = field.parentNode
                    ppn = pn.parentNode.insertBefore
                    ttbe = self._doc.createComment(tbe % getname(field))
                    ttee = self._doc.createComment(tee)
                    ppn(ttbe, pn)
                    ppn(ttee, pn.nextSibling)
                    ph = self._doc.createTextNode(getname(i))
                    field.parentNode.replaceChild(ph, field)
                self._exports['/Templates/%s.dwt' % getname(i)] = i.firstChild
        self._expand(self._templates, self._expandFragments, self._cleanTemplates)
        self._cleanUnexpanded()
    
    def _expandResourcesDW(self):
        self._expand(self._unexpanded, self._expandFragmentsDW, self._exportPackDW)

    def _exportPackDW(self, child, resource):
        tbe = '''InstanceBegin template="/Templates/%s.dwt"
        codeOutsideHTMLIsLocked="false"'''
        tee = 'InstanceEnd'
        instance = child.firstChild
        ttbe = self._doc.createComment(tbe % getclass(resource))
        ttee = self._doc.createComment(tee)
        instance.insertBefore(ttbe, instance.firstChild)
        instance.appendChild(ttee)
        self._exports[getname(resource)] = instance  
    
    def _exportResourcesDW(self):
        for i in self._exports:
            try: toxmlfile(self._exports.get(i), i)
            except IOError:
                try:
                    if i.find('Templates'): 
                        toxmlfile(self._exports.get(i), i[1:])
                except IOError:
                    if i.find('Templates'):
                        os.mkdir('Templates')
                        toxmlfile(self._exports.get(i), i[1:])

    def read(self, file):
        self._doc = nr.parseUri(file)

    def todreamweaverMX(self, file=None):
        self._exports = {}
        if file: self._doc = nr.parseUri(file)
        stripws(self._doc)
        names = [getname(i) for i in tags(self._doc, psins, rsrc)]
        classes = [getclass(i) for i in tags(self._doc, psins, rsrc)]
        self._unexpanded = [i for i in tags(self._doc, psins, rsrc)
                            if getclass(i) in names]
        self._templates = [i for i in tags(self._doc, psins, rsrc)
                           if getname(i) in classes]
        self._expandTemplatesDW()
        self._expandResourcesDW()
        self._exportResourcesDW()

    def tostatic(self, file=None):        
        self._exports = {}
        if file: self._doc = nr.parseUri(file)
        stripws(self._doc)
        names = [getname(i) for i in tags(self._doc, psins, rsrc)]
        classes = [getclass(i) for i in tags(self._doc, psins, rsrc)]
        self._unexpanded = [i for i in tags(self._doc, psins, rsrc)
                            if getclass(i) in names]
        self._templates = [i for i in tags(self._doc, psins, rsrc)
                           if getname(i) in classes]
        self._expandTemplates()
        self._expandResources()
        self._exportResources()