From a89f8e0820e84b4e7b060eea7d69e92085be06a5 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 09:34:08 -0800 Subject: [PATCH 01/21] write to docstring script --- src/fml_doc_gen/docstring_output.txt | 1 + src/fml_doc_gen/ip.ipynb | 68 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/fml_doc_gen/docstring_output.txt create mode 100644 src/fml_doc_gen/ip.ipynb diff --git a/src/fml_doc_gen/docstring_output.txt b/src/fml_doc_gen/docstring_output.txt new file mode 100644 index 0000000..df2ccde --- /dev/null +++ b/src/fml_doc_gen/docstring_output.txt @@ -0,0 +1 @@ +This is a test! \ No newline at end of file diff --git a/src/fml_doc_gen/ip.ipynb b/src/fml_doc_gen/ip.ipynb new file mode 100644 index 0000000..3b55062 --- /dev/null +++ b/src/fml_doc_gen/ip.ipynb @@ -0,0 +1,68 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated doc string file\n" + ] + } + ], + "source": [ + "docstring = \"\"\"This is a test!\"\"\"\n", + "output_path = \"../fml_doc_gen/docstring_output.txt\"\n", + "\n", + "#check if output_path is a file or dir, check if it has right extension\n", + "if os.path.isfile(output_path) and os.access(output_path, os.R_OK):\n", + " if isinstance(docstring, str):\n", + " file = open(output_path, \"w\")\n", + " file.write(docstring)\n", + " file.close()\n", + " print(\"Updated doc string file\")\n", + "\n", + " else:\n", + " file = open(output_path, \"a\")\n", + " file.write(docstring)\n", + " file.close()\n", + " print(\"Generated the doc string\")\n", + "else:\n", + " print(docstring)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "test_env", + "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.11.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 5e94d9bf1a9da2a57cab706cdaf138253ab8206b Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:11:13 -0800 Subject: [PATCH 02/21] write to docstring script --- src/fml_doc_gen/docstring_output.txt | 2 +- src/fml_doc_gen/write_docstring_to_file.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/fml_doc_gen/docstring_output.txt b/src/fml_doc_gen/docstring_output.txt index df2ccde..75329d1 100644 --- a/src/fml_doc_gen/docstring_output.txt +++ b/src/fml_doc_gen/docstring_output.txt @@ -1 +1 @@ -This is a test! \ No newline at end of file +This is a test! tatatatatat ghg bb \ No newline at end of file diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index ba4de45..e690344 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -1,3 +1,4 @@ +from fml_doc_gen import generate_template def write_docstring_to_file(docstring: str, output_file: str) -> None: """ @@ -31,4 +32,11 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: # This writes the docstring to 'docstring_output.txt' """ - pass + print("Generated Docstring:\n") + print(docstring) + print("\n*** End of Docstring ***\n") + + + if isinstance(docstring, str) & len(output_file) > 0: + doc_string = open(f"./{output_file}/docstring_output.txt") + return print(doc_string) From 36fbe6153e7c0f147a746e605185e803d8a7b068 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:13:57 -0800 Subject: [PATCH 03/21] writing docstring --- src/fml_doc_gen/write_docstring_to_file.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index e690344..d57706e 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -32,11 +32,14 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: # This writes the docstring to 'docstring_output.txt' """ + if not docstring.strip(): + raise ValueError("Could not generate docstring, please check your function is well defined") + print("Generated Docstring:\n") print(docstring) print("\n*** End of Docstring ***\n") - + if isinstance(docstring, str) & len(output_file) > 0: doc_string = open(f"./{output_file}/docstring_output.txt") return print(doc_string) From af4a3b9da393e8e7f23ee9d3bcfb75b7abe6b21a Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:15:34 -0800 Subject: [PATCH 04/21] writing docstring --- src/fml_doc_gen/write_docstring_to_file.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index d57706e..e00c79b 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -34,10 +34,10 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: if not docstring.strip(): raise ValueError("Could not generate docstring, please check your function is well defined") - - print("Generated Docstring:\n") - print(docstring) - print("\n*** End of Docstring ***\n") + else: + print("Generated Docstring:\n") + print(docstring) + print("\n*** End of Docstring ***\n") if isinstance(docstring, str) & len(output_file) > 0: From cfb31b59f5d03ff669c78992bf1e875a3d44bf4d Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:18:57 -0800 Subject: [PATCH 05/21] writing docstring --- src/fml_doc_gen/write_docstring_to_file.py | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index e00c79b..4f80a67 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -34,12 +34,21 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: if not docstring.strip(): raise ValueError("Could not generate docstring, please check your function is well defined") + + print("Generated Docstring:\n") + print(docstring) + print("\n*** End of Docstring ***\n") + + output_dir = os.path.dirname(output_file) or "." + if os.path.exists(output_dir): + if not os.access(output_dir, os.W_OK): + raise ValueError(f"This directory '{output_dir}' is not writable") + else: + try: + with open(output_dir, 'w') as file: + file.write(docstring) + print(f"Docstring successfully written to {output_dir}") + except Exception as e: + print(f"An error occurred while writing to the file: {e}") else: - print("Generated Docstring:\n") - print(docstring) - print("\n*** End of Docstring ***\n") - - - if isinstance(docstring, str) & len(output_file) > 0: - doc_string = open(f"./{output_file}/docstring_output.txt") - return print(doc_string) + raise ValueError(f"This directory '{output_dir}' does not exist.") From e2832c87aa8f1f70299db2d749b52c22184f1a99 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:21:29 -0800 Subject: [PATCH 06/21] completed the function --- src/fml_doc_gen/write_docstring_to_file.py | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 4f80a67..46a21e9 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -1,4 +1,4 @@ -from fml_doc_gen import generate_template +import os def write_docstring_to_file(docstring: str, output_file: str) -> None: """ @@ -41,14 +41,16 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: output_dir = os.path.dirname(output_file) or "." if os.path.exists(output_dir): - if not os.access(output_dir, os.W_OK): + raise ValueError(f"This directory '{output_dir}' does not exist.") + + if not os.access(output_dir, os.W_OK): raise ValueError(f"This directory '{output_dir}' is not writable") - else: - try: - with open(output_dir, 'w') as file: - file.write(docstring) - print(f"Docstring successfully written to {output_dir}") - except Exception as e: - print(f"An error occurred while writing to the file: {e}") - else: - raise ValueError(f"This directory '{output_dir}' does not exist.") + + try: + with open(output_dir, 'w') as file: + file.write(docstring) + print(f"Docstring successfully written to {output_dir}") + except Exception as e: + print(f"An error occurred while writing to the file: {e}") + + From df8347cd7e2f0d80f1914c98b197189f37144bf9 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:22:35 -0800 Subject: [PATCH 07/21] fixed indentation error --- src/fml_doc_gen/write_docstring_to_file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 46a21e9..141bd5b 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -41,10 +41,10 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: output_dir = os.path.dirname(output_file) or "." if os.path.exists(output_dir): - raise ValueError(f"This directory '{output_dir}' does not exist.") + raise ValueError(f"This directory '{output_dir}' does not exist.") if not os.access(output_dir, os.W_OK): - raise ValueError(f"This directory '{output_dir}' is not writable") + raise ValueError(f"This directory '{output_dir}' is not writable") try: with open(output_dir, 'w') as file: From f8a89ccba76c48ef437ac3ced7a1cd2942b3b894 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:27:26 -0800 Subject: [PATCH 08/21] making write to file optional --- src/fml_doc_gen/write_docstring_to_file.py | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 141bd5b..2a037d7 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -39,18 +39,19 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: print(docstring) print("\n*** End of Docstring ***\n") - output_dir = os.path.dirname(output_file) or "." - if os.path.exists(output_dir): - raise ValueError(f"This directory '{output_dir}' does not exist.") - - if not os.access(output_dir, os.W_OK): - raise ValueError(f"This directory '{output_dir}' is not writable") + if output_file: + output_dir = os.path.dirname(output_file) or "." + if os.path.exists(output_dir): + raise ValueError(f"This directory '{output_dir}' does not exist.") - try: - with open(output_dir, 'w') as file: - file.write(docstring) - print(f"Docstring successfully written to {output_dir}") - except Exception as e: - print(f"An error occurred while writing to the file: {e}") + if not os.access(output_dir, os.W_OK): + raise ValueError(f"This directory '{output_dir}' is not writable") + + try: + with open(output_dir, 'w') as file: + file.write(docstring) + print(f"Docstring successfully written to {output_dir}") + except Exception as e: + print(f"An error occurred while writing to the file: {e}") From ea7cc8548bc515ada69f757f02c4670a2fca97fc Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:31:41 -0800 Subject: [PATCH 09/21] modified function --- src/fml_doc_gen/write_docstring_to_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 2a037d7..3e8ee2d 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -33,7 +33,7 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: """ if not docstring.strip(): - raise ValueError("Could not generate docstring, please check your function is well defined") + raise ValueError("Could not generate docstring, please ensure your function is well defined") print("Generated Docstring:\n") print(docstring) From 3a7e71fa0b04b6034f1bb5b2a4a47c081ce304d2 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 22:43:59 -0800 Subject: [PATCH 10/21] created test file for write_docstring_to_file.py --- tests/test_write_docstring_to_file.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/test_write_docstring_to_file.py diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py new file mode 100644 index 0000000..e69de29 From 29f0c5846e2c30aaee11a3bcaf8b930f2f974214 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 23:45:35 -0800 Subject: [PATCH 11/21] writing test functions from write_docstring_to_file --- tests/test_write_docstring_to_file.py | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index e69de29..c77a2b5 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -0,0 +1,73 @@ +import os +import pytest +from fml_doc_gen.write_docstring_to_file import write_docstring_to_file +from tempfile import NamedTemporaryFile, TemporaryDirectory + + +@pytest.fixture +def valid_docstring(): + """ + Writes the generated docstring to a specified output file. + + Parameters + ---------- + docstring : str + The docstring to be written to the file. + output_file : str + The path to the output file. + + Returns + ------- + None + This function does not return anything. + """ + +@pytest.fixture +def empty_docstring(): + """Fixture for an empty docstring.""" + return "" + +def test_print_only(valid_docstring, capsys): + """Test that the docstring prints to the screen.""" + write_docstring_to_file(valid_docstring) + captured = capsys.readouterr() + assert "Generated Docstring:" in captured.out + assert valid_docstring.strip() in captured.out + +def test_write_to_valid_file(valid_docstring): + """Test writing the docstring to a valid file.""" + with NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + + try: + write_docstring_to_file(valid_docstring, output_file=file_path) + with open(file_path, "r") as file: + content = file.read() + assert content.strip() == valid_docstring.strip() + finally: + if os.path.exists(file_path): + os.remove(file_path) + +def test_empty_docstring(empty_docstring): + """Test that an empty or whitespace-only docstring raises a ValueError.""" + with pytest.raises(ValueError, match="The docstring is empty or contains only whitespace."): + write_docstring_to_file(empty_docstring) + +def test_invalid_directory(valid_docstring): + """Test writing to a file in a non-existent directory.""" + with TemporaryDirectory() as temp_dir: + non_existent_dir = os.path.join(temp_dir, "nonexistent") + file_path = os.path.join(non_existent_dir, "docstring_output.txt") + with pytest.raises(ValueError, match=f"The directory '{non_existent_dir}' does not exist."): + write_docstring_to_file(valid_docstring, output_file=file_path) + +def test_non_writable_directory(valid_docstring): + """Test writing to a file in a non-writable directory.""" + with TemporaryDirectory() as temp_dir: + os.chmod(temp_dir, 0o500) + file_path = os.path.join(temp_dir, "docstring_output.txt") + try: + with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."): + write_docstring_to_file(valid_docstring, output_file=file_path) + finally: + os.chmod(temp_dir, 0o700) \ No newline at end of file From ac37c6dc59607fe3f789fbf78da6beaeac562565 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Thu, 16 Jan 2025 23:56:26 -0800 Subject: [PATCH 12/21] updated test functions --- tests/test_write_docstring_to_file.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index c77a2b5..157726d 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -1,7 +1,8 @@ import os +from tempfile import NamedTemporaryFile, TemporaryDirectory import pytest from fml_doc_gen.write_docstring_to_file import write_docstring_to_file -from tempfile import NamedTemporaryFile, TemporaryDirectory + @pytest.fixture From c3a38b4a58e148e0cd3d23e7052fc97b216309ba Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 00:16:08 -0800 Subject: [PATCH 13/21] modified write_docstring_function --- src/fml_doc_gen/write_docstring_to_file.py | 2 +- tests/test_write_docstring_to_file.py | 29 +++++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 3e8ee2d..2377af7 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -32,7 +32,7 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: # This writes the docstring to 'docstring_output.txt' """ - if not docstring.strip(): + if not isinstance(docstring, str) or not docstring.strip(): raise ValueError("Could not generate docstring, please ensure your function is well defined") print("Generated Docstring:\n") diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index 157726d..17ec53d 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -26,7 +26,12 @@ def valid_docstring(): @pytest.fixture def empty_docstring(): """Fixture for an empty docstring.""" - return "" + return " " + +@pytest.fixture +def empty_docstring(): + """Fixture for an empty docstring.""" + return " " def test_print_only(valid_docstring, capsys): """Test that the docstring prints to the screen.""" @@ -51,7 +56,7 @@ def test_write_to_valid_file(valid_docstring): def test_empty_docstring(empty_docstring): """Test that an empty or whitespace-only docstring raises a ValueError.""" - with pytest.raises(ValueError, match="The docstring is empty or contains only whitespace."): + with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace."): write_docstring_to_file(empty_docstring) def test_invalid_directory(valid_docstring): @@ -62,13 +67,13 @@ def test_invalid_directory(valid_docstring): with pytest.raises(ValueError, match=f"The directory '{non_existent_dir}' does not exist."): write_docstring_to_file(valid_docstring, output_file=file_path) -def test_non_writable_directory(valid_docstring): - """Test writing to a file in a non-writable directory.""" - with TemporaryDirectory() as temp_dir: - os.chmod(temp_dir, 0o500) - file_path = os.path.join(temp_dir, "docstring_output.txt") - try: - with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."): - write_docstring_to_file(valid_docstring, output_file=file_path) - finally: - os.chmod(temp_dir, 0o700) \ No newline at end of file +# def test_non_writable_directory(valid_docstring): +# """Test writing to a file in a non-writable directory.""" +# with TemporaryDirectory() as temp_dir: +# os.chmod(temp_dir, 0o500) # Make the directory non-writable +# file_path = os.path.join(temp_dir, "docstring_output.txt") +# try: +# with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."): +# write_docstring_to_file(valid_docstring, output_file=file_path) +# finally: +# os.chmod(temp_dir, 0o700) # Restore permissions for cleanup \ No newline at end of file From 7718bc9806c49c671f58ce0b51be33a8602e3987 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 00:31:59 -0800 Subject: [PATCH 14/21] modified write_docstring_function --- tests/test_write_docstring_to_file.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index 17ec53d..3412a16 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -26,38 +26,39 @@ def valid_docstring(): @pytest.fixture def empty_docstring(): """Fixture for an empty docstring.""" - return " " + return " " @pytest.fixture -def empty_docstring(): - """Fixture for an empty docstring.""" - return " " +def output_file(): + """Generate test output_file""" + with NamedTemporaryFile(delete=False) as temp_file: + file_path = temp_file.name + return file_path + def test_print_only(valid_docstring, capsys): """Test that the docstring prints to the screen.""" - write_docstring_to_file(valid_docstring) + write_docstring_to_file(valid_docstring, output_file) captured = capsys.readouterr() assert "Generated Docstring:" in captured.out assert valid_docstring.strip() in captured.out def test_write_to_valid_file(valid_docstring): """Test writing the docstring to a valid file.""" - with NamedTemporaryFile(delete=False) as temp_file: - file_path = temp_file.name try: - write_docstring_to_file(valid_docstring, output_file=file_path) - with open(file_path, "r") as file: + write_docstring_to_file(valid_docstring, output_file=output_file) + with open(output_file, "r") as file: content = file.read() assert content.strip() == valid_docstring.strip() finally: - if os.path.exists(file_path): - os.remove(file_path) + if os.path.exists(output_file): + os.remove(output_file) def test_empty_docstring(empty_docstring): """Test that an empty or whitespace-only docstring raises a ValueError.""" with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace."): - write_docstring_to_file(empty_docstring) + write_docstring_to_file(empty_docstring,output_file) def test_invalid_directory(valid_docstring): """Test writing to a file in a non-existent directory.""" From 05ee07c91a4925c7d1e164537081648bb42bcee0 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 01:54:54 -0800 Subject: [PATCH 15/21] fixing test issues --- src/fml_doc_gen/write_docstring_to_file.py | 8 +-- tests/test_write_docstring_to_file.py | 71 +++++++++++----------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 2377af7..a0b5078 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -1,6 +1,6 @@ import os -def write_docstring_to_file(docstring: str, output_file: str) -> None: +def write_docstring_to_file(docstring: str, output_file: str = None) -> None: """ Writes the generated docstring to a specified output file. @@ -41,16 +41,16 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None: if output_file: output_dir = os.path.dirname(output_file) or "." - if os.path.exists(output_dir): + if not os.path.exists(output_dir): raise ValueError(f"This directory '{output_dir}' does not exist.") if not os.access(output_dir, os.W_OK): raise ValueError(f"This directory '{output_dir}' is not writable") try: - with open(output_dir, 'w') as file: + with open(output_file, 'w') as file: file.write(docstring) - print(f"Docstring successfully written to {output_dir}") + print(f"Docstring successfully written to {output_file}") except Exception as e: print(f"An error occurred while writing to the file: {e}") diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index 3412a16..a52c61a 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -4,23 +4,21 @@ from fml_doc_gen.write_docstring_to_file import write_docstring_to_file - @pytest.fixture def valid_docstring(): - """ - Writes the generated docstring to a specified output file. - + """Fixture for a valid docstring.""" + return """ Parameters ---------- - docstring : str - The docstring to be written to the file. - output_file : str - The path to the output file. + a : int + Description of parameter a. + b : int + Description of parameter b. Returns ------- - None - This function does not return anything. + int + Description of the return value. """ @pytest.fixture @@ -28,53 +26,56 @@ def empty_docstring(): """Fixture for an empty docstring.""" return " " -@pytest.fixture -def output_file(): - """Generate test output_file""" - with NamedTemporaryFile(delete=False) as temp_file: - file_path = temp_file.name - return file_path - - def test_print_only(valid_docstring, capsys): """Test that the docstring prints to the screen.""" - write_docstring_to_file(valid_docstring, output_file) + write_docstring_to_file(valid_docstring) captured = capsys.readouterr() assert "Generated Docstring:" in captured.out assert valid_docstring.strip() in captured.out def test_write_to_valid_file(valid_docstring): """Test writing the docstring to a valid file.""" + import tempfile + + # Create a temporary file using a context manager + with tempfile.NamedTemporaryFile(suffix=".txt",delete=False) as temp_file: + file_path = temp_file.name # Get the temporary file path try: - write_docstring_to_file(valid_docstring, output_file=output_file) - with open(output_file, "r") as file: + # Write the docstring to the temporary file + write_docstring_to_file(valid_docstring, output_file=file_path) + + # Read back the content from the file + with open(file_path, "r") as file: content = file.read() + + # Assert the content matches the expected docstring assert content.strip() == valid_docstring.strip() finally: - if os.path.exists(output_file): - os.remove(output_file) + # Cleanup: Ensure the temporary file is removed + if os.path.exists(file_path): + os.remove(file_path) def test_empty_docstring(empty_docstring): """Test that an empty or whitespace-only docstring raises a ValueError.""" with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace."): - write_docstring_to_file(empty_docstring,output_file) + write_docstring_to_file(empty_docstring) def test_invalid_directory(valid_docstring): """Test writing to a file in a non-existent directory.""" with TemporaryDirectory() as temp_dir: non_existent_dir = os.path.join(temp_dir, "nonexistent") file_path = os.path.join(non_existent_dir, "docstring_output.txt") - with pytest.raises(ValueError, match=f"The directory '{non_existent_dir}' does not exist."): + with pytest.raises(ValueError, match=f"This directory '{non_existent_dir}' does not exist"): write_docstring_to_file(valid_docstring, output_file=file_path) -# def test_non_writable_directory(valid_docstring): -# """Test writing to a file in a non-writable directory.""" -# with TemporaryDirectory() as temp_dir: -# os.chmod(temp_dir, 0o500) # Make the directory non-writable -# file_path = os.path.join(temp_dir, "docstring_output.txt") -# try: -# with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."): -# write_docstring_to_file(valid_docstring, output_file=file_path) -# finally: -# os.chmod(temp_dir, 0o700) # Restore permissions for cleanup \ No newline at end of file +def test_non_writable_directory(valid_docstring): + """Test writing to a file in a non-writable directory.""" + with TemporaryDirectory() as temp_dir: + os.chmod(temp_dir, 0o500) # Make the directory non-writable + file_path = os.path.join(temp_dir, "docstring_output.txt") + try: + with pytest.raises(ValueError, match=f"This directory '{temp_dir}' is not writable"): + write_docstring_to_file(valid_docstring, output_file=file_path) + finally: + os.chmod(temp_dir, 0o700) # Restore permissions for cleanup \ No newline at end of file From 23b3cbf421cdcaf085ebb44c5762252f338e2280 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 02:07:50 -0800 Subject: [PATCH 16/21] modified test functions --- src/fml_doc_gen/write_docstring_to_file.py | 2 +- tests/test_write_docstring_to_file.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index a0b5078..4afdb6f 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -33,7 +33,7 @@ def write_docstring_to_file(docstring: str, output_file: str = None) -> None: """ if not isinstance(docstring, str) or not docstring.strip(): - raise ValueError("Could not generate docstring, please ensure your function is well defined") + raise ValueError("The docstring is empty, None, or contains only whitespace") print("Generated Docstring:\n") print(docstring) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index a52c61a..8e0dabe 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -35,14 +35,11 @@ def test_print_only(valid_docstring, capsys): def test_write_to_valid_file(valid_docstring): """Test writing the docstring to a valid file.""" - import tempfile - # Create a temporary file using a context manager - with tempfile.NamedTemporaryFile(suffix=".txt",delete=False) as temp_file: + with NamedTemporaryFile(suffix=".txt",delete=False) as temp_file: file_path = temp_file.name # Get the temporary file path try: - # Write the docstring to the temporary file write_docstring_to_file(valid_docstring, output_file=file_path) # Read back the content from the file @@ -58,7 +55,7 @@ def test_write_to_valid_file(valid_docstring): def test_empty_docstring(empty_docstring): """Test that an empty or whitespace-only docstring raises a ValueError.""" - with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace."): + with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace"): write_docstring_to_file(empty_docstring) def test_invalid_directory(valid_docstring): From 4af2e394b097cdef6cc2ad70a1831abcd0c759fe Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 02:13:09 -0800 Subject: [PATCH 17/21] removed temp notebook --- src/fml_doc_gen/ip.ipynb | 68 --------------------------- tests/test_write_docstring_to_file.py | 28 +++++------ 2 files changed, 14 insertions(+), 82 deletions(-) delete mode 100644 src/fml_doc_gen/ip.ipynb diff --git a/src/fml_doc_gen/ip.ipynb b/src/fml_doc_gen/ip.ipynb deleted file mode 100644 index 3b55062..0000000 --- a/src/fml_doc_gen/ip.ipynb +++ /dev/null @@ -1,68 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "import os" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Updated doc string file\n" - ] - } - ], - "source": [ - "docstring = \"\"\"This is a test!\"\"\"\n", - "output_path = \"../fml_doc_gen/docstring_output.txt\"\n", - "\n", - "#check if output_path is a file or dir, check if it has right extension\n", - "if os.path.isfile(output_path) and os.access(output_path, os.R_OK):\n", - " if isinstance(docstring, str):\n", - " file = open(output_path, \"w\")\n", - " file.write(docstring)\n", - " file.close()\n", - " print(\"Updated doc string file\")\n", - "\n", - " else:\n", - " file = open(output_path, \"a\")\n", - " file.write(docstring)\n", - " file.close()\n", - " print(\"Generated the doc string\")\n", - "else:\n", - " print(docstring)\n" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "test_env", - "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.11.10" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index 8e0dabe..e7028a7 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -8,17 +8,19 @@ def valid_docstring(): """Fixture for a valid docstring.""" return """ + Writes the generated docstring to a specified output file. + Parameters ---------- - a : int - Description of parameter a. - b : int - Description of parameter b. + docstring : str + The docstring to be written to the file. + output_file : str + The path to the output file. Returns ------- - int - Description of the return value. + None + This function does not return anything. """ @pytest.fixture @@ -27,29 +29,26 @@ def empty_docstring(): return " " def test_print_only(valid_docstring, capsys): - """Test that the docstring prints to the screen.""" + """Test that the docstring prints to the screen""" write_docstring_to_file(valid_docstring) captured = capsys.readouterr() assert "Generated Docstring:" in captured.out assert valid_docstring.strip() in captured.out def test_write_to_valid_file(valid_docstring): - """Test writing the docstring to a valid file.""" + """Test writing the docstring to a valid file""" with NamedTemporaryFile(suffix=".txt",delete=False) as temp_file: - file_path = temp_file.name # Get the temporary file path + file_path = temp_file.name try: write_docstring_to_file(valid_docstring, output_file=file_path) - # Read back the content from the file with open(file_path, "r") as file: content = file.read() - # Assert the content matches the expected docstring assert content.strip() == valid_docstring.strip() finally: - # Cleanup: Ensure the temporary file is removed if os.path.exists(file_path): os.remove(file_path) @@ -69,10 +68,11 @@ def test_invalid_directory(valid_docstring): def test_non_writable_directory(valid_docstring): """Test writing to a file in a non-writable directory.""" with TemporaryDirectory() as temp_dir: - os.chmod(temp_dir, 0o500) # Make the directory non-writable + # Make the temp_dir directory non-writable + os.chmod(temp_dir, 0o500) file_path = os.path.join(temp_dir, "docstring_output.txt") try: with pytest.raises(ValueError, match=f"This directory '{temp_dir}' is not writable"): write_docstring_to_file(valid_docstring, output_file=file_path) finally: - os.chmod(temp_dir, 0o700) # Restore permissions for cleanup \ No newline at end of file + os.chmod(temp_dir, 0o700) \ No newline at end of file From 28b02a58706a56d9eddf175a4bb271c91591fb41 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 02:17:49 -0800 Subject: [PATCH 18/21] modified test for write docstring --- tests/test_write_docstring_to_file.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index e7028a7..3075192 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -37,7 +37,6 @@ def test_print_only(valid_docstring, capsys): def test_write_to_valid_file(valid_docstring): """Test writing the docstring to a valid file""" - with NamedTemporaryFile(suffix=".txt",delete=False) as temp_file: file_path = temp_file.name @@ -68,7 +67,7 @@ def test_invalid_directory(valid_docstring): def test_non_writable_directory(valid_docstring): """Test writing to a file in a non-writable directory.""" with TemporaryDirectory() as temp_dir: - # Make the temp_dir directory non-writable + # Make the temp_dir directory non writable os.chmod(temp_dir, 0o500) file_path = os.path.join(temp_dir, "docstring_output.txt") try: From dc2cd19908251bafb102ea5df04c6e27a9af4974 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 02:22:55 -0800 Subject: [PATCH 19/21] deleted docsrc/fml_doc_gen/docstring_output.txt --- src/fml_doc_gen/docstring_output.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/fml_doc_gen/docstring_output.txt diff --git a/src/fml_doc_gen/docstring_output.txt b/src/fml_doc_gen/docstring_output.txt deleted file mode 100644 index 75329d1..0000000 --- a/src/fml_doc_gen/docstring_output.txt +++ /dev/null @@ -1 +0,0 @@ -This is a test! tatatatatat ghg bb \ No newline at end of file From 0db5dbfbac4d52b3bafc4eefed05aac38b83e5e9 Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 11:02:07 -0800 Subject: [PATCH 20/21] made changes based on mike's review --- src/fml_doc_gen/write_docstring_to_file.py | 8 ++------ tests/test_write_docstring_to_file.py | 16 +--------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/fml_doc_gen/write_docstring_to_file.py b/src/fml_doc_gen/write_docstring_to_file.py index 4afdb6f..76d2eb2 100644 --- a/src/fml_doc_gen/write_docstring_to_file.py +++ b/src/fml_doc_gen/write_docstring_to_file.py @@ -32,10 +32,7 @@ def write_docstring_to_file(docstring: str, output_file: str = None) -> None: # This writes the docstring to 'docstring_output.txt' """ - if not isinstance(docstring, str) or not docstring.strip(): - raise ValueError("The docstring is empty, None, or contains only whitespace") - - print("Generated Docstring:\n") + print(docstring) print("\n*** End of Docstring ***\n") @@ -50,8 +47,7 @@ def write_docstring_to_file(docstring: str, output_file: str = None) -> None: try: with open(output_file, 'w') as file: file.write(docstring) - print(f"Docstring successfully written to {output_file}") - except Exception as e: + except ValueError as e: print(f"An error occurred while writing to the file: {e}") diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index 3075192..df7d708 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -23,17 +23,6 @@ def valid_docstring(): This function does not return anything. """ -@pytest.fixture -def empty_docstring(): - """Fixture for an empty docstring.""" - return " " - -def test_print_only(valid_docstring, capsys): - """Test that the docstring prints to the screen""" - write_docstring_to_file(valid_docstring) - captured = capsys.readouterr() - assert "Generated Docstring:" in captured.out - assert valid_docstring.strip() in captured.out def test_write_to_valid_file(valid_docstring): """Test writing the docstring to a valid file""" @@ -51,10 +40,7 @@ def test_write_to_valid_file(valid_docstring): if os.path.exists(file_path): os.remove(file_path) -def test_empty_docstring(empty_docstring): - """Test that an empty or whitespace-only docstring raises a ValueError.""" - with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace"): - write_docstring_to_file(empty_docstring) + def test_invalid_directory(valid_docstring): """Test writing to a file in a non-existent directory.""" From 0646970884b9cf34282281812aab4878545de78f Mon Sep 17 00:00:00 2001 From: Lukman Lateef Date: Fri, 17 Jan 2025 11:03:15 -0800 Subject: [PATCH 21/21] made changes based on mike's review --- tests/test_write_docstring_to_file.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_write_docstring_to_file.py b/tests/test_write_docstring_to_file.py index df7d708..78e320e 100644 --- a/tests/test_write_docstring_to_file.py +++ b/tests/test_write_docstring_to_file.py @@ -41,7 +41,6 @@ def test_write_to_valid_file(valid_docstring): os.remove(file_path) - def test_invalid_directory(valid_docstring): """Test writing to a file in a non-existent directory.""" with TemporaryDirectory() as temp_dir: