-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4089 from hove-io/jormungandr_implement_pt_journe…
…y_fare [Jormungandr] Implement pt journey fare
- Loading branch information
Showing
16 changed files
with
368 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
source/jormungandr/jormungandr/pt_journey_fare/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright (c) 2001-2023, Hove and/or its affiliates. All rights reserved. | ||
# | ||
# This file is part of Navitia, | ||
# the software to build cool stuff with public transport. | ||
# | ||
# Hope you'll enjoy and contribute to this project, | ||
# powered by Hove (www.hove.com). | ||
# Help us simplify mobility and open public transport: | ||
# a non ending quest to the responsive locomotion way of traveling! | ||
# | ||
# LICENCE: This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Stay tuned using | ||
# twitter @navitia | ||
# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org | ||
# https://groups.google.com/d/forum/navitia | ||
# www.navitia.io | ||
from .pt_journey_fare_backend_manager import PtJourneyFareBackendManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) 2001-2023, Hove and/or its affiliates. All rights reserved. | ||
# | ||
# This file is part of Navitia, | ||
# the software to build cool stuff with public transport. | ||
# | ||
# Hope you'll enjoy and contribute to this project, | ||
# powered by Hove (www.hove.com). | ||
# Help us simplify mobility and open public transport: | ||
# a non ending quest to the responsive locomotion way of traveling! | ||
# | ||
# LICENCE: This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Stay tuned using | ||
# twitter @navitia | ||
# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org | ||
# https://groups.google.com/d/forum/navitia | ||
# www.navitia.io | ||
from jormungandr.pt_journey_fare.pt_journey_fare import AbstractPtJourneyFare | ||
from navitiacommon import response_pb2, request_pb2, type_pb2 | ||
|
||
|
||
class Kraken(AbstractPtJourneyFare): | ||
def __init__(self, instance): | ||
self.instance = instance | ||
|
||
@staticmethod | ||
def _pt_sections(journey): | ||
return [s for s in journey.sections if s.type == response_pb2.PUBLIC_TRANSPORT] | ||
|
||
def create_fare_request(self, pt_journeys): | ||
request = request_pb2.Request() | ||
request.requested_api = type_pb2.pt_fares | ||
pt_fare_request = request.pt_fares | ||
for journey in pt_journeys: | ||
pt_sections = self._pt_sections(journey) | ||
if not pt_sections: | ||
continue | ||
pt_journey_request = pt_fare_request.pt_journeys.add(id=journey.internal_id) | ||
for s in pt_sections: | ||
pt_journey_request.pt_sections.add( | ||
id=s.id, | ||
network_uri=s.uris.network, | ||
start_stop_area_uri=s.origin.stop_point.stop_area.uri, | ||
end_stop_area_uri=s.destination.stop_point.stop_area.uri, | ||
line_uri=s.uris.line, | ||
begin_date_time=s.begin_date_time, | ||
end_date_time=s.end_date_time, | ||
first_stop_point_uri=s.origin.stop_point.uri, | ||
last_stop_point_uri=s.destination.stop_point.uri, | ||
physical_mode=s.uris.physical_mode, | ||
) | ||
return request | ||
|
||
def get_pt_journeys_fares(self, pt_journeys, request_id): | ||
fare_request = self.create_fare_request(pt_journeys) | ||
return self.instance.send_and_receive(fare_request, request_id="{}_fare".format(request_id)) |
40 changes: 40 additions & 0 deletions
40
source/jormungandr/jormungandr/pt_journey_fare/pt_journey_fare.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright (c) 2001-2023, Hove and/or its affiliates. All rights reserved. | ||
# | ||
# This file is part of Navitia, | ||
# the software to build cool stuff with public transport. | ||
# | ||
# Hope you'll enjoy and contribute to this project, | ||
# powered by Hove (www.hove.com). | ||
# Help us simplify mobility and open public transport: | ||
# a non ending quest to the responsive locomotion way of traveling! | ||
# | ||
# LICENCE: This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Stay tuned using | ||
# twitter @navitia | ||
# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org | ||
# https://groups.google.com/d/forum/navitia | ||
# www.navitia.io | ||
import abc | ||
|
||
|
||
# Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.x | ||
# http://stackoverflow.com/a/38668373/1614576 | ||
ABC = abc.ABCMeta(str("ABC"), (object,), {}) | ||
|
||
|
||
class AbstractPtJourneyFare(ABC): # type: ignore | ||
@abc.abstractmethod | ||
def get_pt_journeys_fares(self, pt_journeys, request_id): | ||
raise NotImplementedError("Method not implemented") |
63 changes: 63 additions & 0 deletions
63
source/jormungandr/jormungandr/pt_journey_fare/pt_journey_fare_backend_manager.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (c) 2001-2023, Hove and/or its affiliates. All rights reserved. | ||
# | ||
# This file is part of Navitia, | ||
# the software to build cool stuff with public transport. | ||
# | ||
# Hope you'll enjoy and contribute to this project, | ||
# powered by Hove (www.hove.com). | ||
# Help us simplify mobility and open public transport: | ||
# a non ending quest to the responsive locomotion way of traveling! | ||
# | ||
# LICENCE: This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Stay tuned using | ||
# twitter @navitia | ||
# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org | ||
# https://groups.google.com/d/forum/navitia | ||
# www.navitia.io | ||
from jormungandr import utils | ||
|
||
|
||
class NoRequestedPtJourneyFare(Exception): | ||
pass | ||
|
||
|
||
class PtJourneyFareBackendManager(object): | ||
def __init__(self, instance, configs, db_configs_getter=None): | ||
# TODO: read from db | ||
self._db_configs_getter = db_configs_getter | ||
|
||
self.instance = instance | ||
kraken_default_config = { | ||
"class": "jormungandr.pt_journey_fare.kraken.Kraken", | ||
"args": {"instance": self.instance}, | ||
} | ||
backends_configs = { | ||
"kraken": kraken_default_config, | ||
} | ||
backends_configs.update(configs or {}) | ||
|
||
self.backends = {} | ||
|
||
self.init(backends_configs) | ||
|
||
def init(self, backends_configs): | ||
for k, v in backends_configs.items(): | ||
self.backends[k] = utils.create_object(v) | ||
|
||
def get_pt_journey_fare(self, backend_id): | ||
backend = self.backends.get(backend_id) | ||
if backend: | ||
return backend | ||
raise NoRequestedPtJourneyFare("no requested pt_journey_fare backend: {}".format(backend_id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.