Changeset 3910 for TI01-discovery
- Timestamp:
- 19/05/08 17:15:57 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/molesReadWrite.py
r3800 r3910 1 #Dominic Lowe, BADC 18 October 2006 2 #Updated Sue Latham, BADC 6 December 2006 - add dgStructuredKeyword class 3 4 import cElementTree as ET 5 import sys 1 try: #python 2.5 2 from xml.etree import cElementTree as ET 3 except ImportError: 4 try: 5 # if you've installed it yourself it comes this way 6 import cElementTree as ET 7 except ImportError: 8 # if you've egged it this is the way it comes 9 from ndgUtils.elementtree import cElementTree as ET 10 import sys, re 6 11 7 12 … … 24 29 #if this element is a parent of other elements (excluding annotation and documentation) 25 30 #then return True else return false 26 if elem [:]==[]:31 if elem.getchildren()==[]: 27 32 return False 28 for child in elem [:]:33 for child in elem.getchildren(): 29 34 if child.tag == '{http://www.w3.org/2001/XMLSchema}element': 30 35 return True … … 59 64 60 65 return False 61 62 63 66 64 67 65 68 def __checkChildren(self, parentclassname, elem): 66 69 #if parentclassname == 'dgMetadataProvenance': 67 70 #pdb.set_trace() 68 for child in elem [:]:71 for child in elem.getchildren(): 69 72 childname = None 70 73 if child.attrib.has_key('name'): … … 89 92 tmp= self.sequences[parentclassname] 90 93 tmp.append(childname) 91 self.sequences[parentclassname]=tmp 94 self.sequences[parentclassname]=tmp 92 95 if self.__isParent(child): 93 96 if not self.sequences.has_key(childname): … … 105 108 pass 106 109 107 def setup(self): 110 def setup(self): 108 111 tree = ET.parse(self.schema) 109 112 root = tree.getroot() … … 170 173 setattr(self,childname, childobj) 171 174 172 def toXML(self, molesFrag, schema=None):175 def toXML(self, molesFrag, schema=None): 173 176 if schema != None: 174 177 self.schema=schema 175 178 else: 176 179 self.schema=None 177 #print molesFrag.tag178 180 orderedAttribs=schema.lookupOrder(self.__dict__,molesFrag.tag) 179 181 for item in orderedAttribs: 180 #print "item in orderedAttribs is %s" %item181 182 if type(item) is xmlSchema: 182 183 continue 183 184 if item == '{http://ndg.nerc.ac.uk/moles}': 184 185 continue 186 187 # avoid abstractText being output twice 188 # NB, this is a hack to fix the problem of this class not handling 189 # namespaces correctly - ideally this is what should be fixed here 190 if str(molesFrag.tag) == "abstract": 191 if len(molesFrag) > 0: 192 continue 193 194 frag = None 185 195 if hasattr(self, item): 186 196 if isinstance(self.__dict__[item], molesElement): … … 193 203 frag=ET.Element(item) 194 204 it.toXML(frag, schema=self.schema) 195 molesFrag.append(frag)196 205 else: 197 206 frag=ET.Element(item) 198 207 frag.text=it 199 molesFrag.append(frag) 208 209 molesFrag.append(frag) 200 210 else: 201 211 frag=ET.Element(item) 202 212 frag.text=self.__dict__[item] 203 213 molesFrag.append(frag) 214 204 215 return molesFrag 205 216 … … 224 235 else: 225 236 childWithoutNS, ns=self._stripNS(child.tag) 226 setattr(self,childWithoutNS, child.text) 237 238 # escape any special characters - mainly required for 239 # >, etc which get expanded out whilst processing the 240 # elements here 241 value = self.escapeSpecialCharacters(child.text) 242 setattr(self,childWithoutNS, value) 243 244 def escapeSpecialCharacters(self, text): 245 ''' 246 Escape any special characters included in the input text string 247 @param text: string to escape special characters from 248 @return: string with special characters escaped 249 ''' 250 subtool=subAI() 251 s=subtool.sub(text) 252 #if text != s: 253 # print "NB, adjusted string content: %s -> %s" %(text, s) 254 return s 255 256 257 class subAI: 258 ''' This is Alan Iwi's substitute and replace orphan <> code ''' 259 def __init__(self): 260 self.r1=re.compile('<([^>]*(<|$))') 261 self.r2=re.compile('((^|>)[^<]*)>') 262 def sub(self,s): 263 if s is None: 264 return s 265 266 old='' 267 while s != old: 268 old=s 269 s=self.r1.sub(r'<\1',s) 270 s=self.r2.sub(r'\1>',s) 271 return s 227 272 228 273 … … 253 298 ['dataModelID', \ 254 299 'dgDataGranule', \ 300 'dgGranuleSummary',\ 301 'dgGranuleName',\ 302 'simpleCondition',\ 303 'dgSecurityCondition',\ 304 'accessControlPolicy',\ 255 305 'dgDataEntity', \ 256 306 'dgMetadataRecord', \ … … 263 313 'dgTemporalCoverage', \ 264 314 'dgBoundingBox', \ 315 'dgArea',\ 265 316 'DateRange', \ 266 317 'dgDataSummary', \ … … 279 330 'dgValidTermParentID', \ 280 331 'dgValidSubterm', \ 332 'ListLevel',\ 281 333 'metadataDescriptionID', \ 282 334 'dgMetadataDescription', \ … … 284 336 'abstract',\ 285 337 'descriptionSection',\ 338 'dgReferenceClass',\ 286 339 'descriptionOnlineReference',\ 287 340 'dgSimpleLink',\ … … 318 371 'DataProductionToolID',\ 319 372 'ObservationStationID',\ 373 'ObsStationDeployment',\ 320 374 'dgPrincipalInvestigator',\ 321 375 'dgInvestigator',\ … … 323 377 'dgCruise',\ 324 378 'RelatedDeployment',\ 379 'dgModel',\ 380 'DPTDeployment',\ 381 'dgInstrument',\ 382 'dgDataProductionTool',\ 383 'dgDPTRole',\ 384 'dgObservationStation',\ 385 'dgStationaryPlatform',\ 386 'dgMovingPlatform',\ 387 'dgLandStation',\ 388 'dgMooring',\ 389 'position',\ 390 'dgStationGroup',\ 391 'dgShip',\ 392 'vesselType',\ 393 'RecordCreation',\ 394 'RecordUpdate',\ 395 'dgMetadataProvenance',\ 325 396 ] 326 397 … … 338 409 print key 339 410 print schema.sequences[key] 340 341 342 411 343 412 344 413 if __name__=='__main__': 345 414 main()
Note: See TracChangeset
for help on using the changeset viewer.