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

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

after tarball check, applied: dos2unix conversion, some spell corrections, TAB to Space correction

  • Property mode set to 100644
File size: 4.3 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
[0f89116]44#pylint: disable=wrong-import-position
[ca867de]45sys.path.append('../')
[ba99230]46from Mods.tools import silent_remove, get_list_as_string
[0f89116]47#pylint: enable=wrong-import-position
[2fb99de]48
49# ------------------------------------------------------------------------------
50# CLASS
51# ------------------------------------------------------------------------------
52
53class UioFiles(object):
[0f89116]54    """Collection of files matching a specific pattern.
[6f951ca]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
[0f89116]66        Regular expression pattern. For example: '*.grb'
[6f951ca]67
68    files : list of str
69        List of files matching the pattern in the path.
[0f89116]70    """
[2fb99de]71    # --------------------------------------------------------------------------
[6f951ca]72    # CLASS METHODS
[2fb99de]73    # --------------------------------------------------------------------------
74    def __init__(self, path, pattern):
[0f89116]75        """Assignes a specific pattern for these files.
[2fb99de]76
[274f9ef]77        Parameters
78        ----------
[6f951ca]79        path : str
[274f9ef]80            Directory where to list the files.
[2fb99de]81
[6f951ca]82        pattern : str
[0f89116]83            Regular expression pattern. For example: '*.grb'
[2fb99de]84
[274f9ef]85        Return
86        ------
[2fb99de]87
[0f89116]88        """
[2fb99de]89
90        self.path = path
91        self.pattern = pattern
[25b14be]92        self.files = []
[2fb99de]93
[5cb0eaa]94        self._list_files(self.path)
[2fb99de]95
96        return
97
[6f951ca]98
[5cb0eaa]99    def _list_files(self, path):
[0f89116]100        """Lists all files in the directory with the matching
[274f9ef]101        regular expression pattern.
[2fb99de]102
[274f9ef]103        Parameters
104        ----------
[6f951ca]105        path : str
[274f9ef]106            Path to the files.
[2fb99de]107
[274f9ef]108        Return
109        ------
[2fb99de]110
[0f89116]111        """
[2fb99de]112        # Get the absolute path
113        path = os.path.abspath(path)
114
[25b14be]115        # get all files in the dir and subdir as absolut path
[d9abaac]116        # pylint: disable=W0612
[25b14be]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))
[2fb99de]120
121        return
122
[6f951ca]123
[2fb99de]124    def __str__(self):
[0f89116]125        """Converts the list of files into a single string.
[274f9ef]126        The entries are sepereated by "," sign.
[2fb99de]127
[274f9ef]128        Parameters
129        ----------
[2fb99de]130
[274f9ef]131        Return
132        ------
[6f951ca]133        files_string : str
[274f9ef]134            The content of the list as a single string.
[0f89116]135        """
[2fb99de]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
[6f951ca]142
[2fb99de]143    def delete_files(self):
[0f89116]144        """Deletes the files.
[274f9ef]145
146        Parameters
147        ----------
[2fb99de]148
[274f9ef]149        Return
150        ------
[2fb99de]151
[0f89116]152        """
[2fb99de]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