ProfileInterface

class pyhafas.profile.interfaces.ProfileInterface[source]

Bases: pyhafas.profile.interfaces.helper.request.RequestHelperInterface, pyhafas.profile.interfaces.helper.format_products_filter.FormatProductsFilterHelperInterface, pyhafas.profile.interfaces.helper.parse_lid.ParseLidHelperInterface, pyhafas.profile.interfaces.helper.date_time.DateTimeHelperInterface, pyhafas.profile.interfaces.helper.parse_leg.ParseLegHelperInterface, pyhafas.profile.interfaces.helper.parse_remark.ParseRemarkHelperInterface, pyhafas.profile.interfaces.requests.location.LocationRequestInterface, pyhafas.profile.interfaces.requests.journey.JourneyRequestInterface, pyhafas.profile.interfaces.requests.journeys.JourneysRequestInterface, pyhafas.profile.interfaces.requests.station_board.StationBoardRequestInterface, pyhafas.profile.interfaces.requests.trip.TripRequestInterface, abc.ABC

The profile interface is the abstract class of a profile.

It inherits all interfaces, so it has all methods a normal profile has available as abstract methods. Therefore it can be used as type hint for self in methods which are inherited by profiles.

addChecksum: bool

Whether the checksum authentication method should be activated. Exclusive with addMicMac

addMicMac: bool

Whether the mic-mac authentication method should be activated. Exclusive with addChecksum

availableProducts: Dict[str, List[int]]

Should contain all products available in HaFAS. The key is the name the end-user will use, the value is a list of bitmasks (numbers) needed for the product. In most cases, this is only one number. This bitmasks will be add up to generate the final bitmask.

baseUrl: str

Complete http(s) URL to mgate.exe of the HaFAS deployment. Other endpoints are (currently) incompatible with pyHaFAS

defaultProducts: List[str]

List of products (item must be a key in availableProducts) which should be activated by default

defaultUserAgent: str

(optional) User-Agent in header when connecting to HaFAS. Defaults to pyhafas. A good option would be the app ones. Can be overwritten by the user.

locale: str

(used in future) Locale used for i18n. Should be an IETF Language-Region Tag

Examples: https://tools.ietf.org/html/bcp47#appendix-A

requestBody: dict

Static part of the request body sent to HaFAS. Normally contains informations about the client and another authentication

salt: str

(required if addMicMac or addChecksum is true). The salt for calculating the checksum or mic-mac

timezone: datetime.tzinfo

Timezone HaFAS lives in. Should be a pytz timezone object

userAgent: str

(optional) Do not change, unless you know what you’re doing. Disallows the user to change the user agent. For usage in internal code to get the user-agent which should be used.

Helper

format_products_filter

class pyhafas.profile.interfaces.helper.format_products_filter.FormatProductsFilterHelperInterface[source]

Bases: abc.ABC

abstract format_products_filter(requested_products)[source]

Create the products filter given to HaFAS

Parameters

requested_products (dict) – Mapping of Products to whether it’s enabled or disabled

Return type

dict

Returns

value for HaFAS “jnyFltrL” attribute

date_time

class pyhafas.profile.interfaces.helper.date_time.DateTimeHelperInterface[source]

Bases: abc.ABC

abstract parse_date(date_string)[source]

Parses the date HaFAS returns

Parameters

date_string (str) – Date returned from HaFAS

Return type

date

Returns

Parsed date object

abstract parse_datetime(time_string, date)[source]

Parses the time format HaFAS returns and combines it with a date

Parameters
  • time_string (str) – Time string sent by HaFAS

  • date (date) – Start day of the leg/journey

Return type

datetime

Returns

Parsed date and time as datetime object

abstract parse_timedelta(time_string)[source]

Parses the time HaFAS returns as timedelta object

Example use case is when HaFAS returns a duration of a leg :type time_string: str :param time_string: Time string sent by HaFAS :rtype: timedelta :return: Parsed time as timedelta object

abstract transform_datetime_parameter_timezone(date_time)[source]

Transfers datetime parameters incoming by the user to the profile timezone

Parameters

date_time (datetime) – datetime parameter incoming by user. Can be timezone aware or unaware

Return type

datetime

Returns

Timezone aware datetime object in profile timezone

parse_leg

class pyhafas.profile.interfaces.helper.parse_leg.ParseLegHelperInterface[source]

Bases: abc.ABC

abstract parse_leg(journey, common, departure, arrival, date, jny_type='JNY', gis=None)[source]

Parses Leg HaFAS returns into Leg object

Parameters
  • journey (dict) – Journey object given back by HaFAS (Data of the Leg to parse)

  • common (dict) – Common object given back by HaFAS

  • departure (dict) – Departure object given back by HaFAS

  • arrival (dict) – Arrival object given back by HaFAS

  • date (date) – Parsed date of Journey (Departing date)

  • jny_type (str) – HaFAS Journey type

  • gis – GIS object given back by HaFAS.

Return type

Leg

Returns

Parsed Leg object

abstract parse_legs(jny, common, date)[source]

Parses Legs (when multiple available)

Parameters
  • jny (dict) – Journeys object returned by HaFAS

  • common (dict) – Common object returned by HaFAS

  • date (date) – Parsed date of Journey (Departing date)

Return type

List[Leg]

Returns

Parsed List of Leg objects

parse_lid

class pyhafas.profile.interfaces.helper.parse_lid.ParseLidHelperInterface[source]

Bases: abc.ABC

abstract parse_lid(lid)[source]

Converts the LID given by HaFAS. Splits the LID in multiple elements

Parameters

lid (str) – Location identifier (given by HaFAS)

Return type

dict

Returns

Dict of the elements of the dict

abstract parse_lid_to_station(lid, name='', latitude=0, longitude=0)[source]

Parses the LID given by HaFAS to a station object

Parameters
  • lid (str) – Location identifier (given by HaFAS)

  • name (str) – Station name (optional)

  • latitude (float) – Latitude of the station (optional)

  • longitude (float) – Longitude of the station (optional)

Return type

Station

Returns

Parsed LID as station object

request

class pyhafas.profile.interfaces.helper.request.RequestHelperInterface[source]

Bases: abc.ABC

abstract calculate_checksum(data)[source]

Calculates the checksum of the request (required for most profiles)

Parameters

data (str) – Complete body as string

Return type

str

Returns

Checksum for the request

abstract calculate_mic_mac(data)[source]

Calculates the mic-mac for the request (required for some profiles)

Parameters

data (str) – Complete body as string

Return type

Tuple[str, str]

Returns

Mic and mac to be sent to HaFAS

abstract request(body)[source]

Sends the request and does a basic parsing of the response and error handling

Parameters

body – Reqeust body as dict (without the requestBody of the profile)

Return type

HafasResponse

Returns

HafasRespone object or Exception when HaFAS response returns an error

abstract url_formatter(data)[source]

Formats the URL for HaFAS (adds the checksum or mic-mac)

Parameters

data (str) – Complete body as string

Return type

str

Returns

Request-URL (maybe with checksum or mic-mac)

Mappings

error_codes

class pyhafas.profile.interfaces.mappings.error_codes.ErrorCodesMappingInterface(value)[source]

Bases: enum.Enum

Mapping of the HaFAS error code to the exception class

default defines the error when the error code cannot be found in the mapping

default: Exception

Requests

journey

class pyhafas.profile.interfaces.requests.journey.JourneyRequestInterface[source]

Bases: abc.ABC

abstract format_journey_request(journey)[source]

Creates the HaFAS request body for a journey request

Parameters

journey (Journey) – Id of the journey (ctxRecon)

Return type

dict

Returns

Request body for HaFAS

abstract parse_journey_request(data)[source]

Parses the HaFAS response for a journey request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

Journey

Returns

List of Journey objects

journeys

class pyhafas.profile.interfaces.requests.journeys.JourneysRequestInterface[source]

Bases: abc.ABC

abstract format_journeys_request(origin, destination, via, date, min_change_time, max_changes, products, max_journeys)[source]

Creates the HaFAS request body for a journeys request

Parameters
  • origin (Station) – Origin station

  • destination (Station) – Destionation station

  • via (List[Station]) – Via stations, maybe empty list)

  • date (datetime) – Date and time to search journeys for

  • min_change_time (int) – Minimum transfer/change time at each station

  • max_changes (int) – Maximum number of changes

  • products (Dict[str, bool]) – Allowed products (a product is a mean of transport like ICE,IC)

  • max_journeys (int) – Maximum number of returned journeys

Return type

dict

Returns

Request body for HaFAS

abstract format_search_from_leg_request(origin, destination, via, min_change_time, max_changes, products)[source]

Creates the HaFAS request body for a search from leg request

Parameters
  • origin (Leg) – Origin leg

  • destination (Station) – Destionation station

  • via (List[Station]) – Via stations, maybe empty list)

  • min_change_time (int) – Minimum transfer/change time at each station

  • max_changes (int) – Maximum number of changes

  • products (Dict[str, bool]) – Allowed products (a product is a mean of transport like ICE,IC)

Return type

dict

Returns

Request body for HaFAS

abstract parse_journeys_request(data)[source]

Parses the HaFAS response for a journeys request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

List[Journey]

Returns

List of Journey objects

location

class pyhafas.profile.interfaces.requests.location.LocationRequestInterface[source]

Bases: abc.ABC

abstract format_location_request(term, rtype='S')[source]

Creates the HaFAS request body for a location search request.

Parameters
  • term (str) – Search term

  • rtype (str) – Result types. One of [‘S’ for stations, ‘ALL’ for addresses and stations]

Returns

Request body for HaFAS

abstract parse_location_request(data)[source]

Parses the HaFAS response for a location request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

List[Station]

Returns

List of Station objects

station_board

class pyhafas.profile.interfaces.requests.station_board.StationBoardRequestInterface[source]

Bases: abc.ABC

format_station_board_request(station, request_type, date, max_trips, duration, products, direction)[source]

Creates the HaFAS request for Station Board (departure/arrival)

Parameters
  • station (Station) – Station to get departures/arrivals for

  • request_type (StationBoardRequestType) – ARRIVAL or DEPARTURE

  • date (datetime) – Date and time to get departures/arrival for

  • max_trips (int) – Maximum number of trips that can be returned

  • products (Dict[str, bool]) – Allowed products (a product is a mean of transport like ICE,IC)

  • duration (int) – Time in which trips are searched

  • direction (Optional[Station]) – Direction (end) station of the train. If none, filter will not be applied

Return type

dict

Returns

Request body for HaFAS

parse_station_board_request(data, departure_arrival_prefix)[source]

Parses the HaFAS data for a station board request

Parameters
  • data (HafasResponse) – Formatted HaFAS response

  • departure_arrival_prefix (str) – Prefix for specifying whether its for arrival or departure

Return type

List[StationBoardLeg]

Returns

List of StationBoardLeg objects

trip

class pyhafas.profile.interfaces.requests.trip.TripRequestInterface[source]

Bases: abc.ABC

abstract format_trip_request(trip_id)[source]

Creates the HaFAS request for a trip request

Parameters

trip_id (str) – Id of the trip/leg

Return type

dict

Returns

Request body for HaFAS

abstract parse_trip_request(data)[source]

Parses the HaFAS data for a trip request

Parameters

data (HafasResponse) – Formatted HaFAS response

Return type

Leg

Returns

Leg objects