#!/usr/bin/pyton
# -*- coding: utf-8 -*-
#
# Series of scripts strung together to perform the first few steps of the conversion to ECHO-XML
# Klaus Thoden, 2011-03-03

import urllib,string, subprocess, sys, os, codecs
from xml.dom import minidom

# Usage scenario: I expect that what you so far have is a directory
# with the texts that are to be transformed and that their format is
# Zahn_1702_EMMTUSN3.txt (Author_Year_ID.txt). This tool resides in
# the place where all the other scripts of the workflow are:
scriptpath="/Users/kthoden/XML-ECHO-SVN/trunk/schema/scripts/workflow/"
# We get the filename from the command line
orig = sys.argv[1]
# thus, the id is (maybe make this more foolproof)
echoid = orig[-12:-4]

# Memorize our path and filename and path
origpath=os.getcwd()
origpath_file="%s/%s" % (origpath, orig)

#Create directory structure. Script breaks if directories are already
#there!
os.makedirs("%s/raw"%orig[:-4])

location_testfile="%s/%s/raw/%s" % (origpath,orig[:-4],orig)
# We need the codecs module to write to that file in utf8. Open file!
testfile = codecs.open(location_testfile, 'w', 'utf8')

def get_metadata():
    """This function gets the index.meta file and parses it. minidom
    seems to be deprecated, and the use of lxml or ElementTree is
    suggested. But otherwise, this module is in MacOS 10.5's standard
    python distribution."""

    repo = "http://content.mpiwg-berlin.mpg.de/mpiwg/online/permanent/library/"
    fullpath = "%s%s/index.meta" % (repo, echoid)

    # Get the file and read it
    usock=urllib.urlopen(fullpath)
    xmldoc = minidom.parse(usock)
    usock.close()

    get_creator = xmldoc.getElementsByTagName('author')[0]
    get_title = xmldoc.getElementsByTagName('title')[0]
    get_date = xmldoc.getElementsByTagName('year')[0]
    get_lang = xmldoc.getElementsByTagName('lang')[0]

    # Here comes the ugly bit
    nodes = get_creator.childNodes
    for node in nodes:
        if node.nodeType == node.TEXT_NODE:
            creator= node.data

    def creator_fun(creator):
        """Parses the creator string for semicolon which is used to
    delimit several creators. Right now, it is hardcoded to '; ', but
    this might not always be the case."""

    # we might have several creators, split by ;
        creators=[]

# the rule is a bit hard-coded at the moment
        if "; " in creator:
            creators=creator.split("; ")
        else:
            testfile.write("dcterms:creator (GND:%s) %s\n" % (gnd_db[creator],creator))
        for i in creators:
            testfile.write("dcterms:creator (GND:%s) %s\n" % (gnd_db[i],i))
# def creator_fun ends here


    nodes = get_title.childNodes
    for node in nodes:
        if node.nodeType == node.TEXT_NODE:
            title= node.data

    nodes = get_date.childNodes
    for node in nodes:
        if node.nodeType == node.TEXT_NODE:
            date=node.data

    nodes = get_lang.childNodes
    for node in nodes:
        if node.nodeType == node.TEXT_NODE:
            lang= node.data

# Database of ISO 639 language codes
    iso_639_db={
"de":"deu", "en":"eng","fr":"fra","it":"ita","la":"lat","zh":"zho-Hant"
}

# Database of known GND. Right now we expect that the creators are
# actually inside this DB, otherwise the script fails. The idea is of
# course to make this more interactive and also to offer a link to dnb
# to verify person and GND.
    gnd_db = {
"Adams, George": "122668766",
"Alberti, Leon Battista": "11850147X",
"Aleotti, Giovanni Battista": "120407604",
"Ampère, André-Marie": "118645048",
"Anonym.": "---",               # Conimbricenses
"Angeli, Stefano": "10001271X",
"Apian, Petrus": "118645455",
"Appolonius Pergaeus": "11864548X",
"Archimedes": "118503863",
"Argyropulus, Johannes": "100948375",
"Aristoteles": "118650130",
"Bacon, Francis": "118505696",
"Baldi, Bernardino": "121976998",
"Barbaro, Daniele Matteo Alvise": "119046024",
"Barrow, Isaac": "118963066",
"Benedetti, Giovanni Battista de": "118859676",
"Bernoulli, Daniel": "118656503",
"Bernstein, Aaron": "119083787",
"Berzelius, Jöns Jakob": "118510185",
"Biancani, Giuseppe": "100044069",
"Bianconi, G. Ludovico": "119477912",
"Bion, Nicolas": "117604917",
"Boskovic, Rudjer Josip": "118662074",
"Bélidor, Bernard Forest de": "118201913",
"Cardano, Geronimo": "11863822X",
"Casati, Paolo": "117647993",
"Cataneo, Girolamo": "129226815",
"Cataneo, Pietro": "119542838",
"Cavalieri, Bonaventura": "118872621",
"Clarke, Samuel": "118669478",
"Clavius, Christoph": "118880632",
"Cleonides": "119374846",
"Commandino, Federico": "119205300",
"Descartes, René": "118524844", # We had a problem here!
"Euclides": "118638955",
"Frontinus, Sextus Iulius": "118536443",
"Galilei, Galileo": "118537229",
"Gallaccini, Teofilo": "102501580",
"Gassendi, Pierre": "118537695",
"Genelli, Hans Christian": "10014117X",
"Ghetaldi, Marino": "123521041",
"Gravesande, Willem Jakob": "118939297",
"Gravesande, Willem Jaco": "118939297",
"Heeschen, Volker": "132144344",
"Heron Alexandrinus": "119197430",
"Huygens, Christiaan": "118639749",
"Leonicus Thomaeus, Nicolaus": "102377138",
"Mersenne, Marin": "118581201",
"Musschenbroek, Petrus van": "118785516",
"Newton, Isaac": "118587544",
"Papin, Denis": "118739220",
"Pappus Alexandrinus": "118789589",
"Perrault, Claude": "118740016",
"Philandrier, Guillaume": "12218243X",
"Politianus, Angelus": "118595512",
"Ricci, Matteo": "118600222",
"Rode, August": "116578920",
"Ryff, Walther Hermann": "118604376",
"Schott, Kaspar": "121882128",
"Specklin, Daniel": "118751808",
"Stevin, Simon": "11875372X",
"Strato Lampsacenus": "118755765",
"Song Yingxing": "118958895",
"Tartaglia, Niccolò": "11862086X",
"Trigault, Nicolas" : "118867784",
"Theophrastus": "118621793",
"Ufano, Diego": "124763685",
"Valla, Giorgio": "124198643",
"Varignon, Pierre": "119062372",
"Vatablo, Francisco": "12435937X",
"Vitruvius Pollio, Marcus": "118627252",
"Vitruvius": "118627252",       # also added this bit
"Vittori, Girolamo": "100661580",
"Viviani, Vincenzio": "100673287",
"Voltaire": "118627813",
"Waring, Edward": "122095553",
"Weber, Ernst Heinrich": "118765817",
"Weber, Wilhelm Eduard": "11862976X",
"Weidler, Johann Friedrich": "100696198",
"Wilkins, John": "118771922",
"Zahn, Johannes": "119369826",
"Zanotti, Francesco Maria": "117592307",
"Zonca, Vittorio": "121797031",
"Zubler, Leonhard": "122149270"
}

    # Start writing!
    testfile.write("metadata:\ndcterms:identifier ECHO:%s.xml\n" % echoid)
    creator_fun(creator)
    testfile.write("dcterms:title (%s) %s\ndcterms:date %s\ndcterms:language %s\ntext (%s) free\n\nhttp://echo.mpiwg-berlin.mpg.de/ECHOdocuView?mode=imagepath&url=/mpiwg/online/permanent/library/%s/pageimg&viewMode=images"%  (lang, title, date, iso_639_db[lang],lang,echoid))
# def get_metadata ends here

def get_pageimg():
    """We get the names of the image files also via http, so it is not
    necessary to mount Foxridge."""
    pageimg = "http://digilib.mpiwg-berlin.mpg.de/digitallibrary/dirInfo-xml.jsp?fn=/permanent/library/%s/pageimg/" % echoid

    # Same as above
    usock=urllib.urlopen(pageimg)
    xmldoc = minidom.parse(usock)
    usock.close()

    files = xmldoc.getElementsByTagName("file")
    testfile.write("\n\npageimg:\n")
 
    pages = []
    for bit in files:
        pageObj = bit.getElementsByTagName("fsname")[0]
        pages.append(pageObj)
 
    for page in pages:
        nodes = page.childNodes
        for node in nodes:
            if node.nodeType == node.TEXT_NODE:
                testfile.write("%s\n" % node.data)
#def get_pageimg ends here

def get_unknown(echoorig,testfile):
    """Opens a subprocess to call WS's Check Unknown characters. We
    put this in, because TextWrangler's output is garbled at the
    moment. """
    # print testfile
    print echoorig
    p = subprocess.Popen(["perl","Filter_2_04_check_unknown_characters.pl",echoorig],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    o,e  = p.communicate()
    # Solution!
    # Next appends the perl scripts out put to our file
    # print type(e)
    # seems to work if we decode this to unicode, solution is from http://farmdev.com/talks/unicode/
    e_uni = e.decode('utf-8')
    # print type(e_uni)
    # print e_uni
    print >> testfile, e_uni
# def get_unknown ends here.

# doing all the work.
# Change to scriptpath to execute the perlscript and its dependencies
os.chdir(scriptpath)

get_metadata()
get_pageimg()
get_unknown(origpath_file,testfile)
# now for the rest of the header
testfile.write("log:\n\nreplacements:\n\nparameters:\n\n")


# Change back to original path
os.chdir(origpath)

# And put the file in.
realfile = open(orig, 'r')
for line in realfile:
    # Same trick as before
    line_uni = line.decode('utf-8')
    # Change CRLF to LF
    juppi= string.replace(line_uni, '\x0D\x0A', '\x0A')
    # And write it into the file
    testfile.write(juppi)
testfile.close()
#The end
