source: exist/trunk/python/ndgUtils/ndgObject.py @ 3127

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgObject.py@3127
Revision 3127, 5.2 KB checked in by lawrence, 12 years ago (diff)

This verison of ndgUtils does build to an egg. Next we need to
get dif2moles round trip testing working ... and solve
the related URL problem, and then we can work on the changes
to ndgObject which arise ... before trying to replace
the ows_server code.

Line 
1from ndgRetrieve import ndgRetrieve
2class ndgObject:
3    ''' This class instantiates an ndgObject which describes the various ways of obtaining itself, primarily
4                (1) a downloadable xml representation from a repository,
5                (2) a printable xml representation '''
6   
7    def __init__(self,uri,config=None):
8        ''' Parse the uri and prepare for obtaining the actual content'''
9       
10        #Dom had problem with unicode coming in here ... dunno why @@@@
11        uri=str(uri) 
12       
13        # a priori, assume we can't get content for this object
14        self.gettable=-1 
15       
16        # handle all the known ways of doing an NDG URI ...
17        bits=uri.split(':')
18        bits2=uri.split('__')
19        ok=1
20        if len(bits)==3:
21            repository,schema,localID=bits
22            self.uri=uri.replace(':','__')
23        elif len(bits2)==3:
24            repository,schema,localID=bits2
25            self.uri=uri
26        elif len(bits2)>3:
27            l=bits2[2]
28            for i in bits2[3:]:l+='__%s'%i
29            repository,schema,localID=bits2[0],bits2[1],l
30            self.uri=uri
31        else:
32            bits=uri.split('/')
33            if len(bits)==2:
34                schema='NDG-B0'
35                repository,localID=bits
36                self.uri=None  #
37                ok=0  # I reckon we shouldn't ever see any of these again ...
38                # but if we do, the uri will need fixing too ...
39            else: ok=0
40       
41        if not ok:
42            # after all that, we don't think it's an NDG URI ...
43            raise ValueError,'The identifier [%s] is not a valid NDG style URI'%uri
44        else:
45            # yes, it is an NDG URI ...
46            self.repository,self.schema,self.localID=repository,schema,localID
47       
48        self.setConfig(config)
49       
50    def setConfig(self,config):
51        ''' Set up the configuration for retrieving this document '''
52        self.config=config
53        if config is None:
54            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
55            self.BURL=None
56        else:
57            self.server=self.config.get('DISCOVERY','default')
58            server=self.server
59            qs=None
60            # This NDG object may itself be a discovery record, which makes life easy, but
61            # it might not be, in which case we have to build up all the possible views upon it.
62            # But remember only data entity b records have discovery records ...
63            self.viewService='%s/view/'%server
64            discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID)
65            fmt=self.config.get('DISCOVERY','formatDefault','DIF')
66            self.BURL=None
67            # We'll build the following even if it can't be used (as would be the case for
68            # a non data entity B record or an A record) because it's a useful template.
69            if self.schema!=fmt: qs=('outputSchema',fmt)
70            self.discoveryURL=self.__buildURL(discoveryBASE,[qs])
71            servicehost=self.config.get('NDG_B_SERVICE',self.repository,'unknown')
72            # If this record is itself a discovery record, then we don't have much more to do
73            if self.schema in ['DIF','DC','MDIP','ISO19139']:
74                self.xmlURL=self.__buildURL(
75                    discoveryBASE.replace('/view/','/retrieve/'),[qs,('format','raw')])
76                self.printableURL=self.__buildURL(discoveryBASE,[qs,('format','xml')])
77                self.URL=self.discoveryURL
78                if servicehost!='unknown':self.gettable=1
79            elif self.schema in ['NDG-B0','NDG-B1','NumSim']:
80                # One day we'll use a service binding to get this
81                # This is a mapping from the ndg repository id to an actual repository id
82                # understood by the ndg exist interface
83                blank=self.config.get('NDG_B_SERVICE','instance')
84                url=blank.replace('SERVICEHOST',servicehost)
85                url=url.replace('URI',self.uri)
86                self.URL=url
87                self.xmlURL=url.replace('/view/','/retrieve/')+'?format=raw'
88                self.printableURL=url+'?format=xml'
89                if servicehost!='unknown': self.gettable=0
90            else:
91                #currently we don't know how to get this one
92                self.URL='unknown'
93                self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None
94               
95            #now, we'll build a stub-B url as well, in case that comes in handy
96            if self.schema!='NumSim' and self.gettable<>-1:
97                self.BURL=discoveryBASE.replace(self.schema,'NDG-B1')
98                self.BURL=self.BURL.replace(server,servicehost)
99
100    def __buildURL(self,base,queryStuff):
101        ss=''
102        for i in queryStuff: 
103            if i!=None:ss+='&%s=%s'%(i[0],i[1])
104        if ss!='':ss='?'+ss[1:]
105        return base+ss
106           
107    def get(self,securityCredentials=None):
108        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI '''
109        if self.config is None: return None
110        status,xmlh=ndgRetrieve(self,self.config)
111        if status:
112            return xmlh.xmls
113        else:
114            return status
115       
116    def __str__(self):
117        return self.uri
118
Note: See TracBrowser for help on using the repository browser.