Skip to content

Commit 8134bfb

Browse files
committed
fix: python 3.9 dump version due to docker compose issues
1 parent e9e6814 commit 8134bfb

8 files changed

+54
-51
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ repos:
2121
rev: stable
2222
hooks:
2323
- id: black
24-
language_version: python3.10
24+
language_version: python3.9
2525
- repo: https://github.com/pre-commit/mirrors-isort
2626
rev: v5.4.2
2727
hooks:

Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM python:3.10
1+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
2+
23

34
COPY ./requirements.txt /app/requirements.txt
45
RUN pip install --upgrade pip
@@ -15,4 +16,6 @@ EXPOSE 8000
1516
RUN pip install --no-cache-dir -r /app/requirements.txt
1617

1718
WORKDIR /app
18-
CMD [ "python", "src/main.py" ]
19+
20+
# sleep to wait until the database is ready
21+
CMD sleep 5 && python src/main.py

docker-compose.yaml

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ services:
66
volumes:
77
- ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
88
environment:
9-
- POSTGRES_USER=${PostgresUser}
10-
- POSTGRES_PASSWORD=${PostgresPassword}
11-
- POSTGRES_SERVER=${PostgresServer}
12-
- POSTGRES_DB=${PostgresDb}
9+
- POSTGRES_USER=postgres
10+
- POSTGRES_PASSWORD=postgres
11+
- POSTGRES_DB=appw
1312
ports:
1413
- "5432:5432"
1514
app:
1615
build: .
17-
ports:
18-
- "8000:8000"
1916
depends_on:
2017
- database
18+
ports:
19+
- "8000:8000"

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ per-file-ignores =
1414
__init__.py: F401
1515

1616
[mypy]
17-
python_version = 3.10
17+
python_version = 3.9
1818
disallow_untyped_calls = True
1919
disallow_untyped_defs = True
2020
disallow_incomplete_defs = True

src/core/database.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def query_all(
2525

2626
def query_one(
2727
self, query: str, params: Union[Sequence[Any], Mapping[str, Any], None] = None
28-
) -> Tuple[Any, ...] | None:
28+
) -> Optional[Tuple[Any, ...]]:
2929
"""Execute a query and return the results"""
3030
with self.conn.cursor() as cursor:
3131
cursor.execute(query, params)

src/core/dependencies.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import Optional
23

34
from fastapi import File, UploadFile
45
from starlette.requests import Request
@@ -18,7 +19,7 @@ async def load_json_file(file: UploadFile = File(...)) -> JSONType:
1819
return json_data
1920

2021

21-
async def get_accept_request_header(request: Request) -> str | None:
22+
async def get_accept_request_header(request: Request) -> Optional[str]:
2223
"""
2324
Returns the value of the Accept header of the request.
2425
:param request: request

src/core/xml_parser.py

+37-37
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,16 @@ def parse_element_value(self, value: Optional[str]) -> Any:
5757
raise ValueError("Invalid XML file schema")
5858

5959
# element type validation check
60-
match self:
61-
case XMLElementType.STRING:
62-
return value
63-
case XMLElementType.INTEGER:
64-
return int(value)
65-
case XMLElementType.FLOAT:
66-
return float(value)
67-
case XMLElementType.BOOLEAN:
68-
return value == "true"
69-
case _:
70-
raise NotImplementedError(
71-
f"Etree element type not supported: {self.value}"
72-
)
60+
if self is XMLElementType.STRING:
61+
return value
62+
elif self is XMLElementType.INTEGER:
63+
return int(value)
64+
elif self is XMLElementType.FLOAT:
65+
return float(value)
66+
elif self is XMLElementType.BOOLEAN:
67+
return value == "true"
68+
else:
69+
raise NotImplementedError(f"Etree element type not supported: {self.value}")
7370

7471

7572
class XMLParser:
@@ -145,30 +142,33 @@ def _parse_json_data_to_etree(data: JSONType) -> _Element:
145142
element_value: Optional[str] = None
146143

147144
# Set element children and value based on the type of the item
148-
match element_type:
149-
# if the element is an object, then assign children to element keys recursively
150-
case XMLElementType.OBJECT:
151-
for key, value in data.items(): # type: ignore
152-
child = XMLParser._parse_json_data_to_etree(value)
153-
child.set("key", key)
154-
element.append(child)
155-
# if the element is a list, then append children to element
156-
case XMLElementType.LIST:
157-
for value in data: # type: ignore
158-
child = XMLParser._parse_json_data_to_etree(value)
159-
element.append(child)
160-
# if the element type is a string leaf, then set the value of the element
161-
case XMLElementType.STRING:
162-
element_value = data # type: ignore
163-
# if the element type is a leaf and not a string, then set the json-dumped value of the element
164-
case XMLElementType.INTEGER | XMLElementType.FLOAT | XMLElementType.BOOLEAN:
165-
element_value = json.dumps(data)
166-
# if the element type is null, then do not set the value of the element
167-
case XMLElementType.NULL:
168-
pass
169-
# if the element type is not one of above type, then raise an error
170-
case _:
171-
raise NotImplementedError("Unsupported type for parsing")
145+
# if the element is an object, then assign children to element keys recursively
146+
if element_type is XMLElementType.OBJECT:
147+
for key, value in data.items(): # type: ignore
148+
child = XMLParser._parse_json_data_to_etree(value)
149+
child.set("key", key)
150+
element.append(child)
151+
# if the element is a list, then append children to element
152+
elif element_type is XMLElementType.LIST:
153+
for value in data: # type: ignore
154+
child = XMLParser._parse_json_data_to_etree(value)
155+
element.append(child)
156+
# if the element type is a string leaf, then set the value of the element
157+
elif element_type is XMLElementType.STRING:
158+
element_value = data # type: ignore
159+
# if the element type is a leaf and not a string, then set the json-dumped value of the element
160+
elif element_type in [
161+
XMLElementType.INTEGER,
162+
XMLElementType.FLOAT,
163+
XMLElementType.BOOLEAN,
164+
]:
165+
element_value = json.dumps(data)
166+
# if the element type is null, then do not set the value of the element
167+
elif element_type is XMLElementType.NULL:
168+
pass
169+
# if the element type is not one of above type, then raise an error
170+
else:
171+
raise NotImplementedError("Unsupported type for parsing")
172172

173173
if element_value is not None:
174174
element.set("value", element_value)

src/routers/xml_json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

33
from fastapi import APIRouter, Depends, File, UploadFile
44
from lxml import etree
@@ -26,7 +26,7 @@ async def convert_xml2json_request(file: UploadFile = File(...)) -> JSONResponse
2626
async def convert_json2xml_request(
2727
accept_header: Optional[str] = Depends(get_accept_request_header),
2828
json_data: JSONType = Depends(load_json_file),
29-
) -> Response | JSONResponse:
29+
) -> Union[Response, JSONResponse]:
3030
"""
3131
Endpoint that converts JSON to XML.
3232

0 commit comments

Comments
 (0)