-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
258 lines (212 loc) · 8.29 KB
/
utils.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
This module contains utilities for creating and downloading MARC records.
"""
import re
from pymarc import Record, Field, Subfield
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QTextEdit
class LineHighlightingTextEdit(QTextEdit):
"""
A QTextEdit subclass that highlights the entire line where the mouse is clicked.
"""
def __init__(self, parent=None):
super().__init__(parent)
self.setReadOnly(True)
def mousePressEvent(self, event): # pylint: disable=invalid-name
"""
Highlights the line under the cursor when the mouse is pressed.
"""
cursor = self.cursorForPosition(event.pos())
cursor.select(cursor.LineUnderCursor)
self.setTextCursor(cursor)
super().mousePressEvent(event)
def create_marc_record(console_output, data):
"""
Creates a MARC record based on the provided bibliographic information.
:param console_output: QTextEdit for logging output.
:param data: A dictionary containing bibliographic details.
:return: MARC record in binary format.
"""
if console_output:
console_output.append("Creating MARC record...")
record = Record()
# Title (245) and subtitle, ensure 'c' subfield is not included here for author
if data.get('title'):
record.add_field(Field(tag='245', indicators=['1', '0'], subfields=[
Subfield('a', data['title']),
Subfield('b', data['subtitle'])
]))
# Primary Author (100)
if data.get('author'):
record.add_field(Field(tag='100', indicators=['1', ' '], subfields=[
Subfield('a', data['author'])
]))
# Second and third authors (700)
add_author_fields(record, data.get('second_author'), data.get('third_author'))
# Editors (700)
add_editor_fields(record, data.get('editor'), data.get('second_editor'))
# LCCN (010)
add_lccn_field(record, data.get('lccn'))
# ISBNs (020)
add_isbn_fields(record, data.get('isbn'), data.get('second_isbn'))
# LOC Call Number (050)
add_loc_call_number_field(record, data.get('loc_call_number'))
# Publisher and location (264 for publication with indicator 1)
add_publisher_and_copyright_fields(
record,
data.get('publisher_location'),
data.get('publisher'),
data.get('copyright_year')
)
# Edition (250)
if data.get('edition'):
record.add_field(Field(tag='250', indicators=[' ', ' '], subfields=[
Subfield('a', data['edition'])
]))
# Physical description (300)
add_physical_description_field(record, data.get('pages'), data.get('book_height'))
# Bibliographic references (504)
add_references_field(record, data.get('references'), data.get('references_page_range'))
# Index (500)
if data.get('index'):
record.add_field(Field(tag='500', indicators=[' ', ' '], subfields=[
Subfield('a', "Includes index.")
]))
# Summary (520)
if data.get('summary'):
record.add_field(Field(tag='520', indicators=[' ', ' '], subfields=[
Subfield('a', data['summary'])
]))
# LOC Subject Headings (650)
add_loc_subject_headings(
record,
data.get('loc_subject_1'),
data.get('loc_subject_2'),
data.get('loc_subject_3')
)
return record.as_marc()
def add_author_fields(record, second_author, third_author):
"""
Adds secondary and tertiary authors to the MARC record.
"""
if second_author:
record.add_field(Field(tag='700', indicators=['1', ' '], subfields=[
Subfield('a', second_author)
]))
if third_author:
record.add_field(Field(tag='700', indicators=['1', ' '], subfields=[
Subfield('a', third_author)
]))
def add_editor_fields(record, editor, second_editor):
"""
Adds editors to the MARC record.
"""
if editor:
record.add_field(Field(tag='700', indicators=['1', ' '], subfields=[
Subfield('a', editor)
]))
if second_editor:
record.add_field(Field(tag='700', indicators=['1', ' '], subfields=[
Subfield('a', second_editor)
]))
def add_lccn_field(record, lccn):
"""
Adds LCCN to the MARC record.
"""
if lccn:
record.add_field(Field(tag='010', indicators=[' ', ' '], subfields=[
Subfield('a', lccn)
]))
def add_isbn_fields(record, isbn, second_isbn):
"""
Adds ISBNs to the MARC record.
"""
if isbn:
record.add_field(Field(tag='020', indicators=[' ', ' '], subfields=[
Subfield('a', isbn)
]))
if second_isbn:
record.add_field(Field(tag='020', indicators=[' ', ' '], subfields=[
Subfield('a', second_isbn)
]))
def add_loc_call_number_field(record, loc_call_number):
"""
Adds LOC Call Number to the MARC record.
"""
if loc_call_number:
record.add_field(Field(tag='050', indicators=['0', '4'], subfields=[
Subfield('a', loc_call_number)
]))
def add_publisher_and_copyright_fields(record, publisher_location, publisher, copyright_year):
"""
Adds publisher and copyright information to the MARC record.
"""
if publisher or publisher_location or copyright_year:
record.add_field(Field(tag='264', indicators=[' ', '1'], subfields=[
Subfield('a', publisher_location),
Subfield('b', publisher),
Subfield('c', f"{copyright_year}")
]))
if copyright_year:
record.add_field(Field(tag='264', indicators=[' ', '4'], subfields=[
Subfield('c', f"c{copyright_year}.")
]))
def add_physical_description_field(record, pages, book_height):
"""
Adds physical description (pages and book height) to the MARC record.
"""
if pages or book_height:
record.add_field(Field(tag='300', indicators=[' ', ' '], subfields=[
Subfield('a', f"{pages} p."),
Subfield('c', f"{book_height} cm.")
]))
def add_references_field(record, references, references_page_range):
"""
Adds bibliographic references to the MARC record.
"""
if references:
references_text = "Includes bibliographical references"
if references_page_range:
references_text += f" (p. {references_page_range})."
record.add_field(Field(tag='504', indicators=[' ', ' '], subfields=[
Subfield('a', references_text)
]))
def add_loc_subject_headings(record, loc_subject_1, loc_subject_2, loc_subject_3):
"""
Adds LOC Subject Headings to the MARC record.
"""
if loc_subject_1:
record.add_field(Field(tag='650', indicators=[' ', '0'], subfields=[
Subfield('a', loc_subject_1)
]))
if loc_subject_2:
record.add_field(Field(tag='650', indicators=[' ', '0'], subfields=[
Subfield('a', loc_subject_2)
]))
if loc_subject_3:
record.add_field(Field(tag='650', indicators=[' ', '0'], subfields=[
Subfield('a', loc_subject_3)
]))
def clean_filename(filename):
"""Remove any characters that are not alphanumeric, spaces, or underscores."""
return re.sub(r'[^a-zA-Z0-9\s]', '', filename).strip().replace(' ', '_')
def download_marc_record(marc_record, title, author, console_output):
"""
Downloads the MARC record to a file.
"""
default_filename = f"{clean_filename(author)}_{clean_filename(title)}.mrc"
options = QFileDialog.Options()
save_path, _ = QFileDialog.getSaveFileName(
None, "Save MARC Record", default_filename, "MARC Files (*.mrc)", options=options
)
if save_path:
try:
with open(save_path, 'wb') as file:
file.write(marc_record)
console_output.append("MARC Record saved successfully.")
console_output.append(f"Filename: {save_path}")
except IOError as e:
console_output.append(f"File I/O error: {str(e)}")
QMessageBox.critical(
None, "Error",
"An error occurred during MARC record saving. See the log for details."
)