Skip to content

Commit

Permalink
add vehicle_type_id normalization for moqo
Browse files Browse the repository at this point in the history
  • Loading branch information
hbruch committed Apr 2, 2024
1 parent 071ef83 commit 2d6d674
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 8 additions & 0 deletions x2gbfs/gbfs/base_provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from typing import Any, Dict, Generator, Optional, Tuple

logger = logging.getLogger('x2gbfs.base_provider')
Expand Down Expand Up @@ -74,3 +75,10 @@ def _create_station_status(self, station_id: str, last_reported: int) -> Dict[st
'vehicle_types_available': [],
'last_reported': last_reported,
}

@staticmethod
def _normalize_id(id: str) -> str:
"""
Normalizes the ID by restricting chars to A-Za-z_0-9. Whitespaces are converted to _.
"""
return re.sub('[^A-Za-z_0-9]', '', id.lower().replace(' ', '_')).replace('__', '_')
8 changes: 1 addition & 7 deletions x2gbfs/providers/deer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ def _normalize_model(self, model: str) -> str:
normalized_model = re.sub(r'(?i)e[ -]up!?', 'e-up!', normalized_model)
return re.sub(r'(?i)e[ -]golf', 'e-Golf', normalized_model)

def normalize_id(self, id: str) -> str:
"""
Normalizes the ID by restricting chars to A-Za-z_0-9. Whitespaces are converted to _.
"""
return re.sub('[^A-Za-z_0-9]', '', id.lower().replace(' ', '_')).replace('__', '_')

def pricing_plan_id(self, vehicle: Dict) -> Optional[str]:
"""
Maps deer's vehicle categories to pricing plans (provided viaa config).
Expand Down Expand Up @@ -186,7 +180,7 @@ def _extract_vehicle_and_type(self, fleetster_vehicle: Dict[str, Any]) -> Tuple[
vehicle_id = str(fleetster_vehicle['_id'])
normalized_brand = self._normalize_brand(fleetster_vehicle['brand'])
normalized_model = self._normalize_model(fleetster_vehicle['model'])
vehicle_type_id = self.normalize_id(normalized_brand + '_' + normalized_model)
vehicle_type_id = self._normalize_id(normalized_brand + '_' + normalized_model)

extended_properties = fleetster_vehicle['extended']['Properties']
accessories = []
Expand Down
2 changes: 1 addition & 1 deletion x2gbfs/providers/moqo.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _extract_from_vehicles(
@classmethod
def _extract_vehicle_type(cls, vehicle_types: dict[str, Any], vehicle: dict[str, Any]) -> str:
vehicle_model = vehicle['car_model_name']
id = vehicle_model.replace(' ', '_').lower()
id = cls._normalize_id(vehicle_model)
gbfs_make, gbfs_model = vehicle_model.split(' ')[0], ' '.join(vehicle_model.split(' ')[1:])
form_factor = cls._map_car_type(vehicle['vehicle_type'])
if not vehicle_types.get(id):
Expand Down

0 comments on commit 2d6d674

Please sign in to comment.