Ignore:
Timestamp:
Apr 27, 2012 10:42:24 AM (12 years ago)
Author:
Mathieu Doucet <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:
7d6351e
Parents:
10bfeb3
Message:

Pep-8-ification

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sansdataloader/src/sans/dataloader/loader.py

    r371cb85 rf60a8c2  
    1  
     1""" 
     2    File handler to support different file extensions. 
     3    Uses reflectometry's registry utility. 
     4     
     5    The default readers are found in the 'readers' sub-module 
     6    and registered by default at initialization time. 
     7     
     8    To add a new default reader, one must register it in 
     9    the register_readers method found in readers/__init__.py. 
     10     
     11    A utility method (find_plugins) is available to inspect 
     12    a directory (for instance, a user plug-in directory) and 
     13    look for new readers/writers. 
     14""" 
    215##################################################################### 
    316#This software was developed by the University of Tennessee as part of the 
    417#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    5 #project funded by the US National Science Foundation.  
     18#project funded by the US National Science Foundation. 
    619#See the license text in license.txt 
    720#copyright 2008, University of Tennessee 
    821###################################################################### 
    922 
    10 """ 
    11     File handler to support different file extensions. 
    12     Uses reflectometry's registry utility. 
    13      
    14     The default readers are found in the 'readers' sub-module 
    15     and registered by default at initialization time. 
    16      
    17     To add a new default reader, one must register it in 
    18     the register_readers method found in readers/__init__.py.  
    19      
    20     A utility method (find_plugins) is available to inspect  
    21     a directory (for instance, a user plug-in directory) and 
    22     look for new readers/writers. 
    23 """ 
    24  
    25 import os  
     23import os 
    2624import sys 
    2725import logging 
     
    5553        readers.read_associations(self) 
    5654         
    57         #TODO: remove the following line when ready to switch to  
     55        #TODO: remove the following line when ready to switch to 
    5856        #the new default readers 
    5957        #readers.register_readers(self._identify_plugin) 
     
    6260        #self.find_plugins('plugins') 
    6361 
    64          
    6562    def load(self, path, format=None): 
    6663        """ 
     
    6865 
    6966        :param path: file path 
    70         :param format: explicit extension, to force the use  
     67        :param format: explicit extension, to force the use 
    7168            of a particular reader 
    7269 
    7370        Defaults to the ascii (multi-column) reader 
    7471        if no reader was registered for the file's 
    75         extension.    
     72        extension. 
    7673        """ 
    7774        try: 
     
    107104        dir = temp_path 
    108105        # Check whether the directory exists 
    109         if not os.path.isdir(dir):  
     106        if not os.path.isdir(dir): 
    110107            msg = "DataLoader couldn't locate DataLoader plugin folder." 
    111108            msg += """ "%s" does not exist""" % dir 
     
    157154                        logging.error(msg) 
    158155                      
    159         return readers_found  
     156        return readers_found 
    160157     
    161158    def associate_file_type(self, ext, module): 
    162159        """ 
    163         Look into a module to find whether it contains a  
     160        Look into a module to find whether it contains a 
    164161        Reader class. If so, APPEND it to readers and (potentially) 
    165162        to the list of writers for the given extension 
     
    226223                type_name = loader.type_name 
    227224                 
    228                 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(),  
     225                wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 
    229226                                                ext.lower()) 
    230227                if wcard not in self.wildcards: 
     
    237234        return reader_found 
    238235 
    239      
    240236    def _identify_plugin(self, module): 
    241237        """ 
     
    268264                                                     ext.lower()) 
    269265                    if wcard not in self.wildcards: 
    270                         self.wildcards.append(wcard) 
     266                        self.wildcards.append(wcard) 
    271267                             
    272268                # Check whether writing is supported 
     
    275271                        if ext not in self.writers: 
    276272                            self.writers[ext] = [] 
    277                         self.writers[ext].insert(0,loader.write) 
     273                        self.writers[ext].insert(0, loader.write) 
    278274                         
    279275            except: 
     
    288284         
    289285        :Raises ValueError: if file type is not known. 
    290         """         
     286        """ 
    291287        # Find matching extensions 
    292288        extlist = [ext for ext in self.extensions() if path.endswith(ext)] 
     
    301297            result = [] 
    302298            for L in writers: 
    303                 if L not in result: result.append(L) 
     299                if L not in result: 
     300                    result.append(L) 
    304301            writers = L 
    305302        # Raise an error if there are no matching extensions 
     
    316313        Raises KeyError if format is not available. 
    317314         
    318         May raise a writer-defined exception if writer fails.         
     315        May raise a writer-defined exception if writer fails. 
    319316        """ 
    320317        if format is None: 
     
    326323                return fn(path, data) 
    327324            except: 
    328                 pass # give other loaders a chance to succeed 
     325                pass  # give other loaders a chance to succeed 
    329326        # If we get here it is because all loaders failed 
    330         raise # reraises last exception 
     327        raise  # reraises last exception 
    331328 
    332329         
     
    340337    def associate_file_type(self, ext, module): 
    341338        """ 
    342         Look into a module to find whether it contains a  
     339        Look into a module to find whether it contains a 
    343340        Reader class. If so, append it to readers and (potentially) 
    344341        to the list of writers for the given extension 
     
    394391    def get_wildcards(self): 
    395392        return self.__registry.wildcards 
    396          
    397 if __name__ == "__main__":  
    398     logging.basicConfig(level=logging.INFO, 
    399                         format='%(asctime)s %(levelname)s %(message)s', 
    400                         filename='loader.log', 
    401                         filemode='w') 
    402     l = Loader() 
    403     test_data = l.load('test/cansas1d.xml') 
    404     l.save('test_file.xml', test_data, '.xml') 
    405      
    406     print l.get_wildcards() 
    407          
    408          
    409      
Note: See TracChangeset for help on using the changeset viewer.