From 9bf18ffe68b5f1db895a6ed1c828fcbbc0697c8f Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Mon, 8 Sep 2025 16:18:07 +0300 Subject: [PATCH] getting file content --- .gitignore | 2 ++ .../get_files_info.cpython-312.pyc | Bin 1939 -> 0 bytes functions/config.py | 1 + functions/get_file_content.py | 30 ++++++++++++++++++ tests.py | 28 +++++++--------- 5 files changed, 45 insertions(+), 16 deletions(-) delete mode 100644 functions/__pycache__/get_files_info.cpython-312.pyc create mode 100644 functions/config.py create mode 100644 functions/get_file_content.py diff --git a/.gitignore b/.gitignore index 4c49bd7..fcfcb28 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .env +__pycache__ +functions/__pycache__ \ No newline at end of file diff --git a/functions/__pycache__/get_files_info.cpython-312.pyc b/functions/__pycache__/get_files_info.cpython-312.pyc deleted file mode 100644 index a8099dec3d9ff36bf7d09b4158a79fbaa90a4de9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1939 zcmbVMO-~y~7@pY=uh$sX27@8N$>K&0jzSzzL>d~>rip`E5iY2eLR2B+U0@yKwRYBZ zY^|HBN-K~e5l%LW5Y8!7x$p}j+Jq=lFB>^g(3S&Id+E&zRI13OGsXs&pdxjoefOPr zp67W#_T$(3dI!Mk=IbYuKQI9NPBzs=Rh&%^c5*-h5{!WnAkh+a7mmO*3^0EZ$WWw; zB)d9%(fHcz#5Mh8Llb`_~0Et?lhY#zNOlIFQm>MOzti%wd36N#=N)G@;o%?8byT`l=zP<}U z)^XWjVkhLjA*JVjesoE_iJ|RkqCP#EtOK0pAC4a`_Cp;n-t~sZ= z#Ei2~99LrUC{i+Ve?}0;(z=XJWArH2>CgE3#IlGOjznaHh(3XfN_<=uJGWtA8#*#v zB~PHvqEuutiLgFtF;P{CTU>YymA4M0hcz9g6nw-g2#Z@BX|3!`$he`5Y}vwzgdCSF z8tIzFhi*sYgs!M@tn03HN2=QC&f;s_@nkGETF%pq6OO(Q7DyOZfHUpjI88>$n2sz~ zj_X?5a+Gg7XSSX0Ev8%)lo5I3#l5xtad1+dl7pHQ)|4a)j;X5NCCT4(g_W-G1QrU8 zC*u)PISP`5WMXKPjOzG-o^I$_JX>cEVlJee^H{i`XN|7@y+`R znPOYdecQUXEti^0ZoTQHR8=`9UtmtS3RwvZg%xFOCsGYiS*LMzrbH})6Xg9nv;HRgT!~T z*aqM>JQJWa8%|JuxIckS>;kPFn{E5WDOF0wcQw1>tXi-(rqIpC;nS EKMb9??f?J) diff --git a/functions/config.py b/functions/config.py new file mode 100644 index 0000000..f830741 --- /dev/null +++ b/functions/config.py @@ -0,0 +1 @@ +MAX_FILE_CONTENT_CHARS = 10000 \ No newline at end of file diff --git a/functions/get_file_content.py b/functions/get_file_content.py new file mode 100644 index 0000000..1fc90c0 --- /dev/null +++ b/functions/get_file_content.py @@ -0,0 +1,30 @@ +import os +from functions.config import MAX_FILE_CONTENT_CHARS + + +def get_file_content(working_directory, file_path): + try: + full_path = os.path.join(working_directory, file_path) + + abs_working_directory = os.path.abspath(working_directory) + abs_target_path = os.path.abspath(full_path) + + if not abs_target_path.startswith(abs_working_directory): + return ( + f'Error: Cannot read "{file_path}" as it is outside the permitted working directory' + ) + + if not os.path.isfile(abs_target_path): + return f'Error: File not found or is not a regular file: "{file_path}"' + + with open(abs_target_path, "r", encoding="utf-8", errors="replace") as f: + content = f.read() + + if len(content) > MAX_FILE_CONTENT_CHARS: + truncated = content[:MAX_FILE_CONTENT_CHARS] + return truncated + f'\n[...File "{file_path}" truncated at {MAX_FILE_CONTENT_CHARS} characters]' + else: + return content + + except Exception as e: + return f"Error: {e}" \ No newline at end of file diff --git a/tests.py b/tests.py index c29a555..92ab937 100644 --- a/tests.py +++ b/tests.py @@ -1,29 +1,25 @@ -from functions.get_files_info import get_files_info +from functions.get_file_content import get_file_content def run_tests(): cases = [ - ("calculator", "."), - ("calculator", "pkg"), - ("calculator", "/bin"), - ("calculator", "../"), + ("calculator", "main.py"), + ("calculator", "pkg/calculator.py"), + ("calculator", "/bin/cat"), + ("calculator", "pkg/does_not_exist.py"), ] - for working_dir, directory in cases: - print(f'get_files_info("{working_dir}", "{directory}"):') + for working_dir, file_path in cases: + print(f'get_file_content("{working_dir}", "{file_path}"):') - result = get_files_info(working_dir, directory) + result = get_file_content(working_dir, file_path) - if directory == ".": - print("Result for current directory:") - else: - print(f"Result for '{directory}' directory:") - - # Indent each line of result + print("Result:") + # Indent each line of output for line in result.splitlines(): - print(f" {line}") + print(" " + line) - print() # Extra newline between test cases + print() # Blank line between cases if __name__ == "__main__":