[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 | # |
---|
[6f951ca] | 14 | # February - December 2018 - Anne Philipp (University of Vienna): |
---|
[2fb99de] | 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. |
---|
[6f951ca] | 22 | # - added delete method |
---|
[2fb99de] | 23 | # |
---|
| 24 | # @License: |
---|
[6f951ca] | 25 | # (C) Copyright 2014-2019. |
---|
| 26 | # Anne Philipp, Leopold Haimberger |
---|
[2fb99de] | 27 | # |
---|
[6f951ca] | 28 | # This work is licensed under the Creative Commons Attribution 4.0 |
---|
| 29 | # International License. To view a copy of this license, visit |
---|
| 30 | # http://creativecommons.org/licenses/by/4.0/ or send a letter to |
---|
| 31 | # Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
---|
[2fb99de] | 32 | #******************************************************************************* |
---|
| 33 | |
---|
| 34 | # ------------------------------------------------------------------------------ |
---|
| 35 | # MODULES |
---|
| 36 | # ------------------------------------------------------------------------------ |
---|
| 37 | import os |
---|
[ca867de] | 38 | import sys |
---|
[2fb99de] | 39 | import fnmatch |
---|
| 40 | |
---|
[6f951ca] | 41 | # software specific modules from flex_extract |
---|
[ca867de] | 42 | sys.path.append('../') |
---|
[25b14be] | 43 | from mods.tools import silent_remove, get_list_as_string |
---|
[2fb99de] | 44 | |
---|
| 45 | # ------------------------------------------------------------------------------ |
---|
| 46 | # CLASS |
---|
| 47 | # ------------------------------------------------------------------------------ |
---|
| 48 | |
---|
| 49 | class UioFiles(object): |
---|
[6f951ca] | 50 | '''Collection of files matching a specific pattern. |
---|
| 51 | |
---|
| 52 | The pattern can contain regular expressions for the files. |
---|
| 53 | The files are listed and can be transformed to a single string or |
---|
| 54 | they can be deleted. |
---|
| 55 | |
---|
| 56 | Attributes |
---|
| 57 | ---------- |
---|
| 58 | path : str |
---|
| 59 | Directory where to list the files. |
---|
| 60 | |
---|
| 61 | pattern : str |
---|
| 62 | Regular expression pattern. For example: '\*.grb' |
---|
| 63 | |
---|
| 64 | files : list of str |
---|
| 65 | List of files matching the pattern in the path. |
---|
[2fb99de] | 66 | ''' |
---|
| 67 | # -------------------------------------------------------------------------- |
---|
[6f951ca] | 68 | # CLASS METHODS |
---|
[2fb99de] | 69 | # -------------------------------------------------------------------------- |
---|
| 70 | def __init__(self, path, pattern): |
---|
[274f9ef] | 71 | '''Assignes a specific pattern for these files. |
---|
[2fb99de] | 72 | |
---|
[274f9ef] | 73 | Parameters |
---|
| 74 | ---------- |
---|
[6f951ca] | 75 | path : str |
---|
[274f9ef] | 76 | Directory where to list the files. |
---|
[2fb99de] | 77 | |
---|
[6f951ca] | 78 | pattern : str |
---|
[274f9ef] | 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 | |
---|
[5cb0eaa] | 90 | self._list_files(self.path) |
---|
[2fb99de] | 91 | |
---|
| 92 | return |
---|
| 93 | |
---|
[6f951ca] | 94 | |
---|
[5cb0eaa] | 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 | ---------- |
---|
[6f951ca] | 101 | path : str |
---|
[274f9ef] | 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 | |
---|
[6f951ca] | 118 | |
---|
[2fb99de] | 119 | def __str__(self): |
---|
[274f9ef] | 120 | '''Converts the list of files into a single string. |
---|
| 121 | The entries are sepereated by "," sign. |
---|
[2fb99de] | 122 | |
---|
[274f9ef] | 123 | Parameters |
---|
| 124 | ---------- |
---|
[2fb99de] | 125 | |
---|
[274f9ef] | 126 | Return |
---|
| 127 | ------ |
---|
[6f951ca] | 128 | files_string : str |
---|
[274f9ef] | 129 | The content of the list as a single string. |
---|
[2fb99de] | 130 | ''' |
---|
| 131 | |
---|
| 132 | filenames = [os.path.basename(f) for f in self.files] |
---|
| 133 | files_string = get_list_as_string(filenames, concatenate_sign=', ') |
---|
| 134 | |
---|
| 135 | return files_string |
---|
| 136 | |
---|
[6f951ca] | 137 | |
---|
[2fb99de] | 138 | def delete_files(self): |
---|
[274f9ef] | 139 | '''Deletes the files. |
---|
| 140 | |
---|
| 141 | Parameters |
---|
| 142 | ---------- |
---|
[2fb99de] | 143 | |
---|
[274f9ef] | 144 | Return |
---|
| 145 | ------ |
---|
[2fb99de] | 146 | |
---|
| 147 | ''' |
---|
| 148 | |
---|
| 149 | for old_file in self.files: |
---|
| 150 | silent_remove(old_file) |
---|
| 151 | |
---|
| 152 | return |
---|