From 519a78e49058cc130cea99cef4eb9d19655d4ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= Date: Thu, 28 Sep 2017 19:38:19 +0200 Subject: [PATCH] Refactor main/run methods --- almar/almar.py | 23 +++++++++++++---------- tests/test_almar.py | 25 +++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/almar/almar.py b/almar/almar.py index 77fa99e..a78d81e 100644 --- a/almar/almar.py +++ b/almar/almar.py @@ -342,26 +342,23 @@ def get_config_filename(): return filename -def get_config(config=None): - if config is not None: - return config +def get_config(): filename = get_config_filename() if filename is None: log.error('Could not find "almar.yml" configuration file. See https://github.com/scriptotek/almar for help.') sys.exit(1) - try: - return open(filename) + with open(filename) as fp: + config = yaml.load(fp) except IOError: log.error('Could not read configuration file "%s"', filename) sys.exit(1) + return config -def main(config=None, args=None): - global raven_client - with get_config(config) as fp: - config = yaml.load(fp) +def run(config, argv): + global raven_client username = getpass.getuser() log.info('Running as %s', username) @@ -372,7 +369,7 @@ def main(config=None, args=None): 'username': username }}) - args = parse_args(args or sys.argv[1:], config.get('default_env')) + args = parse_args(argv, config.get('default_env')) if args.verbose: # Do this as early as possible @@ -404,6 +401,8 @@ def get_env(config, args): sru = SruClient(env['sru_url'], args.env) alma = Alma(env['api_region'], env['api_key'], args.env, dry_run=args.dry_run) + log.info('Starting job: %s', ' '.join(argv)) + job = Job(sru=sru, ils=alma, **jargs) job.dry_run = args.dry_run job.interactive = not args.non_interactive @@ -419,5 +418,9 @@ def get_env(config, args): log.exception('Uncaught exception:') +def main(): + run(get_config(), sys.argv[1:]) + + if __name__ == '__main__': main() diff --git a/tests/test_almar.py b/tests/test_almar.py index eb7be0c..0eca5f8 100644 --- a/tests/test_almar.py +++ b/tests/test_almar.py @@ -11,6 +11,7 @@ import pytest import responses import logging +import yaml from mock import Mock, MagicMock, patch, ANY, call from io import BytesIO from io import open @@ -20,7 +21,7 @@ from textwrap import dedent from almar.bib import Bib -from almar.almar import main, job_args, parse_args, log, get_concept +from almar.almar import run, get_config, job_args, parse_args, log, get_concept from almar.authorities import Vocabulary from almar.sru import SruClient, SruErrorResponse, TooManyResults, NSMAP from almar.alma import Alma @@ -823,6 +824,10 @@ def conf(): sru_url: https://sandbox-eu.alma.exlibrisgroup.com/view/sru/DUMMY_SITE ''').encode('utf-8')) + @classmethod + def conf_obj(cls): + return yaml.load(cls.conf()) + @staticmethod def sru_search_mock(*args, **kwargs): recs = get_sample('sru_sample_response_1.xml', True).findall('srw:records/srw:record/srw:recordData/record', NSMAP) @@ -837,7 +842,7 @@ def testMain(self, sru, MockAlma, mock_authorize_term): new_term = 'Test æøå' alma = MockAlma.return_value mock_authorize_term.return_value = {'id': 'REAL030697'} - main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term]) + run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) @@ -851,7 +856,7 @@ def testMainNoHits(self, sru, MockAlma, mock_authorize_term): new_term = 'Test æøå' mock_authorize_term.return_value = {'id': 'REAL030697'} alma = MockAlma.return_value - main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term]) + run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert alma.get_record.call_count == 0 @@ -862,7 +867,7 @@ def testRemoveTerm(self, sru, MockAlma, mock_authorize_term): term = 'Statistiske modeller' mock_authorize_term.return_value = {'id': 'REAL030697'} alma = MockAlma.return_value - main(self.conf(), ['-e test_env', '-n', 'remove', term]) + run(self.conf_obj(), ['-e test_env', '-n', 'remove', term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert alma.get_record.call_count == 14 @@ -879,7 +884,7 @@ def testDiffs(self, sru, MockAlma, mock_authorize_term): bib = Bib(doc) alma.get_record.return_value = bib - main(self.conf(), ['--diffs', '-e test_env', '-n', 'replace', term, new_term]) + run(self.conf_obj(), ['--diffs', '-e test_env', '-n', 'replace', term, new_term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert alma.get_record.call_count == 1 assert alma.put_record.call_count == 1 @@ -896,7 +901,7 @@ def testListCommand(self, sru, MockAlma, mock_authorize_term): bib = Bib(doc) alma.get_record.return_value = bib - main(self.conf(), ['-e test_env', '-n', 'list', term]) + run(self.conf_obj(), ['-e test_env', '-n', 'list', term]) sru.search.assert_called_once_with( 'alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert alma.get_record.call_count == 1 @@ -914,7 +919,7 @@ def testDryRun(self, sru, get_record, mock_authorize_term): doc = get_sample('bib_990705558424702201.xml') get_record.return_value = Bib(doc) - main(self.conf(), ['--dry_run', '-e test_env', '-n', 'replace', term, new_term]) + run(self.conf_obj(), ['--dry_run', '-e test_env', '-n', 'replace', term, new_term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert get_record.call_count == 1 assert len(responses.calls) == 0 @@ -932,7 +937,7 @@ def testCzRecord(self, sru, mock_authorize_term, get_record): bib = Bib(doc) get_record.return_value = bib - main(self.conf(), ['-e test_env', '-n', 'replace', term, new_term]) + run(self.conf_obj(), ['-e test_env', '-n', 'replace', term, new_term]) sru.search.assert_called_once_with('alma.subjects = "%s" AND alma.authority_vocabulary = "%s"' % (term, 'noubomn')) assert bib.cz_link is not None @@ -946,7 +951,7 @@ def testConfigMissing(self, exists, mock_open): mock_open.side_effect = IOError('File not found') with pytest.raises(SystemExit): - main(args=['replace', 'old', 'new']) + get_config() mock_open.assert_called_once_with('./almar.yml') @@ -1111,4 +1116,4 @@ def test_advanced_syntax2(self): assert jargs['target_concepts'][0].sf == {'a': 'Habsburg', 'c': 'slekten', '0': '90200245', '2': 'bare'} if __name__ == '__main__': - unittest.main() + unittest.run()