source: flex_extract.git/python/UIOFiles.py @ 991df6a

ctbtodev
Last change on this file since 991df6a was 991df6a, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

finished documentation (except plot_retrieved)

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[64cf353]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
[507d47f]3#************************************************************************
4# TODO AP
[64cf353]5# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
[507d47f]6# und dann hier implementieren
[991df6a]7# cleanup hier rein
[507d47f]8#************************************************************************
[991df6a]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#*******************************************************************************
[9ac56ea]46
[ab3dbdd]47# ------------------------------------------------------------------------------
48# MODULES
49# ------------------------------------------------------------------------------
[d69b677]50import os
51import glob
[efdb01a]52import fnmatch
53import time
[991df6a]54
55# software specific module from flex_extract
[efdb01a]56import profiling
[991df6a]57from Tools import silentremove
58
[ab3dbdd]59# ------------------------------------------------------------------------------
[efdb01a]60# CLASS
[ab3dbdd]61# ------------------------------------------------------------------------------
[991df6a]62
[d69b677]63class UIOFiles:
[507d47f]64    '''
65    Class to manipulate files. At initialisation it has the attribute
[991df6a]66    pattern which stores a regular expression pattern for the files associated
[507d47f]67    with the instance of the class.
68    '''
[ab3dbdd]69    # --------------------------------------------------------------------------
[efdb01a]70    # CLASS FUNCTIONS
[ab3dbdd]71    # --------------------------------------------------------------------------
[991df6a]72    def __init__(self, pattern):
[507d47f]73        '''
74        @Description:
[991df6a]75            Assignes a specific pattern for these files.
[507d47f]76
77        @Input:
78            self: instance of UIOFiles
79                Description see class documentation.
80
[991df6a]81            pattern: string
82                Regular expression pattern. For example: '*.grb'
[507d47f]83
84        @Return:
85            <nothing>
86        '''
[02c8c50]87
[991df6a]88        self.pattern = pattern
[02c8c50]89
[507d47f]90        return
91
[efdb01a]92    #@profiling.timefn
[991df6a]93    def listFiles(self, path, callid=0):
[507d47f]94        '''
95        @Description:
96            Lists all files in the directory with the matching
[991df6a]97            regular expression pattern.
[507d47f]98
99        @Input:
100            self: instance of UIOFiles
101                Description see class documentation.
102
[02c8c50]103            path: string
[507d47f]104                Directory where to list the files.
105
[efdb01a]106            callid: integer
107                Id which tells the function if its the first call
108                or a recursive call. Default and first call is 0.
109                Everything different from 0 is ment to be a recursive case.
110
[507d47f]111        @Return:
112            <nothing>
113        '''
[02c8c50]114
[efdb01a]115        # initialize variable in first function call
116        if callid == 0:
117            self.files = []
118
[02c8c50]119        # Get the absolute path
120        path = os.path.abspath(path)
[507d47f]121
[efdb01a]122        # get the file list of the path if its not a directory and
[991df6a]123        # if it contains the pattern
[efdb01a]124        self.files.extend([os.path.join(path, k) for k in os.listdir(path)
[991df6a]125                           if fnmatch.fnmatch(k, self.pattern)])
[efdb01a]126
127        # find possible sub-directories in the path
128        subdirs = [s for s in os.listdir(path)
129                   if os.path.isdir(os.path.join(path, s))]
130
131        # do recursive calls for sub-direcorties
132        if subdirs:
133            for subdir in subdirs:
[991df6a]134                self.listFiles(os.path.join(path, subdir), callid=1)
135
136        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)
[efdb01a]153
154        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG