Changeset 991df6a in flex_extract.git for python/UIOFiles.py


Ignore:
Timestamp:
May 14, 2018, 10:11:29 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
812283d
Parents:
efdb01a
Message:

finished documentation (except plot_retrieved)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/UIOFiles.py

    refdb01a r991df6a  
    33#************************************************************************
    44# TODO AP
    5 #AP
    65# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
    76# und dann hier implementieren
    8 # - add description of file!
     7# cleanup hier rein
    98#************************************************************************
    10 """
    11 @Author: Anne Fouilloux (University of Oslo)
     9#*******************************************************************************
     10# @Author: Anne Fouilloux (University of Oslo)
     11#
     12# @Date: October 2014
     13#
     14# @Change History:
     15#
     16#    November 2015 - Leopold Haimberger (University of Vienna):
     17#        - modified method listFiles to work with glob instead of listdir
     18#        - added pattern search in method listFiles
     19#
     20#    February 2018 - Anne Philipp (University of Vienna):
     21#        - applied PEP8 style guide
     22#        - added documentation
     23#        - optimisation of method listFiles since it didn't work correctly
     24#          for sub directories
     25#        - additional speed up of method listFiles
     26#        - modified the class so that it is initiated with a pattern instead
     27#          of suffixes. Gives more precision in selection of files.
     28#
     29# @License:
     30#    (C) Copyright 2014-2018.
     31#
     32#    This software is licensed under the terms of the Apache Licence Version 2.0
     33#    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
     34#
     35# @Class Decription:
     36#    The class is for file manipulation. It is initiated with a regular
     37#    expression pattern for this instance and can produce a list of Files
     38#    from the given file pattern. These files can be deleted.
     39#
     40# @Class Content:
     41#    - __init__
     42#    - listFiles
     43#    - deleteFiles
     44#
     45#*******************************************************************************
    1246
    13 @Date: October 2014
    14 
    15 @ChangeHistory:
    16     November 2015 - Leopold Haimberger (University of Vienna):
    17         - modified method listFiles to work with glob instead of listdir
    18         - added pattern search in method listFiles
    19 
    20     February 2018 - Anne Philipp (University of Vienna):
    21         - applied PEP8 style guide
    22         - added documentation
    23         - optimisation of method listFiles since it didn't work correctly
    24           for sub directories
    25         - additional speed up of method listFiles
    26 
    27 @License:
    28     (C) Copyright 2014-2018.
    29 
    30     This software is licensed under the terms of the Apache Licence Version 2.0
    31     which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
    32 
    33 @Requirements:
    34     A standard python 2.6 or 2.7 installation
    35 
    36 @Description:
    37     ...
    38 """
    3947# ------------------------------------------------------------------------------
    4048# MODULES
     
    4452import fnmatch
    4553import time
     54
     55# software specific module from flex_extract
    4656import profiling
     57from Tools import silentremove
     58
    4759# ------------------------------------------------------------------------------
    4860# CLASS
    4961# ------------------------------------------------------------------------------
     62
    5063class UIOFiles:
    5164    '''
    5265    Class to manipulate files. At initialisation it has the attribute
    53     suffix which stores a list of suffixes of the files associated
     66    pattern which stores a regular expression pattern for the files associated
    5467    with the instance of the class.
    5568    '''
     
    5770    # CLASS FUNCTIONS
    5871    # --------------------------------------------------------------------------
    59     def __init__(self, suffix):
     72    def __init__(self, pattern):
    6073        '''
    6174        @Description:
    62             Assignes the suffixes of the files which should be
    63             associated with the instance of the class.
     75            Assignes a specific pattern for these files.
    6476
    6577        @Input:
     
    6779                Description see class documentation.
    6880
    69             suffix: list of strings
    70                 The types of files which should be manipulated such as
    71                 ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2']
     81            pattern: string
     82                Regular expression pattern. For example: '*.grb'
    7283
    7384        @Return:
     
    7586        '''
    7687
    77         self.suffix = suffix
     88        self.pattern = pattern
    7889
    7990        return
    8091
    8192    #@profiling.timefn
    82     def listFiles(self, path, pattern, callid=0):
     93    def listFiles(self, path, callid=0):
    8394        '''
    8495        @Description:
    8596            Lists all files in the directory with the matching
    86             regular expression pattern. The suffixes are already stored
    87             in a list attribute "suffix".
     97            regular expression pattern.
    8898
    8999        @Input:
     
    93103            path: string
    94104                Directory where to list the files.
    95 
    96             pattern: string
    97                 Regular expression pattern. For example:
    98                 '*OG_acc_SL*.'+c.ppid+'.*'
    99105
    100106            callid: integer
     
    115121
    116122        # get the file list of the path if its not a directory and
    117         # if it contains one of the suffixes
     123        # if it contains the pattern
    118124        self.files.extend([os.path.join(path, k) for k in os.listdir(path)
    119                            if fnmatch.fnmatch(k, pattern) and
    120                            os.path.splitext(k)[-1] in self.suffix])
     125                           if fnmatch.fnmatch(k, self.pattern)])
    121126
    122127        # find possible sub-directories in the path
     
    127132        if subdirs:
    128133            for subdir in subdirs:
    129                 self.listFiles(os.path.join(path, subdir), pattern, callid=1)
     134                self.listFiles(os.path.join(path, subdir), callid=1)
    130135
    131136        return
     137
     138    def deleteFiles(self):
     139        '''
     140        @Description:
     141            Deletes the files.
     142
     143        @Input:
     144            self: instance of UIOFiles
     145                Description see class documentation.
     146
     147        @Return:
     148            <nothing>
     149        '''
     150
     151        for f in self.files:
     152            silentremove(f)
     153
     154        return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG