Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module for exporting the data back out to csv #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pygtfs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .loader import append_feed, delete_feed, overwrite_feed, list_feeds
from .schedule import Schedule
from .exporter import export_feed, export_shapefile

__version__ = "0.1.2"
__version__ = "0.1.2.1"
59 changes: 59 additions & 0 deletions pygtfs/exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from __future__ import division, absolute_import, print_function, unicode_literals

from .schedule import Schedule
from .gtfs_entities import gtfs_all
#from .gtfs_entities import Agency, Stop, Route, Trip, StopTime, Service, ServiceException, Fare, FareRule, ShapePoint, Frequency, Transfer
import os
import csv
from zipfile import ZipFile

def export_feed(schedule, save_path, filename, zipped=False):
"""Export feeds either to a directory as a series of text files or to a
zip file

:param schedule: The schedule object
:type schedule: Schedule
:param save_path: The path where the directory or zip file should be created
:type save_path: str
:param filename: The name of the directory or zip file to be created
:type filename: str
:param zipped: Export as a zipped file? (default=False)
:type zipped: bool import
"""

# Build the full path
file_path = os.path.join(save_path,filename)
if zipped: file_path += ".zip"
verify_path(file_path)

# Create the dir
os.mkdir(file_path)

# Export each table
for gtfs_class in gtfs_all:
gtfs_filename = os.path.join(file_path, gtfs_class.__tablename__ + '.txt')
print('Exporting %s' % gtfs_filename)
fh = open(gtfs_filename, 'wb')
outFile = csv.writer(fh)
outFile.writerow("table headers here")
outFile.writerow("table data here")
#outFile.writerow(gtfs_class)
fh.close()


def export_shapefile(save_path, filename, trip_id):
"""Future enhancement to export a trip shapefile from a feed if the feed
has a shapes.txt file
:param save_path: The path where the shapefile should be created
:type save_path: str
:param filename: The name of the shapefile to be created
:type filename: str
:param trip_id: The trip ID to be exported
:type trip_id: str
"""
pass

def verify_path(path):
if not os.path.isdir(os.path.dirname(path)):
raise IOError('%s is not a valid directory' % os.path.dirname(path))
if os.path.isdir(path) or os.path.isfile(path): raise IOError('%s already exists' % path)
2 changes: 1 addition & 1 deletion pygtfs/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, db_connection):

def drop_feed(self, feed_id):
""" Delete a feed from a database by feed id"""
# the following does not cascade unfortunatly.
# the following does not cascade unfortunately.
#self.session.query(Feed).filter(Feed.feed_id == feed_id).delete()
feed = self.session.query(Feed).get(feed_id)
self.session.delete(feed)
Expand Down