Ignore:
Timestamp:
06/12/07 14:57:56 (12 years ago)
Author:
lawrence
Message:

ndgUtils taking shape with test cases etc

Location:
exist/trunk/python/ndgUtils
Files:
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/ndgObject.py

    r3073 r3125  
     1from ndgRetrieve import ndgRetrieve 
    12class ndgObject: 
    2  
    33    ''' This class instantiates an ndgObject which describes the various ways of obtaining itself, primarily 
    44                (1) a downloadable xml representation from a repository, 
     
    66     
    77    def __init__(self,uri,config=None): 
    8         ''' Instantiate an ndg URI, and potentially methods for retrieving documents described by this URI 
    9         if a config file is available.  Three default views should be available: 
    10             (1) a rendered html version 
    11             (2) a downloadable xml representation, and 
    12             (3) a printable xml version. 
    13         Real services should also be available, and at some point of course the services should be looked up  
    14         at a service registry, but that's not yet available. ''' 
    15         uri=str(uri) #Dom had problem with unicode coming in here ... dunno why @@@@ 
    16         self.gettable=-1  # The specific record is not known to be gettable via any service 
    17                           # other values of gettable allowed are  
    18                           # 0: browseable, 1: discoverable  
    19                            
     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         
    2016        # handle all the known ways of doing an NDG URI ... 
    2117        bits=uri.split(':') 
     
    4238                # but if we do, the uri will need fixing too ... 
    4339            else: ok=0 
     40         
    4441        if not ok: 
    4542            # after all that, we don't think it's an NDG URI ... 
     
    4845            # yes, it is an NDG URI ... 
    4946            self.repository,self.schema,self.localID=repository,schema,localID 
     47         
    5048        self.setConfig(config) 
    5149         
    5250    def setConfig(self,config): 
    5351        ''' Set up the configuration for retrieving this document ''' 
     52        self.config=config 
    5453        if config is None: 
    5554            self.discoveryURL,self.baseURL,self.xmlURL,self.printableURL=None,None,None,None 
    5655            self.BURL=None 
    5756        else: 
    58             self.config=config 
     57            self.server=self.config.get('DISCOVERY','default') 
     58            server=self.server 
    5959            qs=None 
    6060            # This NDG object may itself be a discovery record, which makes life easy, but 
    6161            # it might not be, in which case we have to build up all the possible views upon it. 
    6262            # But remember only data entity b records have discovery records ... 
    63             server=self.config.get('DISCOVERY','default') 
    6463            self.viewService='%s/view/'%server 
    6564            discoveryBASE='%s%s__%s__%s'%(self.viewService,self.repository,self.schema,self.localID) 
     
    106105        return base+ss 
    107106             
    108     def get(self,type='xml'): 
     107    def get(self,securityCredentials=None): 
    109108        ''' Actually retrieve an ORIGINAL xml record corresponding to an ndg URI ''' 
    110         if self.gettable: 
    111             pass 
     109        if self.config is None: return None 
     110        status,xmlh=ndgRetrieve(self.uri,self.config) 
     111        if status: 
     112            return xmlh.xmls 
    112113        else: 
    113             pass 
    114          
     114            return status 
    115115         
    116116    def __str__(self): 
    117117        return self.uri 
    118118 
    119 import unittest 
    120  
    121 class TestCase(unittest.TestCase): 
    122     """ Tests as required """ 
    123      
    124     def getConfig(self): 
    125         from Utilities import myConfig 
    126         config=myConfig('ndgDiscovery.config') 
    127         return config 
    128      
    129     def testURI1(self): 
    130         ''' Testing the underscore ID type ''' 
    131         uri='badc.nerc.ac.uk__NDG-B0__local1' 
    132         n=ndgObject(uri) 
    133         self.__assertions(n) 
    134          
    135     def __assertions(self,uri): 
    136         self.assertEqual('badc.nerc.ac.uk',uri.repository) 
    137         self.assertEqual('NDG-B0',uri.schema) 
    138         self.assertEqual('local1',uri.localID) 
    139          
    140     def testURI2(self): 
    141         ''' Testing the colon form of ID ''' 
    142         uri='badc.nerc.ac.uk:NDG-B0:local1' 
    143         n=ndgObject(uri) 
    144         self.__assertions(n) 
    145      
    146     def notestURI3(self): 
    147         ''' Testing the default MOLES form of id ''' 
    148         uri='badc.nerc.ac.uk/local1' 
    149         n=ndgObject(uri) 
    150         self.__assertions(n) 
    151      
    152     def testMakeURL(self): 
    153         ''' Tests making a url via a configuration file ''' 
    154         uri='badc.nerc.ac.uk__NDG-B0__local1' 
    155         n=ndgObject(uri) 
    156         n.schema='DIF' 
    157         n.setConfig(self.getConfig()) 
    158         if n.gettable: 
    159             self.assertEqual(n.discoveryURL[0:5],'http:') 
    160             print n.discoveryURL 
    161      
    162     def testStubB(self): 
    163         ''' Test the url components of a stub-b ''' 
    164         uri='badc.nerc.ac.uk__NDG-B1__dataent_chablis' 
    165         n=ndgObject(uri,self.getConfig()) 
    166         print n.discoveryURL 
    167         print n.URL 
    168         print n.xmlURL 
    169         print n.printableURL 
    170  
    171     def testPMLuri(self): 
    172         '''Make sure the pml uris work in ndg object ''' 
    173         uri='npm.ac.uk__NDG-B1__dataent.692' 
    174         n=ndgObject(uri,self.getConfig()) 
    175         print n.URL 
    176  
    177     def testHorribleURI(self): 
    178         ''' tests __ in localID ''' 
    179         uri='repository__schema__something__else' 
    180         n=ndgObject(uri) 
    181         self.assertEqual(('repository','schema','something__else'), 
    182             (n.repository,n.schema,n.localID)) 
    183          
    184  
    185 if __name__=="__main__": 
    186     unittest.main() 
Note: See TracChangeset for help on using the changeset viewer.