source: flex_extract.git/Source/Python/Classes/UioFiles.py @ 0f89116

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

diverse changes due to PEP8 style guide and eliminating grib2flexpart; removed unused parameter

  • Property mode set to 100644
File size: 4.3 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
44#pylint: disable=wrong-import-position
45sys.path.append('../')
46from Mods.tools import silent_remove, get_list_as_string
47#pylint: enable=wrong-import-position
48
49# ------------------------------------------------------------------------------
50# CLASS
51# ------------------------------------------------------------------------------
52
53class UioFiles(object):
54    """Collection of files matching a specific pattern.
55
56    The pattern can contain regular expressions for the files.
57    The files are listed and can be transformed to a single string or
58    they can be deleted.
59
60    Attributes
61    ----------
62    path : str
63        Directory where to list the files.
64
65    pattern : str
66        Regular expression pattern. For example: '*.grb'
67
68    files : list of str
69        List of files matching the pattern in the path.
70    """
71    # --------------------------------------------------------------------------
72    # CLASS METHODS
73    # --------------------------------------------------------------------------
74    def __init__(self, path, pattern):
75        """Assignes a specific pattern for these files.
76
77        Parameters
78        ----------
79        path : str
80            Directory where to list the files.
81
82        pattern : str
83            Regular expression pattern. For example: '*.grb'
84
85        Return
86        ------
87
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
99    def _list_files(self, path):
100        """Lists all files in the directory with the matching
101        regular expression pattern.
102
103        Parameters
104        ----------
105        path : str
106            Path to the files.
107
108        Return
109        ------
110
111        """
112        # Get the absolute path
113        path = os.path.abspath(path)
114
115        # get all files in the dir and subdir as absolut path
116            # pylint: disable=W0612
117        for root, dirnames, filenames in os.walk(path):
118            for filename in fnmatch.filter(filenames, self.pattern):
119                self.files.append(os.path.join(root, filename))
120
121        return
122
123
124    def __str__(self):
125        """Converts the list of files into a single string.
126        The entries are sepereated by "," sign.
127
128        Parameters
129        ----------
130
131        Return
132        ------
133        files_string : str
134            The content of the list as a single string.
135        """
136
137        filenames = [os.path.basename(f) for f in self.files]
138        files_string = get_list_as_string(filenames, concatenate_sign=', ')
139
140        return files_string
141
142
143    def delete_files(self):
144        """Deletes the files.
145
146        Parameters
147        ----------
148
149        Return
150        ------
151
152        """
153
154        for old_file in self.files:
155            silent_remove(old_file)
156
157        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG