1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | #AP |
---|
6 | # - checken welche regelmässigen methoden auf diese Files noch angewendet werden |
---|
7 | # und dann hier implementieren |
---|
8 | # - add description of file! |
---|
9 | #************************************************************************ |
---|
10 | """ |
---|
11 | @Author: Anne Fouilloux (University of Oslo) |
---|
12 | |
---|
13 | @Date: October 2014 |
---|
14 | |
---|
15 | @ChangeHistory: |
---|
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 | |
---|
27 | @License: |
---|
28 | (C) Copyright 2014-2018. |
---|
29 | |
---|
30 | This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
31 | which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
32 | |
---|
33 | @Requirements: |
---|
34 | A standard python 2.6 or 2.7 installation |
---|
35 | |
---|
36 | @Description: |
---|
37 | ... |
---|
38 | """ |
---|
39 | # ------------------------------------------------------------------------------ |
---|
40 | # MODULES |
---|
41 | # ------------------------------------------------------------------------------ |
---|
42 | import os |
---|
43 | import glob |
---|
44 | import fnmatch |
---|
45 | import time |
---|
46 | import profiling |
---|
47 | # ------------------------------------------------------------------------------ |
---|
48 | # CLASS |
---|
49 | # ------------------------------------------------------------------------------ |
---|
50 | class UIOFiles: |
---|
51 | ''' |
---|
52 | Class to manipulate files. At initialisation it has the attribute |
---|
53 | suffix which stores a list of suffixes of the files associated |
---|
54 | with the instance of the class. |
---|
55 | ''' |
---|
56 | # -------------------------------------------------------------------------- |
---|
57 | # CLASS FUNCTIONS |
---|
58 | # -------------------------------------------------------------------------- |
---|
59 | def __init__(self, suffix): |
---|
60 | ''' |
---|
61 | @Description: |
---|
62 | Assignes the suffixes of the files which should be |
---|
63 | associated with the instance of the class. |
---|
64 | |
---|
65 | @Input: |
---|
66 | self: instance of UIOFiles |
---|
67 | Description see class documentation. |
---|
68 | |
---|
69 | suffix: list of strings |
---|
70 | The types of files which should be manipulated such as |
---|
71 | ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2'] |
---|
72 | |
---|
73 | @Return: |
---|
74 | <nothing> |
---|
75 | ''' |
---|
76 | |
---|
77 | self.suffix = suffix |
---|
78 | |
---|
79 | return |
---|
80 | |
---|
81 | #@profiling.timefn |
---|
82 | def listFiles(self, path, pattern, callid=0): |
---|
83 | ''' |
---|
84 | @Description: |
---|
85 | Lists all files in the directory with the matching |
---|
86 | regular expression pattern. The suffixes are already stored |
---|
87 | in a list attribute "suffix". |
---|
88 | |
---|
89 | @Input: |
---|
90 | self: instance of UIOFiles |
---|
91 | Description see class documentation. |
---|
92 | |
---|
93 | path: string |
---|
94 | Directory where to list the files. |
---|
95 | |
---|
96 | pattern: string |
---|
97 | Regular expression pattern. For example: |
---|
98 | '*OG_acc_SL*.'+c.ppid+'.*' |
---|
99 | |
---|
100 | callid: integer |
---|
101 | Id which tells the function if its the first call |
---|
102 | or a recursive call. Default and first call is 0. |
---|
103 | Everything different from 0 is ment to be a recursive case. |
---|
104 | |
---|
105 | @Return: |
---|
106 | <nothing> |
---|
107 | ''' |
---|
108 | |
---|
109 | # initialize variable in first function call |
---|
110 | if callid == 0: |
---|
111 | self.files = [] |
---|
112 | |
---|
113 | # Get the absolute path |
---|
114 | path = os.path.abspath(path) |
---|
115 | |
---|
116 | # get the file list of the path if its not a directory and |
---|
117 | # if it contains one of the suffixes |
---|
118 | self.files.extend([os.path.join(path, k) for k in os.listdir(path) |
---|
119 | if fnmatch.fnmatch(k, pattern) and |
---|
120 | os.path.splitext(k)[-1] in self.suffix]) |
---|
121 | |
---|
122 | # find possible sub-directories in the path |
---|
123 | subdirs = [s for s in os.listdir(path) |
---|
124 | if os.path.isdir(os.path.join(path, s))] |
---|
125 | |
---|
126 | # do recursive calls for sub-direcorties |
---|
127 | if subdirs: |
---|
128 | for subdir in subdirs: |
---|
129 | self.listFiles(os.path.join(path, subdir), pattern, callid=1) |
---|
130 | |
---|
131 | return |
---|