[2fb99de] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 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): |
---|
| 11 | # - modified method list_files to work with glob instead of listdir |
---|
| 12 | # - added pattern search in method list_files |
---|
| 13 | # |
---|
| 14 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 15 | # - applied PEP8 style guide |
---|
| 16 | # - added documentation |
---|
| 17 | # - optimisation of method list_files since it didn't work correctly |
---|
| 18 | # for sub directories |
---|
| 19 | # - additional speed up of method list_files |
---|
| 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__ |
---|
| 36 | # - __str__ |
---|
| 37 | # - __list_files__ |
---|
| 38 | # - delete_files |
---|
| 39 | # |
---|
| 40 | # @Class Attributes: |
---|
| 41 | # - pattern |
---|
| 42 | # - files |
---|
| 43 | # |
---|
| 44 | #******************************************************************************* |
---|
| 45 | |
---|
| 46 | # ------------------------------------------------------------------------------ |
---|
| 47 | # MODULES |
---|
| 48 | # ------------------------------------------------------------------------------ |
---|
| 49 | import os |
---|
[ca867de] | 50 | import sys |
---|
[2fb99de] | 51 | import fnmatch |
---|
| 52 | |
---|
| 53 | # software specific module from flex_extract |
---|
[ca867de] | 54 | sys.path.append('../') |
---|
[2fb99de] | 55 | #import profiling |
---|
[25b14be] | 56 | from mods.tools import silent_remove, get_list_as_string |
---|
[2fb99de] | 57 | |
---|
| 58 | # ------------------------------------------------------------------------------ |
---|
| 59 | # CLASS |
---|
| 60 | # ------------------------------------------------------------------------------ |
---|
| 61 | |
---|
| 62 | class UioFiles(object): |
---|
[274f9ef] | 63 | '''Class to manipulate files. At initialisation it has the pattern |
---|
[25b14be] | 64 | which stores a regular expression pattern for the files, the path |
---|
| 65 | to the files and the files already. |
---|
[2fb99de] | 66 | ''' |
---|
| 67 | # -------------------------------------------------------------------------- |
---|
| 68 | # CLASS FUNCTIONS |
---|
| 69 | # -------------------------------------------------------------------------- |
---|
| 70 | def __init__(self, path, pattern): |
---|
[274f9ef] | 71 | '''Assignes a specific pattern for these files. |
---|
[2fb99de] | 72 | |
---|
[274f9ef] | 73 | Parameters |
---|
| 74 | ---------- |
---|
| 75 | path : :obj:`string` |
---|
| 76 | Directory where to list the files. |
---|
[2fb99de] | 77 | |
---|
[274f9ef] | 78 | pattern : :obj:`string` |
---|
| 79 | Regular expression pattern. For example: '\*.grb' |
---|
[2fb99de] | 80 | |
---|
[274f9ef] | 81 | Return |
---|
| 82 | ------ |
---|
[2fb99de] | 83 | |
---|
| 84 | ''' |
---|
| 85 | |
---|
| 86 | self.path = path |
---|
| 87 | self.pattern = pattern |
---|
[25b14be] | 88 | self.files = [] |
---|
[2fb99de] | 89 | |
---|
| 90 | self.__list_files__(self.path) |
---|
| 91 | |
---|
| 92 | return |
---|
| 93 | |
---|
| 94 | #@profiling.timefn |
---|
[25b14be] | 95 | def __list_files__(self, path): |
---|
[274f9ef] | 96 | '''Lists all files in the directory with the matching |
---|
| 97 | regular expression pattern. |
---|
[2fb99de] | 98 | |
---|
[274f9ef] | 99 | Parameters |
---|
| 100 | ---------- |
---|
| 101 | path : :obj:`string` |
---|
| 102 | Path to the files. |
---|
[2fb99de] | 103 | |
---|
[274f9ef] | 104 | Return |
---|
| 105 | ------ |
---|
[2fb99de] | 106 | |
---|
| 107 | ''' |
---|
| 108 | # Get the absolute path |
---|
| 109 | path = os.path.abspath(path) |
---|
| 110 | |
---|
[25b14be] | 111 | # get all files in the dir and subdir as absolut path |
---|
| 112 | for root, dirnames, filenames in os.walk(path): |
---|
| 113 | for filename in fnmatch.filter(filenames, self.pattern): |
---|
| 114 | self.files.append(os.path.join(root, filename)) |
---|
[2fb99de] | 115 | |
---|
| 116 | return |
---|
| 117 | |
---|
| 118 | def __str__(self): |
---|
[274f9ef] | 119 | '''Converts the list of files into a single string. |
---|
| 120 | The entries are sepereated by "," sign. |
---|
[2fb99de] | 121 | |
---|
[274f9ef] | 122 | Parameters |
---|
| 123 | ---------- |
---|
[2fb99de] | 124 | |
---|
[274f9ef] | 125 | Return |
---|
| 126 | ------ |
---|
| 127 | files_string : :obj:`string` |
---|
| 128 | The content of the list as a single string. |
---|
[2fb99de] | 129 | ''' |
---|
| 130 | |
---|
| 131 | filenames = [os.path.basename(f) for f in self.files] |
---|
| 132 | files_string = get_list_as_string(filenames, concatenate_sign=', ') |
---|
| 133 | |
---|
| 134 | return files_string |
---|
| 135 | |
---|
| 136 | def delete_files(self): |
---|
[274f9ef] | 137 | '''Deletes the files. |
---|
| 138 | |
---|
| 139 | Parameters |
---|
| 140 | ---------- |
---|
[2fb99de] | 141 | |
---|
[274f9ef] | 142 | Return |
---|
| 143 | ------ |
---|
[2fb99de] | 144 | |
---|
| 145 | ''' |
---|
| 146 | |
---|
| 147 | for old_file in self.files: |
---|
| 148 | silent_remove(old_file) |
---|
| 149 | |
---|
| 150 | return |
---|