[507d47f] | 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 | #************************************************************************ |
---|
[9ac56ea] | 9 | """ |
---|
| 10 | @Author: Anne Fouilloux (University of Oslo) |
---|
| 11 | |
---|
| 12 | @Date: October 2014 |
---|
| 13 | |
---|
| 14 | @ChangeHistory: |
---|
[6180177] | 15 | Anne Philipp (University of Vienna) - February 2018: |
---|
[9ac56ea] | 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 | """ |
---|
[507d47f] | 24 | |
---|
[ab3dbdd] | 25 | |
---|
| 26 | # ------------------------------------------------------------------------------ |
---|
| 27 | # MODULES |
---|
| 28 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 29 | import os |
---|
| 30 | import glob |
---|
| 31 | |
---|
[ab3dbdd] | 32 | # ------------------------------------------------------------------------------ |
---|
| 33 | # Class |
---|
| 34 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 35 | class UIOFiles: |
---|
[507d47f] | 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 | ''' |
---|
[ab3dbdd] | 41 | # -------------------------------------------------------------------------- |
---|
| 42 | # FUNCTIONS |
---|
| 43 | # -------------------------------------------------------------------------- |
---|
[507d47f] | 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 |
---|
[d69b677] | 86 | pathname = os.path.abspath(pathname) |
---|
[507d47f] | 87 | |
---|
| 88 | # Get a list of files in pathname |
---|
| 89 | filesInCurDir0 = glob.glob(pathname + '/' + pattern) |
---|
| 90 | filesInCurDir = [] |
---|
[d69b677] | 91 | for f in filesInCurDir0: |
---|
| 92 | filesInCurDir.append(f.split('/')[-1]) |
---|
| 93 | self.counter = 0 |
---|
| 94 | self.files = [] |
---|
[507d47f] | 95 | # Traverse through all files |
---|
[d69b677] | 96 | for file in filesInCurDir: |
---|
| 97 | curFile = os.path.join(pathname, file) |
---|
[507d47f] | 98 | |
---|
| 99 | # Check if it's a normal file or directory |
---|
[d69b677] | 100 | if os.path.isfile(curFile): |
---|
| 101 | # Get the file extension |
---|
[507d47f] | 102 | fileNoExt, curFileExtension = os.path.splitext(curFile) |
---|
[d69b677] | 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 |
---|