Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 10 additions & 61 deletions routes/projects.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,15 +11,6 @@

@projects_routes.get("/projects/<string:project_name>/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]
Expand All @@ -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/<string:project>/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():
Expand 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({
Expand All @@ -77,27 +59,11 @@ async def get_file(project):

@projects_routes.post("/projects/<string:project>/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)
Expand All @@ -107,35 +73,18 @@ async def set_file_contents(project):

@projects_routes.put("/projects/<string:project>/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:
Expand Down