From 39529aa7ca7d5c7114b70b747e682b72fa5d7e63 Mon Sep 17 00:00:00 2001 From: Francesco Ioli Date: Thu, 14 Sep 2023 12:16:10 +0200 Subject: [PATCH] updated file organizer and added notebook --- notebooks/file_organization.ipynb | 127 ++++++++++++++++++ notebooks/image_renaming.ipynb | 3 +- .../image_renaming_sandbox.py | 0 .../potree_preview_sandbox.ipynb | 0 .../potree_preview_sandbox.py | 0 .../utm_projection_sandbox.py | 0 src/impreproc/files.py | 57 +++++--- 7 files changed, 168 insertions(+), 19 deletions(-) create mode 100755 notebooks/file_organization.ipynb rename {notebooks => sandbox}/image_renaming_sandbox.py (100%) rename {notebooks => sandbox}/potree_preview_sandbox.ipynb (100%) rename {notebooks => sandbox}/potree_preview_sandbox.py (100%) rename {notebooks => sandbox}/utm_projection_sandbox.py (100%) diff --git a/notebooks/file_organization.ipynb b/notebooks/file_organization.ipynb new file mode 100755 index 0000000..50913b9 --- /dev/null +++ b/notebooks/file_organization.ipynb @@ -0,0 +1,127 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Organize files based on their extensions" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "\n", + "from impreproc.files import Organizer\n", + "\n", + "# Define parameters\n", + "# Note that in Jupyter Notebooks, the path are relative to the notebooks folder!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Single directory - move files\n", + "imdir = \"../data/conversion/DJI_202303031031_001\"\n", + "recursive = False\n", + "inplace = True\n", + "organizer = Organizer(recursive=recursive, inplace=inplace)\n", + "organizer.organize(imdir)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Single directory - copy files\n", + "imdir = \"../data/conversion/DJI_202303031031_001\"\n", + "recursive = False\n", + "inplace = False\n", + "organizer = Organizer(recursive=recursive, inplace=inplace)\n", + "organizer.organize(imdir)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Batch on multiple directories - move files\n", + "imdir = \"../data/conversion\"\n", + "recursive = True\n", + "organizer = Organizer(recursive=recursive)\n", + "organizer.organize(imdir)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Define your own organization rules\n", + "\n", + "RULES = {\n", + " \"raw\": [\"dng\", \"cr2\", \"nef\", \"arw\"],\n", + " \"jpg\": [\"jpg\", \"jpeg\"],\n", + " \"png\": [\"png\"],\n", + " \"tif\": [\"tif\", \"tiff\"],\n", + " \"dji_gnss\": [\"obs\", \"nav\", \"bin\", \"mrk\"],\n", + " \"videos\": [\"mp4\", \"mkv\", \"avi\", \"mov\", \"wmv\", \"flv\", \"webm\"],\n", + " \"documents\": [\"pdf\", \"docx\", \"pptx\", \"xls\", \"doc\", \"ppt\"],\n", + " \"audios\": [\"mp3\", \"wav\", \"ogg\", \"flac\", \"aac\", \"wma\", \"m4a\"],\n", + " \"archives\": [\"zip\", \"rar\", \"7z\", \"tar\", \"gz\", \"pkg\", \"deb\", \"rpm\"],\n", + " \"executables\": [\"exe\", \"msi\", \"apk\", \"app\", \"bat\", \"sh\"],\n", + " \"code\": [\"py\", \"js\", \"html\", \"css\", \"cpp\", \"c\", \"java\", \"go\", \"php\", \"rb\", \"json\", \"xml\", \"sql\"], \n", + "}\n", + "\n", + "imdir = \"../data/conversion/DJI_202303031031_001\"\n", + "organizer = Organizer(rules=RULES)\n", + "organizer.organize(imdir)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "imgpreproc", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/image_renaming.ipynb b/notebooks/image_renaming.ipynb index 13d9479..3ab5027 100755 --- a/notebooks/image_renaming.ipynb +++ b/notebooks/image_renaming.ipynb @@ -18,8 +18,7 @@ "\n", "import pandas as pd\n", "\n", - "from impreproc.images import Image, ImageList\n", - "from impreproc.renaming import ImageRenamer\n", + "from impreproc.files import Organizer\n", "\n", "# Define parameters\n", "# Note that in Jupyter Notebooks, the path are relative to the notebooks folder!\n", diff --git a/notebooks/image_renaming_sandbox.py b/sandbox/image_renaming_sandbox.py similarity index 100% rename from notebooks/image_renaming_sandbox.py rename to sandbox/image_renaming_sandbox.py diff --git a/notebooks/potree_preview_sandbox.ipynb b/sandbox/potree_preview_sandbox.ipynb similarity index 100% rename from notebooks/potree_preview_sandbox.ipynb rename to sandbox/potree_preview_sandbox.ipynb diff --git a/notebooks/potree_preview_sandbox.py b/sandbox/potree_preview_sandbox.py similarity index 100% rename from notebooks/potree_preview_sandbox.py rename to sandbox/potree_preview_sandbox.py diff --git a/notebooks/utm_projection_sandbox.py b/sandbox/utm_projection_sandbox.py similarity index 100% rename from notebooks/utm_projection_sandbox.py rename to sandbox/utm_projection_sandbox.py diff --git a/src/impreproc/files.py b/src/impreproc/files.py index 263229f..46c16ad 100644 --- a/src/impreproc/files.py +++ b/src/impreproc/files.py @@ -61,34 +61,57 @@ def organize_files( class Organizer: def __init__( - self, rules: dict = RULES, recursive: bool = False, keep_dir_tree: bool = False + self, + rules: dict = RULES, + inplace: bool = True, + recursive: bool = False, ) -> None: assert isinstance(rules, dict), "Rules must be a dictionary." + assert isinstance(inplace, bool), "inplace must be a boolean." assert isinstance(recursive, bool), "Recursive must be a boolean." - assert isinstance(keep_dir_tree, bool), "Keep_dir_tree must be a boolean." self.rules = rules + self.inplace = inplace self.recursive = recursive - self.keep_dir_tree = keep_dir_tree def organize( self, + directory: Union[str, Path], ) -> bool: - pass - + if not os.path.isdir(directory): + raise ValueError("Directory must be a valid directory.") -if __name__ == "__main__": - # path = input(r"PATH: ") - # organize_files(path) + if self.recursive: + subfolders = sorted( + [Path(f.path) for f in os.scandir(directory) if f.is_dir()] + ) + for dir in subfolders: + organize_files(dir, self.rules, self.inplace) + else: + organize_files(directory, self.rules, self.inplace) - # imdir = "data/conversion/DJI_202303031031_001" + return True - # organize_files(imdir, inplace=False) - # Batch on multiple directories - # root_dir = "/mnt/labmgf/Belvedere/2023/00_img" - root_dir = "data/conversion" - subfolders = sorted([Path(f.path) for f in os.scandir(root_dir) if f.is_dir()]) - - for dir in subfolders: - organize_files(dir) +if __name__ == "__main__": + # Single directory - move files + imdir = "data/conversion/DJI_202303031031_001" + recursive = False + inplace = True + organizer = Organizer(recursive=recursive, inplace=inplace) + organizer.organize(imdir) + + # Single directory - copy files + imdir = "data/conversion/DJI_202303031031_001" + recursive = False + inplace = False + organizer = Organizer(recursive=recursive, inplace=inplace) + organizer.organize(imdir) + + # Batch on multiple directories - move files + imdir = "data/conversion" + recursive = True + organizer = Organizer(recursive=recursive) + organizer.organize(imdir) + + print("Done.")