Changeset 60
- Timestamp:
- 12/18/05 13:16:54 (3 years ago)
- Files:
-
- branches/timcommithook/pyramid/dictutils.py (modified) (1 diff)
- branches/timcommithook/pyramid/flatteners.py (modified) (5 diffs)
- branches/timcommithook/pyramid/pyramid (modified) (1 diff)
- branches/timcommithook/pyramid/yamlRegistry.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/timcommithook/pyramid/dictutils.py
r50 r60 5 5 def merge(a,b,dir=None): 6 6 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: 8 8 # special case. If b is the same as a, it has been inhereited - sectionnav's 9 9 # should not inherit in this fashion however branches/timcommithook/pyramid/flatteners.py
r54 r60 85 85 class AcquireFlattener(components.Adapter): 86 86 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 87 89 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 90 97 data = inheritAndMergeLocals(self,ctx,dir,name,parentdata).get(key,{}) 91 98 return flatten(data, ctx, dir) 92 99 93 100 def acquireTemplate(ctx,template): 101 """ recursively ascends the data directory looking for an appropriate template 102 """ 94 103 searchpath = ctx.path 95 104 while searchpath != ctx.root: … … 107 116 def flatten(self, ctx, dir): 108 117 118 # Get the canonical directory and filename for this file 109 119 dir,name = os.path.split(os.path.join(dir,self.original.file)) 110 120 … … 112 122 print '=== Current Fragment ( %s )' % name 113 123 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 116 132 mergedData = inheritAndMergeLocals(self,ctx,dir,name,parentdata) 117 133 134 # Flatten the resultant data ready for use in the template 118 135 flattenedMergedData = flatten( mergedData, ctx, dir ) 119 136 … … 129 146 ''' 130 147 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 176 def getFragmentData(self,ctx,dir,name,parentdata): 177 ''' Get the fragment data and assign it to self.original, preserving the filename 139 178 ''' 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 189 def 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 ''' 143 193 parentpath = str(ctx.path.parent) 144 194 parentdata = copy.deepcopy(ctx.data.get(parentpath,{}).get(name,Y.fragment({}))) 145 try:146 # read then parse the file147 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 up151 # also only looks up a single directory152 self.original = copy.deepcopy(parentdata)153 195 return parentdata 154 196 155 197 def inheritAndMergeLocals(self,ctx,dir,name,parentdata): 156 198 ''' store a copy of the data (merged with it's parentdata) on the context and then merge it with the current local data … … 165 207 # store it on the context 166 208 # 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 167 210 node = Y.fragment( { 168 211 'template':self.original.template, 169 212 'global':dictutils.merge(parentdata.globals.copy(),globals,dir=path(dir).normpath()), 170 'local':{} 213 'local':locals, 214 'file':self.original.file 171 215 } ) 172 216 # 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 2 2 import sys, os 3 3 #sys.path.insert(0, os.path.join(sys.prefix, 'lib/pyramid/dependencies')) branches/timcommithook/pyramid/yamlRegistry.py
r54 r60 13 13 self.locals = node.get('local',{}) 14 14 self.globals = node.get('global',{}) 15 self.file = node.get('file',None) 15 16 16 17 def __repr__(self):
