source: flex_extract.git/python/UioFiles.py @ ff99eae

ctbtodevfeature/makefilesorigin/task/language-editingtask/language-editing
Last change on this file since ff99eae was ff99eae, checked in by Anne Philipp <anne.philipp@…>, 3 years ago

completed application of pep8 style guide and pylint investigations. added documentation almost everywhere

  • Property mode set to 100644
File size: 4.5 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#    - list_files
37#    - delete_files
38#
39# @Class Attributes:
40#    - pattern
41#    - files
42#
43#*******************************************************************************
44
45# ------------------------------------------------------------------------------
46# MODULES
47# ------------------------------------------------------------------------------
48import os
49import fnmatch
50
51# software specific module from flex_extract
52#import profiling
53from tools import silent_remove
54
55# ------------------------------------------------------------------------------
56# CLASS
57# ------------------------------------------------------------------------------
58
59class UioFiles(object):
60    '''
61    Class to manipulate files. At initialisation it has the attribute
62    pattern which stores a regular expression pattern for the files associated
63    with the instance of the class.
64    '''
65    # --------------------------------------------------------------------------
66    # CLASS FUNCTIONS
67    # --------------------------------------------------------------------------
68    def __init__(self, pattern):
69        '''
70        @Description:
71            Assignes a specific pattern for these files.
72
73        @Input:
74            self: instance of UioFiles
75                Description see class documentation.
76
77            pattern: string
78                Regular expression pattern. For example: '*.grb'
79
80        @Return:
81            <nothing>
82        '''
83
84        self.pattern = pattern
85        self.files = None
86
87        return
88
89    #@profiling.timefn
90    def list_files(self, path, callid=0):
91        '''
92        @Description:
93            Lists all files in the directory with the matching
94            regular expression pattern.
95
96        @Input:
97            self: instance of UioFiles
98                Description see class documentation.
99
100            path: string
101                Directory where to list the files.
102
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
108        @Return:
109            <nothing>
110        '''
111
112        # initialize variable in first function call
113        if callid == 0:
114            self.files = []
115
116        # Get the absolute path
117        path = os.path.abspath(path)
118
119        # get the file list of the path if its not a directory and
120        # if it contains the pattern
121        self.files.extend([os.path.join(path, k) for k in os.listdir(path)
122                           if fnmatch.fnmatch(k, self.pattern)])
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:
131                self.list_files(os.path.join(path, subdir), callid=1)
132
133        return
134
135    def delete_files(self):
136        '''
137        @Description:
138            Deletes the files.
139
140        @Input:
141            self: instance of UioFiles
142                Description see class documentation.
143
144        @Return:
145            <nothing>
146        '''
147
148        for old_file in self.files:
149            silent_remove(old_file)
150
151        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG