Skip to content

Commit

Permalink
updated file organizer and added notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
franioli committed Sep 14, 2023
1 parent 65fc1a5 commit 39529aa
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 19 deletions.
127 changes: 127 additions & 0 deletions notebooks/file_organization.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions notebooks/image_renaming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
57 changes: 40 additions & 17 deletions src/impreproc/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

0 comments on commit 39529aa

Please sign in to comment.