files.py 3.22 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#        Copyright (c) IRAP CNRS
#        Odile Coeur-Joly, Toulouse, France
#
"""

src.filenames Created on 13 déc. 2017
"""
import os


class PireneaFiles(object):
    """
    Manage the files generated by PIRENEA : renaming, converting
    """

    def __init__(self, folder="D:\PIRENEA\DATA_1"):
        """
        Constructor. Raises ValueError if input folder does not exist.
        """
        self.folder = os.path.abspath(folder)
        if not os.path.isdir(self.folder):
            raise ValueError("Not a valid directory")
        if "PIRENEA" not in self.folder.upper() or "DATA" not in self.folder.upper():
            raise ValueError("Not a valid PIRENEA DATA directory")

    def add_prefix(self, prefix="P0"):
        """
        Search all files recursively and add a prefix to them.
        """
        self.__check_prefix(prefix)
        prefix += "_"
        for path, _dirs, files in os.walk(self.folder):
            for filename in files:
                new_filename = prefix + filename
                os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

    def remove_prefix(self, prefix="P0"):
        """
        Search all files recursively and remove their setup prefix.
        """
        self.__check_prefix(prefix)
        prefix += "_"
        for path, _dirs, files in os.walk(self.folder):
            for filename in files:
                f = filename.split(prefix)
                new_filename = prefix.join(f[1:])
                os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

    def __check_prefix(self, prefix="P0"):
        """
        Check if a prefix is valid.
        P0 : PIRENEA files produced with the Villa setup
        P1 : PIRENEA files produced from the IRAP setup
        P2 : PIRENEA files produced from the PILAB setup
        """
        prefix_list = {"P0", "P1", "P2"}
        if prefix not in prefix_list:
            raise ValueError("Not a valid prefix for a PIRENEA setup : (P0, P1 or P2 only)")


class PireneaDir(object):
    """
    Manage the files generated by PIRENEA : naming, converting
    """

    def __init__(self, folder="", year=2002):
        """
        Constructor
        """
        self.folder = folder
        self.year = int(year)


if __name__ == '__main__':

    """ test within one directory """
    try:
        root = input("Root directory for Pirenea: ")
        p = PireneaFiles(root)
        print("Working directory is: {0}".format(os.path.abspath(p.folder)))

        c = input("Add or Remove a prefix (A/R): ")
        if "A" in c.upper():
            """ Add a prefix """
            prefix = input("Choose a prefix to rename files (P0, P1, P2): ")
            print("Your input prefix is: {} ", prefix)
            p.add_prefix(prefix)
            print("... renaming with prefix is DONE !")
        elif "R" in c.upper():
            """ Remove a prefix """
            prefix = input("Choose a prefix to remove (P0, P1, P2): ")
            print("Your input prefix is: {} ", prefix)
            p.remove_prefix(prefix)
            print("... removing the prefix is DONE !")

    except ValueError as err:
        print("Error: {0}".format(err))

else:
    print("\nImporting... ", __name__)