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

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

Adding security options back in to scanner

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