️ Stop.postal_region and Line.id/operator_id can be integer values

This commit is contained in:
2023-05-08 17:15:21 +02:00
parent 0f1c16ab53
commit 0ba4c1e6fa
5 changed files with 36 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
from collections import defaultdict
from logging import getLogger
from re import compile as re_compile
from time import time
from typing import (
@@ -37,6 +38,10 @@ from .idfm_types import (
from .ratp_types import Picto as RatpPicto
logger = getLogger(__name__)
class IdfmInterface:
IDFM_ROOT_URL = "https://prim.iledefrance-mobilites.fr/marketplace"
@@ -358,9 +363,22 @@ class IdfmInterface:
picto_id = fields.picto.id_ if fields.picto is not None else None
line_id = fields.id_line
try:
formatted_line_id = int(line_id[1:] if line_id[0] == "C" else line_id)
except ValueError:
logger.warning("Unable to format %s line id.", line_id)
continue
try:
operator_id = int(fields.operatorref) # type: ignore
except (ValueError, TypeError):
logger.warning("Unable to format %s operator id.", fields.operatorref)
operator_id = 0
ret.append(
Line(
id=fields.id_line,
id=formatted_line_id,
short_name=fields.shortname_line,
name=fields.name_line,
status=IdfmLineState(fields.status.value),
@@ -373,7 +391,7 @@ class IdfmInterface:
),
colour_web_hexa=fields.colourweb_hexa,
text_colour_hexa=fields.textcolourprint_hexa,
operator_id=optional_value(fields.operatorref),
operator_id=operator_id,
operator_name=optional_value(fields.operatorname),
accessibility=IdfmState(fields.accessibility.value),
visual_signs_available=IdfmState(
@@ -403,13 +421,21 @@ class IdfmInterface:
fields.arrxepsg2154, fields.arryepsg2154
)
try:
postal_region = int(fields.arrpostalregion)
except ValueError:
logger.warning(
"Unable to format %s postal region.", fields.arrpostalregion
)
continue
yield Stop(
id=int(fields.arrid),
name=fields.arrname,
epsg3857_x=epsg3857_point[0],
epsg3857_y=epsg3857_point[1],
town_name=fields.arrtown,
postal_region=fields.arrpostalregion,
postal_region=postal_region,
transport_mode=TransportMode(fields.arrtype.value),
version=fields.arrversion,
created_ts=created_ts,

View File

@@ -27,7 +27,7 @@ from .stop import _Stop
class LineStopAssociations(Base):
id = mapped_column(BigInteger, primary_key=True)
line_id = mapped_column(String, ForeignKey("lines.id"))
line_id = mapped_column(BigInteger, ForeignKey("lines.id"))
stop_id = mapped_column(BigInteger, ForeignKey("_stops.id"))
__tablename__ = "line_stop_associations"
@@ -53,7 +53,7 @@ class Line(Base):
db = db
id = mapped_column(String, primary_key=True)
id = mapped_column(BigInteger, primary_key=True)
short_name = mapped_column(String)
name = mapped_column(String, nullable=False)
@@ -68,7 +68,7 @@ class Line(Base):
colour_web_hexa = mapped_column(String, nullable=False)
text_colour_hexa = mapped_column(String, nullable=False)
operator_id = mapped_column(String)
operator_id = mapped_column(Integer)
operator_name = mapped_column(String)
accessibility = mapped_column(Enum(IdfmState), nullable=False)

View File

@@ -53,7 +53,7 @@ class _Stop(Base):
name = mapped_column(String, nullable=False, index=True)
town_name = mapped_column(String, nullable=False)
postal_region = mapped_column(String, nullable=False)
postal_region = mapped_column(Integer, nullable=False)
epsg3857_x = mapped_column(Float, nullable=False)
epsg3857_y = mapped_column(Float, nullable=False)

View File

@@ -46,7 +46,7 @@ class TransportMode(StrEnum):
class Line(BaseModel):
id: str
id: int
shortName: str
name: str
status: IdfmLineState

View File

@@ -73,13 +73,11 @@ trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
tracer = trace.get_tracer(APP_NAME)
with tracer.start_as_current_span("foo"):
print("Hello world!")
idfm_interface = IdfmInterface(API_KEY, db)
# TODO: Add command line argument to force database reset.
@app.on_event("startup")
async def startup():
await db.connect(DB_PATH, clear_static_data=True)
@@ -102,7 +100,7 @@ def optional_datetime_to_ts(dt: datetime | None) -> int | None:
@app.get("/line/{line_id}", response_model=LineSchema)
async def get_line(line_id: str) -> LineSchema:
async def get_line(line_id: int) -> LineSchema:
line: Line | None = await Line.get_by_id(line_id)
if line is None: