Skip to content

Commit

Permalink
MDE resolved pylint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mantvydasdeltuva committed Aug 22, 2024
1 parent d1fe777 commit 93f5721
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 91 deletions.
8 changes: 6 additions & 2 deletions app/back-end/gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
- Number of worker processes based on the number of CPU cores.
Dependencies:
- multiprocessing: Used to determine the number of CPU cores for configuring the number of Gunicorn workers.
- src.setup.env.Env: The class responsible for retrieving environment settings such as host and port.
- multiprocessing: Used to determine the number of CPU cores for configuring the
number of Gunicorn workers.
- src.setup.env.Env: The class responsible for retrieving environment settings such
as host and port.
"""

# pylint: disable=invalid-name

import multiprocessing

# Bind to specified host and port
Expand Down
15 changes: 14 additions & 1 deletion app/back-end/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
black
astroid==3.2.4
black==24.8.0
click==8.1.7
dill==0.3.8
isort==5.13.2
mccabe==0.7.0
mypy-extensions==1.0.0
packaging==24.1
pathspec==0.12.1
platformdirs==4.2.2
pylint==3.2.6
tomli==2.0.1
tomlkit==0.13.2
typing_extensions==4.12.2
5 changes: 4 additions & 1 deletion app/back-end/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
Dependencies:
- src.create_app: The function that sets up and returns the configured Flask app instance.
- src.socketio: The Socket.IO instance initialized in the application setup.
- src.setup.env.Env: The class responsible for managing environment variables, including retrieving the host and port settings.
- src.setup.env.Env: The class responsible for managing environment variables, including
retrieving the host and port settings.
"""

# pylint: disable=import-error

from src import create_app, socketio, env

# Initialize the Flask app
Expand Down
5 changes: 4 additions & 1 deletion app/back-end/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Dependencies:
- Flask: The core web framework.
- gevent.monkey: Provides monkey patches to enable cooperative multitasking.
- src.setup.extensions: Contains initialization for Flask extensions like compression, Socket.IO, and CORS.
- src.setup.extensions: Contains initialization for Flask extensions like compression, Socket.IO,
and CORS.
- src.setup.router: Defines and manages application routing through blueprints.
- src.setup.eventer: Sets up event handlers for application events.
- src.constants: Provides constants used in configuration, such as BASE_ROUTE.
Expand All @@ -20,6 +21,8 @@
Flask: A fully configured Flask application instance with all extensions and routes initialized.
"""

# pylint: disable=import-error

import gevent.monkey
from flask import Flask

Expand Down
2 changes: 2 additions & 0 deletions app/back-end/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- dotenv: Used for loading environment variables from a `.env` file.
"""

# pylint: disable=import-error

import os
from dotenv import load_dotenv

Expand Down
5 changes: 4 additions & 1 deletion app/back-end/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
- Workspace directory paths, including a template directory.
- Base API route and specific routes for various functionalities.
These constants are typically used for file handling, directory management, and routing in the Flask application.
These constants are typically used for file handling, directory management, and routing in the
Flask application.
Dependencies:
- os: Used for interacting with the operating system to manage file and directory paths.
"""

# pylint: disable=import-error

import os


Expand Down
33 changes: 18 additions & 15 deletions app/back-end/src/events/workspace_event.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""
This module contains functions for handling workspace-related WebSocket events.
It includes functions that manage file operations within the workspace directory. The module provides real-time
feedback to clients through WebSocket events.
It includes functions that manage file operations within the workspace directory. The module
provides real-time feedback to clients through WebSocket events.
Dependencies:
- os: Used for file and directory operations.
- src.setup.extensions.socketio: The Socket.IO instance used for handling real-time communication.
- src.setup.constants.WORKSPACE_DIR: The base directory for workspace files.
"""

# pylint: disable=import-error

import os

from src.setup.extensions import socketio
from src.utils.exceptions import UnexpectedError
from src.constants import WORKSPACE_DIR


Expand All @@ -21,18 +24,20 @@ def handle_workspace_file_update(data):
"""
Handle the 'workspace_file_update' WebSocket event.
This function processes the data received from the client to update a file in the workspace directory.
It ensures that required fields are present, writes the content to the specified file, and handles any errors
by sending appropriate status updates back to the client.
This function processes the data received from the client to update a file in the workspace
directory. It ensures that required fields are present, writes the content to the specified
file, and handles any errors by sending appropriate status updates back to the client.
Args:
data (dict): The data received from the client, expected to contain 'uuid', 'fileId', and 'content'.
data (dict): The data received from the client, expected to contain 'uuid', 'fileId', and
'content'.
Emits:
'workspace_file_update_status': Emits a status message indicating success or failure of the file update operation.
'workspace_file_update_status': Emits a status message indicating success or failure of
the file update operation.
"""
uuid = data.get("uuid")
fileId = data.get("fileId")
file_id = data.get("fileId")
content = data.get("content")

# Ensure the uuid is provided
Expand All @@ -44,28 +49,26 @@ def handle_workspace_file_update(data):
return

# Ensure the fileId is provided
if not fileId:
if not file_id:
socketio.emit(
"workspace_file_update_status",
{"status": "error", "message": "File ID is missing"},
)
return

file_path = os.path.join(WORKSPACE_DIR, uuid, fileId)
file_path = os.path.join(WORKSPACE_DIR, uuid, file_id)

try:
# Ensure the directory exists
os.makedirs(os.path.dirname(file_path), exist_ok=True)

# Write the content to the file
with open(file_path, "w") as file:
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)

# Notify the client of the successful update
socketio.emit("workspace_file_update_status", {"status": "success"})

except Exception as e:
except UnexpectedError as e:
# Notify the client of an error during the update
socketio.emit(
"workspace_file_update_status", {"status": "error", "message": str(e)}
)
socketio.emit("workspace_file_update_status", {"status": "error", "message": str(e)})
63 changes: 35 additions & 28 deletions app/back-end/src/routes/workspace_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
Dependencies:
- os: For file and directory operations, such as checking existence and copying directories.
- shutil: For copying directory trees, ensuring that user-specific directories are properly initialized.
- shutil: For copying directory trees, ensuring that user-specific directories are properly
initialized.
- flask.Blueprint: To create and organize route blueprints for modular route management in Flask.
- flask.request: To handle incoming HTTP requests and extract headers and parameters.
- flask.jsonify: To create JSON responses for API endpoints.
- flask.send_file: To serve files for download.
Extensions:
- src.setup.extensions.compress: Used for compressing responses to optimize data transfer.
- src.setup.extensions.logger: Provides logging functionalities for capturing and recording events and errors.
- src.setup.constants: Contains constants for directory paths and routes used in the workspace management.
- src.setup.extensions.logger: Provides logging functionalities for capturing and recording events
and errors.
- src.setup.constants: Contains constants for directory paths and routes used in the workspace
management.
"""

# pylint: disable=import-error

import os
import shutil
from flask import Blueprint, request, jsonify, send_file

from src.setup.extensions import compress, logger
from src.utils.helpers import socketio_emit_to_user_session, build_workspace_structure
from src.utils.exceptions import UnexpectedError
from src.constants import (
WORKSPACE_DIR,
WORKSPACE_TEMPLATE_DIR,
Expand All @@ -40,13 +46,14 @@ def get_workspace():
"""
Retrieve the structure of the workspace directory.
This endpoint provides a JSON representation of the user's workspace directory structure. If the directory
does not exist, it copies a template directory to the user's workspace. The structure includes metadata
about files and folders in the workspace.
This endpoint provides a JSON representation of the user's workspace directory structure. If the
directory does not exist, it copies a template directory to the user's workspace. The structure
includes metadata about files and folders in the workspace.
Process:
- Extracts the UUID and SID from request headers to identify the user session.
- Ensures that the user-specific workspace directory exists; if not, copies a template directory.
- Ensures that the user-specific workspace directory exists; if not, copies a template
directory.
- Builds the directory structure as a nested JSON object.
- Emits feedback to the user's console about the status of the workspace retrieval process.
Expand All @@ -55,7 +62,8 @@ def get_workspace():
Returns:
Response: A Flask response object with the following possible outcomes:
- `200 OK`: If the workspace structure is successfully retrieved, returns a JSON representation of the directory structure.
- `200 OK`: If the workspace structure is successfully retrieved, returns a JSON
representation of the directory structure.
- `400 Bad Request`: If the UUID or SID header is missing in the request.
- `403 Forbidden`: If there is a permission issue accessing the workspace directory.
- `404 Not Found`: If the workspace directory or files are not found.
Expand All @@ -64,9 +72,11 @@ def get_workspace():
Errors and Feedback:
- If the `uuid` or `sid` headers are missing, returns a `400 Bad Request` response.
- On successful retrieval, a success message is emitted to the user's console.
- In case of errors, appropriate feedback is emitted to the user's console and an error response is returned:
- In case of errors, appropriate feedback is emitted to the user's console and an error
response is returned:
- `FileNotFoundError`: Indicates that the directory or file was not found.
- `PermissionError`: Indicates permission issues while accessing the workspace directory.
- `PermissionError`: Indicates permission issues while accessing the workspace
directory.
- Other exceptions: Logs and reports unexpected errors.
"""

Expand Down Expand Up @@ -99,9 +109,7 @@ def get_workspace():

# Build and return the workspace structure as a JSON object
workspace_structure = [
build_workspace_structure(
os.path.join(user_workspace_dir, child), user_workspace_dir
)
build_workspace_structure(os.path.join(user_workspace_dir, child), user_workspace_dir)
for child in os.listdir(user_workspace_dir)
]

Expand All @@ -117,7 +125,7 @@ def get_workspace():
return jsonify(workspace_structure)

except FileNotFoundError as e:
logger.error(f"FileNotFoundError: {e} while accessing {user_workspace_dir}")
logger.error("FileNotFoundError: %s while accessing %s", e, user_workspace_dir)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
Expand All @@ -130,7 +138,7 @@ def get_workspace():
)
return jsonify({"error": "Requested file not found"}), 404
except PermissionError as e:
logger.error(f"PermissionError: {e} while accessing {user_workspace_dir}")
logger.error("PermissionError: %s while accessing %s", e, user_workspace_dir)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
Expand All @@ -142,14 +150,14 @@ def get_workspace():
sid,
)
return jsonify({"error": "Permission denied"}), 403
except Exception as e:
logger.error(f"Unexpected error while serving workspace for UUID '{uuid}': {e}")
except UnexpectedError as e:
logger.error("UnexpectedError: %s while accessing %s", e.message, user_workspace_dir)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"Unexpected error while serving workspace for UUID '{uuid}': {e}",
"message": f"UnexpectedError: {e.message} while accessing {user_workspace_dir}",
},
uuid,
sid,
Expand All @@ -165,8 +173,8 @@ def get_workspace_file(relative_path):
This endpoint serves files from the user's workspace directory. If the directory does not exist,
it copies a template directory to the user's workspace. The file specified by `relative_path`
is then served for download. Feedback about the file retrieval process is sent to the user's console
via Socket.IO.
is then served for download. Feedback about the file retrieval process is sent to the user's
console via Socket.IO.
Args:
relative_path (str): The path to the file within the user's workspace directory.
Expand All @@ -182,7 +190,8 @@ def get_workspace_file(relative_path):
Errors and Feedback:
- If the `uuid` or `sid` headers are missing, a `400 Bad Request` response is returned.
- On successful file retrieval, a success message is emitted to the user's console.
- On errors, appropriate feedback is emitted to the user's console and an error response is returned:
- On errors, appropriate feedback is emitted to the user's console and an error response is
returned:
- `FileNotFoundError`: Indicates the requested file was not found.
- `PermissionError`: Indicates permission issues while accessing the file.
- Other exceptions: Logs and reports unexpected errors.
Expand Down Expand Up @@ -231,7 +240,7 @@ def get_workspace_file(relative_path):
return send_file(file_path, as_attachment=False)

except FileNotFoundError as e:
logger.error(f"FileNotFoundError: {e} while accessing {file_path}")
logger.error("FileNotFoundError: %s while accessing %s", e, file_path)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
Expand All @@ -244,7 +253,7 @@ def get_workspace_file(relative_path):
)
return jsonify({"error": "Requested file not found"}), 404
except PermissionError as e:
logger.error(f"PermissionError: {e} while accessing {file_path}")
logger.error("PermissionError: %s while accessing %s", e, file_path)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
Expand All @@ -256,16 +265,14 @@ def get_workspace_file(relative_path):
sid,
)
return jsonify({"error": "Permission denied"}), 403
except Exception as e:
logger.error(
f"Unexpected error while serving file '{relative_path}' for UUID '{uuid}': {e}"
)
except UnexpectedError as e:
logger.error("UnexpectedError: %s while accessing %s", e.message, file_path)
# Emit a feedback to the user's console
socketio_emit_to_user_session(
CONSOLE_FEEDBACK_EVENT,
{
"type": "errr",
"message": f"Unexpected error while serving file '{relative_path}' for UUID '{uuid}': {e}",
"message": f"UnexpectedError: {e.message} while accessing {file_path}",
},
uuid,
sid,
Expand Down
Loading

0 comments on commit 93f5721

Please sign in to comment.