diff --git a/Makefile b/Makefile deleted file mode 100644 index 09d932c..0000000 --- a/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line, and also -# from the environment for the first two. -SPHINXOPTS ?= -SPHINXBUILD ?= sphinx-build -SOURCEDIR = . -BUILDDIR = docs/_build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/README.md b/README.md index 95685d0..fac15c9 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,3 @@ coverage run -m pytest coverage report coverage html ``` - -## Sphinx - -```sh -sphinx-quickstart -``` \ No newline at end of file diff --git a/conf.py b/conf.py deleted file mode 100644 index 7cb3281..0000000 --- a/conf.py +++ /dev/null @@ -1,31 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# For the full list of built-in configuration values, see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -import os -import sys -# Añadir el directorio donde están tus módulos Python al sys.path -sys.path.insert(0, os.path.abspath('./')) -# -- Project information ----------------------------------------------------- -# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information - -project = 'Python Modules' -copyright = '2025, Adrian Epifanio' -author = 'Adrian Epifanio' - -# -- General configuration --------------------------------------------------- -# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration - -extensions = ['sphinx.ext.autodoc'] - -templates_path = ['_templates'] -exclude_patterns = ['docs/_build', 'Thumbs.db', '.DS_Store'] - - - -# -- Options for HTML output ------------------------------------------------- -# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output - -html_theme = 'sphinx_rtd_theme' -html_static_path = ['docs/_static'] \ No newline at end of file diff --git a/index.rst b/index.rst deleted file mode 100644 index 5f90b10..0000000 --- a/index.rst +++ /dev/null @@ -1,29 +0,0 @@ -.. Python Modules documentation master file, created by - sphinx-quickstart on Fri Mar 28 19:49:52 2025. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Python Modules documentation -============================ - -.. toctree:: - :maxdepth: 2 - :caption: Contents: - - -Date -==== - -.. automodule:: modules.datetime.date - :members: - :undoc-members: - :show-inheritance: - - -ErrorHandler -============ - -.. automodule:: modules.errors.error_handler - :members: - :undoc-members: - :show-inheritance: diff --git a/make.bat b/make.bat deleted file mode 100644 index 65e2800..0000000 --- a/make.bat +++ /dev/null @@ -1,35 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=. -set BUILDDIR=docs/_build - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.https://www.sphinx-doc.org/ - exit /b 1 -) - -if "%1" == "" goto help - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% - -:end -popd diff --git a/modules/__init__.py b/modules/__init__.py index 16a34d3..e69de29 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -1,11 +0,0 @@ -# -*- coding: utf-8 -*- -# @Proyect: Personal -# @Author: Adrian Epifanio -# @File: __init__.py -# @Author: Adrian Epifanio -# @Date: 2025-03-28 17:17:52 -# @Email: adrianepi@gmail.com -# @GitHub: https://github.com/AdrianEpi -# @Last Modified by: Adrian Epifanio -# @Last Modified time: 2025-03-28 17:17:52 -# @Description: ... \ No newline at end of file diff --git a/modules/datetime/date.py b/modules/datetime/date.py index 6e6192b..9548cc6 100644 --- a/modules/datetime/date.py +++ b/modules/datetime/date.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# @Proyect: Python +# @Proyect: Python Modules # @Author: Adrian Epifanio # @File: date.py # @Author: Adrian Epifanio @@ -7,7 +7,7 @@ # @Email: adrianepi@gmail.com # @GitHub: https://github.com/AdrianEpi # @Last Modified by: Adrian Epifanio -# @Last Modified time: 2025-03-28 17:26:05 +# @Last Modified time: 2025-03-29 09:46:34 # @Description: This file describes a Date class diff --git a/modules/errors/error_handler.py b/modules/errors/error_handler.py index 6b37c65..70f58ff 100644 --- a/modules/errors/error_handler.py +++ b/modules/errors/error_handler.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# @Proyect: Personal +# @Proyect: Python Modules # @Author: Adrian Epifanio # @File: error_handler.py # @Author: Adrian Epifanio @@ -7,7 +7,7 @@ # @Email: adrianepi@gmail.com # @GitHub: https://github.com/AdrianEpi # @Last Modified by: Adrian Epifanio -# @Last Modified time: 2025-03-29 08:25:00 +# @Last Modified time: 2025-03-29 09:46:13 # @Description: This file describes an ErrorHandler diff --git a/modules/files/__init__.py b/modules/filesystem/__init__.py similarity index 100% rename from modules/files/__init__.py rename to modules/filesystem/__init__.py diff --git a/modules/filesystem/dir_manager.py b/modules/filesystem/dir_manager.py new file mode 100644 index 0000000..b9f001a --- /dev/null +++ b/modules/filesystem/dir_manager.py @@ -0,0 +1,178 @@ +# -*- coding: utf-8 -*- +# @Proyect: Python Modules +# @Author: Adrian Epifanio +# @File: dir_manager.py +# @Author: Adrian Epifanio +# @Date: 2025-03-29 09:37:27 +# @Email: adrianepi@gmail.com +# @GitHub: https://github.com/AdrianEpi +# @Last Modified by: Adrian Epifanio +# @Last Modified time: 2025-03-29 10:24:16 +# @Description: This file describes a directory manager. Contains all +# the necessary methods to work with directories in the OS. + +import os +import shutil + +class DirManager: + """ + This class describes a directory manager for the OS. + """ + + @staticmethod + def list_dirs(path: str) -> list: + """ + List all the directories in the given path + + :param path: The path + :type path: str + + :returns: List with the directories names + :rtype: list + """ + try: + return [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))] + except FileNotFoundError: + return f"Error: The directory '{path}' does not exist." + except PermissionError: + return f"Error: Permission denied for accessing '{path}'." + + + @staticmethod + def list_files(path: str, extension = None) -> list: + """ + List all the files in the given path with the given extension, it gets + all the files with all the extension if no extension is provided. + + :param path: The path + :type path: str + :param extension: The extension + :type extension: str + + :returns: List of files + :rtype: list + """ + try: + if extension: + return [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name)) and name.endswith(extension)] + else: + return [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name))] + except FileNotFoundError: + return f"Error: The directory '{path}' does not exist." + except PermissionError: + return f"Error: Permission denied for accessing '{path}'." + + + @staticmethod + def list_all_files_recursive(path: str, extension = None) -> list: + """ + List all files in the given dir and in all the subdirs of that directory + + :param path: The path + :type path: str + :param extension: The extension + :type extension: str + + :returns: The list of files + :rtype: list + """ + files = DirManager.list_files(path, extension) + dirs = DirManager.list_dirs(path) + for d in dirs: + files += DirManager.list_all_files_recursive(path=os.path.join(path, d), extension=extension) + return files + + + @staticmethod + def dir_exist(path: str) -> bool: + """ + Checks if a dir exists or not + + :param path: The file name + :type path: str + + :returns: True if the dir exists, false otherwise + :rtype: bool + """ + return os.path.isdir(path) + + + @staticmethod + def create_dir(path=''): + """ + Creates a dir. + + :param path: The path + :type path: str + """ + try: + os.makedirs(path) + except Exception as e: + return f"Error while creating dir '{path}': {e}" + + + @staticmethod + def create_dir_hierarchy(data: list, path: str) -> list: + """ + Creates a dir hierarchy recursively.. If a data element is a list, then + that full list of dirs will be created under the previous data element or + each of the previous data elements in case this was a list. Each of the + elements in data will be a father of all the next elements of type list in + data. + Example [A, B, C] + ∟ A + ∟ B + ∟ C + Example [[A, B], X, Y, Z]: + A + ∟ X + ∟ Y + ∟ Z + B + ∟ X + ∟ Y + ∟ Z + :param data: The the hierarchy list of directory names. + :type data: list + :param path: The path + :type path: str + + :returns: List with the created paths + :rtype: list + """ + path_list = [] + for i in range(0, len(data), 1): + if type(data[i]) == list: + path_l = DirManager.create_dir_hierarchy(data = data[i], path = path) + for j in path_l: + DirManager.create_dir_hierarchy(data = data[i + 1:], path = j) + break + elif DirManager.dir_exist(path): + new_path = path + '/' + data[i] + if(not DirManager.dir_exist(new_path)): + DirManager.create_dir(path = new_path) + path_list.append(new_path) + return path_list + + + @staticmethod + def delete_dir(path: str): + """ + Deletes a dir and all the content in it + + :param path: The path + :type path: str + """ + if DirManager.dir_exist(path): + shutil.rmtree(path) + + + @staticmethod + def get_current_dir() -> str: + """ + Gets the current dir. + + :returns: The current dir. + :rtype: str + """ + return os.getcwd() + '/' \ No newline at end of file diff --git a/modules/files/file_manager.py b/modules/filesystem/file_manager.py similarity index 93% rename from modules/files/file_manager.py rename to modules/filesystem/file_manager.py index b5fd0a4..5b3371c 100644 --- a/modules/files/file_manager.py +++ b/modules/filesystem/file_manager.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# @Proyect: Personal +# @Proyect: Python Modules # @Author: Adrian Epifanio # @File: file_manager.py # @Author: Adrian Epifanio @@ -7,9 +7,9 @@ # @Email: adrianepi@gmail.com # @GitHub: https://github.com/AdrianEpi # @Last Modified by: Adrian Epifanio -# @Last Modified time: 2025-03-29 09:12:34 +# @Last Modified time: 2025-03-29 09:45:52 # @Description: This file describes a file manager. Contains all -# the necessary methods to work with files and directories. +# the necessary methods to work with files. import os @@ -19,7 +19,7 @@ class FileManager: """ This class describes a file manager, it cotains all the static methods - needed to work with files, folders and directories. + needed to work with files. """ diff --git a/modules/files/requirements.txt b/modules/filesystem/requirements.txt similarity index 100% rename from modules/files/requirements.txt rename to modules/filesystem/requirements.txt diff --git a/requirements.txt b/requirements.txt index 4f3d834..6434122 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,6 @@ pytest>=8.3.5 # Testing coverage>=7.7.1 # Coverage -sphinx>=8.1.3 # Documentation -sphinx-rtd-theme>=3.0.2 # Github Pages Documentation -r modules/datetime/requirements.txt --r modules/files/requirements.txt +-r modules/filesystem/requirements.txt -r modules/errors/requirements.txt \ No newline at end of file diff --git a/tests/test_dir_manager.py b/tests/test_dir_manager.py new file mode 100644 index 0000000..eb0b894 --- /dev/null +++ b/tests/test_dir_manager.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +# @Proyect: Personal +# @Author: Adrian Epifanio +# @File: test_dir_manager.py +# @Author: Adrian Epifanio +# @Date: 2025-03-29 09:37:35 +# @Email: adrianepi@gmail.com +# @GitHub: https://github.com/AdrianEpi +# @Last Modified by: Adrian Epifanio +# @Last Modified time: 2025-03-29 10:27:16 +# @Description: Test modules/filesystem/dir_manager.py + + +from modules.filesystem.dir_manager import DirManager + +import pytest + +def test_list_dirs(): + assert('tmp' in DirManager.list_dirs(path='tests')) + assert(DirManager.list_dirs(path='NonExistingPath') == "Error: The directory 'NonExistingPath' does not exist.") + +def test_list_files(): + tmp_list = ['sample.txt'] + assert(tmp_list == DirManager.list_files(path='tests/tmp/')) + assert([] == DirManager.list_files(path='tests/tmp/', extension='.py')) + assert('Error: The directory \'tests/NonExistingPath/\' does not exist.' == DirManager.list_files(path='tests/NonExistingPath/')) + +def test_list_all_files_recursive(): + tmp_list = ['sample.txt'] + assert(tmp_list == DirManager.list_all_files_recursive(path='tests/tmp/')) + assert([] == DirManager.list_all_files_recursive(path='tests/tmp/', extension='.py')) + +def test_dir_exist(): + assert(DirManager.dir_exist(path='tests') == True) + +def test_create_dir(): + DirManager.create_dir(path='tests/tmp/new_dir') + assert(DirManager.dir_exist(path='tests/tmp/new_dir') == True) + assert('Error while creating dir' in DirManager.create_dir(path='tests/tmp//')) + +def test_get_current_dir(): + d = DirManager.get_current_dir() + assert(DirManager.dir_exist(d)) + +def test_create_dir_hierarchy(): + a = ['a', 'b', 'c'] + b = [['d', 'e'], 'x', 'y'] + DirManager.create_dir_hierarchy(data=a, path='tests/tmp/new_dir') + DirManager.create_dir_hierarchy(data=b, path='tests/tmp/new_dir') + for i in a: + assert(DirManager.dir_exist('tests/tmp/new_dir/' + i) == True) + for i in b[0]: + assert(DirManager.dir_exist('tests/tmp/new_dir/' + i + '/x') == True) + assert(DirManager.dir_exist('tests/tmp/new_dir/' + i + '/y') == True) + +def test_delete_dir(): + DirManager.delete_dir(path='tests/tmp/new_dir') + assert(DirManager.dir_exist(path='tests/tmp/new_dir') == False) \ No newline at end of file diff --git a/tests/test_file_manager.py b/tests/test_file_manager.py index f2f560f..d876d3e 100644 --- a/tests/test_file_manager.py +++ b/tests/test_file_manager.py @@ -7,11 +7,11 @@ # @Email: adrianepi@gmail.com # @GitHub: https://github.com/AdrianEpi # @Last Modified by: Adrian Epifanio -# @Last Modified time: 2025-03-29 09:12:42 -# @Description: Tests for modules/files/file_manager.py +# @Last Modified time: 2025-03-29 09:51:00 +# @Description: Tests for modules/filesystem/file_manager.py -from modules.files.file_manager import FileManager +from modules.filesystem.file_manager import FileManager import pytest