source: flex_extract.git/python/UIOTools.py @ 507d47f

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

pep8 changes + docs added + todo on UIOTools.py

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