source: flex_extract.git/source/python/classes/UioFiles.py @ ca867de

ctbtodev
Last change on this file since ca867de was ca867de, checked in by Anne Philipp <anne.philipp@…>, 5 years ago

refactored functions in EcFlexpart? and did some minor changes

  • Property mode set to 100644
File size: 4.6 KB
Line 
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# ------------------------------------------------------------------------------
49import os
50import sys
51import fnmatch
52
53# software specific module from flex_extract
54sys.path.append('../')
55#import profiling
56from mods.tools import silent_remove, get_list_as_string
57
58# ------------------------------------------------------------------------------
59# CLASS
60# ------------------------------------------------------------------------------
61
62class UioFiles(object):
63    '''
64    Class to manipulate files. At initialisation it has the pattern
65    which stores a regular expression pattern for the files, the path
66    to the files and the files already.
67    '''
68    # --------------------------------------------------------------------------
69    # CLASS FUNCTIONS
70    # --------------------------------------------------------------------------
71    def __init__(self, path, pattern):
72        '''
73        @Description:
74            Assignes a specific pattern for these files.
75
76        @Input:
77            self: instance of UioFiles
78                Description see class documentation.
79
80            path: string
81                Directory where to list the files.
82
83            pattern: string
84                Regular expression pattern. For example: '*.grb'
85
86        @Return:
87            <nothing>
88        '''
89
90        self.path = path
91        self.pattern = pattern
92        self.files = []
93
94        self.__list_files__(self.path)
95
96        return
97
98    #@profiling.timefn
99    def __list_files__(self, path):
100        '''
101        @Description:
102            Lists all files in the directory with the matching
103            regular expression pattern.
104
105        @Input:
106            self: instance of UioFiles
107                Description see class documentation.
108
109            path: string
110                Path to the files.
111
112        @Return:
113            <nothing>
114        '''
115        # Get the absolute path
116        path = os.path.abspath(path)
117
118        # get all files in the dir and subdir as absolut path
119        for root, dirnames, filenames in os.walk(path):
120            for filename in fnmatch.filter(filenames, self.pattern):
121                self.files.append(os.path.join(root, filename))
122
123        return
124
125    def __str__(self):
126        '''
127        @Description:
128            Converts the list of files into a single string.
129            The entries are sepereated by "," sign.
130
131        @Input:
132            self: instance of UioFiles
133                Description see class documentation.
134
135        @Return:
136            files_string: string
137                The content of the list as a single string.
138        '''
139
140        filenames = [os.path.basename(f) for f in self.files]
141        files_string = get_list_as_string(filenames, concatenate_sign=', ')
142
143        return files_string
144
145    def delete_files(self):
146        '''
147        @Description:
148            Deletes the files.
149
150        @Input:
151            self: instance of UioFiles
152                Description see class documentation.
153
154        @Return:
155            <nothing>
156        '''
157
158        for old_file in self.files:
159            silent_remove(old_file)
160
161        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG