Changeset 60

Show
Ignore:
Timestamp:
12/18/05 13:16:54 (3 years ago)
Author:
tim
Message:

updated code to ensure it produces exactly the same output as before refactoring, it is also generating the correct cache files for each directory

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/timcommithook/pyramid/dictutils.py

    r50 r60  
    55def merge(a,b,dir=None): 
    66     
    7     if isinstance(a,Y.sectionnav) and isinstance(b,Y.sectionnav)
     7    if isinstance(a,Y.sectionnav) and isinstance(b,Y.sectionnav) and dir is not None
    88        # special case. If b is the same as a, it has been inhereited - sectionnav's 
    99        # should not inherit in this fashion however 
  • branches/timcommithook/pyramid/flatteners.py

    r54 r60  
    8585class AcquireFlattener(components.Adapter): 
    8686    def flatten(self, ctx, dir): 
     87        # An acquite flattener gets the data from another file (i.e. if you want the nav data from the main index file. 
     88        # original.file is the filename of the data file and orginal.name is the dictionary key 
    8789        key = self.original.name 
    88         # get the parent data for the breadcrumb's file and dictionary key 
    89         dir, name, parentdata = getFragmentData(self,ctx,dir) 
     90        # Get the canonical directory and filename for this file 
     91        dir,name = os.path.split(os.path.join(dir,self.original.file)) 
     92        # get the parent data for the current file 
     93        parentdata = getParentData(self,ctx,dir,name) 
     94        # Add the template data (locals,globals and template) to self.original 
     95        getFragmentData(self,ctx,dir,name,parentdata) 
     96        # inherit data from parent and merge the result with any 'local' data 
    9097        data = inheritAndMergeLocals(self,ctx,dir,name,parentdata).get(key,{}) 
    9198        return flatten(data, ctx, dir) 
    9299 
    93100def acquireTemplate(ctx,template): 
     101    """ recursively ascends the data directory looking for an appropriate template 
     102    """  
    94103    searchpath = ctx.path 
    95104    while searchpath != ctx.root: 
     
    107116    def flatten(self, ctx, dir): 
    108117 
     118        # Get the canonical directory and filename for this file 
    109119        dir,name = os.path.split(os.path.join(dir,self.original.file)) 
    110120 
     
    112122            print '=== Current Fragment ( %s )' % name 
    113123         
    114         parentdata = getFragmentData(self,ctx,dir,name) 
    115  
     124 
     125        # get the parent data for the current file 
     126        parentdata = getParentData(self,ctx,dir,name) 
     127 
     128        # Add the template data (locals,globals and template) to self.original 
     129        getFragmentData(self,ctx,dir,name,parentdata) 
     130 
     131        # inherit data from parent and merge the result with any 'local' data 
    116132        mergedData = inheritAndMergeLocals(self,ctx,dir,name,parentdata) 
    117133 
     134        # Flatten the resultant data ready for use in the template 
    118135        flattenedMergedData = flatten( mergedData, ctx, dir ) 
    119136 
     
    129146    ''' 
    130147    def flatten(self, ctx, dir): 
    131         # Search for the template in this directory and above 
    132         #dir, name = os.path.split(os.path.join(dir,self.original.file)) 
    133         template = acquireTemplate(ctx,self.original.template) 
    134         flattenedData = flatten( self.original.globals, ctx, dir ) 
    135         return T.xml(page.Fragment(os.path.join(dir,template), flattenedData).generate())     
    136  
    137 def getFragmentData(self,ctx,dir,name): 
    138     ''' this is a file fragment so try to parse 
     148        if self.original.file is not None: 
     149            # Search for the template in this directory and above 
     150            template = acquireTemplate(ctx,self.original.template) 
     151            flattenedData = flatten( self.original.globals, ctx, dir ) 
     152            return T.xml(page.Fragment(os.path.join(dir,template), flattenedData).generate())     
     153        else: 
     154            # Get the canonical directory and filename for this file 
     155            dir,name = os.path.split(os.path.join(dir,self.original.file)) 
     156     
     157            if ctx.verbose: 
     158                print '=== Current Fragment ( %s )' % name 
     159             
     160            # get the parent data for the current file 
     161            parentdata = getParentData(self,ctx,dir,name) 
     162 
     163            # inherit data from parent and merge the result with any 'local' data 
     164            mergedData = inheritAndMergeLocals(self,ctx,dir,name,parentdata) 
     165     
     166            # Flatten the resultant data ready for use in the template 
     167            flattenedMergedData = flatten( mergedData, ctx, dir ) 
     168     
     169            if ctx.verbose: 
     170                logPageData(mergedData) 
     171     
     172            # Search for the template in this directory and above 
     173            template = acquireTemplate(ctx,self.original.template) 
     174            return T.xml(page.Fragment(os.path.join(dir,template), flattenedMergedData).generate()) 
     175 
     176def getFragmentData(self,ctx,dir,name,parentdata): 
     177    ''' Get the fragment data and assign it to self.original, preserving the filename 
    139178    '''  
    140          
    141     # get the parents data for this .yml fie (or set it as an empty fragment if not available) 
    142     # this currently only looks 'up' one directory 
     179    try: 
     180        yaml = file(os.path.join(dir,name)).read() 
     181        selffile = self.original.file 
     182        self.original = syck.load(yaml,Loader=Y.Loader, implicit_typing=False) 
     183        self.original.file = selffile 
     184    except: 
     185        # if the file can't be read. fall back to the parent data 
     186        self.original = copy.deepcopy(parentdata) 
     187 
     188 
     189def getParentData(self,ctx,dir,name): 
     190    ''' get the parents data for this .yml fie (or set it as an empty fragment if not available) 
     191        this currently only looks 'up' one directory 
     192    '''  
    143193    parentpath = str(ctx.path.parent) 
    144194    parentdata = copy.deepcopy(ctx.data.get(parentpath,{}).get(name,Y.fragment({}))) 
    145     try: 
    146         # read then parse the file 
    147         yaml = file(os.path.join(dir,name)).read() 
    148         self.original = syck.load(yaml,Loader=Y.Loader, implicit_typing=False) 
    149     except: 
    150         # if the file can't be read. fall back to the parent data. This is to allow a missing file to be inherited from further up 
    151         # also only looks up a single directory 
    152         self.original = copy.deepcopy(parentdata) 
    153195    return parentdata 
    154          
     196 
    155197def inheritAndMergeLocals(self,ctx,dir,name,parentdata): 
    156198    ''' store a copy of the data (merged with it's parentdata) on the context and then merge it with the current local data 
     
    165207    # store it on the context 
    166208    # take a copy of the data from this .yml file's globals and merge it with the parent data 
     209    # Also assigning the locals with the current local data 
    167210    node = Y.fragment( { 
    168211        'template':self.original.template, 
    169212        'global':dictutils.merge(parentdata.globals.copy(),globals,dir=path(dir).normpath()), 
    170         'local':{} 
     213        'local':locals, 
     214        'file':self.original.file 
    171215        } ) 
    172216    # set this fragment on the context with a key of the yaml file name 
  • branches/timcommithook/pyramid/pyramid

    r59 r60  
    1  
     1#!/usr/bin/python 
    22import sys, os 
    33#sys.path.insert(0, os.path.join(sys.prefix, 'lib/pyramid/dependencies')) 
  • branches/timcommithook/pyramid/yamlRegistry.py

    r54 r60  
    1313        self.locals = node.get('local',{}) 
    1414        self.globals = node.get('global',{}) 
     15        self.file = node.get('file',None) 
    1516 
    1617    def __repr__(self):