From: Dan Scott Date: Thu, 23 Feb 2012 20:18:52 +0000 (-0500) Subject: Add Boreal to the ebook handling script X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=a01baa1e586461513bff8b85d1143d1794fa2db6;p=contrib%2FConifer.git Add Boreal to the ebook handling script Also simplify some code paths through the power of Python list comprehensions and dictionaries, instead of repetitive code. Signed-off-by: Dan Scott --- diff --git a/tools/ebooks/prep_ebook_records.py b/tools/ebooks/prep_ebook_records.py index eae159896f..9c40d33117 100644 --- a/tools/ebooks/prep_ebook_records.py +++ b/tools/ebooks/prep_ebook_records.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- """ Prepare sets of electronic resource MARC records for loading into Evergreen @@ -37,7 +38,8 @@ class Institution(): "code": "OSBO", \ "ebrary_code": "boreal", \ "proxy": "http://ra.ocls.ca/ra/login.aspx?url=", \ - "link_text": "Disponible en ligne" \ + "link_text": "Disponible en ligne", \ + "access_note": "Accès réservé aux utilisateurs avec un ID valide Collège Boréal ;" \ } self.laurentian = { \ @@ -110,6 +112,8 @@ Required arguments: -A / --algoma: Add an 856 for Algoma University + -B / --boreal: Add an 856 for College Boreal + -L / --laurentian: Add an 856 for Laurentian University -W / --windsor : Add an 856 for University of Windsor @@ -132,35 +136,28 @@ Examples: def consolidate_options(opts): """Make long arguments the standard form in command line options""" + _shortlong = { + '-i': '--input', + '-o': '--output', + '-a': '--authorization', + '-c': '--consortium', + '-e': '--ebrary', + '-p': '--publisher', + '-P': '--platform', + '-n': '--note', + '-A': '--algoma', + '-B': '--boreal', + '-L': '--laurentian', + '-W': '--windsor', + '-s': '--sample', + '-h': '--help' + } + _options = dict(opts) for key, val in opts: - if key == '-i': - _options['--input'] = val - elif key == '-o': - _options['--output'] = val - elif key == '-a': - _options['--authorization'] = val - elif key == '-c': - _options['--consortium'] = val - elif key == '-e': - _options['--ebrary'] = val - elif key == '-p': - _options['--publisher'] = val - elif key == '-P': - _options['--platform'] = val - elif key == '-n': - _options['--note'] = val - elif key == '-A': - _options['--algoma'] = val - elif key == '-L': - _options['--laurentian'] = val - elif key == '-W': - _options['--windsor'] = val - elif key == '-s': - _options['--sample'] = val - elif key == '-h': - _options['--help'] = val + if key in _shortlong: + _options[_shortlong[key]] = val return _options @@ -168,29 +165,21 @@ def check_options(options): """Check the validity of options that were passed in""" _help = False + _req = { + '--input': "* Missing -i / --input argument!", + '--output': "* Missing -o / --output argument!", + '--consortium': "* Missing -c / --consortium argument!", + '--authorization': "* Missing -a / --authorization argument!", + '--publisher': "* Missing -p / --publisher argument!" + } if '--help' in options: do_help() - if '--input' not in options: - print "* Missing -i / --input argument!" - _help = True - - if '--output' not in options: - print "* Missing -o / --output argument!" - _help = True - - if '--consortium' not in options: - print "* Missing -c / --consortium argument!" - _help = True - - if '--authorization' not in options: - print "* Missing -a / --authorization argument!" - _help = True - - if '--publisher' not in options: - print "* Missing -p / --publisher argument!" - _help = True + for reqkey, reqwarn in _req.iteritems(): + if reqkey not in options: + print reqwarn + _help = True _libraries = check_libraries(options) if len(_libraries.keys()) == 0: @@ -254,14 +243,9 @@ def check_libraries(options): """Build a dict of the libraries that were requested for this batch""" _libraries = dict() - if '--algoma' in options: - _libraries['algoma'] = True - - if '--laurentian' in options: - _libraries['laurentian'] = True - - if '--windsor' in options: - _libraries['windsor'] = True + for lib in ['algoma', 'boreal', 'laurentian', 'windsor']: + if '--' + lib in options: + _libraries[lib] = True return _libraries @@ -269,9 +253,9 @@ def check_libraries(options): def parse_opts(): """Get command-line arguments from the script""" try: - _short_opts = 'i:o:a:c:p:ALWen:P:s:h' + _short_opts = 'i:o:a:c:p:ABLWen:P:s:h' _long_opts = ['input=', 'output=', 'authorization=', 'consortium=', - 'publisher=', 'algoma', 'laurentian', 'windsor', 'ebrary', + 'publisher=', 'algoma', 'boreal', 'laurentian', 'windsor', 'ebrary', 'note=', 'platform=', 'sample=', 'help' ] opts = getopt.getopt(sys.argv[1:], _short_opts, _long_opts) @@ -287,10 +271,19 @@ def process_records(options): global RECORD_COUNT sample = '' - reader = pymarc.MARCReader( - open(options['input'], mode='rb'), to_unicode=True - ) - writer = pymarc.MARCWriter(open(options['output'], mode='wb')) + + try: + reader = pymarc.MARCReader( + open(options['input'], mode='rb'), to_unicode=True + ) + except Exception, ex: + print("Could not open input file [%s]" % options['input']) + + try: + writer = pymarc.MARCWriter(open(options['output'], mode='wb')) + except Exception, ex: + print("Could not open output file [%s]" % options['output']) + if ('sample' in options): sample = pymarc.MARCWriter(open(options['sample'], mode='wb')) @@ -899,6 +892,9 @@ def get_subfields(field, data): if ebrary: ebrary_url = re.search(r'^(.+?/lib/).+?(/.+?)$', url) url = ebrary_url.group(1) + data['ebrary_code'] + ebrary_url.group(2) + + # Only Boreal still wants proxied ebrary links + if ebrary and data['ebrary_code'] != 'boreal': subs.extend(['u', url]) else: subs.extend(['u', data['proxy'] + field['u']])