Ignore:
Timestamp:
14/11/08 21:19:43 (4 years ago)
Author:
Paul Kienzle
Message:

python: use NeXusError rather than RuntimeError. Refs #101.

Location:
trunk/bindings/python/nxs
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/python/nxs/__init__.py

    r1122 r1124  
    1111The NeXus file interface requires compiled libraries to read the 
    1212underlying HDF_ files.  Binary packages are available for some 
    13 platforms from the NeXus site.  Details of where the nxs package  
     13platforms from the NeXus site.  Details of where the nxs package 
    1414searches for the libraries are recorded in `nxs.napi`. 
    1515 
     
    4444When converting code to python from other languages you do not 
    4545necessarily want to rewrite the file handling code using the 
    46 tree view.  The `nxs.napi` module provides an interface which  
    47 more closely follows the NeXus application programming  
     46tree view.  The `nxs.napi` module provides an interface which 
     47more closely follows the NeXus application programming 
    4848interface (NAPI_). 
    4949 
     
    5555from nxs.napi import * 
    5656from nxs.tree import * 
    57  
  • trunk/bindings/python/nxs/napi.py

    r1123 r1124  
    123123.. _NAPI:  http://www.nexusformat.org/Application_Program_Interface 
    124124""" 
    125 __all__ = ['UNLIMITED', 'MAXRANK', 'MAXNAMELEN','MAXPATHLEN','NeXus','open'] 
     125__all__ = ['UNLIMITED', 'MAXRANK', 'MAXNAMELEN','MAXPATHLEN', 
     126           'NeXus','NeXusError','open'] 
    126127 
    127128import sys, os, numpy, ctypes 
     
    263264    return NeXus(filename, mode) 
    264265 
     266class NeXusError(Exception): 
     267    """NeXus Error""" 
     268    pass 
     269 
    265270class NeXus(object): 
    266271 
     
    282287        Raises ValueError if the open mode is invalid. 
    283288 
    284         Raises RuntimeError if the file could not be opened, with the 
     289        Raises NeXusError if the file could not be opened, with the 
    285290        filename as part of the error message. 
    286291 
     
    303308            else: 
    304309                op = 'create' 
    305             raise RuntimeError, "Could not %s %s"%(op,filename) 
     310            raise NeXusError, "Could not %s %s"%(op,filename) 
    306311        self.isopen = True 
    307312 
     
    324329        Opens the NeXus file handle if it is not already open. 
    325330 
    326         Raises RuntimeError if the file could not be opened. 
     331        Raises NeXusError if the file could not be opened. 
    327332 
    328333        Corresponds to NXopen(filename,mode,&handle) 
     
    335340        status = nxlib.nxiopen_(self.filename,mode,_ref(self.handle)) 
    336341        if status == ERROR: 
    337             raise RuntimeError, "Could not open %s"%(self.filename) 
     342            raise NeXusError, "Could not open %s"%(self.filename) 
    338343        self.path = [] 
    339344 
     
    344349        Close the NeXus file associated with handle. 
    345350 
    346         Raises RuntimeError if file could not be closed. 
     351        Raises NeXusError if file could not be closed. 
    347352 
    348353        Corresponds to NXclose(&handle) 
     
    352357            status = nxlib.nxiclose_(_ref(self.handle)) 
    353358            if status == ERROR: 
    354                 raise RuntimeError, "Could not close NeXus file %s"%(self.filename) 
     359                raise NeXusError, "Could not close NeXus file %s"%(self.filename) 
    355360        self.path = [] 
    356361 
     
    361366        Flush all data to the NeXus file. 
    362367 
    363         Raises RuntimeError if this fails. 
     368        Raises NeXusError if this fails. 
    364369 
    365370        Corresponds to NXflush(&handle) 
     
    367372        status = nxlib.nxiflush_(_ref(self.handle)) 
    368373        if status == ERROR: 
    369             raise RuntimeError, "Could not flush NeXus file %s"%(self.filename) 
     374            raise NeXusError, "Could not flush NeXus file %s"%(self.filename) 
    370375 
    371376    nxlib.nxisetnumberformat_.restype = c_int 
     
    393398        Create the group nxclass:name. 
    394399 
    395         Raises RuntimeError if the group could not be created. 
     400        Raises NeXusError if the group could not be created. 
    396401 
    397402        Corresponds to NXmakegroup(handle, name, nxclass) 
     
    399404        status = nxlib.nximakegroup_(self.handle, name, nxclass) 
    400405        if status == ERROR: 
    401             raise RuntimeError,\ 
     406            raise NeXusError,\ 
    402407                "Could not create %s:%s in %s"%(nxclass,name,self._loc()) 
    403408 
     
    468473        Close the currently open group. 
    469474 
    470         Raises RuntimeError if the group could not be closed. 
     475        Raises NeXusError if the group could not be closed. 
    471476 
    472477        Corresponds to NXclosegroup(handle) 
     
    476481        group = self.path.pop() 
    477482        if status == ERROR: 
    478             raise RuntimeError, "Could not close %s:"%(group,self._loc()) 
     483            raise NeXusError, "Could not close %s:"%(group,self._loc()) 
    479484 
    480485    nxlib.nxigetinfo_.restype = c_int 
     
    506511        Reset getnextentry to return the first entry in the group. 
    507512 
    508         Raises RuntimeError if this fails. 
     513        Raises NeXusError if this fails. 
    509514 
    510515        Corresponds to NXinitgroupdir(handle) 
     
    512517        status = nxlib.nxiinitgroupdir_(self.handle) 
    513518        if status == ERROR: 
    514             raise RuntimeError, \ 
     519            raise NeXusError, \ 
    515520                "Could not reset group scan: %s"%(self._loc()) 
    516521 
     
    521526        Return the next entry in the group as name,nxclass tuple. 
    522527 
    523         Raises RuntimeError if this fails, or if there is no next entry. 
     528        Raises NeXusError if this fails, or if there is no next entry. 
    524529 
    525530        Corresponds to NXgetnextentry(handle,name,nxclass,&storage). 
     
    534539        status = nxlib.nxigetnextentry_(self.handle,name,nxclass,_ref(storage)) 
    535540        if status == ERROR or status == EOD: 
    536             raise RuntimeError, \ 
     541            raise NeXusError, \ 
    537542                "Could not get next entry: %s"%(self._loc()) 
    538543        ## Note: ignoring storage --- it is useless without dimensions 
     
    594599        complex values. 
    595600 
    596         Raises RuntimeError if this fails. 
     601        Raises NeXusError if this fails. 
    597602 
    598603        Note that this is the recommended way to establish if you have 
     
    609614                                     _ref(storage)) 
    610615        if status == ERROR: 
    611             raise RuntimeError, "Could not get data info: %s"%(self._loc()) 
     616            raise NeXusError, "Could not get data info: %s"%(self._loc()) 
    612617        shape = shape[:rank.value]+0 
    613618        dtype = _pytype_code[storage.value] 
     
    637642        Close the currently open data set. 
    638643 
    639         Raises RuntimeError if this fails (e.g., because no 
     644        Raises NeXusError if this fails (e.g., because no 
    640645        dataset is open). 
    641646 
     
    646651        name = self.path.pop() 
    647652        if status == ERROR: 
    648             raise RuntimeError,\ 
     653            raise NeXusError,\ 
    649654                "Could not close data %s: %s"%(name,self._loc()) 
    650655 
     
    807812        Reset the getnextattr list to the first attribute. 
    808813 
    809         Raises RuntimeError if this fails. 
     814        Raises NeXusError if this fails. 
    810815 
    811816        Corresponds to NXinitattrdir(handle) 
     
    813818        status = nxlib.nxiinitattrdir_(self.handle) 
    814819        if status == ERROR: 
    815             raise RuntimeError, \ 
     820            raise NeXusError, \ 
    816821                "Could not reset attribute list: %s"%(self._loc()) 
    817822 
     
    824829        this number of times. 
    825830 
    826         Raises RuntimeError if this fails. 
     831        Raises NeXusError if this fails. 
    827832 
    828833        Corresponds to NXgetattrinfo(handl, &n) 
     
    831836        status = nxlib.nxigetattrinfo_(self.handle,_ref(n)) 
    832837        if status == ERROR: 
    833             raise RuntimeError, "Could not get attr info: %s"%(self._loc()) 
     838            raise NeXusError, "Could not get attr info: %s"%(self._loc()) 
    834839        #print "num attrs",n.value 
    835840        return n.value 
     
    845850        attribute. 
    846851 
    847         Raises RuntimeError if NeXus returns ERROR or EOD. 
     852        Raises NeXusError if NeXus returns ERROR or EOD. 
    848853 
    849854        Corresponds to NXgetnextattr(handle,name,&length,&storage) 
     
    859864        status = nxlib.nxigetnextattr_(self.handle,name,_ref(length),_ref(storage)) 
    860865        if status == ERROR or status == EOD: 
    861             raise RuntimeError, "Could not get next attr: %s"%(self._loc()) 
     866            raise NeXusError, "Could not get next attr: %s"%(self._loc()) 
    862867        dtype = _pytype_code[storage.value] 
    863868        #print "next attr",name.value,length.value,dtype 
     
    894899 
    895900        Raises TypeError if the value type is incorrect. 
    896         Raises RuntimeError if the attribute could not be saved. 
     901        Raises NeXusError if the attribute could not be saved. 
    897902 
    898903        Corresponds to NXputattr(handle,name,data,length,storage) 
     
    934939        status = nxlib.nxiputattr_(self.handle,name,data,length,storage) 
    935940        if status == ERROR: 
    936             raise RuntimeError, "Could not write attr %s: %s"%(name,self._loc()) 
     941            raise NeXusError, "Could not write attr %s: %s"%(name,self._loc()) 
    937942 
    938943    def attrs(self): 
     
    963968        Return the id of the current group so we can link to it later. 
    964969 
    965         Raises RuntimeError 
     970        Raises NeXusError 
    966971 
    967972        Corresponds to NXgetgroupID(handle, &ID) 
     
    970975        status = nxlib.nxigetgroupid_(self.handle,_ref(ID)) 
    971976        if status == ERROR: 
    972             raise RuntimeError, "Could not link to group: %s"%(self._loc()) 
     977            raise NeXusError, "Could not link to group: %s"%(self._loc()) 
    973978        return ID 
    974979 
     
    979984        Return the id of the current data so we can link to it later. 
    980985 
    981         Raises RuntimeError 
     986        Raises NeXusError 
    982987 
    983988        Corresponds to NXgetdataID(handle, &ID) 
     
    986991        status = nxlib.nxigetdataid_(self.handle,_ref(ID)) 
    987992        if status == ERROR: 
    988             raise RuntimeError, "Could not link to data: %s"%(self._loc()) 
     993            raise NeXusError, "Could not link to data: %s"%(self._loc()) 
    989994        return ID 
    990995 
     
    9961001        open group. 
    9971002 
    998         Raises RuntimeError 
     1003        Raises NeXusError 
    9991004 
    10001005        Corresponds to NXmakelink(handle, &ID) 
     
    10021007        status = nxlib.nximakelink_(self.handle,_ref(ID)) 
    10031008        if status == ERROR: 
    1004             raise RuntimeError, "Could not make link: %s"%(self._loc()) 
     1009            raise NeXusError, "Could not make link: %s"%(self._loc()) 
    10051010 
    10061011    nxlib.nximakenamedlink_.restype = c_int 
     
    10111016        open group, but under a different name. 
    10121017 
    1013         Raises RuntimeError 
     1018        Raises NeXusError 
    10141019 
    10151020        Corresponds to NXmakenamedlink(handle,name,&ID) 
     
    10171022        status = nxlib.nximakenamedlink_(self.handle,name,_ref(ID)) 
    10181023        if status == ERROR: 
    1019             raise RuntimeError, "Could not make link %s: %s"%(name,self._loc()) 
     1024            raise NeXusError, "Could not make link %s: %s"%(name,self._loc()) 
    10201025 
    10211026    nxlib.nxisameid_.restype = c_int 
     
    10421047        perhaps the existence of a 'target' attribute in the current item. 
    10431048 
    1044         Raises RuntimeError. 
     1049        Raises NeXusError. 
    10451050 
    10461051        Corresponds to NXopensourcegroup(handle) 
     
    10481053        status = nxlib.nxiopensourcegroup_(self.handle) 
    10491054        if status == ERROR: 
    1050             raise RuntimeError, "Could not open source group: %s"%(self._loc()) 
     1055            raise NeXusError, "Could not open source group: %s"%(self._loc()) 
    10511056 
    10521057    def link(self): 
     
    10861091        group is an external link to another file. 
    10871092 
    1088         Raises RuntimeError if this fails. 
     1093        Raises NeXusError if this fails. 
    10891094 
    10901095        Corresponds to NXinquirefile(&handle,file,len) 
     
    10931098        status = nxlib.nxiinquirefile_(self.handle,filename,maxnamelen) 
    10941099        if status == ERROR: 
    1095             raise RuntimeError,\ 
     1100            raise NeXusError,\ 
    10961101                "Could not determine filename: %s"%(self._loc()) 
    10971102        return filename.value 
     
    11051110        otherwise return None. 
    11061111 
    1107         Raises RuntimeError if link fails. 
     1112        Raises NeXusError if link fails. 
    11081113 
    11091114        Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len) 
     
    11111116        status = nxlib.nxilinkexternal_(self.handle,name,nxclass,url) 
    11121117        if status == ERROR: 
    1113             raise RuntimeError,\ 
     1118            raise NeXusError,\ 
    11141119                "Could not link %s to %s: %s"%(name,url,self._loc()) 
    11151120 
  • trunk/bindings/python/nxs/tree.py

    r1122 r1124  
    126126import nxs.napi 
    127127import nxs.unit 
     128from nxs.napi import NeXusError 
    128129 
    129130 
     
    403404            try: 
    404405                gid[target] = self.getdataID() 
    405             except RuntimeError: 
     406            except NeXusError: 
    406407                gid[target] = self.getgroupID() 
    407408 
     
    718719    """ 
    719720    def plot(self, signal, axes, entry, title, **opts): 
     721        """ 
     722        Plot the data entry. 
     723         
     724        Raises NeXusError if the data cannot be plotted. 
     725        """ 
    720726        import pylab 
    721727        pylab.cla() 
     
    755761        # No support for higher dimensions yet 
    756762        else: 
    757             raise RuntimeError, "Cannot plot a dataset of rank 3 or greater." 
     763            raise NeXusError, "Cannot plot a dataset of rank 3 or greater." 
    758764    @staticmethod 
    759765    def show(): 
     
    798804        """ 
    799805        Plot data contained within the group. 
     806         
     807        Raises NeXusError if the data could not be plotted. 
    800808        """ 
    801809         
     
    806814                break 
    807815        else: 
    808             raise RuntimeError('No plottable signal') 
     816            raise NeXusError('No plottable signal') 
    809817         
    810818        # Find the associated axes 
     
    812820            axes = [getattr(self,a) for a in signal.Aaxes.nxdata.split(':')] 
    813821        else: 
    814             raise RuntimeError('Axes attribute missing from signal') 
     822            raise NeXusError('Axes attribute missing from signal') 
    815823 
    816824        # Construct title 
Note: See TracChangeset for help on using the changeset viewer.