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 - 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 | import os |
---|
26 | import glob |
---|
27 | |
---|
28 | class UIOFiles: |
---|
29 | ''' |
---|
30 | Class to manipulate files. At initialisation it has the attribute |
---|
31 | suffix which stores a list of suffixes of the files associated |
---|
32 | with the instance of the class. |
---|
33 | ''' |
---|
34 | |
---|
35 | def __init__(self, suffix): |
---|
36 | ''' |
---|
37 | @Description: |
---|
38 | Assignes the suffixes of the files which should be |
---|
39 | associated with the instance of the class. |
---|
40 | |
---|
41 | @Input: |
---|
42 | self: instance of UIOFiles |
---|
43 | Description see class documentation. |
---|
44 | |
---|
45 | suffix: list of strings |
---|
46 | Types of files to manipulate such as |
---|
47 | ['.grib', 'grb', 'grib1', 'grib2', 'grb1','grb2'] |
---|
48 | |
---|
49 | @Return: |
---|
50 | <nothing> |
---|
51 | ''' |
---|
52 | self.suffix = suffix |
---|
53 | return |
---|
54 | |
---|
55 | def listFiles(self, pathname, pattern): |
---|
56 | ''' |
---|
57 | @Description: |
---|
58 | Lists all files in the directory with the matching |
---|
59 | regular expression pattern. The suffixes are already stored |
---|
60 | in a list attribute "suffix". |
---|
61 | |
---|
62 | @Input: |
---|
63 | self: instance of UIOFiles |
---|
64 | Description see class documentation. |
---|
65 | |
---|
66 | pathname: string |
---|
67 | Directory where to list the files. |
---|
68 | |
---|
69 | pattern: string |
---|
70 | Regular expression pattern. For example: |
---|
71 | '*OG_acc_SL*.'+c.ppid+'.*' |
---|
72 | |
---|
73 | @Return: |
---|
74 | <nothing> |
---|
75 | ''' |
---|
76 | # Get the absolute path of the pathname parameter |
---|
77 | pathname = os.path.abspath(pathname) |
---|
78 | |
---|
79 | # Get a list of files in pathname |
---|
80 | filesInCurDir0 = glob.glob(pathname + '/' + pattern) |
---|
81 | filesInCurDir = [] |
---|
82 | for f in filesInCurDir0: |
---|
83 | filesInCurDir.append(f.split('/')[-1]) |
---|
84 | self.counter = 0 |
---|
85 | self.files = [] |
---|
86 | # Traverse through all files |
---|
87 | for file in filesInCurDir: |
---|
88 | curFile = os.path.join(pathname, file) |
---|
89 | |
---|
90 | # Check if it's a normal file or directory |
---|
91 | if os.path.isfile(curFile): |
---|
92 | # Get the file extension |
---|
93 | fileNoExt, curFileExtension = os.path.splitext(curFile) |
---|
94 | # Check if the file has an extension of typical video files |
---|
95 | if curFileExtension in self.suffix: |
---|
96 | # We have got a file file! Increment the counter |
---|
97 | self.counter += 1 |
---|
98 | # add this filename in the list |
---|
99 | self.files.append(curFile) |
---|
100 | else: |
---|
101 | # We got a directory, enter into it for further processing |
---|
102 | self.listFiles(curFile) |
---|
103 | |
---|
104 | return |
---|