<s> """<STR_LIT>""" <EOL> from datetime import date , time , timedelta <EOL> from string import capwords <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import transaction , reset_queries <EOL> from django . utils . translation import ugettext_noop as _ <EOL> from molly . apps . places . models import ( Entity , EntityType , EntityTypeCategory , <EOL> Source , Route , Journey , ScheduledStop ) <EOL> from molly . apps . places . providers import BaseMapsProvider <EOL> from molly . conf . provider import task <EOL> class CifTimetableProvider ( BaseMapsProvider ) : <EOL> def __init__ ( self , <EOL> entity_manager = Entity . objects , <EOL> entity_type_manager = EntityType . objects , <EOL> entity_type_category_manager = EntityTypeCategory . objects , <EOL> source_manager = Source . objects , <EOL> route_manager = Route . objects , <EOL> journey_manager = Journey . objects , <EOL> scheduled_stop_manager = ScheduledStop . objects , <EOL> filename = None ) : <EOL> self . _entity_manager = entity_manager <EOL> self . _entity_type_manager = entity_type_manager <EOL> self . _entity_type_category_manager = entity_type_category_manager <EOL> self . _source_manager = source_manager <EOL> self . _route_manager = route_manager <EOL> self . _journey_manager = journey_manager <EOL> self . _scheduled_stop_manager = scheduled_stop_manager <EOL> self . _tiplocs = { } <EOL> self . _routes = { } <EOL> self . _filename = filename <EOL> def _parse_tiploc ( self , line ) : <EOL> tiploc = line [ <NUM_LIT:2> : <NUM_LIT:9> ] . strip ( ) <EOL> entities = self . _entity_manager . get_entity ( "<STR_LIT>" , tiploc ) <EOL> try : <EOL> entity = entities [ <NUM_LIT:0> ] <EOL> except ObjectDoesNotExist : <EOL> entity = self . _entity_manager . create ( <EOL> source = self . source , <EOL> primary_type = self . entity_type , <EOL> identifiers = { '<STR_LIT>' : tiploc } , <EOL> titles = { '<STR_LIT>' : capwords ( line [ <NUM_LIT> : <NUM_LIT> ] . strip ( ) ) } <EOL> ) <EOL> self . _tiplocs [ tiploc ] = entity <EOL> def save_journey ( self ) : <EOL> pass <EOL> def _parse_cif_date ( self , cifdate ) : <EOL> y = int ( cifdate [ : <NUM_LIT:2> ] ) <EOL> m = int ( cifdate [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> d = int ( cifdate [ <NUM_LIT:4> : ] ) <EOL> if y < <NUM_LIT> : <EOL> y += <NUM_LIT> <EOL> else : <EOL> y += <NUM_LIT> <EOL> return date ( y , m , d ) <EOL> def _parse_cif_time ( self , ciftime ) : <EOL> if ciftime . strip ( ) == '<STR_LIT>' : <EOL> return None <EOL> h = int ( ciftime [ : <NUM_LIT:2> ] ) <EOL> m = int ( ciftime [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> s = <NUM_LIT:30> if ciftime [ <NUM_LIT:4> ] == '<STR_LIT:H>' else <NUM_LIT:0> <EOL> return time ( h , m , s ) <EOL> def _parse_basic_schedule ( self , line ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> reset_queries ( ) <EOL> with transaction . commit_on_success ( ) : <EOL> self . _save_stops ( self . _save_journey ( self . _save_route ( ) ) ) <EOL> self . _current_journey = { <EOL> '<STR_LIT>' : line [ <NUM_LIT:3> : <NUM_LIT:9> ] , <EOL> '<STR_LIT>' : self . _parse_cif_date ( line [ <NUM_LIT:9> : <NUM_LIT:15> ] ) , <EOL> '<STR_LIT>' : self . _parse_cif_date ( line [ <NUM_LIT:15> : <NUM_LIT> ] ) , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] == '<STR_LIT:1>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : line [ <NUM_LIT> ] not in ( '<STR_LIT:X>' , '<STR_LIT:E>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . _current_route = { <EOL> '<STR_LIT>' : line [ <NUM_LIT:3> : <NUM_LIT:9> ] , <EOL> '<STR_LIT>' : line [ <NUM_LIT:32> : <NUM_LIT> ] <EOL> } <EOL> self . _stops = [ ] <EOL> def _parse_extended_schedule ( self , line ) : <EOL> self . _current_route [ '<STR_LIT>' ] = line [ <NUM_LIT:11> : <NUM_LIT> ] <EOL> def _parse_origin ( self , line ) : <EOL> self . _stops . append ( { <EOL> '<STR_LIT>' : self . _tiplocs [ line [ <NUM_LIT:2> : <NUM_LIT:9> ] . strip ( ) ] , <EOL> '<STR_LIT>' : self . _parse_cif_time ( line [ <NUM_LIT:10> : <NUM_LIT:15> ] ) , <EOL> '<STR_LIT>' : '<STR_LIT:O>' <EOL> } ) <EOL> def _parse_intermediate ( self , line ) : <EOL> sta = self . _parse_cif_time ( line [ <NUM_LIT:10> : <NUM_LIT:15> ] ) <EOL> std = self . _parse_cif_time ( line [ <NUM_LIT:15> : <NUM_LIT:20> ] ) <EOL> if sta is None and std is None : <EOL> std = self . _parse_cif_time ( line [ <NUM_LIT:20> : <NUM_LIT> ] ) <EOL> activity = '<STR_LIT:N>' <EOL> else : <EOL> for i in range ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:2> ) : <EOL> activity = { <EOL> '<STR_LIT:D>' : '<STR_LIT:D>' , <EOL> '<STR_LIT:T>' : '<STR_LIT:B>' , <EOL> '<STR_LIT>' : '<STR_LIT:P>' , <EOL> } . get ( line [ i : i + <NUM_LIT:2> ] . strip ( ) ) <EOL> if activity is not None : <EOL> break <EOL> else : <EOL> activity = '<STR_LIT:N>' <EOL> self . _stops . append ( { <EOL> '<STR_LIT>' : self . _tiplocs [ line [ <NUM_LIT:2> : <NUM_LIT:9> ] . strip ( ) ] , <EOL> '<STR_LIT>' : sta , <EOL> '<STR_LIT>' : std , <EOL> '<STR_LIT>' : activity , <EOL> '<STR_LIT>' : False <EOL> } ) <EOL> def _parse_terminate ( self , line ) : <EOL> self . _stops . append ( { <EOL> '<STR_LIT>' : self . _tiplocs [ line [ <NUM_LIT:2> : <NUM_LIT:9> ] . strip ( ) ] , <EOL> '<STR_LIT>' : self . _parse_cif_time ( line [ <NUM_LIT:10> : <NUM_LIT:15> ] ) , <EOL> '<STR_LIT>' : '<STR_LIT:F>' <EOL> } ) <EOL> def _save_route ( self ) : <EOL> route , created = self . _route_manager . get_or_create ( <EOL> external_ref = self . _current_route [ '<STR_LIT>' ] , <EOL> defaults = self . _current_route <EOL> ) <EOL> if not created : <EOL> for key , value in self . _current_route . items ( ) : <EOL> setattr ( route , key , value ) <EOL> route . save ( ) <EOL> return route <EOL> def _save_journey ( self , route ) : <EOL> self . _current_journey [ '<STR_LIT>' ] = route <EOL> journey , created = self . _journey_manager . get_or_create ( <EOL> external_ref = self . _current_journey [ '<STR_LIT>' ] , <EOL> defaults = self . _current_journey <EOL> ) <EOL> if not created : <EOL> for key , value in self . _current_journey . items ( ) : <EOL> setattr ( journey , key , value ) <EOL> journey . save ( ) <EOL> return journey <EOL> def _save_stops ( self , journey ) : <EOL> journey . scheduledstop_set . all ( ) . delete ( ) <EOL> for order , stop in enumerate ( self . _stops ) : <EOL> stop [ '<STR_LIT>' ] = journey <EOL> stop [ '<STR_LIT>' ] = order <EOL> self . _scheduled_stop_manager . create ( ** stop ) <EOL> def import_from_string ( self , cif ) : <EOL> for line in cif . split ( '<STR_LIT:\n>' ) : <EOL> self . _handle_line ( line ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> with transaction . commit_on_success ( ) : <EOL> self . _save_stops ( self . _save_journey ( self . _save_route ( ) ) ) <EOL> @ task ( run_every = timedelta ( days = <NUM_LIT:7> ) ) <EOL> def import_from_file ( self , ** metadata ) : <EOL> with open ( self . _filename ) as file : <EOL> for line in file : <EOL> self . _handle_line ( line ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> with transaction . commit_on_success ( ) : <EOL> self . _save_stops ( self . _save_journey ( self . _save_route ( ) ) ) <EOL> def _handle_line ( self , line ) : <EOL> if line [ : <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> self . _parse_tiploc ( line ) <EOL> elif line [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> self . _parse_basic_schedule ( line ) <EOL> elif line [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> self . _parse_extended_schedule ( line ) <EOL> elif line [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> self . _parse_origin ( line ) <EOL> elif line [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> self . _parse_intermediate ( line ) <EOL> elif line [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> self . _parse_terminate ( line ) <EOL> @ property <EOL> def source ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _source = self . _source_manager . get_or_create ( <EOL> module_name = __name__ , <EOL> defaults = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ) [ <NUM_LIT:0> ] <EOL> return self . _source <EOL> @ property <EOL> def entity_type ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> category = self . _entity_type_category_manager . get_or_create ( <EOL> name = _ ( '<STR_LIT>' ) ) [ <NUM_LIT:0> ] <EOL> self . _entity_type = self . _entity_type_manager . get_or_create ( <EOL> slug = '<STR_LIT>' , <EOL> category = category , <EOL> defaults = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> } <EOL> } <EOL> ) [ <NUM_LIT:0> ] <EOL> return self . _entity_type </s>
