# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
import re
import xml.etree.ElementTree as ET

from .marcdata import MARCData

class ParseMarcXml:

    xmlns = "http://www.loc.gov/MARC21/slim"

    def __call__ (self, xml):
        if xml is None:
            return MARCData()

        if not isinstance(xml, ET.Element):
            try:
                xml = ET.fromstring(xml)

            except ET.ParseError:
                return MARCData()

        marc = { }

        marc["bib"] = self.__text_or_null(
                xml.find(".//{{{}}}controlfield[@tag='001']".format(self.xmlns)))

        marc["callno"] = self.__text_or_null(
                xml.find(".//{{{xmlns}}}datafield[@tag='MDP']/" \
                         "{{{xmlns}}}subfield[@code='h']".format(
                                xmlns=self.xmlns)))

        marc["title"] = self.__text_or_null(
                xml.find(".//{{{xmlns}}}datafield[@tag='245']/" \
                         "{{{xmlns}}}subfield[@code='a']".format(
                                xmlns=self.xmlns)))

        oclcs = xml.findall(".//{{{xmlns}}}datafield[@tag='035']/" \
                         "{{{xmlns}}}subfield[@code='a']".format(
                                xmlns=self.xmlns))

        for maybe in oclcs:
            match = re.match(r"^\(OCoLC\).*?([0-9]+)$", maybe.text)
            if match:
                marc["oclc"] = match.group(1)
                break

        years = xml.find(".//{{{}}}controlfield[@tag='008']".format(self.xmlns))

        if years is not None:
            year1, year2 = years.text[7:11], years.text[11:15]
            if year1 == "^^^^": year1 = None
            if year2 == "^^^^": year2 = None
            marc["years"] = (year1, year2)

        return MARCData(**marc)

    def __text_or_null (self, obj):
        return getattr(obj, "text", None)

get_marc_data_from_xml = ParseMarcXml()

@@ -592,25 +592,24 @@
 c = %7B %7D%0A
-%0A
         
 marc%5B%22bi
@@ -604,123 +604,71 @@
     
-marc%5B%22bib%22%5D = self.__text_or_null(%0A                xml.find(%22.//%7B%7B%7B%7D%7D%7Dcontrolfield%5B@tag='001'%5D%22.format(self.xmlns))
+self.xml = xml%0A%0A        marc%5B%22bib%22%5D = self.__controlfield(%22001%22
 )%0A%0A 
@@ -720,32 +720,37 @@
                 
+self.
 xml.find(%22.//%7B%7B%7B
@@ -963,16 +963,21 @@
         
+self.
 xml.find
@@ -1149,24 +1149,29 @@
     oclcs = 
+self.
 xml.findall(
@@ -1536,71 +1536,33 @@
 s = 
-xml.find(%22.//%7B%7B%7B%7D%7D%7Dcontrolfield%5B@tag='008'%5D%22.format(self.xmlns)
+self.__controlfield(%22008%22
 )%0A%0A 
@@ -1622,21 +1622,16 @@
  = years
-.text
 %5B7:11%5D, 
@@ -1635,21 +1635,16 @@
 %5D, years
-.text
 %5B11:15%5D%0A
@@ -1810,16 +1810,205 @@
 *marc)%0A%0A
+    def __controlfield (self, tag):%0A        return self.__text_or_null(%0A                self.xml.find(%22.//%7B%7B%7B%7D%7D%7Dcontrolfield%5B@tag='%7B%7D'%5D%22.format(%0A                        self.xmlns, tag)))%0A%0A
     def 

Refactor into __controlfield method