import sys
import csv

from utils.config_utils import job_config


def strip_columns(row, columns_to_keep):
	for k in row.keys():
		if k not in columns_to_keep:
			del row[k]


def process_gtfs_file(input_file, output_file, columns_to_keep):
	with open( input_file, 'r' ) as in_file:
		with open(output_file, 'w+') as out_file:

			reader = csv.DictReader(in_file)
			writer = csv.DictWriter(out_file, fieldnames=columns_to_keep)

			writer.writeheader()

			for row in reader:
				strip_columns(row, columns_to_keep)
				writer.writerow(row)


def main():
	config, config_file = job_config(sys.argv[1:])

	try:
		to_process = (
			(
				'./gtfs/in/{0}'.format(config['gtfs']['routes']['file']),
				'./gtfs/out/{0}'.format(config['gtfs']['routes']['file']),
				config['gtfs']['routes']['columns'],
			),
			(
				'./gtfs/in/{0}'.format(config['gtfs']['stops']['file']),
				'./gtfs/out/{0}'.format(config['gtfs']['stops']['file']),
				config['gtfs']['stops']['columns'],
			),
			(
				'./gtfs/in/{0}'.format(config['gtfs']['trips']['file']),
				'./gtfs/out/{0}'.format(config['gtfs']['trips']['file']),
				config['gtfs']['trips']['columns'],
			),
			(
				'./gtfs/in/{0}'.format(config['gtfs']['stop_times']['file']),
				'./gtfs/out/{0}'.format(config['gtfs']['stop_times']['file']),
				config['gtfs']['stop_times']['columns'],
			),
		)
	except KeyError:
		sys.exit("Config file '{0}' is not formatted properly".format(config_file))

	for item in to_process:
		process_gtfs_file(item[0], item[1], item[2])


if __name__ == '__main__':
	main()

@@ -59,16 +59,136 @@
 config%0A%0A
+def clean_up_row(row):%0A%09clean_row = %7B%7D%0A%09for k in row.keys():%0A%09%09clean_row%5Bk.strip()%5D = row%5Bk%5D.strip()%0A%09return clean_row%0A%0A
 %0Adef str
@@ -221,16 +221,35 @@
 _keep):%0A
+%09stripped_row = %7B%7D%0A
 %09for k i
@@ -269,20 +269,16 @@
 %0A%09%09if k 
-not 
 in colum
@@ -296,18 +296,53 @@
 %0A%09%09%09
-del row%5Bk%5D
+stripped_row%5Bk%5D = row%5Bk%5D%0A%09return stripped_row
 %0A%0A%0Ad
@@ -645,16 +645,65 @@
 er:%0A%09%09%09%09
+clean_row = clean_up_row(row)%0A%09%09%09%09stripped_row = 
 strip_co
@@ -700,32 +700,38 @@
 = strip_columns(
+clean_
 row, columns_to_
@@ -752,24 +752,33 @@
 er.writerow(
+stripped_
 row)%0A%0A%0Adef m

Clean up extra white space in rows