Include record count in 598 $c, conditional . in 506 $e
authorDan Scott <dan@coffeecode.net>
Wed, 20 Jul 2011 14:28:30 +0000 (10:28 -0400)
committerDan Scott <dscott@laurentian.ca>
Tue, 7 May 2013 18:36:26 +0000 (14:36 -0400)
We might as well include the record offset in the 598 so that we can
track which record includes a given problem.

Also, according to Shuzhen, we only want to add a period at the end of
the 506 $e subfield in particular cases (like when it ends in a letter
or number; not with punctuation).

Signed-off-by: Dan Scott <dscott@laurentian.ca>
tools/ebooks/prep_ebook_records.py

index 67f924c..b79452a 100644 (file)
@@ -17,6 +17,8 @@ import os, os.path, sys, getopt, pymarc, pymarc.marc8, re, urllib2
 from datetime import date
 from BeautifulSoup import BeautifulSoup
 
+RECORD_COUNT = 0
+
 class Institution():
     """Defines standard settings for each Conifer institution"""
 
@@ -258,6 +260,7 @@ def parse_opts():
 def process_records(options):
     """Converts raw ebook MARC records to Conifer-ready MARC records"""
 
+    global RECORD_COUNT
     sample = ''
     reader = pymarc.MARCReader(
         open(options['input'], mode='rb'), to_unicode=True
@@ -266,22 +269,21 @@ def process_records(options):
     if ('sample' in options):
         sample = pymarc.MARCWriter(open(options['sample'], mode='wb'))
 
-    cnt = 0
     for record in reader:
-        cnt = cnt + 1
+        RECORD_COUNT += 1
         try:
             if not (record['856'] and record['856']['u']):
-                print("* No 856 for record # %s in file %s"
-                        % (cnt, options['input'])
+                print("* No 856 for record # %d in file %s"
+                        % (RECORD_COUNT, options['input'])
                 )
 
             new_record = process_fields(record, options)
 
             writer.write(new_record)
-            if (sample and ((cnt == 1) or (cnt % 100 == 0))):
+            if (sample and ((RECORD_COUNT == 1) or (RECORD_COUNT % 100 == 0))):
                 sample.write(new_record)
         except Exception, ex:
-            print("* Error processing record %s - %s" % (cnt, ex))
+            print("* Error processing record %d - %s" % (RECORD_COUNT, ex))
 
 def process_fields(record, options):
     """Decide which fields to add, delete, and keep"""
@@ -486,6 +488,11 @@ def add_restriction(new_record, options):
       * $9 - Institutional code to which this note applies
     """
 
+    # Add a period if the authorization ends with a number or letter
+    authnote = options['authorization']
+    if re.match(r'[a-zA-Z0-9]', authnote[-1]):
+        authnote += '.'
+
     for library in options['libraries']:
         libopts = options['settings'].get_settings(library)
         # Add the access restriction note
@@ -494,7 +501,7 @@ def add_restriction(new_record, options):
             subfields = [
                 'a', libopts['access_note'],
                 'b', options['consortium'] + ' ; ',
-                'e', options['authorization'] + '.',
+                'e', authnote,
                 '9', libopts['code']
             ]
         )
@@ -524,13 +531,16 @@ def add_marc_source(record, options):
     Add a 598 field identifying the source MARC file name and processing date
     """
 
+    global RECORD_COUNT
+
     source = os.path.basename(options['input'])
 
     marc_source = pymarc.Field(tag = '598',
         indicators = [' ', ' '],
         subfields = [
             'a', source,
-            'b', date.today().isoformat()
+            'b', date.today().isoformat(),
+            'c', str(RECORD_COUNT)
         ]
     )
     record.add_field(marc_source)