from zipfile import ZipFile

from django.db import models


class Feed(models.Model):
    """Represents a single GTFS feed.

    This data is not part of the General Transit Feed Specification.  It is
    used to allow storage of several GTFS feeds in the same database.
    """
    name = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'feed'
        app_label = 'multigtfs'

    def __unicode__(self):
        if self.name:
            return u"%d %s" % (self.id, self.name)
        else:
            return u"%d" % self.id

    def import_gtfs(self, gtfs_file):
        """Import a GTFS file as feed

        Keyword arguments:
        gtfs_file - A path or file-like object for the GTFS feed

        Returns is a list of objects imported
        """
        from multigtfs.models import (
            Agency, Fare, FareRule, FeedInfo, Frequency, Route, Service,
            ServiceDate, ShapePoint, Stop, StopTime, Transfer, Trip)

        z = ZipFile(gtfs_file, 'r')
        files = z.namelist()

        gtfs_order = (
            ('agency.txt', Agency),
            ('stops.txt', Stop),
            ('routes.txt', Route),
            ('calendar.txt', Service),
            ('calendar_dates.txt', ServiceDate),
            ('shapes.txt', ShapePoint),
            ('trips.txt', Trip),
            ('stop_times.txt', StopTime),
            ('frequencies.txt', Frequency),
            ('fare_attributes.txt', Fare),
            ('fare_rules.txt', FareRule),
            ('transfers.txt', Transfer),
            ('feed_info.txt', FeedInfo),
        )

        for table_name, klass in gtfs_order:
            for f in files:
                if f.endswith(table_name):
                    table = z.open(f)
                    klass.import_txt(table, self)

    def export_gtfs(self, gtfs_file):
        """Export a GTFS file as feed

        Keyword arguments:
        gtfs_file - A path or file-like object for the GTFS feed

        This function will close the file in order to finalize it.
        """
        from multigtfs.models import (
            Agency, Fare, FareRule, FeedInfo, Frequency, Route, Service,
            ServiceDate, ShapePoint, Stop, StopTime, Transfer, Trip)

        z = ZipFile(gtfs_file, 'w')

        gtfs_order = (
            ('agency.txt', Agency),
            ('calendar.txt', Service),
            ('calendar_dates.txt', ServiceDate),
            ('fare_attributes.txt', Fare),
            ('fare_rules.txt', FareRule),
            ('feed_info.txt', FeedInfo),
            ('frequencies.txt', Frequency),
            ('routes.txt', Route),
            ('shapes.txt', ShapePoint),
            ('stop_times.txt', StopTime),
            ('stops.txt', Stop),
            ('transfers.txt', Transfer),
            ('trips.txt', Trip),
        )

        for filename, exporter in gtfs_order:
            path = 'feed/%s' % filename
            content = exporter.objects.in_feed(self).export_txt()
            if content:
                z.writestr(path, content)
        z.close()

@@ -52,16 +52,382 @@
 models%0A%0A
+from agency import Agency%0Afrom fare import Fare%0Afrom fare_rule import FareRule%0Afrom feed_info import FeedInfo%0Afrom frequency import Frequency%0Afrom route import Route%0Afrom service import Service%0Afrom service_date import ServiceDate%0Afrom shape import ShapePoint%0Afrom stop import Stop%0Afrom stop_time import StopTime%0Afrom transfer import Transfer%0Afrom trip import Trip%0A%0A
 %0Aclass F
@@ -1194,190 +1194,8 @@
 %22%22%22%0A
-        from multigtfs.models import (%0A            Agency, Fare, FareRule, FeedInfo, Frequency, Route, Service,%0A            ServiceDate, ShapePoint, Stop, StopTime, Transfer, Trip)%0A%0A
     
@@ -2250,16 +2250,16 @@
 ize it.%0A
+
         
@@ -2266,190 +2266,8 @@
 %22%22%22%0A
-        from multigtfs.models import (%0A            Agency, Fare, FareRule, FeedInfo, Frequency, Route, Service,%0A            ServiceDate, ShapePoint, Stop, StopTime, Transfer, Trip)%0A%0A
     

Move function-level imports