#!/usr/bin/env python
"""
Parse the holdings from Unicorn generated by the export_unicorn.pl script
and generate data to COPY into a holdings staging table
"""

#    Copyright (C) 2009, Dan Scott
#    Thanks to the Robertson Library, UPEI, for funding these scripts. 

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import UnicornImport as ui

if __name__ == '__main__':
    # Sample data from Laurentian holdings dump
    
    # Flexkey|Library|Home location|Current location|Item type|Barcode|Catalog key|Price|Creation date|Number of charges|Shadowed (1=YES/2=NO)|Call number|Classification
    # i9780195402537|DESMARAIS|DESM-CIR|DESM-CIR|BOOKS|30007000000033  |1|0|20030612|0|0|FC 23 C65|LC|
    
    # Make this accept command-line arguments eventually
    fields_in = ("flexkey", "library", "home_loc", "curr_loc", "item_type", "barcode", "cat_key", "price", "creation_date", "charges", "shadowed", "call_number", "class")
    
    # This is all that the staging table currently wants
    fields_out = ('call_number', 'cat_key', 'creation_date', 'home_loc', 'barcode', 'item_type', 'library')

    date_fields = ('creation_date')
    
    # create our Unicorn reader
    hldg = ui.Parser('holdings_clean.lst', 'holdings_eg.sql', fields_in, fields_out, date_fields)
    
    # print out the header
    hldg.file_out.write('COPY staging_items(callnum, bibkey, createdate, location, barcode, item_type, owning_lib) from STDIN;\n')
    
    hldg.dump_fields()
