forked from SavinaRoja/OpenAccess_EPUB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contributor.py
96 lines (84 loc) · 3.23 KB
/
contributor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""contributor.py"""
from utils import getTagData, serializeText
class Contributor:
"""Represents a contributor to the article.
In practical terms, this generally means either an Author,
or an Editor when it comes to academic journalism.
More may be added as necessary to this class.
"""
def __init__(self, contribnode):
self.surname = ''
self.givenname = ''
self.affiliation = []
self.contact = []
self.footnotes = []
try:
namenode = contribnode.getElementsByTagName('name')[0]
except IndexError:
pass
else:
self.surname = getTagData(namenode.getElementsByTagName('surname'))
try:
self.givenname = getTagData(namenode.getElementsByTagName('given-names'))
except:
pass
try:
collab_node = contribnode.getElementsByTagName('collab')[0]
except IndexError:
self.collab = None
else:
self.collab = collab_node
self.xrefs = contribnode.getElementsByTagName('xref')
for ref in self.xrefs:
reftype = ref.getAttribute('ref-type')
if reftype == 'aff':
self.affiliation.append(ref.getAttribute('rid'))
elif reftype == 'corresp':
self.contact.append(ref.getAttribute('rid'))
elif reftype == 'fn':
self.footnotes.append(ref.getAttribute('rid'))
def __str__(self):
out = 'Surname: {0}, Given Name: {1}, AffiliationID: {2}, Contact: {3}'
return out.format(self.surname, self.givenname,
self.affiliation, self.contact)
def get_surname(self):
'''An alternative to get_name which provides only the surname and an
option for collab'''
if not self.collab:
return(self.surname)
else:
return(serializeText(self.collab, stringlist = []))
def get_name(self):
"""Get the name. Formatted as: Carl Sagan"""
if not self.collab:
return(u'{0} {1}'.format(self.givenname, self.surname))
else:
return(serializeText(self.collab, stringlist = []))
def get_fileas_name(self):
'''Get the name. Formatted as: Sagan C'''
if not self.collab:
names = self.givenname.split(' ')
initials = ''
for name in names:
try:
initials += name[0]
except IndexError:
return(self.surname)
else:
return(u'{0}, {1}'.format(self.surname, initials))
else:
return(serializeText(self.collab, stringlist = []))
class Author(Contributor):
"""Represents an author."""
def __init__(self, contribnode):
Contributor.__init__(self, contribnode)
class Editor(Contributor):
"""Represents an editor."""
def __init__(self, contribnode):
Contributor.__init__(self, contribnode)
try:
self.role = contribnode.getElementsByTagName('role')[0]
except KeyError:
self.role = None
else:
self.role = serializeText(self.role, stringlist = [])