import os
import logging

from ott.utils import file_utils
from ott.utils.cache_base import CacheBase


class Fix(CacheBase):
    """ Diff Two Gtfs Zip Files, looking at feed_info.txt & calendar_date.txt file to see differences between them
    """
    gtfs_name = None
    gtfs_path = None

    def __init__(self, gtfs_name):
        self.gtfs_name = gtfs_name
        self.gtfs_path = os.path.join(self.cache_dir, gtfs_name)

    def cp(self, to_gtfs_path=None):
        ''' copy gtfs file to new file (good for testing, so you don't harm original gtfs.zip)
        '''
        if to_gtfs_path is None:
            to_gtfs_path = self.gtfs_path + '.zip'
        file_utils.cp(self.gtfs_path, to_gtfs_path)
        self.gtfs_path = to_gtfs_path

    def rename_agency_in_routes_txt(self, regex_str, replace_str):
        file_utils.replace_strings_in_zipfile(self.gtfs_path, "routes.txt", regex_str, replace_str)

    def rename_agency_in_agency_txt(self, regex_str, replace_str):
        file_utils.replace_strings_in_zipfile(self.gtfs_path, "agency.txt", regex_str, replace_str)

    @classmethod
    def rename_sam_agency(cls):
        fix = Fix("SAM.zip")
        fix.rename_agency_in_routes_txt("^86", "SAM")
        fix.rename_agency_in_agency_txt("^86", "SAM")

    @classmethod
    def rename_trimet_agency(cls):
        ''' don't rename the agency.txt ... just rename routes (good for OTP) '''
        fix = Fix("TRIMET.zip")
        fix.rename_agency_in_routes_txt("PSC",  "TRIMET")
        fix.rename_agency_in_routes_txt("TRAM", "TRIMET")


def main():
    
    fix = Fix()
    fix.cp()
    fix.rename_agency_in_routes_txt("^101", "SMART")
    fix.rename_agency_in_agency_txt("^101", "SMART")

if __name__ == '__main__':
    main()

@@ -1117,32 +1117,215 @@
 am_agency(cls):%0A
+        ''' rename both routes and agecy .txt files%0A            bin/gtfs_fix SAM.zip -a -r -f %22%5E86%22 -t %22SAM%22%0A            bin/gtfs_fix SMART.zip -a -r -f %22%5E108%22 -t %22SMART%22%0A        '''%0A
         fix = Fi
@@ -1510,20 +1510,19 @@
     ''' 
-don'
+jus
 t rename
@@ -1526,60 +1526,147 @@
 ame 
-the agency.txt ... just rename routes (good for OTP)
+routes.txt (good for OTP alerts) ... don't rename the agency.txt%0A            bin/gtfs_fix TRIMET.zip -r -f %22(PSC%7CTRAM)%22 -t %22TRIMET%22%0A       
  '''
@@ -1739,19 +1739,26 @@
 es_txt(%22
+(
 PSC
+%7CTRAM)
 %22,  %22TRI
@@ -1767,167 +1767,1196 @@
 T%22)%0A
+%0A
     
-    fix.rename_agency_in_routes_txt(%22TRAM%22, %22TRIMET%22)%0A%0A%0Adef main():%0A    %0A    fix = Fix()%0A    fix.cp()%0A    fix.rename_agency_in_routes_txt(%22%5E101%22, %22SMART%22)%0A
+@classmethod%0A    def get_args(cls):%0A        ''' database load command-line arg parser and help util...%0A        '''%0A        import argparse%0A        parser = argparse.ArgumentParser(prog='gtfs-fix', formatter_class=argparse.ArgumentDefaultsHelpFormatter)%0A        parser.add_argument('gtfs', help=%22Name of GTFS zip that is in the 'cache' (e.g., TRIMET.zip)%22)%0A        parser.add_argument('--regex',   '-f', required=True, help=%22string (regex) to find in files%22)%0A        parser.add_argument('--replace', '-t', required=True, help=%22string to replace found regex strings%22)%0A        parser.add_argument('--routes',  '-r', default=True,  action='store_true', help='fix routes.txt')%0A        parser.add_argument('--agency',  '-a', default=False, action='store_true', help='fix agency.txt')%0A        parser.add_argument('--copy',    '-c', default=False, action='store_true', help=%22make a copy of the gtfs.zip (don't edit original%22)%0A        args = parser.parse_args()%0A        return args%0A%0A%0Adef main():%0A    args = Fix.get_args()%0A    fix = Fix(args.gtfs)%0A    if args.copy:%0A        fix.cp()%0A    if args.routes:%0A        fix.rename_agency_in_routes_txt(args.regex, args.replace)%0A    if args.agency:%0A    
     
@@ -2991,23 +2991,32 @@
 txt(
-%22%5E101%22, %22SMART%22
+args.regex, args.replace
 )%0A%0Ai

add cmd line processor