from collections import namedtuple
import logging
import sys

# FIXME ipeds_reporter isn't normally importable
from ipeds_reporter.utils import IpedsCSVReader
from tx_highered.models import (
    Institution, PriceTrends, TestScores, Enrollment, GraduationRates,
)


# This tells the importer how to interpret IPEDS short variables for import
# into a Django model and field
ReportDatum = namedtuple('ReportDatum', ['model', 'field', 'year_type'])
FIELD_MAPPINGS = {
    # Price Trends
    'chg2ay3': ReportDatum(PriceTrends, 'tuition_fees_in_state', 'fall'),
    'chg3ay3': ReportDatum(PriceTrends, 'tuition_fees_outof_state', 'fall'),
    'chg4ay3': ReportDatum(PriceTrends, 'books_and_supplies', 'fall'),
    'chg5ay3': ReportDatum(PriceTrends, 'room_and_board_on_campus', 'fall'),

    # Test Scores
    'SATNUM': ReportDatum(TestScores, 'sat_submitted_number', 'fall'),
    'SATPCT': ReportDatum(TestScores, 'sat_submitted_percent', 'fall'),
    'ACTNUM': ReportDatum(TestScores, 'act_submitted_number', 'fall'),
    'ACTPCT': ReportDatum(TestScores, 'act_submitted_percent', 'fall'),
    'SATVR25': ReportDatum(TestScores, 'sat_verbal_25th_percentile', 'fall'),
    'SATVR75': ReportDatum(TestScores, 'sat_verbal_75th_percentile', 'fall'),
    'SATMT25': ReportDatum(TestScores, 'sat_math_25th_percentile', 'fall'),
    'SATMT75': ReportDatum(TestScores, 'sat_math_75th_percentile', 'fall'),
    'SATWR25': ReportDatum(TestScores, 'sat_writing_25th_percentile', 'fall'),
    'SATWR75': ReportDatum(TestScores, 'sat_writing_75th_percentile', 'fall'),
    'ACTCM25': ReportDatum(TestScores, 'act_composite_25th_percentile', 'fall'),
    'ACTCM75': ReportDatum(TestScores, 'act_composite_75th_percentile', 'fall'),
    'ACTEN25': ReportDatum(TestScores, 'act_english_25th_percentile', 'fall'),
    'ACTEN75': ReportDatum(TestScores, 'act_english_75th_percentile', 'fall'),
    'ACTMT25': ReportDatum(TestScores, 'act_math_25th_percentile', 'fall'),
    'ACTMT75': ReportDatum(TestScores, 'act_math_75th_percentile', 'fall'),
    'ACTWR25': ReportDatum(TestScores, 'act_writing_25th_percentile', 'fall'),
    'ACTWR75': ReportDatum(TestScores, 'act_writing_75th_percentile', 'fall'),

    # Enrollment
    'PctEnrWh': ReportDatum(Enrollment, 'total_percent_white', 'fall'),
    'PctEnrBK': ReportDatum(Enrollment, 'total_percent_black', 'fall'),
    'PctEnrHS': ReportDatum(Enrollment, 'total_percent_hispanic', 'fall'),
    'PctEnrAP': ReportDatum(Enrollment, 'total_percent_asian', 'fall'),
    'PctEnrAN': ReportDatum(Enrollment, 'total_percent_native', 'fall'),
    'PctEnrUn': ReportDatum(Enrollment, 'total_percent_unknown', 'fall'),
    'ENRTOT': ReportDatum(Enrollment, 'total', 'fall'),
    'FTE': ReportDatum(Enrollment, 'fulltime_equivalent', 'fall'),
    'EnrFt': ReportDatum(Enrollment, 'fulltime', 'fall'),
    'EnrPt': ReportDatum(Enrollment, 'parttime', 'fall'),

    # GraduationRates
    'GBA4RTT': ReportDatum(GraduationRates, 'bachelor_4yr', 'aug'),
    'GBA5RTT': ReportDatum(GraduationRates, 'bachelor_5yr', 'aug'),
    'GBA6RTT': ReportDatum(GraduationRates, 'bachelor_6yr', 'aug'),
}


def generic(path):
    """Read the CSV and import into the appropriate model."""
    logger = logging.getLogger(__name__)
    reader = IpedsCSVReader(open(path, 'rb'))
    for row in reader:
        unit_id, ipeds_id = row[0]
        assert unit_id == 'UnitID'
        institution = Institution.objects.get(ipeds_id=ipeds_id)
        for key, value in row[2:]:
            logger.debug(u'{} {}'.format(key, value))
            if key is None or value is '':
                # skip cells with no data, CSVs will give empty strings for
                # missing values
                continue
            try:
                finder = FIELD_MAPPINGS[key.short_name]
            except KeyError:
                logger.error('MISSING: cannot interpret {}'
                    .format(key.short_name))
                continue
            defaults = {
                finder.field: value,
                'year_type': finder.year_type,
            }
            instance, created = finder.model.objects.get_or_create(
                institution=institution, year=key.year,
                defaults=defaults)
            if not created:
                instance.__dict__.update(defaults)
                instance.save()
            logger.debug(u'{} {}'.format(instance, created))


if __name__ == '__main__':
    path = sys.argv[-1]
    generic(path)

@@ -1,12 +1,99 @@
+%22%22%22%0AImport IPEDS Custom Reports.%0A%0AUsage:%0A  ./import_customreport %3Ccsv1%3E %3Ccsv2%3E ...%0A%22%22%22%0A
 from collect
@@ -130,16 +130,26 @@
 logging%0A
+import os%0A
 import s
@@ -4517,14 +4517,19 @@
     
+for 
 path 
-=
+in
  sys
@@ -4538,12 +4538,54 @@
 rgv%5B
--1%5D%0A
+1:%5D:%0A        if os.path.isfile(path):%0A        
     

add ability to process multiple csvs