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
RevLine 
[8463d78]1#!/usr/bin/env python3
[2fb99de]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#
[6f951ca]14#    February - December 2018 - Anne Philipp (University of Vienna):
[2fb99de]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.
[6f951ca]22#        - added delete method
[2fb99de]23#
24# @License:
[6f951ca]25#    (C) Copyright 2014-2019.
26#    Anne Philipp, Leopold Haimberger
[2fb99de]27#
[44174de]28#    SPDX-License-Identifier: CC-BY-4.0
29#
[6f951ca]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.
[2fb99de]34#*******************************************************************************
35
36# ------------------------------------------------------------------------------
37# MODULES
38# ------------------------------------------------------------------------------
39import os
[ca867de]40import sys
[2fb99de]41import fnmatch
42
[6f951ca]43# software specific modules from flex_extract
[ca867de]44sys.path.append('../')
[ba99230]45from Mods.tools import silent_remove, get_list_as_string
[2fb99de]46
47# ------------------------------------------------------------------------------
48# CLASS
49# ------------------------------------------------------------------------------
50
51class UioFiles(object):
[6f951ca]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.
[2fb99de]68    '''
69    # --------------------------------------------------------------------------
[6f951ca]70    # CLASS METHODS
[2fb99de]71    # --------------------------------------------------------------------------
72    def __init__(self, path, pattern):
[274f9ef]73        '''Assignes a specific pattern for these files.
[2fb99de]74
[274f9ef]75        Parameters
76        ----------
[6f951ca]77        path : str
[274f9ef]78            Directory where to list the files.
[2fb99de]79
[6f951ca]80        pattern : str
[274f9ef]81            Regular expression pattern. For example: '\*.grb'
[2fb99de]82
[274f9ef]83        Return
84        ------
[2fb99de]85
86        '''
87
88        self.path = path
89        self.pattern = pattern
[25b14be]90        self.files = []
[2fb99de]91
[5cb0eaa]92        self._list_files(self.path)
[2fb99de]93
94        return
95
[6f951ca]96
[5cb0eaa]97    def _list_files(self, path):
[274f9ef]98        '''Lists all files in the directory with the matching
99        regular expression pattern.
[2fb99de]100
[274f9ef]101        Parameters
102        ----------
[6f951ca]103        path : str
[274f9ef]104            Path to the files.
[2fb99de]105
[274f9ef]106        Return
107        ------
[2fb99de]108
109        '''
110        # Get the absolute path
111        path = os.path.abspath(path)
112
[25b14be]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))
[2fb99de]117
118        return
119
[6f951ca]120
[2fb99de]121    def __str__(self):
[274f9ef]122        '''Converts the list of files into a single string.
123        The entries are sepereated by "," sign.
[2fb99de]124
[274f9ef]125        Parameters
126        ----------
[2fb99de]127
[274f9ef]128        Return
129        ------
[6f951ca]130        files_string : str
[274f9ef]131            The content of the list as a single string.
[2fb99de]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
[6f951ca]139
[2fb99de]140    def delete_files(self):
[274f9ef]141        '''Deletes the files.
142
143        Parameters
144        ----------
[2fb99de]145
[274f9ef]146        Return
147        ------
[2fb99de]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