Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tilgovi committed May 18, 2015
1 parent 4a46873 commit 189d300
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 48 deletions.
6 changes: 3 additions & 3 deletions annotator/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def __init__(self, key):


class User(object):
def __init__(self, id, consumer, is_admin):
self.id = id
def __init__(self, userid, consumer, is_admin):
self.id = userid
self.consumer = consumer
self.is_admin = is_admin

Expand Down Expand Up @@ -108,7 +108,7 @@ def encode_token(token, secret):

def decode_token(token, secret='', ttl=DEFAULT_TTL, verify=True):
try:
if not type(token) is bytes:
if not isinstance(token, bytes):
if six.PY3:
token = bytes(token, 'utf-8')
else:
Expand Down
2 changes: 1 addition & 1 deletion annotator/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _get_all_by_uris(cls, uris):
# index, so ignore this sort instruction if
# 'updated' appears unmapped due to an empty
# index.
'ignore_unmapped': True,}}]}
'ignore_unmapped': True}}]}

res = cls.es.conn.search(index=cls.es.index,
doc_type=cls.__type__,
Expand Down
28 changes: 13 additions & 15 deletions annotator/elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import absolute_import

import csv
import json
import logging
import datetime

Expand All @@ -10,26 +8,26 @@
import elasticsearch
from six import iteritems
from six.moves.urllib.parse import urlparse
from annotator.atoi import atoi

log = logging.getLogger(__name__)

RESULTS_MAX_SIZE = 200
RESULTS_DEFAULT_SIZE = 20


class ElasticSearch(object):
"""
Thin wrapper around an ElasticSearch connection to make connection handling
more convenient.
Settings for the ES host and index name etcetera can still be changed in the
corresponding attributes before the connection (self.conn) is used.
Settings for the ES host and index name etcetera can still be changed in
the corresponding attributes before the connection (self.conn) is used.
"""

def __init__(self,
host = 'http://127.0.0.1:9200',
index = 'annotator',
authorization_enabled = False):
host='http://127.0.0.1:9200',
index='annotator',
authorization_enabled=False):
self.host = host
self.index = index
self.authorization_enabled = authorization_enabled
Expand All @@ -41,7 +39,7 @@ def _connect(self):
parsed = urlparse(host)

connargs = {
'host': parsed.hostname,
'host': parsed.hostname,
}

username = parsed.username
Expand Down Expand Up @@ -89,7 +87,7 @@ class _Model(dict):

@classmethod
def create_all(cls):
log.info("Creating index '%s'." % cls.es.index)
log.info("Creating index '%s'.", cls.es.index)
conn = cls.es.conn
try:
conn.indices.create(cls.es.index)
Expand All @@ -100,7 +98,7 @@ def create_all(cls):
or e.error.startswith('InvalidIndexNameException')):
log.fatal("Failed to create an Elasticsearch index")
raise
log.warn("Index creation failed as index appears to already exist.")
log.warn("Index creation failed: index appears to already exist.")
mapping = cls.get_mapping()
conn.indices.put_mapping(index=cls.es.index,
doc_type=cls.__type__,
Expand Down Expand Up @@ -130,13 +128,13 @@ def drop_all(cls):
# It would be lovely if this were called 'get', but the dict semantics
# already define that method name.
@classmethod
def fetch(cls, id):
def fetch(cls, docid):
doc = cls.es.conn.get(index=cls.es.index,
doc_type=cls.__type__,
ignore=404,
id=id)
id=docid)
if doc.get('found', True):
return cls(doc['_source'], id=id)
return cls(doc['_source'], id=docid)

@classmethod
def _build_query(cls, query=None, offset=None, limit=None, sort=None, order=None):
Expand Down Expand Up @@ -189,7 +187,7 @@ def search_raw(cls, query=None, params=None, raw_result=False):
def count(cls, **kwargs):
"""Like search, but only count the number of matches."""
kwargs.setdefault('params', {})
kwargs['params'].update({'search_type':'count'})
kwargs['params'].update({'search_type': 'count'})
res = cls.search(raw_result=True, **kwargs)
return res['hits']['total']

Expand Down
30 changes: 16 additions & 14 deletions annotator/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
from __future__ import absolute_import

import csv
import json

from elasticsearch.exceptions import TransportError
Expand All @@ -25,6 +26,7 @@

from annotator.atoi import atoi
from annotator.annotation import Annotation
from annotator.elasticsearch import RESULTS_MAX_SIZE

store = Blueprint('store', __name__)

Expand Down Expand Up @@ -90,7 +92,7 @@ def root():
'read': {
'method': 'GET',
'url': url_for('.read_annotation',
id=':id',
docid=':id',
_external=True),
'desc': "Get an existing annotation"
},
Expand All @@ -99,7 +101,7 @@ def root():
'url':
url_for(
'.update_annotation',
id=':id',
docid=':id',
_external=True),
'query': {
'refresh': {
Expand All @@ -113,7 +115,7 @@ def root():
'delete': {
'method': 'DELETE',
'url': url_for('.delete_annotation',
id=':id',
docid=':id',
_external=True),
'desc': "Delete an annotation"
}
Expand Down Expand Up @@ -173,7 +175,7 @@ def create_annotation():
refresh = request.args.get('refresh') != 'false'
annotation.save(refresh=refresh)

location = url_for('.read_annotation', id=annotation['id'])
location = url_for('.read_annotation', docid=annotation['id'])

return jsonify(annotation), 201, {'Location': location}
else:
Expand All @@ -182,9 +184,9 @@ def create_annotation():


# READ
@store.route('/annotations/<id>')
def read_annotation(id):
annotation = g.annotation_class.fetch(id)
@store.route('/annotations/<docid>')
def read_annotation(docid):
annotation = g.annotation_class.fetch(docid)
if not annotation:
return jsonify('Annotation not found!', status=404)

Expand All @@ -196,9 +198,9 @@ def read_annotation(id):


# UPDATE
@store.route('/annotations/<id>', methods=['POST', 'PUT'])
def update_annotation(id):
annotation = g.annotation_class.fetch(id)
@store.route('/annotations/<docid>', methods=['POST', 'PUT'])
def update_annotation(docid):
annotation = g.annotation_class.fetch(docid)
if not annotation:
return jsonify('Annotation not found! No update performed.',
status=404)
Expand All @@ -209,7 +211,7 @@ def update_annotation(id):

if request.json is not None:
updated = _filter_input(request.json, UPDATE_FILTER_FIELDS)
updated['id'] = id # use id from URL, regardless of what arrives in
updated['id'] = docid # use id from URL, regardless of what arrives in
# JSON payload

changing_permissions = (
Expand Down Expand Up @@ -238,9 +240,9 @@ def update_annotation(id):


# DELETE
@store.route('/annotations/<id>', methods=['DELETE'])
def delete_annotation(id):
annotation = g.annotation_class.fetch(id)
@store.route('/annotations/<docid>', methods=['DELETE'])
def delete_annotation(docid):
annotation = g.annotation_class.fetch(docid)

if not annotation:
return jsonify('Annotation not found. No delete performed.',
Expand Down
6 changes: 3 additions & 3 deletions reindex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
import sys
import argparse

from elasticsearch import Elasticsearch
Expand All @@ -12,7 +11,8 @@
WARNING: Documents that are created while reindexing may be lost!
"""

def main(argv):

def main():
argparser = argparse.ArgumentParser(description=description)
argparser.add_argument('old_index', help="Index to read from")
argparser.add_argument('new_index', help="Index to write to")
Expand All @@ -39,4 +39,4 @@ def main(argv):
reindexer.alias(new_index, alias)

if __name__ == '__main__':
main(sys.argv)
main()
21 changes: 12 additions & 9 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@

here = os.path.dirname(__file__)

def main():

def main(argv):
app = Flask(__name__)

cfg_file = 'annotator.cfg'
if len(sys.argv) == 2:
cfg_file = sys.argv[1]
if len(argv) == 2:
cfg_file = argv[1]

cfg_path = os.path.join(here, cfg_file)

try:
app.config.from_pyfile(cfg_path)
except IOError:
print("Could not find config file %s" % cfg_path, file=sys.stderr)
print("Perhaps you need to copy annotator.cfg.example to annotator.cfg", file=sys.stderr)
print("Perhaps copy annotator.cfg.example to annotator.cfg",
file=sys.stderr)
sys.exit(1)

if app.config.get('ELASTICSEARCH_HOST') is not None:
Expand All @@ -69,10 +71,11 @@ def main():
date = time.strftime('%Y-%m-%d')
log.fatal("Elasticsearch index mapping is incorrect! Please "
"reindex it. You can use reindex.py for this, e.g. "
"python reindex.py --host {0} {1} {1}-{2}".format(
es.host,
es.index,
date))
"python reindex.py --host %s %s %s-%s",
es.host,
es.index,
es.index,
date)
raise

@app.before_request
Expand Down Expand Up @@ -108,4 +111,4 @@ def before_request():
app.run(host=host, port=port)

if __name__ == '__main__':
main()
main(sys.argv)
4 changes: 2 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def __init__(self, key='mockconsumer'):
self.ttl = 86400

class MockUser(object):
def __init__(self, id='alice', consumer=None):
self.id = id
def __init__(self, userid='alice', consumer=None):
self.id = userid
self.consumer = MockConsumer(consumer if consumer is not None else 'mockconsumer')
self.is_admin = False

Expand Down
1 change: 0 additions & 1 deletion tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ def test_update_ignore_auth_in_payload(self):
assert_equal(upd['user'], self.user.id, "annotation 'user' field should not be futzable by API")
assert_equal(upd['consumer'], self.user.consumer.key, "annotation 'consumer' field should not be futzable by API")


def test_delete(self):
kwargs = dict(text=u"Bar", id='456')
ann = self._create_annotation(**kwargs)
Expand Down

0 comments on commit 189d300

Please sign in to comment.