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

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

security info now added to csmldocs during scanning

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        acp=csml.parser.AccessControlPolicy()
91        if self.secallow:
92            for condition in self.secallow.split(','):
93                dgSecurityCondition=csml.parser.DgSecurityCondition()
94                dgSecurityCondition.effect=csml.parser.csString('allow')
95                dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
96                acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
97        if self.secdeny:
98            for condition in self.secallow.split(','):
99                dgSecurityCondition=csml.parser.DgSecurityCondition()
100                dgSecurityCondition.effect=csml.parser.csString('deny')
101                dgSecurityCondition.addChildElem('simpleCondition', _buildSimpleCondition(condition))
102                acp.addChildElem('dgSecurityCondition', dgSecurityCondition)
103        self.ds.accessControlPolicy=acp       
104       
105    def createFeatureFileMap(self):
106        print "Create FFMAP"
107         #    print "creating filemap"
108        fmm = csml.csmllibs.csmlfiles.FileMapMaker(self.directory, self.csmlfeaturetype)
109        if self.mapping=='onetomany':
110            self.ffmap = fmm.onetomany()
111        elif self.mapping=='onetoseveral':
112            self.ffmap = fmm.onetoseveral()
113        elif self.mapping=='onetoone':
114            self.ffmap = fmm.onetoone()
115        elif self.mapping=='oneonly':
116            self.ffmap = fmm.oneonly()
117       
118    def makeFileExtracts(self):
119        print "make FEs"
120        feBuilder =csml.csmllibs.csmlfileextracts.fileExtractBuilder( self.ds, self.ffmap, self.timedimension)
121        self.ds = feBuilder.dataset_element
122        self.extractType = feBuilder.extractType
123        self.extractDictionary=feBuilder.fileExtractDictionary
124
125   
126    def createFeatureCollection(self):
127        #creates empty feature collection (list)
128        self.featureCollection = csml.parser.CSMLFeatureCollection()
129 
130    def createFeatures(self):
131        print "create Features"
132        if not hasattr(self, 'extractType'):
133            self.extractType=None
134        if not hasattr(self, 'extractDictionary'):
135            self.extractDictionary=None
136        thefeatures = csml.csmllibs.csmlfeaturetypes.featureBuilder(self.ds,self.featureCollection, self.ffmap, self.extractDictionary,self.timedimension, self.timestorage, self.spatialstorage,self.valuestorage, self.minaxes)
137               
138        #self.csml = thefeatures.csml
139        self.ds=thefeatures.ds_element
140        self.featureCollection = thefeatures.featureCollection
141        #try:
142            #self.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection, self.ffmap)       
143        #except:
144            #pass
145        setattr(self.featureCollection, 'id', csml.csmllibs.csmlextra.getRandomID())
146       
147        self.ds.featureCollection=self.featureCollection
148               
149
150          #addEnvelope needs to work for different feature types
151        self.ds.featureCollection=self.featureCollection
152
153                   
154    def insertXlinks(self):
155        self.ds=csml.csmllibs.csmlxlink.createXlinks(self.ds)
156       
157    def insertBoundingBoxes(self):
158        self.ds.featureCollection =csml.csmllibs.csmlextra.addBoundingBoxes(self.ds) 
159        self.ds.featureCollection =csml.csmllibs.csmlextra.addEnvelope(self.featureCollection) 
160       
161    def saveFile(self):
162        print "Creating CSML document and saving file"
163        #call the toXML method of the Dataset object:
164        csmlf = self.ds.toXML()
165        #parse and pretty print csml
166       
167        self.strCSML = csml.parser_extra.PrettyPrint(csmlf)
168        #tidy up elementtree style namespaces
169        self.strCSML= csml.parser_extra.removeInlineNS(self.strCSML)
170        #open file, write to it.
171        f = open(self.outputfile, "w")
172        f.write('<?xml version="1.0" encoding="UTF-8"?>')
173        f.write(self.strCSML)
174        f.close()
175
176    def printToScreen(self):
177        if self.printscreen=='1':
178            print self.strCSML
179
Note: See TracBrowser for help on using the repository browser.