Commit de45ca6c876012b5ea8997c38a551e6dbca02eeb

Authored by Odile@pc2050
1 parent 2aba81e8
Exists in master

first commit

Showing 3 changed files with 144 additions and 0 deletions   Show diff stats
src/__init__.py 0 → 100644
src/files.py 0 → 100644
... ... @@ -0,0 +1,103 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +#
  4 +# Copyright (c) IRAP CNRS
  5 +# Odile Coeur-Joly, Toulouse, France
  6 +#
  7 +"""
  8 +
  9 +src.filenames Created on 13 déc. 2017
  10 +"""
  11 +import os
  12 +
  13 +
  14 +class PireneaFiles(object):
  15 + """
  16 + Manage the files generated by PIRENEA : renaming, converting
  17 + """
  18 +
  19 + def __init__(self, folder="D:\PIRENEA\DATA_1"):
  20 + """
  21 + Constructor. Raises ValueError if input folder does not exist.
  22 + """
  23 + self.folder = os.path.abspath(folder)
  24 + if not os.path.isdir(self.folder):
  25 + raise ValueError("Not a valid directory")
  26 + if "PIRENEA" not in self.folder.upper() or "DATA" not in self.folder.upper():
  27 + raise ValueError("Not a valid PIRENEA DATA directory")
  28 +
  29 + def add_prefix(self, prefix="P0"):
  30 + """
  31 + Search all files recursively and add a prefix to them.
  32 + """
  33 + self.__check_prefix(prefix)
  34 + prefix += "_"
  35 + for path, _dirs, files in os.walk(self.folder):
  36 + for filename in files:
  37 + new_filename = prefix + filename
  38 + os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
  39 +
  40 + def remove_prefix(self, prefix="P0"):
  41 + """
  42 + Search all files recursively and remove their setup prefix.
  43 + """
  44 + self.__check_prefix(prefix)
  45 + prefix += "_"
  46 + for path, _dirs, files in os.walk(self.folder):
  47 + for filename in files:
  48 + f = filename.split(prefix)
  49 + new_filename = prefix.join(f[1:])
  50 + os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
  51 +
  52 + def __check_prefix(self, prefix="P0"):
  53 + """
  54 + Check if a prefix is valid.
  55 + P0 : PIRENEA files produced with the Villa setup
  56 + P1 : PIRENEA files produced from the IRAP setup
  57 + P2 : PIRENEA files produced from the PILAB setup
  58 + """
  59 + prefix_list = {"P0", "P1", "P2"}
  60 + if prefix not in prefix_list:
  61 + raise ValueError("Not a valid prefix for a PIRENEA setup : (P0, P1 or P2 only)")
  62 +
  63 +
  64 +class PireneaDir(object):
  65 + """
  66 + Manage the files generated by PIRENEA : naming, converting
  67 + """
  68 +
  69 + def __init__(self, folder="", year=2002):
  70 + """
  71 + Constructor
  72 + """
  73 + self.folder = folder
  74 + self.year = int(year)
  75 +
  76 +
  77 +if __name__ == '__main__':
  78 +
  79 + """ test within one directory """
  80 + try:
  81 + root = input("Root directory for Pirenea: ")
  82 + p = PireneaFiles(root)
  83 + print("Working directory is: {0}".format(os.path.abspath(p.folder)))
  84 +
  85 + c = input("Add or Remove a prefix (A/R): ")
  86 + if "A" in c.upper():
  87 + """ Add a prefix """
  88 + prefix = input("Choose a prefix to rename files (P0, P1, P2): ")
  89 + print("Your input prefix is: {} ", prefix)
  90 + p.add_prefix(prefix)
  91 + print("... renaming with prefix is DONE !")
  92 + elif "R" in c.upper():
  93 + """ Remove a prefix """
  94 + prefix = input("Choose a prefix to remove (P0, P1, P2): ")
  95 + print("Your input prefix is: {} ", prefix)
  96 + p.remove_prefix(prefix)
  97 + print("... removing the prefix is DONE !")
  98 +
  99 + except ValueError as err:
  100 + print("Error: {0}".format(err))
  101 +
  102 +else:
  103 + print("\nImporting... ", __name__)
... ...
tests/test_files.py 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +#
  4 +# Copyright (c) IRAP CNRS
  5 +# Odile Coeur-Joly, Toulouse, France
  6 +#
  7 +"""
  8 +
  9 +tests.test_files Created on 18 dec. 2017
  10 +"""
  11 +
  12 +import unittest
  13 +
  14 +from src.files import PireneaFiles
  15 +
  16 +
  17 +class PireneaFilesTestCase(unittest.TestCase):
  18 +
  19 + def setUp(self):
  20 + """Call before every test case."""
  21 + self.pirenea = PireneaFiles()
  22 + self.folder = "D:/PIRENEA/DATA_1"
  23 + self.bad_folder = "c:/documents"
  24 + self.empty = ""
  25 +
  26 + def test_add_prefix(self):
  27 + with self.assertRaises(ValueError):
  28 + self.pirenea.__init__(self.bad_folder)
  29 + with self.assertRaises(ValueError):
  30 + self.pirenea.__init__(self.empty)
  31 + with self.assertRaises(ValueError):
  32 + self.pirenea.__init__(self.folder)
  33 + self.pirenea.add_prefix("PO")
  34 +
  35 + def test_remove_prefix(self):
  36 + with self.assertRaises(ValueError):
  37 + self.pirenea.__init__(self.folder)
  38 + self.pirenea.remove_prefix(self.empty)
  39 +
  40 +if __name__ == "__main__":
  41 + unittest.main()
... ...