source: flex_extract.git/Source/Python/Classes/UioFiles.py @ 44174de

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

commented out the WRF parts since they are still under construction and added License SPDX tags

  • Property mode set to 100644
File size: 4.2 KB
Line 
1#!/usr/bin/env python3
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 - December 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#        - added delete method
23#
24# @License:
25#    (C) Copyright 2014-2019.
26#    Anne Philipp, Leopold Haimberger
27#
28#    SPDX-License-Identifier: CC-BY-4.0
29#
30#    This work is licensed under the Creative Commons Attribution 4.0
31#    International License. To view a copy of this license, visit
32#    http://creativecommons.org/licenses/by/4.0/ or send a letter to
33#    Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
34#*******************************************************************************
35
36# ------------------------------------------------------------------------------
37# MODULES
38# ------------------------------------------------------------------------------
39import os
40import sys
41import fnmatch
42
43# software specific modules from flex_extract
44sys.path.append('../')
45from Mods.tools import silent_remove, get_list_as_string
46
47# ------------------------------------------------------------------------------
48# CLASS
49# ------------------------------------------------------------------------------
50
51class UioFiles(object):
52    '''Collection of files matching a specific pattern.
53
54    The pattern can contain regular expressions for the files.
55    The files are listed and can be transformed to a single string or
56    they can be deleted.
57
58    Attributes
59    ----------
60    path : str
61        Directory where to list the files.
62
63    pattern : str
64        Regular expression pattern. For example: '\*.grb'
65
66    files : list of str
67        List of files matching the pattern in the path.
68    '''
69    # --------------------------------------------------------------------------
70    # CLASS METHODS
71    # --------------------------------------------------------------------------
72    def __init__(self, path, pattern):
73        '''Assignes a specific pattern for these files.
74
75        Parameters
76        ----------
77        path : str
78            Directory where to list the files.
79
80        pattern : str
81            Regular expression pattern. For example: '\*.grb'
82
83        Return
84        ------
85
86        '''
87
88        self.path = path
89        self.pattern = pattern
90        self.files = []
91
92        self._list_files(self.path)
93
94        return
95
96
97    def _list_files(self, path):
98        '''Lists all files in the directory with the matching
99        regular expression pattern.
100
101        Parameters
102        ----------
103        path : str
104            Path to the files.
105
106        Return
107        ------
108
109        '''
110        # Get the absolute path
111        path = os.path.abspath(path)
112
113        # get all files in the dir and subdir as absolut path
114        for root, dirnames, filenames in os.walk(path):
115            for filename in fnmatch.filter(filenames, self.pattern):
116                self.files.append(os.path.join(root, filename))
117
118        return
119
120
121    def __str__(self):
122        '''Converts the list of files into a single string.
123        The entries are sepereated by "," sign.
124
125        Parameters
126        ----------
127
128        Return
129        ------
130        files_string : str
131            The content of the list as a single string.
132        '''
133
134        filenames = [os.path.basename(f) for f in self.files]
135        files_string = get_list_as_string(filenames, concatenate_sign=', ')
136
137        return files_string
138
139
140    def delete_files(self):
141        '''Deletes the files.
142
143        Parameters
144        ----------
145
146        Return
147        ------
148
149        '''
150
151        for old_file in self.files:
152            silent_remove(old_file)
153
154        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG