From d2e778629f9f47b8a6f2510de47c918bbb4bbc81 Mon Sep 17 00:00:00 2001 From: Colin Bitterfield Date: Tue, 24 Jun 2025 23:55:59 -0700 Subject: [PATCH] Update projects.py Fixed slicing on generator issues --- routes/projects.py | 71 +++++++--------------------------------------- 1 file changed, 10 insertions(+), 61 deletions(-) diff --git a/routes/projects.py b/routes/projects.py index 0bf9c13..e2697f7 100644 --- a/routes/projects.py +++ b/routes/projects.py @@ -1,7 +1,7 @@ import json - import quart from quart import Blueprint +from itertools import islice from project import projects from utils import file_search @@ -11,15 +11,6 @@ @projects_routes.get("/projects//files") async def get_files(project_name): - """ - Get the list of files in a project. - - Args: - project_name (str): The name of the project. - - Returns: - quart.Response: A response object with the list of files. - """ print(f'Querying files for project "{project_name}"') try: project = projects[project_name] @@ -28,26 +19,19 @@ async def get_files(project_name): "error": f"Project {project_name} not found", }), status=404) - project.file_cache = file_search(project, '*') - print(project.file_cache[:20]) - print(len(project.file_cache)) + # Generate and cache the file list + files = list(file_search(project, '*')) + project.file_cache = files - return quart.Response(response=json.dumps(project.file_cache), status=200) + print(files[:20]) + print(len(files)) + + return quart.Response(response=json.dumps(files), status=200) @projects_routes.get("/projects//file") async def get_file(project): - """ - Get the contents of a file in a project. - - Args: - project (str): The name of the project. - - Returns: - quart.Response: A response object with the file contents. - """ filename = quart.request.args.get("filename") - print(f'Querying file "{filename}" for project "{project}"') if project not in projects.get_all(): @@ -63,8 +47,6 @@ async def get_file(project): }), status=404) contents = file.read_text().splitlines() - - # Add a line number to each line contents = [f"{i + 1}: {line}" for i, line in enumerate(contents)] return quart.Response(response=json.dumps({ @@ -77,27 +59,11 @@ async def get_file(project): @projects_routes.post("/projects//file") async def set_file_contents(project): - """ - Set the contents of a file in a project. - - Args: - project (str): The name of the project. - - Returns: - quart.Response: A response object indicating the success of the operation. - """ data = await quart.request.get_json(force=True) - - print(data) - filename = quart.request.args.get("filename") - print(f'Setting file "{filename}" for project "{project}"') file = projects[project].path / filename - - print(data) - contents = '\n'.join(data["contents"]) file.parent.mkdir(parents=True, exist_ok=True) @@ -107,35 +73,18 @@ async def set_file_contents(project): @projects_routes.put("/projects//file") async def edit_file(project): - """ - Edit specific lines in a file in a project. - - Args: - project (str): The name of the project. - - Returns: - quart.Response: A response object indicating the success of the operation. - """ data = await quart.request.get_json(force=True) - filename = quart.request.args.get("filename") - print(f'Editing file "{filename}" for project "{project}"') - file_path = projects[project].path / filename + file_path = projects[project].path / filename first_line = data["first_line"] last_line = data["last_line"] - content = data["content"] - - # append newlines to the content - content = [f"{line}\n" for line in content] - - print(f'Editing lines {first_line} to {last_line} with:\n\n"{content}"') + content = [f"{line}\n" for line in data["content"]] with file_path.open() as f: lines = f.readlines() - # Replace the specified lines with the new content lines[first_line - 1:last_line] = content with file_path.open('w') as f: