Skip to content

Commit

Permalink
Merge pull request #27 from wrznr/bibl_short
Browse files Browse the repository at this point in the history
Compile the magic element "bibl"
  • Loading branch information
wrznr authored Jul 31, 2019
2 parents 4a713d4 + ce49e02 commit 4585111
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
10 changes: 7 additions & 3 deletions mets_mods2teiHeader/api/mets.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self):
self.collections = None
self.languages = None
self.extents = None
self.series = None

@classmethod
def read(cls, source):
Expand Down Expand Up @@ -108,8 +109,11 @@ def __spur(self):
# alternatively identify the corresponding dmdSec via <structMap type="Logical" />

#
# main title
self.title = self.tree.xpath("//mets:dmdSec[1]//mods:mods/mods:titleInfo/mods:title", namespaces=ns)[0].text
# main title and manuscript type
title = self.tree.xpath('//mets:structMap[@TYPE="LOGICAL"]/mets:div', namespaces=ns)
if title:
self.title = title[0].get("LABEL")
self.type = title[0].get("TYPE")

#
# sub titles
Expand Down Expand Up @@ -240,7 +244,7 @@ def __spur(self):
collections = self.tree.xpath("//mets:dmdSec[1]//mods:mods/mods:relatedItem[@type='series']", namespaces=ns)
self.collections = []
for collection in collections:
title = collection.xpath("//mods:titleinfo/mods:title", namespaces=ns)
title = collection.xpath("./mods:titleInfo/mods:title", namespaces=ns)
if title:
self.collections.append(title[0].text)

Expand Down
61 changes: 60 additions & 1 deletion mets_mods2teiHeader/api/tei.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def main_title(self):
by the TEI Header.
"""
return self.tree.xpath('//tei:titleStmt/tei:title[@type="main"]', namespaces=ns)[0].text

@property
def publication_level(self):
"""
Returns the level of publication ('monographic' vs. 'analytic')
"""
return self.tree.xpath('//tei:sourceDesc/tei:biblFull/tei:titleStmt/tei:title[@type="main"]', namespaces=ns)[0].get("level")

@property
def subtitles(self):
Expand All @@ -52,7 +59,7 @@ def authors(self):
"""
authors = []
for author in self.tree.xpath('//tei:fileDesc/tei:titleStmt/tei:author', namespaces=ns):
authors.append(" ".join(author.xpath('descendant-or-self::*/text()')))
authors.append(", ".join(author.xpath('descendant-or-self::*/text()')))
return authors

@property
Expand Down Expand Up @@ -119,13 +126,35 @@ def extents(self):
"""
return [extent.text for extent in self.tree.xpath('//tei:msDesc/tei:physDesc/tei:objectDesc/tei:supportDesc/tei:extent', namespaces=ns)]

@property
def collections(self):
"""
Returns information on the collections of the work represented
by the TEI Header.
"""
return [collection.text for collection in self.tree.xpath('//tei:profileDesc/tei:creation', namespaces=ns)]

@property
def bibl(self):
"""
Returns the short citation of the work represented
by the TEI Header.
"""
return self.tree.xpath("//tei:fileDesc/tei:sourceDesc/tei:bibl", namespaces=ns)[0]

def set_main_title(self, string):
"""
Sets the main title of the title statements.
"""
for main_title in self.tree.xpath('//tei:titleStmt/tei:title[@type="main"]', namespaces=ns):
main_title.text = string

def set_publication_level(self, level):
"""
Sets the level of publication ('monographic' vs. 'analytic')
"""
self.tree.xpath('//tei:sourceDesc/tei:biblFull/tei:titleStmt/tei:title[@type="main"]', namespaces=ns)[0].set("level", level)

def add_sub_title(self, string):
"""
Adds a sub title to the title statements.
Expand Down Expand Up @@ -322,3 +351,33 @@ def add_extent(self, extent):
support_desc = phys_desc.xpath('/tei:objectDesc/tei:supportDesc', namespaces=ns)[0]
extent_elem = etree.SubElement(support_desc, "%sextent" % TEI)
extent_elem.text = extent

def add_collection(self, collection):
"""
Adds a (free-text) collection of the digital document
"""
profile_desc = self.tree.xpath('//tei:profileDesc', namespaces=ns)[0]
creation = etree.SubElement(profile_desc, "%screation" % TEI)
creation.text = collection

def compile_bibl(self):
"""
Compile the content of the short citation element 'bibl' based on the current state
"""
if self.publication_level:
self.bibl.set("type", self.publication_level)
bibl_text = ""
if self.authors:
bibl_text += "; ".join(self.authors) + ": "
elif self.publication_level == "monograph":
bibl_text = "[N. N.], "
bibl_text += self.main_title + "."
if self.places:
bibl_text += " " + self.places[0].split(":")[1]
if len(self.places) > 1:
bibl_text += "u. a."
if self.dates:
if self.places:
bibl_text += ","
bibl_text += " " + self.dates[0] + "."
self.bibl.text = bibl_text
3 changes: 0 additions & 3 deletions mets_mods2teiHeader/data/tei_skeleton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
<profileDesc>
<langUsage>
</langUsage>
<textClass>
<classCode scheme="http://www.deutschestextarchiv.de/doku/klassifikation#DTACorpus">dtae</classCode>
</textClass>
</profileDesc>
</teiHeader>
<text>
Expand Down
10 changes: 10 additions & 0 deletions mets_mods2teiHeader/scripts/mets_mods2teiHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def cli(mets):
# main title
tei.set_main_title(mets.get_main_title())

# publication level
tei.set_publication_level(mets.type)

# sub titles
for sub_title in mets.get_sub_titles():
tei.add_sub_title(sub_title)
Expand Down Expand Up @@ -101,6 +104,13 @@ def cli(mets):
for extent in mets.extents:
tei.add_extent(extent)

# collection
for collection in mets.collections:
tei.add_collection(collection)

# citation
tei.compile_bibl()

click.echo(tei.tostring())


Expand Down
3 changes: 3 additions & 0 deletions tests/test_mets.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ def test_data_assignment(subtests, datadir):

with subtests.test("Check manuscript extent"):
assert(mets.extents == ['[8] Bl., 783 S., [1] Bl.'])

with subtests.test("Check collections"):
assert(mets.collections == ['Drucke des 18. Jahrhunderts', 'Saxonica'])
16 changes: 14 additions & 2 deletions tests/test_tei.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def test_data_assignment(subtests):
tei.set_main_title("Testbuch")
assert(tei.main_title == "Testbuch")

with subtests.test("Check publication level"):
tei.set_publication_level("m")
assert(tei.publication_level == "m")

with subtests.test("Check first subtitle"):
tei.add_sub_title("Untertitel 1")
assert(tei.subtitles == ["Untertitel 1"])
Expand All @@ -33,11 +37,11 @@ def test_data_assignment(subtests):

with subtests.test("Check first author"):
tei.add_author({'family': 'Mustermann', 'given': 'Max', 'date': '12.10.1956', 'title': 'Dr.'}, "personal")
assert(tei.authors == ["Mustermann Max Dr."])
assert(tei.authors == ["Mustermann, Max, Dr."])

with subtests.test("Check further author (organisation)"):
tei.add_author({'family': 'Mustermann', 'given': 'Max', 'date': '12.10.1956', 'title': 'Dr.'}, "corporate")
assert(tei.authors == ["Mustermann Max Dr.", "Mustermann Max 12.10.1956 Dr."])
assert(tei.authors == ["Mustermann, Max, Dr.", "Mustermann Max 12.10.1956 Dr."])

with subtests.test("Check date(s)"):
tei.add_date({"from": "01.01.1823", "to": "25.01.1823"})
Expand Down Expand Up @@ -74,3 +78,11 @@ def test_data_assignment(subtests):
with subtests.test("Check further extent"):
tei.add_extent("5 Abb.")
assert(tei.extents == ["32 S.", "5 Abb."])

with subtests.test("Check collections"):
tei.add_collection("LDP")
assert(tei.collections == ["LDP"])

with subtests.test("Check bibl"):
tei.compile_bibl()
assert(tei.bibl.text == "Mustermann, Max, Dr.; Mustermann Max 12.10.1956 Dr.: Testbuch. Dresden, 01.01.1823.")

0 comments on commit 4585111

Please sign in to comment.