source: flex_extract.git/python/UioFiles.py @ 97e09f4

ctbtodev
Last change on this file since 97e09f4 was 54a8a01, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

restructuring, documentations and bug fixes

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[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__
[54a8a01]36#    - __str__
37#    - __list_files__
[ff99eae]38#    - delete_files
39#
40# @Class Attributes:
41#    - pattern
42#    - files
[991df6a]43#
44#*******************************************************************************
[9ac56ea]45
[ab3dbdd]46# ------------------------------------------------------------------------------
47# MODULES
48# ------------------------------------------------------------------------------
[d69b677]49import os
[efdb01a]50import fnmatch
[991df6a]51
52# software specific module from flex_extract
[ff99eae]53#import profiling
[54a8a01]54from tools import silent_remove, get_list_as_string
[991df6a]55
[ab3dbdd]56# ------------------------------------------------------------------------------
[efdb01a]57# CLASS
[ab3dbdd]58# ------------------------------------------------------------------------------
[991df6a]59
[ff99eae]60class UioFiles(object):
[507d47f]61    '''
62    Class to manipulate files. At initialisation it has the attribute
[991df6a]63    pattern which stores a regular expression pattern for the files associated
[507d47f]64    with the instance of the class.
65    '''
[ab3dbdd]66    # --------------------------------------------------------------------------
[efdb01a]67    # CLASS FUNCTIONS
[ab3dbdd]68    # --------------------------------------------------------------------------
[54a8a01]69    def __init__(self, path, pattern):
[507d47f]70        '''
71        @Description:
[991df6a]72            Assignes a specific pattern for these files.
[507d47f]73
74        @Input:
[ff99eae]75            self: instance of UioFiles
[507d47f]76                Description see class documentation.
77
[54a8a01]78            path: string
79                Directory where to list the files.
80
[991df6a]81            pattern: string
82                Regular expression pattern. For example: '*.grb'
[507d47f]83
84        @Return:
85            <nothing>
86        '''
[02c8c50]87
[54a8a01]88        self.path = path
[991df6a]89        self.pattern = pattern
[ff99eae]90        self.files = None
[02c8c50]91
[54a8a01]92        self.__list_files__(self.path)
93
[507d47f]94        return
95
[efdb01a]96    #@profiling.timefn
[54a8a01]97    def __list_files__(self, path, callid=0):
[507d47f]98        '''
99        @Description:
100            Lists all files in the directory with the matching
[991df6a]101            regular expression pattern.
[507d47f]102
103        @Input:
[ff99eae]104            self: instance of UioFiles
[507d47f]105                Description see class documentation.
106
[02c8c50]107            path: string
[54a8a01]108                Path to the files.
[507d47f]109
[efdb01a]110            callid: integer
111                Id which tells the function if its the first call
112                or a recursive call. Default and first call is 0.
113                Everything different from 0 is ment to be a recursive case.
114
[507d47f]115        @Return:
116            <nothing>
117        '''
[02c8c50]118
[efdb01a]119        # initialize variable in first function call
120        if callid == 0:
121            self.files = []
122
[02c8c50]123        # Get the absolute path
124        path = os.path.abspath(path)
[507d47f]125
[efdb01a]126        # get the file list of the path if its not a directory and
[991df6a]127        # if it contains the pattern
[efdb01a]128        self.files.extend([os.path.join(path, k) for k in os.listdir(path)
[991df6a]129                           if fnmatch.fnmatch(k, self.pattern)])
[efdb01a]130
131        # find possible sub-directories in the path
132        subdirs = [s for s in os.listdir(path)
133                   if os.path.isdir(os.path.join(path, s))]
134
135        # do recursive calls for sub-direcorties
136        if subdirs:
137            for subdir in subdirs:
[54a8a01]138                self.__list_files__(os.path.join(path, subdir), callid=1)
[991df6a]139
140        return
141
[54a8a01]142    def __str__(self):
143        '''
144        @Description:
145            Converts the list of files into a single string.
146            The entries are sepereated by "," sign.
147
148        @Input:
149            self: instance of UioFiles
150                Description see class documentation.
151
152        @Return:
153            files_string: string
154                The content of the list as a single string.
155        '''
156
157        filenames = [os.path.basename(f) for f in self.files]
158        files_string = get_list_as_string(filenames, concatenate_sign=', ')
159
160        return files_string
161
[ff99eae]162    def delete_files(self):
[991df6a]163        '''
164        @Description:
165            Deletes the files.
166
167        @Input:
[ff99eae]168            self: instance of UioFiles
[991df6a]169                Description see class documentation.
170
171        @Return:
172            <nothing>
173        '''
174
[ff99eae]175        for old_file in self.files:
176            silent_remove(old_file)
[efdb01a]177
178        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG