Skip to content

Commit

Permalink
refactor: Pylint cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Jun 22, 2017
1 parent b811675 commit a4e1223
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 163 deletions.
2 changes: 1 addition & 1 deletion lokar/alma.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def bibs(self, mms_id):
bib = Bib(self, response.text)
if bib.mms_id != mms_id:
raise RuntimeError('Response does not contain the requested MMS ID. %s != %s'
% (doc.mms_id, mms_id))
% (bib.mms_id, mms_id))
return bib

def get(self, url, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions lokar/bib.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ def save(self, diff=False, dry_run=False):

def dump(self, filename):
# Dump record to file
with open(filename, 'wb') as f:
f.write(etree.tostring(self.doc, pretty_print=True))
with open(filename, 'wb') as file:
file.write(etree.tostring(self.doc, pretty_print=True))
28 changes: 14 additions & 14 deletions lokar/concept.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from copy import copy, deepcopy

from future.utils import python_2_unicode_compatible
import logging

log = logging.getLogger(__name__)

Expand All @@ -27,33 +28,32 @@ def __init__(self, term, vocabulary, tag='650'):
raise RuntimeError('Strings with more than two components are not supported')

def __copy__(self):
c = Concept(self.term, self.vocabulary, self.tag)
c.sf = copy(self.sf) # to include $0 and any other subfields not part of the term
return c
concept = Concept(self.term, self.vocabulary, self.tag)
concept.sf = copy(self.sf) # to include $0 and any other subfields not part of the term
return concept

def __deepcopy__(self, memodict):
c = Concept(self.term, deepcopy(self.vocabulary), self.tag)
c.sf = copy(self.sf) # to include $0 and any other subfields not part of the term
return c
concept = Concept(self.term, deepcopy(self.vocabulary), self.tag)
concept.sf = copy(self.sf) # to include $0 and any other subfields not part of the term
return concept

@property
def components(self):
return [v for k, v in self.sf.items() if k in ['a', 'b', 'x', 'y', 'z'] and v is not None]
return [value for key, value in self.sf.items() if key in ['a', 'b', 'x', 'y', 'z'] and value is not None]

@property
def term(self):
return ' : '.join(self.components)

def __str__(self):
c = ['${} {}'.format(x, self.sf[x]) for x in ['a', 'x', '0'] if self.sf[x] is not None]
return ' '.join(c)
return ' '.join(['${} {}'.format(key, self.sf[key]) for key in ['a', 'x', '0'] if self.sf[key] is not None])

def authorize(self, skosmos):
c = skosmos.authorize_term(self.term, self.tag)
if c is not None:
cid = c['localname'].strip('c')
concept = skosmos.authorize_term(self.term, self.tag)
if concept is not None:
cid = concept['localname'].strip('c')
self.sf['0'] = self.vocabulary.marc_prefix + cid
log.info('Authorized %s %s', self.tag, self)

def field(self):
return {'tag': self.tag, 'sf': self.sf}
return {'tag': self.tag, 'sf': self.sf}
15 changes: 7 additions & 8 deletions lokar/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@


class Job(object):
def __init__(self, action, source_concept, target_concepts=None, sru=None, alma=None, mailer=None, list_options=None):
def __init__(self, action, source_concept, target_concepts=None, sru=None, alma=None, mailer=None,
list_options=None):
self.dry_run = False
self.interactive = True
self.show_progress = True
Expand Down Expand Up @@ -82,9 +83,7 @@ def generate_steps(self):
self.steps.append(InteractiveReplaceTask(self.source_concept, self.target_concepts))

elif self.action == 'list':
task = ListTask(self.source_concept)
task.set_options(**self.list_options)
self.steps.append(task)
self.steps.append(ListTask(self.source_concept, **self.list_options))

elif self.action == 'rename':
src = self.source_concept
Expand Down Expand Up @@ -156,8 +155,8 @@ def start(self):
if self.alma.name is not None:
log.info('Alma environment: %s', self.alma.name)

for n, step in enumerate(self.steps):
log.info('Step %d of %d: %s', n + 1, len(self.steps), step)
for i, step in enumerate(self.steps):
log.info('Step %d of %d: %s', i + 1, len(self.steps), step)

if self.dry_run:
log.info('Dry run: No catalog records will be touched!')
Expand Down Expand Up @@ -215,9 +214,9 @@ def start(self):
# Del 2: Nå har vi en liste over MMS-IDer for bibliografiske poster vi vil endre.
# Vi går gjennom dem én for én, henter ut posten med Bib-apiet, endrer og poster tilbake.

for n, mms_id in enumerate(valid_records):
for i, mms_id in enumerate(valid_records):
if self.action != 'list':
print(' %3d/%d: %s' % (n + 1, len(valid_records), mms_id))
print(' %3d/%d: %s' % (i + 1, len(valid_records), mms_id))
bib = self.alma.bibs(mms_id)
self.update_record(bib)

Expand Down
75 changes: 36 additions & 39 deletions lokar/lokar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,39 @@
from __future__ import unicode_literals

import argparse
import getpass
import logging.handlers
from io import open
import sys
import re
import getpass

from raven import Client

from email.mime.text import MIMEText
import sys
from email.header import Header
from email.mime.text import MIMEText
from io import open # pylint: disable=redefined-builtin
from subprocess import Popen, PIPE

import requests
import yaml
from raven import Client
from six import binary_type

from . import __version__
from .job import Job
from .concept import Concept
from .alma import Alma
from .concept import Concept
from .job import Job
from .sru import SruClient


logger = logging.getLogger()
logger.setLevel(logging.INFO)
log = logging.getLogger()
log.setLevel(logging.INFO)
logging.getLogger('requests').setLevel(logging.WARNING)
formatter = logging.Formatter('[%(asctime)s %(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%I:%S')

formatter = logging.Formatter('[%(asctime)s %(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%I:%S')
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
log.addHandler(console_handler)

supported_tags = ['084', '648', '650', '651', '655']
SUPPORTED_TAGS = ['084', '648', '650', '651', '655']


class Vocabulary(object):
class Vocabulary(object): # pylint: disable=too-few-public-methods

marc_code = ''
skosmos_code = ''
Expand Down Expand Up @@ -68,8 +65,8 @@ def send_using_sendmail(self, subject, body):
msg['From'] = self.config.get('sender')
msg['To'] = self.config.get('recipient')
msg['Subject'] = Header(subject, 'utf-8')
p = Popen(['sendmail', '-t'], stdin=PIPE)
p.communicate(msg.as_string())
process = Popen(['sendmail', '-t'], stdin=PIPE)
process.communicate(msg.as_string())

def send_using_mailgun(self, subject, body):
request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(self.config['domain'])
Expand All @@ -86,7 +83,7 @@ def parse_args(args, default_env=None):
parser = argparse.ArgumentParser(prog='lokar', description='''
Edit or remove subject fields in Alma catalog records.
Supported fields: {}
'''.format(', '.join(supported_tags)))
'''.format(', '.join(SUPPORTED_TAGS)))
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)

parser.add_argument('-e', '--env', dest='env', nargs='?',
Expand Down Expand Up @@ -150,10 +147,10 @@ def parse_args(args, default_env=None):
if args.new_term2 != '':
args.new_terms.append(args.new_term2)

def normalize_arg(x):
if type(x) == binary_type:
return x.decode('utf-8')
return x
def normalize_arg(arg):
if isinstance(arg, binary_type):
return arg.decode('utf-8')
return arg

args.term = normalize_arg(args.term)
args.env = normalize_arg(args.env)
Expand All @@ -163,15 +160,15 @@ def normalize_arg(x):


def get_concept(term, vocabulary, default_tag='650', default_term=None):
m = re.match('^({})$'.format('|'.join(supported_tags)), term)
if m:
match = re.match('^({})$'.format('|'.join(SUPPORTED_TAGS)), term)
if match:
if default_term is None:
raise RuntimeError('No source term specified')
return Concept(default_term, vocabulary, m.group(1))
return Concept(default_term, vocabulary, match.group(1))

m = re.match('^({}) (.+)$'.format('|'.join(supported_tags)), term)
if m:
return Concept(m.group(2), vocabulary, m.group(1))
match = re.match('^({}) (.+)$'.format('|'.join(SUPPORTED_TAGS)), term)
if match:
return Concept(match.group(2), vocabulary, match.group(1))

return Concept(term, vocabulary, default_tag)

Expand Down Expand Up @@ -214,14 +211,14 @@ def job_args(config=None, args=None):
def main(config=None, args=None):

try:
with config or open('lokar.yml') as f:
config = yaml.load(f)
with config or open('lokar.yml') as file:
config = yaml.load(file)
except IOError:
logger.error('Fant ikke lokar.yml. Se README.md for mer info.')
log.error('Fant ikke lokar.yml. Se README.md for mer info.')
return

username = getpass.getuser()
logger.info('Running as %s', username)
log.info('Running as %s', username)
try:
if config.get('sentry') is not None:
raven = Client(config['sentry']['dsn'])
Expand All @@ -233,13 +230,13 @@ def main(config=None, args=None):
jargs = job_args(config, args)

if args.verbose:
logger.setLevel(logging.DEBUG)
log.setLevel(logging.DEBUG)

if not args.dry_run:
file_handler = logging.FileHandler('lokar.log')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
logger.addHandler(file_handler)
log.addHandler(file_handler)

if args.env is None:
raise RuntimeError('No environment specified in config file')
Expand All @@ -257,12 +254,12 @@ def main(config=None, args=None):
job.show_diffs = args.show_diffs

job.start()
logger.info('{:=^70}'.format(' Job complete '))
log.info('Job complete')

except Exception:
except Exception: # # pylint: disable=broad-except
if config.get('sentry') is not None:
raven.captureException()
logger.exception('Uncaught exception:')
log.exception('Uncaught exception:')


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit a4e1223

Please sign in to comment.