source: TI02-CSML/trunk/csml/csmllibs/csmlbuilder.py @ 3241

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/csmlbuilder.py@3241
Revision 3241, 7.9 KB checked in by domlowe, 12 years ago (diff)

no security by default

Line 
1import csml
2import sys
3
4#Class for building the csml document.
5class csmlBuilder(object):
6    #this class contains all the method calls to create and populate a csmldoc
7    #it needs to be provided with several bits of information to be able to do this.
8    def __init__(self,datasetid, directory,csmlft, mapping, timedimension ,outputfile,printscreen,timestorage,spatialstorage,valuestorage, minaxes, secallow, secdeny):
9        if datasetid is not None:
10            self.datasetid = datasetid
11        else:
12            self.datasetid =csml.csmllibs.csmlextra.getRandomID()
13        self.directory = directory # top level directory containing data files
14        self.csmlfeaturetype  = csmlft
15        self.mapping = mapping
16        self.timedimension=timedimension
17        self.outputfile=outputfile
18        self.printscreen=printscreen
19        self.timestorage=timestorage
20        self.spatialstorage=spatialstorage
21        self.valuestorage=valuestorage
22        self.minaxes=minaxes
23        self.csml = None  # csml object holds the csml document in memory.
24        self.ds_element = None #<Dataset> tag, root node.
25        self.secallow=secallow
26        self.secdeny=secdeny
27         
28    def build(self):
29        #wrapper method to call methods in correct order.
30        self.createDataset()
31        try:
32            self.addGMLMetadata()
33        except TypeError:
34            #this is caused by an incorrect directory name
35            print 'error: Cannot find a config file at this location.'
36            sys.exit()
37        self.addSecurity()
38        self.createFeatureFileMap()
39        if self.spatialstorage=='fileextract':
40           self.makeFileExtracts()
41        self.createFeatureCollection()
42        self.createFeatures()
43        self.insertXlinks()
44        try:
45            self.insertBoundingBoxes()
46        except:
47            print 'warning, scanner could not calculate bounding boxes'
48        self.saveFile()
49        self.printToScreen()
50
51    def createDataset(self):
52        #*************************************************************************************
53        #Creates empty CSML Dataset object with standard attributes
54        #*************************************************************************************
55             
56        ########### The Dataset  ##############
57        #Create an Empty Dataset
58        self.ds = csml.parser.Dataset()
59        #Use random id for dataset id.
60        setattr(self.ds,'id',self.datasetid)       
61        setattr(self.ds, 'name', csml.parser.csString('NAME OF DATASET GOES HERE'))
62        ########################################
63
64    def addGMLMetadata(self):
65        #*********************************************************************************
66        # Adds main GML Metadata elements to dataset element
67        #*********************************************************************************
68        #Note: TODO: The Parser has a problem with gml:Name from AbstractGML.
69        strMetaDataProperty = 'http://ndg.nerc.ac.uk/Metadata' + self.directory
70        strCodespace = 'http://ndg/nerc/ac/uk/NDGData'
71        strGmlDescription = 'Dataset for directory ' + self.directory
72       
73        #TODO for V2
74        #self.ds.metaDataProperty=[csml.parser.MetaDataProperty(href=strMetaDataProperty)]
75        #self.ds.name=csml.parser.Name( codeSpace=strCodespace, val=[self.directory])
76        #self.ds.description=csml.parser.Description(strGmlDescription)
77   
78    def addSecurity(self):       
79        '''Adds NDG style AccessControlPolicy to the dataset based on info in config file'''
80        def _buildSimpleCondition(condition):
81            #builds the tag structure for a SimpleCondition
82            scond=csml.parser.SimpleCondition()
83            parts=condition.split(' ')
84            attauth=csml.parser.csString(parts[0])
85            role=csml.parser.csString(parts[1]) 
86            scond.attrauthRole=role
87            scond.dgAttributeAuthority=attauth
88            return scond
89       
90        #don't do anything if no config options are set.
91        if not self.secallow:
92            if not self.secdeny:
93                return None
94           
95       
96        acp=csml.parser.AccessControlPolicy()
97        if self.secallow:
98            for condition in self.secallow.split(','):
99                dgSecurityCondition=csml.parser.DgSecurityCondition()
100                dgSecurityCondition.effect=csml.parser.csString('allow')
101                dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
102                acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
103        if self.secdeny:
104            for condition in self.secallow.split(','):
105                dgSecurityCondition=csml.parser.DgSecurityCondition()
106                dgSecurityCondition.effect=csml.parser.csString('deny')
107                dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
108                acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
109        self.ds.accessControlPolicy=acp       
110       
111    def createFeatureFileMap(self):
112        print "Create FFMAP"
113         #    print "creating filemap"
114        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
115        if self.mapping=='onetomany':
116            self.ffmap = fmm.onetomany()
117        elif self.mapping=='onetoseveral':
118            self.ffmap = fmm.onetoseveral()
119        elif self.mapping=='onetoone':
120            self.ffmap = fmm.onetoone()
121        elif self.mapping=='oneonly':
122            self.ffmap = fmm.oneonly()
123       
124    def makeFileExtracts(self):
125        print "make FEs"
126        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
127        self.ds = feBuilder.dataset_element
128        self.extractType = feBuilder.extractType
129        self.extractDictionary=feBuilder.fileExtractDictionary
130
131   
132    def createFeatureCollection(self):
133        #creates empty feature collection (list)
134        self.featureCollection = csml.parser.CSMLFeatureCollection()
135 
136    def createFeatures(self):
137        print "create Features"
138        if not hasattr(self, 'extractType'):
139            self.extractType=None
140        if not hasattr(self, 'extractDictionary'):
141            self.extractDictionary=None
142        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage, self.minaxes)
143               
144        #self.csml = thefeatures.csml
145        self.ds=thefeatures.ds_element
146        self.featureCollection = thefeatures.featureCollection
147        #try:
148            #self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
149        #except:
150            #pass
151        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
152       
153        self.ds.featureCollection=self.featureCollection
154               
155
156          #addEnvelope needs to work for different feature types
157        self.ds.featureCollection=self.featureCollection
158
159                   
160    def insertXlinks(self):
161        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
162       
163    def insertBoundingBoxes(self):
164        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds) 
165        self.ds.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection) 
166       
167    def saveFile(self):
168        print "Creating CSML document and saving file"
169        #call the toXML method of the Dataset object:
170        csmlf = self.ds.toXML()
171        #parse and pretty print csml
172       
173        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
174        #tidy up elementtree style namespaces
175        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
176        #open file, write to it.
177        f = open(self.outputfile, "w")
178        f.write('<?xml version="1.0" encoding="UTF-8"?>')
179        f.write(self.strCSML)
180        f.close()
181
182    def printToScreen(self):
183        if self.printscreen=='1':
184            print self.strCSML
185
Note: See TracBrowser for help on using the repository browser.