forked from SavinaRoja/OpenAccess_EPUB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdublincore.py
221 lines (197 loc) · 8.92 KB
/
dublincore.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from utils import serializeText
# Currently, the Dublin Core will only be extended by the OPF Spec
# See http://idpf.org/epub/20/spec/OPF_2.0.1_draft.htm
def alreadyExists(dc_term, dc_string, parent):
'''Method to determine if a specified Dublin Core Term already exists in
the metadata which contains the specified string'''
terms = parent.getElementsByTagName(dc_term)
for term in terms:
term_str = serializeText(term, stringlist = [])
if term_str == dc_string:
return True
else:
return False
def dc_identifier(mydoc, parent, artmeta, col_str = False):
'''Create dc:identifer node for OPF'''
newchild = mydoc.createElement('dc:identifier')
if not col_str:
for (_data, _id) in artmeta.identifiers:
if _id == 'doi':
newchild.appendChild(mydoc.createTextNode(_data))
newchild.setAttribute('id', 'PrimaryID')
newchild.setAttribute('opf:scheme', 'DOI') #Extended by OPF
parent.appendChild(newchild)
else:
if not alreadyExists('dc:identifier', col_str, parent):
newchild.appendChild(mydoc.createTextNode(col_str))
newchild.setAttribute('id', 'PrimaryID')
parent.appendChild(newchild)
def dc_title(mydoc, parent, artmeta, title_text = ''):
'''Create dc:title node for OPF'''
newchild = mydoc.createElement('dc:title')
if not title_text:
title_text = serializeText(artmeta.article_title, stringlist = [])
if not alreadyExists('dc:title', title_text, parent):
newchild.appendChild(mydoc.createTextNode(title_text))
parent.appendChild(newchild)
def dc_rights(mydoc, parent, artmeta, copyright_text = ''):
'''Create dc:rights node for OPF'''
newchild = mydoc.createElement('dc:rights')
if not copyright_text:
copyright_text = serializeText(artmeta.art_copyright_statement, stringlist = [])
if not alreadyExists('dc:rights', copyright_text, parent):
newchild.appendChild(mydoc.createTextNode(copyright_text))
parent.appendChild(newchild)
def dc_creator(mydoc, parent, artmeta):
'''Create dc:creator node(s) for OPF'''
for auth in artmeta.art_auths:
newchild = mydoc.createElement('dc:creator')
newchild.appendChild(mydoc.createTextNode(auth.get_name()))
if not alreadyExists('dc:creator', auth.get_name(), parent):
newchild.setAttribute('opf:role', 'aut') #Extended by OPF
newchild.setAttribute('opf:file-as', auth.get_fileas_name()) #Extended by OPF
parent.appendChild(newchild)
def dc_contributor(mydoc, parent, artmeta):
'''Create dc:contributor node(s) for OPF'''
for contr in artmeta.art_edits:
newchild = mydoc.createElement('dc:contributor')
newchild.appendChild(mydoc.createTextNode(contr.get_name()))
if not alreadyExists('dc:contributor', contr.get_name(), parent):
newchild.setAttribute('opf:role', 'edt')
newchild.setAttribute('opf:file-as', contr.get_fileas_name())
parent.appendChild(newchild)
for contr in artmeta.art_other_contrib:
newchild = mydoc.createElement('dc:contributor')
newchild.appendChild(mydoc.createTextNode(contr.get_name()))
newchild.setAttribute('opf:file-as', contr.get_fileas_name())
parent.appendChild(newchild)
def dc_coverage(mydoc, parent, artmeta):
'''Create dc:coverage node for OPF'''
pass #Do nothing with this tag for now
def dc_date(mydoc, parent, artmeta):
'''Create dc:date nodes for OPF'''
try:
accepted = artmeta.history['accepted']
except KeyError:
pass
else:
newchild = mydoc.createElement('dc:date')
datastring = accepted.dateString()
newchild.appendChild(mydoc.createTextNode(datastring))
newchild.setAttribute('opf:event', 'creation') # Extended by OPF
parent.appendChild(newchild)
newchild = mydoc.createElement('dc:date')
datastring = artmeta.art_dates['epub'].dateString()
newchild.appendChild(mydoc.createTextNode(datastring))
newchild.setAttribute('opf:event', 'publication') # Extended by OPF
parent.appendChild(newchild)
try:
ecorrected = artmeta.art_dates['ecorrected']
except KeyError:
pass
else:
newchild = mydoc.createElement('dc:date')
datastring = ecorrected.dateString()
newchild.appendChild(mydoc.createTextNode(datastring))
newchild.setAttribute('opf:event', 'modification') # Extended by OPF
parent.appendChild(newchild)
def dc_description(mydoc, parent, artmeta):
'''Create dc:description node for OPF'''
from utils import serializeText #Recursively extract only TextNode data
newchild = mydoc.createElement('dc:description')
#This lists the abstract types in decreasing preference for use in dc:description
type_hierarchy = ['default', 'ASCII', 'summary', 'web-summary', 'editor',
'short', 'executive-summary']
abstract_node = None
for type in type_hierarchy:
try:
abstract_node = artmeta.abstracts[type]
except KeyError:
pass
else:
break #If we find an appropriate key, break out of the for loop
if abstract_node:
abstract_text = serializeText(abstract_node, stringlist = [])
newchild.appendChild(mydoc.createTextNode(abstract_text))
parent.appendChild(newchild)
def dc_relation(mydoc, parent, artmeta):
'''Create dc:relation node(s) for OPF'''
for related in artmeta.related_articles:
newchild = mydoc.createElement('dc:relation')
newchild.appendChild(mydoc.createTextNode(related))
parent.appendChild(newchild)
def dc_source(mydoc, parent, artmeta):
'''Create dc:source node for OPF'''
pass #Do nothing with this tag for now
def dc_subject(mydoc, parent, artmeta):
'''Create dc:subject node(s) for OPF'''
from utils import serializeText
subj_list = []
try:
subj_list += artmeta.article_categories.subj_groups['Discipline']
except KeyError:
pass
try:
subj_list += artmeta.article_categories.subj_groups['Discipline-v2']
except KeyError:
pass
for subject in subj_list:
newchild = mydoc.createElement('dc:subject')
subject_text = serializeText(subject, stringlist = [])
if not alreadyExists('dc:subject', subject_text, parent):
newchild.appendChild(mydoc.createTextNode(subject_text))
parent.appendChild(newchild)
def dc_format(mydoc, parent):
'''Create dc:format node(s) for OPF'''
newchild = mydoc.createElement('dc:format')
if not alreadyExists('dc:format', 'application/epub+zip', parent):
newchild.appendChild(mydoc.createTextNode('application/epub+zip'))
parent.appendChild(newchild)
def dc_type(mydoc, parent):
'''Creates dc:type node for OPF'''
#Recommended best practice is to use a controlled vocabulary such as:
#http://dublincore.org/documents/dcmi-type-vocabulary/
#This is implemented here:
newchild = mydoc.createElement('dc:type')
type_str = 'text'
if not alreadyExists('dc:type', type_str, parent):
newchild.appendChild(mydoc.createTextNode(type_str))
parent.appendChild(newchild)
#An alternative implementation might utilize the "heading" subject group:
#newchild = mydoc.createElement('dc:type')
#datastring = artmeta.article_categories.subj_groups['heading'][0]
#newchild.appendChild(mydoc.createTextNode(datastring))
#parent.appendChild(newchild)
def dc_language(mydoc, parent):
'''Creates dc:language for OPF'''
newchild = mydoc.createElement('dc:language')
newchild.appendChild(mydoc.createTextNode('en')) # Presume en for now
if not alreadyExists('dc:language', 'en', parent):
parent.appendChild(newchild)
def dc_publisher(mydoc, parent):
'''Creates dc:publisher for OPF'''
pub_str = 'Public Library of Science'
newchild = mydoc.createElement('dc:publisher')
newchild.appendChild(mydoc.createTextNode(pub_str))
if not alreadyExists('dc:publisher', pub_str, parent):
parent.appendChild(newchild)
def generateDCMetadata(mydoc, opfmetanode, artmeta, jrnmeta):
'''The method for generating all the Dublin Core metadata tags
which are to be created in the OPF document under the metadata node.
'''
parent = opfmetanode
dc_identifier(mydoc, parent, artmeta)
dc_title(mydoc, parent, artmeta)
dc_rights(mydoc, parent, artmeta)
dc_creator(mydoc, parent, artmeta)
dc_contributor(mydoc, parent, artmeta)
dc_coverage(mydoc, parent, artmeta)
dc_date(mydoc, parent, artmeta)
dc_description(mydoc, parent, artmeta)
dc_relation(mydoc, parent, artmeta)
dc_source(mydoc, parent, artmeta)
dc_subject(mydoc, parent, artmeta)
dc_format(mydoc, parent) # Format is epub, independent of input
dc_type(mydoc, parent)
dc_language(mydoc, parent)
dc_publisher(mydoc, parent)