diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/__init__.py diff --git a/src/files.py b/src/files.py new file mode 100644 index 0000000..f4065c1 --- /dev/null +++ b/src/files.py @@ -0,0 +1,103 @@ +#!/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__) diff --git a/tests/test_files.py b/tests/test_files.py new file mode 100644 index 0000000..ae16d11 --- /dev/null +++ b/tests/test_files.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (c) IRAP CNRS +# Odile Coeur-Joly, Toulouse, France +# +""" + +tests.test_files Created on 18 dec. 2017 +""" + +import unittest + +from src.files import PireneaFiles + + +class PireneaFilesTestCase(unittest.TestCase): + + def setUp(self): + """Call before every test case.""" + self.pirenea = PireneaFiles() + self.folder = "D:/PIRENEA/DATA_1" + self.bad_folder = "c:/documents" + self.empty = "" + + def test_add_prefix(self): + with self.assertRaises(ValueError): + self.pirenea.__init__(self.bad_folder) + with self.assertRaises(ValueError): + self.pirenea.__init__(self.empty) + with self.assertRaises(ValueError): + self.pirenea.__init__(self.folder) + self.pirenea.add_prefix("PO") + + def test_remove_prefix(self): + with self.assertRaises(ValueError): + self.pirenea.__init__(self.folder) + self.pirenea.remove_prefix(self.empty) + +if __name__ == "__main__": + unittest.main() -- libgit2 0.21.2