Skip to content

Commit ea38cfd

Browse files
#92 initial pylint fixes
1 parent 22b126d commit ea38cfd

10 files changed

+119
-78
lines changed

server/README.md

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
# Flask server
2+
23
This server has API endpoints supporting the application. Here's how you run this component individually.
34

45
1. cd to server and follow the below steps based on your OS:
56

67
### On Mac
8+
79
The first time you want to run this code, you will need to:
10+
811
1. Create a virtual environment: <code>python3 -m venv venv</code>
9-
2. Activate virtual environment:
10-
* On Mac or Linux: <code>source venv/bin/activate</code>
12+
2. Activate virtual environment:
13+
- On Mac or Linux: <code>source venv/bin/activate</code>
1114
3. Install dependencies into the virtual environment: <code>pip3 install -r requirements.txt</code>
1215

1316
On all subsequent runs, you will need to:
14-
1. Activate virtual environment:
15-
* On Mac or Linux: <code>source venv/bin/activate</code>
17+
18+
1. Activate virtual environment:
19+
- On Mac or Linux: <code>source venv/bin/activate</code>
1620
2. Run the development server:
17-
* On Mac or Linux: <code>bash run_dev_server.sh</code>
21+
- On Mac or Linux: <code>bash run_dev_server.sh</code>
1822

1923
### On Windows
20-
The first time you want to run this code, you will need to:
21-
3. Install Git Bash (if not already installed) and in VS code open the terminal of type Git Bash
22-
4. Create a virtual environment: <code>py -m venv venv</code>
23-
5. Activate virtual environment:
24-
* On Windows: <code>source venv/Scripts/activate</code>
24+
25+
The first time you want to run this code, you will need to: 3. Install Git Bash (if not already installed) and in VS code open the terminal of type Git Bash 4. Create a virtual environment: <code>py -m venv venv</code> 5. Activate virtual environment:
26+
27+
- On Windows: <code>source venv/Scripts/activate</code>
28+
2529
6. Install dependencies into the virtual environment: <code>pip install -r requirements.txt</code>
2630

27-
On all subsequent runs, you will need to:
28-
7. Activate virtual environment:
29-
* On Windows: <code>source venv/Scripts/activate</code>
31+
On all subsequent runs, you will need to: 7. Activate virtual environment:
32+
33+
- On Windows: <code>source venv/Scripts/activate</code>
34+
3035
8. Run the development server:
31-
* On Windows: <code>bash run_dev_server.sh</code>
36+
- On Windows: <code>bash run_dev_server.sh</code>

server/application/config.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=too-few-public-methods
2+
13
'''
24
Configuration settings for the application are handled in this module
35
'''
+27-31
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
"""
22
This module contains the RESTful route handlers
3-
for searching,previewing and downloading files.
3+
for searching, previewing, and downloading files.
44
"""
55
import json
66
from flask import Blueprint, Response, request, make_response
7-
from responses import ResponseTypes, ResponseSuccess, ResponseFailure
7+
from responses import ResponseSuccess
88
from usecases.search_orca_data import find_sections_use_case, preview_document_use_case
9-
9+
from utils.http_status_mapping import HTTP_STATUS_CODES_MAPPING
1010

1111
blueprint = Blueprint("search_orca_data", __name__)
1212

13-
HTTP_STATUS_CODES_MAPPING = {
14-
ResponseTypes.NOT_FOUND: 404,
15-
ResponseTypes.SYSTEM_ERROR: 500,
16-
ResponseTypes.AUTHORIZATION_ERROR: 403,
17-
ResponseTypes.PARAMETER_ERROR: 400,
18-
ResponseTypes.SUCCESS: 200,
19-
ResponseTypes.CONFLICT: 409
20-
}
21-
2213
@blueprint.route('/preview', methods=['POST'])
2314
def preview_document():
24-
'''
25-
This method defines the API endpoint to preview
26-
the document
27-
'''
15+
"""
16+
API endpoint to preview the document.
17+
"""
2818
data = request.get_json(force=True)
2919
response = preview_document_use_case(data)
3020
return Response(
@@ -35,23 +25,29 @@ def preview_document():
3525

3626
@blueprint.route('/find-sections', methods=['POST'])
3727
def find_sections():
38-
'''
39-
This method defines the API endpoint to find the sections
40-
based on the search input and to download the output as
41-
word document
42-
'''
28+
"""
29+
API endpoint to find sections and download as Word document.
30+
"""
4331
data = request.get_json(force=True)
4432
response = find_sections_use_case(data)
33+
4534
if isinstance(response, ResponseSuccess):
4635
docx_content = response.value
47-
response = make_response(docx_content)
48-
response.headers.set('Content-Type',
49-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
50-
response.headers.set('Content-Disposition', 'attachment', filename='output.docx')
51-
return response
52-
elif isinstance(response, ResponseFailure):
53-
return Response(
54-
json.dumps(response.value),
55-
mimetype="application/json",
56-
status=HTTP_STATUS_CODES_MAPPING[response.response_type],
36+
docx_response = make_response(docx_content)
37+
docx_response.headers.set(
38+
'Content-Type',
39+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
5740
)
41+
docx_response.headers.set(
42+
'Content-Disposition',
43+
'attachment',
44+
filename='output.docx'
45+
)
46+
return docx_response
47+
48+
# Handle ResponseFailure
49+
return Response(
50+
json.dumps(response.value),
51+
mimetype="application/json",
52+
status=HTTP_STATUS_CODES_MAPPING[response.response_type],
53+
)

server/application/rest/upload_files.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@
77
from flask_cors import cross_origin
88
from responses import ResponseTypes
99
from usecases.upload_files import file_upload_use_case, get_data_use_case
10+
from utils.http_status_mapping import HTTP_STATUS_CODES_MAPPING
1011

1112
blueprint = Blueprint("upload_files", __name__)
1213

13-
HTTP_STATUS_CODES_MAPPING = {
14-
ResponseTypes.NOT_FOUND: 404,
15-
ResponseTypes.SYSTEM_ERROR: 500,
16-
ResponseTypes.AUTHORIZATION_ERROR: 403,
17-
ResponseTypes.PARAMETER_ERROR: 400,
18-
ResponseTypes.SUCCESS: 200,
19-
ResponseTypes.CONFLICT: 409
20-
}
21-
2214
@blueprint.route('/upload', methods=['POST'])
2315
@cross_origin()
2416
def file_upload():

server/responses.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=too-few-public-methods
2+
13
'''
24
This module defines the response classes and types for the server.
35
@@ -33,22 +35,40 @@ def _format_message(self, msg):
3335

3436
@property
3537
def value(self):
38+
'''
39+
Returns the response as a dictionary containing the type and message.
40+
'''
3641
return {"type": self.response_type, "message": self.message}
3742

3843
def __bool__(self):
44+
'''
45+
Determines the truthiness of the response (always False).
46+
'''
3947
return False
4048

4149

4250
class ResponseSuccess:
51+
'''
52+
Represents a successful response.
53+
'''
4354
def __init__(self, value=None):
55+
'''
56+
Initializes a ResponseSuccess instance.
57+
'''
4458
self.value = value
4559
self.response_type = ResponseTypes.SUCCESS
4660

4761
def __bool__(self):
62+
'''
63+
Determines the truthiness of the response (always True).
64+
'''
4865
return True
4966

5067

5168
def build_response_from_invalid_request(invalid_request):
69+
'''
70+
Builds a ResponseFailure from an invalid request, compiling its errors.
71+
'''
5272
message = "\n".join(
5373
[f"{err['parameter']}: {err['message']}"
5474
for err in invalid_request.errors]

server/services/file_search_operations.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
'''
2+
This module provides functions for extracting sections from an ORCA log file
3+
based on specified search terms and saving the extracted data to a byte stream.
4+
'''
15
from io import BytesIO
26
import re
37

4-
def extract_sections(file_path, search_terms, sections, specify_lines, use_total_lines, total_lines):
8+
def extract_sections(file_path, search_terms, sections, specify_lines,
9+
use_total_lines, total_lines):
510
'''
611
Extracts the data from orca log file based on search terms and sections.
712
'''
@@ -15,11 +20,7 @@ def extract_sections(file_path, search_terms, sections, specify_lines, use_total
1520

1621
# Function to determine if a line is content
1722
def is_content_line(line, term, header_pattern=None):
18-
if line.strip() == "":
19-
return False
20-
if line.startswith("-----"):
21-
return False
22-
if line.startswith(term):
23+
if line.strip() == "" or line.startswith("-----") or line.startswith(term):
2324
return False
2425
if header_pattern and re.match(header_pattern, line):
2526
return False

server/services/file_upload_operations.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
'''
2+
This module contains functions for handling file upload operations.
3+
'''
4+
15
import os
26
from flask import current_app
37

@@ -11,7 +15,7 @@ def get_uploads_dir():
1115

1216
def save_uploaded_file(file):
1317
'''
14-
saves the uploaded file
18+
saves the uploaded file to the uploads directory
1519
'''
1620
uploads_dir = get_uploads_dir()
1721
filename = os.path.join(uploads_dir, file.filename)

server/usecases/search_orca_data.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
This module provides use cases for searching and processing
3+
data in ORCA log files, including functions for previewing document
4+
content and finding specific sections based on search terms.
5+
"""
6+
17
from responses import ResponseSuccess, ResponseFailure, ResponseTypes
28
from docx import Document
39
from services.file_search_operations import extract_sections, save_document_to_bytes
@@ -21,8 +27,8 @@ def preview_document_use_case(data):
2127
try:
2228
sections = list(map(int, sections))
2329
specify_lines = [temp_specify_lines] * len(sections)
24-
document_content = extract_sections(file_path, search_terms, sections,
25-
specify_lines, use_total_lines, total_lines)
30+
document_content = extract_sections(file_path, search_terms, sections,
31+
specify_lines, use_total_lines, total_lines)
2632
return ResponseSuccess({'document_content': document_content})
2733
except FileNotFoundError as e:
2834
return ResponseFailure(ResponseTypes.PARAMETER_ERROR, f'File not found: {str(e)}')
@@ -47,24 +53,19 @@ def find_sections_use_case(data):
4753
if not all([file_path, search_terms, sections, temp_specify_lines]):
4854
return ResponseFailure(ResponseTypes.PARAMETER_ERROR, 'Missing required fields')
4955

50-
sections = list(map(int, sections))
51-
specify_lines = [temp_specify_lines] * len(sections)
52-
5356
try:
54-
document_content = extract_sections(file_path, search_terms, sections,
57+
sections = list(map(int, sections))
58+
specify_lines = [temp_specify_lines] * len(sections)
59+
document_content = extract_sections(file_path, search_terms, sections,
5560
specify_lines, use_total_lines, total_lines)
5661
document = Document()
5762
for paragraph in document_content.split('\n'):
5863
document.add_paragraph(paragraph.strip())
5964
docx_bytes = save_document_to_bytes(document)
60-
return ResponseSuccess(docx_bytes)
61-
except FileNotFoundError as e:
62-
return ResponseFailure(ResponseTypes.PARAMETER_ERROR, f'File not found: {str(e)}')
63-
except PermissionError as e:
64-
return ResponseFailure(ResponseTypes.PARAMETER_ERROR, f'Permission denied: {str(e)}')
65-
except ValueError as e:
66-
return ResponseFailure(ResponseTypes.PARAMETER_ERROR, f'Value error: {str(e)}')
67-
except AttributeError as e:
68-
return ResponseFailure(ResponseTypes.SYSTEM_ERROR, f'Document processing error: {str(e)}')
69-
except TypeError as e:
70-
return ResponseFailure(ResponseTypes.SYSTEM_ERROR, f'Document type error: {str(e)}')
65+
result = ResponseSuccess(docx_bytes)
66+
except (FileNotFoundError, PermissionError, ValueError) as e:
67+
result = ResponseFailure(ResponseTypes.PARAMETER_ERROR, str(e))
68+
except (AttributeError, TypeError) as e:
69+
result = ResponseFailure(ResponseTypes.SYSTEM_ERROR, str(e))
70+
71+
return result

server/usecases/upload_files.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
This module provides use cases for uploading and handling files,
3+
including file upload validation and saving to a temporary folder.
4+
It also includes a sample data function to test server connection.
5+
"""
6+
17
from responses import ResponseSuccess, ResponseFailure, ResponseTypes
28
from services.file_upload_operations import save_uploaded_file
39

server/utils/http_status_mapping.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Utility module for HTTP status code mappings.
3+
"""
4+
5+
from responses import ResponseTypes
6+
7+
HTTP_STATUS_CODES_MAPPING = {
8+
ResponseTypes.NOT_FOUND: 404,
9+
ResponseTypes.SYSTEM_ERROR: 500,
10+
ResponseTypes.AUTHORIZATION_ERROR: 403,
11+
ResponseTypes.PARAMETER_ERROR: 400,
12+
ResponseTypes.SUCCESS: 200,
13+
ResponseTypes.CONFLICT: 409
14+
}

0 commit comments

Comments
 (0)