#!/usr/bin/python
#
# This file is part of Archivematica.
#
# Archivematica 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 2 of the License, or
# (at your option) any later version.
#
# Archivematica 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 Archivematica.  If not, see <http://www.gnu.org/licenses/>.

# @package Archivematica
# @subpackage Ingest
# @author Joseph Perry <joseph@artefactual.com>
# @author Peter Van Garderen <peter@artefactual.com>
# @version svn: $Id$

import os
import sys
import lxml.etree as etree

def newChild(parent, tag, text=None, tailText=None):
  child = etree.Element(tag)
  parent.append(child)
  child.text = text
  child.tail = tailText
  return child
      
def loadDublin(root, dublincore):      
#  dtree = etree.parse("/home/demo/ingestLogs/" + sys.argv[2] + "/DublinCore.xml")
#  droot = dtree.getroot()
#  child = newChild(root,  "dmdSec")
  child = etree.Element("dmdSec")
  root.insert(0, child)
  child.set("ID", "SIP-description")
  child = newChild(child, "mdWrap")
  child = newChild(child, "xmlData")
  child.append(dublincore)

if __name__ == '__main__':
  if not os.path.isfile(sys.argv[1]+"/METS.xml"):
    print("Archivematica error - METS.xml doesn't exist")

  tree = etree.parse(sys.argv[1]+"/METS.xml")
  root = tree.getroot()

  dtree = etree.parse(sys.argv[2]+"/dublincore.xml")
  dublincore = dtree.getroot()

  loadDublin(root, dublincore)
  
  tree.write(sys.argv[1]+"/METS.xml")


@@ -792,1044 +792,693 @@
 %0A# @
-author Peter Van Garderen %3Cpeter@artefactual.com%3E%0A# @version svn: $Id$%0A%0Aimport os%0Aimport sys%0Aimport lxml.etree as etree%0A%0Adef newChild(parent, tag, text=None, tailText=None):%0A  child = etree.Element(tag)%0A  parent.append(child)%0A  child.text = text%0A  child.tail = tailText%0A  return child%0A      %0Adef loadDublin(root, dublincore):      %0A#  dtree = etree.parse(%22/home/demo/ingestLogs/%22 + sys.argv%5B2%5D + %22/DublinCore.xml%22)%0A#  droot = dtree.getroot()%0A#  child = newChild(root,  %22dmdSec%22)%0A  child = etree.Element(%22dmdSec%22)%0A  root.insert(0, child)%0A  child.set(%22ID%22, %22SIP-description%22)%0A  child = newChild(child, %22mdWrap%22)%0A  child = newChild(child, %22xmlData%22)%0A  child.append(dublincore)%0A%0Aif __name__ == '__main__':%0A  if not os.path.isfile(sys.argv%5B1%5D+%22/METS.xml%22):%0A    print(%22Archivematica error - METS.xml doesn't exist%22)%0A%0A  tree = etree.parse(sys.argv%5B1%5D+%22/METS.xml%22)%0A  root = tree.getroot()%0A%0A  dtree = etree.parse(sys.argv%5B2%5D+%22/dublincore.xml%22)%0A  dublincore = dtree.getroot()%0A%0A  loadDublin(root, dublincore)%0A  %0A  tree.write(sys.argv%5B1%5D+%22/METS.xml%22)
+version svn: $Id$%0A%0A%0AdctermsNS = %22http://purl.org/dc/terms/%22%0AxsiNS = %22http://www.w3.org/2001/XMLSchema-instance%22%0AmetsNS = %22http://www.loc.gov/METS/%22%0ApremisNS = %22info:lc/xmlns/premis-v2%22%0AdctermsNS = %22http://purl.org/dc/terms/%22%0AfitsNS = %22http://hul.harvard.edu/ois/xml/ns/fits/fits_output%22%0AxlinkNS = %22http://www.w3.org/1999/xlink%22%0A%0A%0AdctermsBNS = %22%7B%22 + dctermsNS + %22%7D%22%0AxsiBNS = %22%7B%22 + xsiNS + %22%7D%22%0AmetsBNS = %22%7B%22 + metsNS + %22%7D%22%0ApremisBNS = %22%7B%22 + premisNS + %22%7D%22%0AdctermsBNS = %22%7B%22 + dctermsNS + %22%7D%22%0AfitsBNS = %22%7B%22 + fitsNS + %22%7D%22%0AxlinkBNS = %22%7B%22 + xlinkNS + %22%7D%22%0A%0ANSMAP = %7B %22dcterms%22 : dctermsNS, %5C%0A%22xsi%22 : xsiNS, %5C%0A%22mets%22 : metsNS, %5C%0A%22premis%22 : premisNS, %5C%0A%22dcterms%22 : dctermsNS, %5C%0A%22xlink%22: xlinkNS %7D
 %0A%0A

Correct namespace