1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | # - checken welche regelmässigen methoden auf diese Files noch angewendet werden |
---|
6 | # und dann hier implementieren |
---|
7 | # cleanup hier rein |
---|
8 | #************************************************************************ |
---|
9 | #******************************************************************************* |
---|
10 | # @Author: Anne Fouilloux (University of Oslo) |
---|
11 | # |
---|
12 | # @Date: October 2014 |
---|
13 | # |
---|
14 | # @Change History: |
---|
15 | # |
---|
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 | # - modified the class so that it is initiated with a pattern instead |
---|
27 | # of suffixes. Gives more precision in selection of files. |
---|
28 | # |
---|
29 | # @License: |
---|
30 | # (C) Copyright 2014-2018. |
---|
31 | # |
---|
32 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
33 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
34 | # |
---|
35 | # @Class Decription: |
---|
36 | # The class is for file manipulation. It is initiated with a regular |
---|
37 | # expression pattern for this instance and can produce a list of Files |
---|
38 | # from the given file pattern. These files can be deleted. |
---|
39 | # |
---|
40 | # @Class Content: |
---|
41 | # - __init__ |
---|
42 | # - listFiles |
---|
43 | # - deleteFiles |
---|
44 | # |
---|
45 | #******************************************************************************* |
---|
46 | |
---|
47 | # ------------------------------------------------------------------------------ |
---|
48 | # MODULES |
---|
49 | # ------------------------------------------------------------------------------ |
---|
50 | import os |
---|
51 | import glob |
---|
52 | import fnmatch |
---|
53 | import time |
---|
54 | |
---|
55 | # software specific module from flex_extract |
---|
56 | import profiling |
---|
57 | from Tools import silentremove |
---|
58 | |
---|
59 | # ------------------------------------------------------------------------------ |
---|
60 | # CLASS |
---|
61 | # ------------------------------------------------------------------------------ |
---|
62 | |
---|
63 | class UIOFiles: |
---|
64 | ''' |
---|
65 | Class to manipulate files. At initialisation it has the attribute |
---|
66 | pattern which stores a regular expression pattern for the files associated |
---|
67 | with the instance of the class. |
---|
68 | ''' |
---|
69 | # -------------------------------------------------------------------------- |
---|
70 | # CLASS FUNCTIONS |
---|
71 | # -------------------------------------------------------------------------- |
---|
72 | def __init__(self, pattern): |
---|
73 | ''' |
---|
74 | @Description: |
---|
75 | Assignes a specific pattern for these files. |
---|
76 | |
---|
77 | @Input: |
---|
78 | self: instance of UIOFiles |
---|
79 | Description see class documentation. |
---|
80 | |
---|
81 | pattern: string |
---|
82 | Regular expression pattern. For example: '*.grb' |
---|
83 | |
---|
84 | @Return: |
---|
85 | <nothing> |
---|
86 | ''' |
---|
87 | |
---|
88 | self.pattern = pattern |
---|
89 | |
---|
90 | return |
---|
91 | |
---|
92 | #@profiling.timefn |
---|
93 | def listFiles(self, path, callid=0): |
---|
94 | ''' |
---|
95 | @Description: |
---|
96 | Lists all files in the directory with the matching |
---|
97 | regular expression pattern. |
---|
98 | |
---|
99 | @Input: |
---|
100 | self: instance of UIOFiles |
---|
101 | Description see class documentation. |
---|
102 | |
---|
103 | path: string |
---|
104 | Directory where to list the files. |
---|
105 | |
---|
106 | callid: integer |
---|
107 | Id which tells the function if its the first call |
---|
108 | or a recursive call. Default and first call is 0. |
---|
109 | Everything different from 0 is ment to be a recursive case. |
---|
110 | |
---|
111 | @Return: |
---|
112 | <nothing> |
---|
113 | ''' |
---|
114 | |
---|
115 | # initialize variable in first function call |
---|
116 | if callid == 0: |
---|
117 | self.files = [] |
---|
118 | |
---|
119 | # Get the absolute path |
---|
120 | path = os.path.abspath(path) |
---|
121 | |
---|
122 | # get the file list of the path if its not a directory and |
---|
123 | # if it contains the pattern |
---|
124 | self.files.extend([os.path.join(path, k) for k in os.listdir(path) |
---|
125 | if fnmatch.fnmatch(k, self.pattern)]) |
---|
126 | |
---|
127 | # find possible sub-directories in the path |
---|
128 | subdirs = [s for s in os.listdir(path) |
---|
129 | if os.path.isdir(os.path.join(path, s))] |
---|
130 | |
---|
131 | # do recursive calls for sub-direcorties |
---|
132 | if subdirs: |
---|
133 | for subdir in subdirs: |
---|
134 | self.listFiles(os.path.join(path, subdir), callid=1) |
---|
135 | |
---|
136 | return |
---|
137 | |
---|
138 | def deleteFiles(self): |
---|
139 | ''' |
---|
140 | @Description: |
---|
141 | Deletes the files. |
---|
142 | |
---|
143 | @Input: |
---|
144 | self: instance of UIOFiles |
---|
145 | Description see class documentation. |
---|
146 | |
---|
147 | @Return: |
---|
148 | <nothing> |
---|
149 | ''' |
---|
150 | |
---|
151 | for f in self.files: |
---|
152 | silentremove(f) |
---|
153 | |
---|
154 | return |
---|