[64cf353] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
[991df6a] | 3 | #******************************************************************************* |
---|
| 4 | # @Author: Anne Fouilloux (University of Oslo) |
---|
| 5 | # |
---|
| 6 | # @Date: October 2014 |
---|
| 7 | # |
---|
| 8 | # @Change History: |
---|
| 9 | # |
---|
| 10 | # November 2015 - Leopold Haimberger (University of Vienna): |
---|
[ff99eae] | 11 | # - modified method list_files to work with glob instead of listdir |
---|
| 12 | # - added pattern search in method list_files |
---|
[991df6a] | 13 | # |
---|
| 14 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 15 | # - applied PEP8 style guide |
---|
| 16 | # - added documentation |
---|
[ff99eae] | 17 | # - optimisation of method list_files since it didn't work correctly |
---|
[991df6a] | 18 | # for sub directories |
---|
[ff99eae] | 19 | # - additional speed up of method list_files |
---|
[991df6a] | 20 | # - modified the class so that it is initiated with a pattern instead |
---|
| 21 | # of suffixes. Gives more precision in selection of files. |
---|
| 22 | # |
---|
| 23 | # @License: |
---|
| 24 | # (C) Copyright 2014-2018. |
---|
| 25 | # |
---|
| 26 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 27 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 28 | # |
---|
| 29 | # @Class Decription: |
---|
| 30 | # The class is for file manipulation. It is initiated with a regular |
---|
| 31 | # expression pattern for this instance and can produce a list of Files |
---|
| 32 | # from the given file pattern. These files can be deleted. |
---|
| 33 | # |
---|
| 34 | # @Class Content: |
---|
| 35 | # - __init__ |
---|
[ff99eae] | 36 | # - list_files |
---|
| 37 | # - delete_files |
---|
| 38 | # |
---|
| 39 | # @Class Attributes: |
---|
| 40 | # - pattern |
---|
| 41 | # - files |
---|
[991df6a] | 42 | # |
---|
| 43 | #******************************************************************************* |
---|
[9ac56ea] | 44 | |
---|
[ab3dbdd] | 45 | # ------------------------------------------------------------------------------ |
---|
| 46 | # MODULES |
---|
| 47 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 48 | import os |
---|
[efdb01a] | 49 | import fnmatch |
---|
[991df6a] | 50 | |
---|
| 51 | # software specific module from flex_extract |
---|
[ff99eae] | 52 | #import profiling |
---|
| 53 | from tools import silent_remove |
---|
[991df6a] | 54 | |
---|
[ab3dbdd] | 55 | # ------------------------------------------------------------------------------ |
---|
[efdb01a] | 56 | # CLASS |
---|
[ab3dbdd] | 57 | # ------------------------------------------------------------------------------ |
---|
[991df6a] | 58 | |
---|
[ff99eae] | 59 | class UioFiles(object): |
---|
[507d47f] | 60 | ''' |
---|
| 61 | Class to manipulate files. At initialisation it has the attribute |
---|
[991df6a] | 62 | pattern which stores a regular expression pattern for the files associated |
---|
[507d47f] | 63 | with the instance of the class. |
---|
| 64 | ''' |
---|
[ab3dbdd] | 65 | # -------------------------------------------------------------------------- |
---|
[efdb01a] | 66 | # CLASS FUNCTIONS |
---|
[ab3dbdd] | 67 | # -------------------------------------------------------------------------- |
---|
[991df6a] | 68 | def __init__(self, pattern): |
---|
[507d47f] | 69 | ''' |
---|
| 70 | @Description: |
---|
[991df6a] | 71 | Assignes a specific pattern for these files. |
---|
[507d47f] | 72 | |
---|
| 73 | @Input: |
---|
[ff99eae] | 74 | self: instance of UioFiles |
---|
[507d47f] | 75 | Description see class documentation. |
---|
| 76 | |
---|
[991df6a] | 77 | pattern: string |
---|
| 78 | Regular expression pattern. For example: '*.grb' |
---|
[507d47f] | 79 | |
---|
| 80 | @Return: |
---|
| 81 | <nothing> |
---|
| 82 | ''' |
---|
[02c8c50] | 83 | |
---|
[991df6a] | 84 | self.pattern = pattern |
---|
[ff99eae] | 85 | self.files = None |
---|
[02c8c50] | 86 | |
---|
[507d47f] | 87 | return |
---|
| 88 | |
---|
[efdb01a] | 89 | #@profiling.timefn |
---|
[ff99eae] | 90 | def list_files(self, path, callid=0): |
---|
[507d47f] | 91 | ''' |
---|
| 92 | @Description: |
---|
| 93 | Lists all files in the directory with the matching |
---|
[991df6a] | 94 | regular expression pattern. |
---|
[507d47f] | 95 | |
---|
| 96 | @Input: |
---|
[ff99eae] | 97 | self: instance of UioFiles |
---|
[507d47f] | 98 | Description see class documentation. |
---|
| 99 | |
---|
[02c8c50] | 100 | path: string |
---|
[507d47f] | 101 | Directory where to list the files. |
---|
| 102 | |
---|
[efdb01a] | 103 | callid: integer |
---|
| 104 | Id which tells the function if its the first call |
---|
| 105 | or a recursive call. Default and first call is 0. |
---|
| 106 | Everything different from 0 is ment to be a recursive case. |
---|
| 107 | |
---|
[507d47f] | 108 | @Return: |
---|
| 109 | <nothing> |
---|
| 110 | ''' |
---|
[02c8c50] | 111 | |
---|
[efdb01a] | 112 | # initialize variable in first function call |
---|
| 113 | if callid == 0: |
---|
| 114 | self.files = [] |
---|
| 115 | |
---|
[02c8c50] | 116 | # Get the absolute path |
---|
| 117 | path = os.path.abspath(path) |
---|
[507d47f] | 118 | |
---|
[efdb01a] | 119 | # get the file list of the path if its not a directory and |
---|
[991df6a] | 120 | # if it contains the pattern |
---|
[efdb01a] | 121 | self.files.extend([os.path.join(path, k) for k in os.listdir(path) |
---|
[991df6a] | 122 | if fnmatch.fnmatch(k, self.pattern)]) |
---|
[efdb01a] | 123 | |
---|
| 124 | # find possible sub-directories in the path |
---|
| 125 | subdirs = [s for s in os.listdir(path) |
---|
| 126 | if os.path.isdir(os.path.join(path, s))] |
---|
| 127 | |
---|
| 128 | # do recursive calls for sub-direcorties |
---|
| 129 | if subdirs: |
---|
| 130 | for subdir in subdirs: |
---|
[ff99eae] | 131 | self.list_files(os.path.join(path, subdir), callid=1) |
---|
[991df6a] | 132 | |
---|
| 133 | return |
---|
| 134 | |
---|
[ff99eae] | 135 | def delete_files(self): |
---|
[991df6a] | 136 | ''' |
---|
| 137 | @Description: |
---|
| 138 | Deletes the files. |
---|
| 139 | |
---|
| 140 | @Input: |
---|
[ff99eae] | 141 | self: instance of UioFiles |
---|
[991df6a] | 142 | Description see class documentation. |
---|
| 143 | |
---|
| 144 | @Return: |
---|
| 145 | <nothing> |
---|
| 146 | ''' |
---|
| 147 | |
---|
[ff99eae] | 148 | for old_file in self.files: |
---|
| 149 | silent_remove(old_file) |
---|
[efdb01a] | 150 | |
---|
| 151 | return |
---|