Changeset f06d7fc in sasview


Ignore:
Timestamp:
Mar 3, 2015 7:59:47 AM (9 years ago)
Author:
Doucet, Mathieu <doucetm@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
c8a6c3d7
Parents:
038c00cf
Message:

Pylint fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/dataloader/loader.py

    r4749514 rf06d7fc  
    22    File handler to support different file extensions. 
    33    Uses reflectometry's registry utility. 
    4      
     4 
    55    The default readers are found in the 'readers' sub-module 
    66    and registered by default at initialization time. 
    7      
     7 
    88    To add a new default reader, one must register it in 
    99    the register_readers method found in readers/__init__.py. 
    10      
     10 
    1111    A utility method (find_plugins) is available to inspect 
    1212    a directory (for instance, a user plug-in directory) and 
     
    3737    Readers and writers are supported. 
    3838    """ 
    39      
     39 
    4040    def __init__(self): 
    4141        super(Registry, self).__init__() 
    42          
     42 
    4343        ## Writers 
    4444        self.writers = {} 
    45          
     45 
    4646        ## List of wildcards 
    4747        self.wildcards = ['All (*.*)|*.*'] 
    48          
     48 
    4949        ## Creation time, for testing 
    5050        self._created = time.time() 
    51          
     51 
    5252        # Register default readers 
    5353        readers.read_associations(self) 
    54          
    55         #TODO: remove the following line when ready to switch to 
    56         #the new default readers 
    57         #readers.register_readers(self._identify_plugin) 
    58          
    59         # Look for plug-in readers 
    60         #self.find_plugins('plugins') 
    6154 
    6255    def load(self, path, format=None): 
     
    8275                cansas_loader = cansas_reader.Reader() 
    8376                return cansas_loader.read(path) 
    84              
     77 
    8578    def find_plugins(self, dir): 
    8679        """ 
     
    8881        can be used to inspect user plug-in directories to 
    8982        find new readers/writers. 
    90          
     83 
    9184        :param dir: directory to search into 
    92          
    9385        :return: number of readers found 
    9486        """ 
     
    10193        if not os.path.isdir(temp_path): 
    10294            temp_path = os.path.join(os.path.dirname(sys.path[0]), dir) 
    103          
     95 
    10496        dir = temp_path 
    10597        # Check whether the directory exists 
     
    109101            logging.warning(msg) 
    110102            return readers_found 
    111          
     103 
    112104        for item in os.listdir(dir): 
    113105            full_path = os.path.join(dir, item) 
    114106            if os.path.isfile(full_path): 
    115                  
     107 
    116108                # Process python files 
    117109                if item.endswith('.py'): 
     
    126118                        msg += "%s\n  %s" % (item, sys.exc_value) 
    127119                        logging.error(msg) 
    128                              
     120 
    129121                # Process zip files 
    130122                elif item.endswith('.zip'): 
     
    133125                        zfile = ZipFile(item) 
    134126                        nlist = zfile.namelist() 
    135                          
     127 
    136128                        sys.path.insert(0, item) 
    137129                        for mfile in nlist: 
     
    141133                                fullname = os.path.splitext(fullname)[0] 
    142134                                module = __import__(fullname, globals(), 
    143                                                      locals(), [""]) 
     135                                                    locals(), [""]) 
    144136                                if self._identify_plugin(module): 
    145137                                    readers_found += 1 
     
    148140                                msg += " %s\n  %s" % (mfile, sys.exc_value) 
    149141                                logging.error(msg) 
    150                              
     142 
    151143                    except: 
    152144                        msg = "Loader: Error importing " 
    153145                        msg += " %s\n  %s" % (item, sys.exc_value) 
    154146                        logging.error(msg) 
    155                       
     147 
    156148        return readers_found 
    157      
     149 
    158150    def associate_file_type(self, ext, module): 
    159151        """ 
     
    161153        Reader class. If so, APPEND it to readers and (potentially) 
    162154        to the list of writers for the given extension 
    163          
     155 
    164156        :param ext: file extension [string] 
    165157        :param module: module object 
    166158        """ 
    167159        reader_found = False 
    168          
     160 
    169161        if hasattr(module, "Reader"): 
    170162            try: 
     
    177169 
    178170                reader_found = True 
    179                  
     171 
    180172                # Keep track of wildcards 
    181173                type_name = module.__name__ 
    182174                if hasattr(loader, 'type_name'): 
    183175                    type_name = loader.type_name 
    184                      
     176 
    185177                wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 
    186                                                  ext.lower()) 
     178                                                ext.lower()) 
    187179                if wcard not in self.wildcards: 
    188180                    self.wildcards.append(wcard) 
    189                              
     181 
    190182                # Check whether writing is supported 
    191183                if hasattr(loader, 'write'): 
     
    194186                    # Append the new writer to the list 
    195187                    self.writers[ext].append(loader.write) 
    196                          
     188 
    197189            except: 
    198190                msg = "Loader: Error accessing" 
     
    204196        """ 
    205197        Append a reader object to readers 
    206          
     198 
    207199        :param ext: file extension [string] 
    208200        :param module: reader object 
    209201        """ 
    210202        reader_found = False 
    211          
     203 
    212204        try: 
    213205            # Find supported extensions 
     
    218210 
    219211            reader_found = True 
    220              
     212 
    221213            # Keep track of wildcards 
    222214            if hasattr(loader, 'type_name'): 
    223215                type_name = loader.type_name 
    224                  
     216 
    225217                wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 
    226218                                                ext.lower()) 
    227219                if wcard not in self.wildcards: 
    228220                    self.wildcards.append(wcard) 
    229                   
     221 
    230222        except: 
    231223            msg = "Loader: Error accessing Reader " 
     
    236228    def _identify_plugin(self, module): 
    237229        """ 
    238         Look into a module to find whether it contains a  
     230        Look into a module to find whether it contains a 
    239231        Reader class. If so, add it to readers and (potentially) 
    240232        to the list of writers. 
    241233        :param module: module object 
    242          
     234 
    243235        """ 
    244236        reader_found = False 
    245          
     237 
    246238        if hasattr(module, "Reader"): 
    247239            try: 
     
    256248 
    257249                    reader_found = True 
    258                      
     250 
    259251                    # Keep track of wildcards 
    260252                    type_name = module.__name__ 
     
    262254                        type_name = loader.type_name 
    263255                    wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 
    264                                                      ext.lower()) 
     256                                                    ext.lower()) 
    265257                    if wcard not in self.wildcards: 
    266258                        self.wildcards.append(wcard) 
    267                              
     259 
    268260                # Check whether writing is supported 
    269261                if hasattr(loader, 'write'): 
     
    272264                            self.writers[ext] = [] 
    273265                        self.writers[ext].insert(0, loader.write) 
    274                          
     266 
    275267            except: 
    276268                msg = "Loader: Error accessing Reader" 
     
    282274        """ 
    283275        :return: the loader associated with the file type of path. 
    284          
    285276        :Raises ValueError: if file type is not known. 
    286277        """ 
     
    312303        Raises ValueError if no writer is available. 
    313304        Raises KeyError if format is not available. 
    314          
    315305        May raise a writer-defined exception if writer fails. 
    316306        """ 
     
    327317        raise  # reraises last exception 
    328318 
    329          
     319 
    330320class Loader(object): 
    331321    """ 
     
    334324    ## Registry instance 
    335325    __registry = Registry() 
    336      
     326 
    337327    def associate_file_type(self, ext, module): 
    338328        """ 
     
    340330        Reader class. If so, append it to readers and (potentially) 
    341331        to the list of writers for the given extension 
    342          
     332 
    343333        :param ext: file extension [string] 
    344334        :param module: module object 
     
    349339        """ 
    350340        Append a reader object to readers 
    351          
     341 
    352342        :param ext: file extension [string] 
    353343        :param module: reader object 
     
    358348        """ 
    359349        Load a file 
    360          
     350 
    361351        :param file: file name (path) 
    362352        :param format: specified format to use (optional) 
     
    364354        """ 
    365355        return self.__registry.load(file, format) 
    366      
     356 
    367357    def save(self, file, data, format): 
    368358        """ 
     
    370360        :param file: file name (path) 
    371361        :param data: DataInfo object 
    372         :param format: format to write the data in  
     362        :param format: format to write the data in 
    373363        """ 
    374364        return self.__registry.save(file, data, format) 
    375          
     365 
    376366    def _get_registry_creation_time(self): 
    377367        """ 
     
    380370        """ 
    381371        return self.__registry._created 
    382      
    383     def find_plugins(self, dir): 
     372 
     373    def find_plugins(self, directory): 
    384374        """ 
    385375        Find plugins in a given directory 
    386          
     376 
    387377        :param dir: directory to look into to find new readers/writers 
    388378        """ 
    389         return self.__registry.find_plugins(dir) 
    390      
     379        return self.__registry.find_plugins(directory) 
     380 
    391381    def get_wildcards(self): 
     382        """ 
     383        Return the list of wildcards 
     384        """ 
    392385        return self.__registry.wildcards 
Note: See TracChangeset for help on using the changeset viewer.