"""
Import USHMM's 
"""

import os
import re
import sys
import datetime
from lxml import etree

import babel

from django.db import transaction
from django.db.models import signals
from django.core import management
from django.core.management.base import BaseCommand, CommandError

from portal import models

def gs(doc, attr):
    try: return doc.xpath(".//field[@name='" + attr + "']")[0].text
    except IndexError: pass

def gm(doc, attr):
    return [n.text for n in doc.xpath(".//field[@name='" + attr + "']")]

def stripdot(text):
    if text and text[-1] == ".":
        return text[:-1]
    return text

class Command(BaseCommand):

    def handle(self, *args, **kwargs):
        """Run scrape."""
        # input a file containing urls to scrap
        if not len(args) == 1:
            raise CommandError("No input url file given.")

        # get a reverse dict of language-name -> code
        self.langcodes = dict([(v, k) for k, v in \
                babel.Locale("en").languages.iteritems()])

        # lookup the repository
        self.repo = models.Repository.objects.filter(othername__name="USHMM")[0]
        count = 0

        # disconnect Haystack realtime signals
        signals.pre_save.disconnect(dispatch_uid="setup_index_signals")
        signals.pre_delete.disconnect(dispatch_uid="setup_index_signals")

        starttime = datetime.datetime.now()

        with transaction.commit_on_success():
            #sys.stderr.write("Clearing current collections...\n")
            #self.repo.collection_set.all().delete()
            with file(args[0], "r") as infile:
                for _, element in etree.iterparse(infile, tag="doc", strip_cdata=False):
                    for rectype in element.xpath(".//field[text()='Archives Collection ISAD(G)']"):
                        # for the time being, only collection-level items
                        leveldesc = gs(element, "level_desc")
                        if leveldesc and leveldesc == "Collection":
                            count += 1
                            self.import_item(element, count)

        # update the haystack index for Collections
        management.call_command("update_index", "portal.Collection", "portal.Authority",
                start_date=starttime.isoformat(), remove=True)

    def get_data(self, doc):
        """Extract XML doc node to a dictionary."""
        data = {}