source: flex_extract.git/python/UIOTools.py @ 6180177

ctbtodev
Last change on this file since 6180177 was 6180177, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

added Affiliation

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#************************************************************************
2# TODO AP
3#
4# - File name und Klassenname gleichsetzen?
5# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
6# und dann hier implementieren
7# - löschen?
8#************************************************************************
9"""
10@Author: Anne Fouilloux (University of Oslo)
11
12@Date: October 2014
13
14@ChangeHistory:
15   Anne Philipp (University of Vienna) - February 2018:
16       Added documentation and applied pep8 style guides
17
18@License:
19    (C) Copyright 2014 UIO.
20
21    This software is licensed under the terms of the Apache Licence Version 2.0
22    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
23"""
24
25
26# ------------------------------------------------------------------------------
27# MODULES
28# ------------------------------------------------------------------------------
29import os
30import glob
31
32# ------------------------------------------------------------------------------
33# Class
34# ------------------------------------------------------------------------------
35class UIOFiles:
36    '''
37    Class to manipulate files. At initialisation it has the attribute
38    suffix which stores a list of suffixes of the files associated
39    with the instance of the class.
40    '''
41    # --------------------------------------------------------------------------
42    # FUNCTIONS
43    # --------------------------------------------------------------------------
44    def __init__(self, suffix):
45        '''
46        @Description:
47            Assignes the suffixes of the files which should be
48            associated with the instance of the class.
49
50        @Input:
51            self: instance of UIOFiles
52                Description see class documentation.
53
54            suffix: list of strings
55                Types of files to manipulate such as
56                ['.grib', 'grb', 'grib1', 'grib2', 'grb1','grb2']
57
58        @Return:
59            <nothing>
60        '''
61        self.suffix = suffix
62        return
63
64    def listFiles(self, pathname, pattern):
65        '''
66        @Description:
67            Lists all files in the directory with the matching
68            regular expression pattern. The suffixes are already stored
69            in a list attribute "suffix".
70
71        @Input:
72            self: instance of UIOFiles
73                Description see class documentation.
74
75            pathname: string
76                Directory where to list the files.
77
78            pattern: string
79                Regular expression pattern. For example:
80                '*OG_acc_SL*.'+c.ppid+'.*'
81
82        @Return:
83            <nothing>
84        '''
85        # Get the absolute path of the pathname parameter
86        pathname = os.path.abspath(pathname)
87
88        # Get a list of files in pathname
89        filesInCurDir0 = glob.glob(pathname + '/' + pattern)
90        filesInCurDir = []
91        for f in filesInCurDir0:
92            filesInCurDir.append(f.split('/')[-1])
93        self.counter = 0
94        self.files = []
95        # Traverse through all files
96        for file in filesInCurDir:
97            curFile = os.path.join(pathname, file)
98
99            # Check if it's a normal file or directory
100            if os.path.isfile(curFile):
101                # Get the file extension
102                fileNoExt, curFileExtension = os.path.splitext(curFile)
103                # Check if the file has an extension of typical video files
104                if curFileExtension in self.suffix:
105                    # We have got a file file! Increment the counter
106                    self.counter += 1
107                    # add this filename in the list
108                    self.files.append(curFile)
109            else:
110                # We got a directory, enter into it for further processing
111                self.listFiles(curFile)
112
113        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG