3 # Copyright (C) 2008 Ian Weller <ianweller@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 from xml
.dom
import minidom
21 from csv
import DictWriter
23 def time_addition(time
, addthis
):
24 addthis
= int(addthis
)
25 (hours
, minutes
) = time
.split(':')
27 minutes
= int(minutes
) + addthis
29 minutes
= minutes
- 60
32 hours
= "0" + str(hours
)
36 minutes
= "0" + str(minutes
)
38 minutes
= str(minutes
)
39 return "%s:%s" % (hours
, minutes
)
41 if __name__
== "__main__":
43 trips
.append(["route_id", "service_id", "trip_id", "direction_id",
46 stop_times
.append(["trip_id", "arrival_time", "departure_time", "stop_id",
48 dom
= minidom
.parse("time_table.xml")
49 time_table
= dom
.documentElement
50 for route
in time_table
.getElementsByTagName("route"):
51 route_id
= route
.getAttribute("id")
52 direction_id
= route
.getAttribute("direction")
53 stops
= route
.getElementsByTagName("stops")[0]
54 stops
= stops
.getElementsByTagName("stop")
55 for schedule
in route
.getElementsByTagName("start_times"):
56 service_id
= schedule
.getAttribute("schedule")
57 for time
in schedule
.getElementsByTagName("start"):
58 start_time
= time
.getAttribute("time")
59 block_id
= time
.getAttribute("block")
60 late
= time
.hasAttribute("late")
61 trip_id
= ''.join([route_id
, direction_id
, service_id
,
62 start_time
.replace(':', '')])
63 trips
.append([route_id
, service_id
, trip_id
, direction_id
,
66 trips
.append([route_id
, service_id
+'_EARLY',
67 trip_id
+'_EARLY', direction_id
, block_id
])
70 stop_sequence
= stop_sequence
+ 1
71 stop_id
= stop
.getAttribute("id")
72 if stop
.hasAttribute("time"):
73 time
= stop
.getAttribute("time")
74 arrival_time
= time_addition(start_time
, time
)
75 arrival_time
= arrival_time
+ ":00"
76 departure_time
= arrival_time
78 (arrival_time
, departure_time
) = ('', '')
79 stop_times
.append([trip_id
, arrival_time
, departure_time
,
80 stop_id
, stop_sequence
])
82 stop_times
.append([trip_id
+'_EARLY', arrival_time
,
83 departure_time
, stop_id
,
88 that
["route_id"] = trip
[0]
89 that
["service_id"] = trip
[1]
90 that
["trip_id"] = trip
[2]
91 that
["direction_id"] = trip
[3]
92 that
["block_id"] = trip
[4]
94 trips_f
= file('trips.txt', 'w')
95 trips_w
= DictWriter(trips_f
, trips
[0])
96 trips_w
.writerows(trips_d
)
99 for stop
in stop_times
:
101 that
["trip_id"] = stop
[0]
102 that
["arrival_time"] = stop
[1]
103 that
["departure_time"] = stop
[2]
104 that
["stop_id"] = stop
[3]
105 that
["stop_sequence"] = stop
[4]
107 stops_f
= file('stop_times.txt', 'w')
108 stops_w
= DictWriter(stops_f
, stop_times
[0])
109 stops_w
.writerows(stops_d
)