Skip to content

Commit 9ac06ef

Browse files
committed
Lint
1 parent 3f1205a commit 9ac06ef

File tree

8 files changed

+99
-81
lines changed

8 files changed

+99
-81
lines changed

autoapp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""Create an application instance."""
33
import logging
4+
45
from flask.helpers import get_debug_flag
56

67
from tsa.app import create_app
@@ -10,8 +11,10 @@
1011

1112
app = create_app(CONFIG)
1213

14+
1315
@app.before_first_request
1416
def setup_logging():
17+
"""Set up logging to STDOUT from INFO level up in production environment."""
1518
if not app.debug:
1619
# In production mode, add log handler to sys.stderr.
1720
app.logger.addHandler(logging.StreamHandler())

tsa/analyzer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import json
2-
import uuid
31
from collections import defaultdict
2+
43
from rdflib.namespace import RDF
5-
from tsa.extensions import redis
4+
65

76
class Analyzer(object):
87

tsa/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
from flask import Flask, render_template, g
6+
from flask import Flask, g, render_template
77

88
from tsa import commands, public
99
from tsa.extensions import bcrypt, cache, csrf_protect, db, debug_toolbar, migrate, sentry, webpack

tsa/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
from functools import wraps
77

8-
from flask import request, make_response
8+
from flask import make_response, request
99

1010
from tsa.extensions import cache
1111

tsa/celery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import celery
44
import raven
5-
from raven.contrib.celery import register_signal, register_logger_signal
5+
from raven.contrib.celery import register_logger_signal, register_signal
66

77

88
class Celery(celery.Celery):

tsa/public/views.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# -*- coding: utf-8 -*-
22
"""Public section, including homepage and signup."""
33
import logging
4+
import uuid
5+
46
import redis
57
import rfc3987
68
from atenvironment import environment
7-
from collections import defaultdict
8-
from flask import abort, Blueprint, current_app, jsonify, render_template, request, url_for
9-
from tsa.tasks import hello, system_check, analyze, analyze_upload
9+
from celery import group
10+
from flask import Blueprint, abort, current_app, jsonify, render_template, request
11+
12+
from tsa.tasks import analyze, analyze_upload, hello, system_check
1013

1114
blueprint = Blueprint('public', __name__, static_folder='../static')
1215

@@ -19,7 +22,7 @@ def home():
1922

2023
@blueprint.route('/api/v1/test/base')
2124
def test_basic():
22-
return "Hello world!"
25+
return 'Hello world!'
2326

2427

2528
@blueprint.route('/api/v1/test/job')
@@ -32,7 +35,7 @@ def test_celery():
3235
def test_system():
3336
x = (system_check.s() | hello.si()).delay().get()
3437
log = logging.getLogger(__name__)
35-
log.info(f"System check result: {x!s}")
38+
log.info(f'System check result: {x!s}')
3639
return str(x)
3740

3841

@@ -41,7 +44,7 @@ def api_analyze_iri():
4144
iri = request.args.get('iri', None)
4245
etl = bool(int(request.args.get('etl', 1)))
4346

44-
current_app.logger.info("ETL: " + str(etl))
47+
current_app.logger.info(f'ETL:{etl!s}')
4548

4649
if rfc3987.match(iri):
4750
return jsonify(analyze.delay(iri, etl).get())
@@ -56,7 +59,9 @@ def api_analyze_upload(redis_url):
5659

5760
def read_in_chunks(file_object, chunk_size=1024):
5861
"""Lazy function (generator) to read a file piece by piece.
59-
Default chunk size: 1k."""
62+
63+
Default chunk size: 1k.
64+
"""
6065
while True:
6166
data = file_object.read(chunk_size)
6267
if not data:
@@ -65,7 +70,7 @@ def read_in_chunks(file_object, chunk_size=1024):
6570

6671
keys = []
6772
mimes = []
68-
r = redis.StrictRedis.from_url(redis_url, charset="utf-8", decode_responses=True)
73+
r = redis.StrictRedis.from_url(redis_url, charset='utf-8', decode_responses=True)
6974
for file in request.files:
7075
key = str(uuid.uuid4())
7176
keys.append(key)
@@ -77,13 +82,13 @@ def read_in_chunks(file_object, chunk_size=1024):
7782
g = group(analyze_upload.s(k, m, etl) for k, m in zip(keys, mimes))
7883
return jsonify(g.apply_async().get())
7984

80-
85+
8186
@blueprint.route('/api/v1/query')
8287
@environment('REDIS')
8388
def index(redis_url):
84-
r = redis.StrictRedis.from_url(redis_url, charset="utf-8", decode_responses=True)
89+
r = redis.StrictRedis.from_url(redis_url, charset='utf-8', decode_responses=True)
8590
iri = request.args.get('iri', None)
86-
current_app.logger.info("Querying for: " + iri)
91+
current_app.logger.info(f'Querying for: {iri}')
8792
if rfc3987.match(iri):
8893
if not r.exists(iri):
8994
abort(404)

tsa/tasks.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
"""Celery tasks invoked from the API endpoints."""
22
import json
33
import logging
4+
from urllib.parse import urlparse
5+
46
import rdflib
57
import redis
68
import requests
79
from atenvironment import environment
810
from rdflib import URIRef
9-
from urllib.parse import urlparse
10-
from urllib.error import URLError
11+
1112
from tsa.analyzer import Analyzer
1213
from tsa.celery import celery
1314
from tsa.transformation import PipelineFactory
1415

16+
1517
@celery.task
1618
@environment('ETL', 'VIRTUOSO')
1719
def system_check(etl, virtuoso):
1820
log = logging.getLogger(__name__)
19-
log.info("System check started")
20-
log.info(f"Testing LP-ETL, URL: {etl!s}")
21+
log.info('System check started')
22+
log.info(f'Testing LP-ETL, URL: {etl!s}')
2123
requests.get(etl).raise_for_status()
2224

23-
virtuoso_url = f"{virtuoso!s}/sparql"
24-
log.info(f"Testing virtuoso, URL: {virtuoso_url}")
25+
virtuoso_url = f'{virtuoso!s}/sparql'
26+
log.info(f'Testing virtuoso, URL: {virtuoso_url}')
2527
requests.get(virtuoso_url).raise_for_status()
2628

27-
log.info("System check successful")
29+
log.info('System check successful')
2830

2931

3032
@celery.task
3133
def hello():
32-
return "Hello world!"
34+
return 'Hello world!'
3335

3436

3537
@celery.task
3638
def analyze(iri, etl=True):
3739
log = logging.getLogger(__name__)
38-
log.info(f"Analyzing {iri!s}")
40+
log.info(f'Analyzing {iri!s}')
3941
if etl:
4042
(transform.s(iri) | poll.s() | inspect.s()).apply_async()
4143
else:
@@ -45,17 +47,18 @@ def analyze(iri, etl=True):
4547
r.raise_for_status()
4648
guess = r.headers.get('content-type')
4749
g = rdflib.ConjunctiveGraph()
48-
log.info(f"Guessing format to be {guess!s}")
50+
log.info(f'Guessing format to be {guess!s}')
4951
g.parse(iri, format=guess)
5052
a = Analyzer()
5153
index(g, iri)
5254
return a.analyze(g)
5355

56+
5457
@environment('REDIS')
5558
def index(g, source_iri, redis_cfg):
5659
r = redis.StrictRedis.from_url(redis_cfg)
5760
pipe = r.pipeline()
58-
exp = 60*60 #1H
61+
exp = 60 * 60 # 1H
5962
for (s, p, o) in g:
6063
s = str(s)
6164
p = str(p)
@@ -73,24 +76,24 @@ def index(g, source_iri, redis_cfg):
7376
pipe.expire(source_iri, exp)
7477
pipe.execute()
7578

79+
7680
@celery.task
7781
@environment('REDIS')
7882
def analyze_upload(key, mime, etl, redis_cfg):
7983
log = logging.getLogger(__name__)
8084
r = redis.StrictRedis.from_url(redis_cfg)
81-
if r.strlen(key) < 1024 * 1024: #approx 1MB
85+
if r.strlen(key) < 1024 * 1024: # approx 1MB
8286
g = rdflib.ConjunctiveGraph()
8387
g.parse(data=r.get(key), format=mime)
8488
a = Analyzer()
8589
return a.analyze(g)
8690
else:
87-
log.warn(f"Not analyzing an upload as it's too big: {key!s}")
88-
r.delete(key)
91+
log.warn(f"Not analyzing an upload as it's too big: {key!s}")
92+
r.delete(key)
8993

9094

9195
@celery.task
9296
def inspect(iri):
93-
log = logging.getLogger(__name__)
9497
g = rdflib.ConjunctiveGraph()
9598
g.parse(iri)
9699
a = Analyzer()
@@ -99,81 +102,84 @@ def inspect(iri):
99102

100103
@celery.task
101104
@environment('ETL', 'VIRTUOSO', 'DBA_PASSWORD')
102-
def transform(iri, etl, virtuoso, dbaPass):
105+
def transform(iri, etl, virtuoso, dba_pass):
103106
log = logging.getLogger(__name__)
104-
#create pipeline and call to start executions
107+
# create pipeline and call to start executions
105108
# prepare JSON-LD pipeline
106109

107-
log.info(f"Prepare pipeline for {iri!s}")
110+
log.info(f'Prepare pipeline for {iri!s}')
108111
pf = PipelineFactory()
109112
p = urlparse(virtuoso)
110-
pipeline = json.dumps(pf.createPipeline(iri, {'server': p.hostname, 'port': 1111, 'user': 'dba', 'password': dbaPass, 'iri': iri}))
113+
pipeline = json.dumps(pf.create_pipeline(iri, {'server': p.hostname,
114+
'port': 1111,
115+
'user': 'dba',
116+
'password': dba_pass,
117+
'iri': iri}))
111118

112-
log.info(f"Pipeline:\n{pipeline!s}")
119+
log.info(f'Pipeline:\n{pipeline!s}')
113120

114121
# create the pipeline
115-
r = requests.post(f"{etl!s}/resources/pipelines", files={'pipeline': pipeline})
122+
r = requests.post(f'{etl!s}/resources/pipelines', files={'pipeline': pipeline})
116123
r.raise_for_status()
117124

118125
g = rdflib.ConjunctiveGraph()
119-
g.parse(data=r.text, format="trig")
126+
g.parse(data=r.text, format='trig')
120127

121-
pipeline = g.value(object=URIRef("http://linkedpipes.com/ontology/Pipeline"), predicate=rdflib.namespace.RDF.type)
122-
log.info(f"Pipeline IRI: {pipeline!s}")
128+
pipeline = g.value(object=URIRef('http://linkedpipes.com/ontology/Pipeline'), predicate=rdflib.namespace.RDF.type)
129+
log.info(f'Pipeline IRI: {pipeline!s}')
123130

124131
# POST /resources/executions
125-
r = requests.post(f"{etl!s}/resources/executions?pipeline={pipeline}")
132+
r = requests.post(f'{etl!s}/resources/executions?pipeline={pipeline}')
126133
r.raise_for_status()
127-
log.info(f"Execution trigger result:\n{r.json()!s}")
134+
log.info(f'Execution trigger result:\n{r.json()!s}')
128135
return f"{etl!s}/resources/executions/{r.json()['iri'].split('/')[-1]}"
129136

130137

131-
@celery.task(bind=True, retry_backoff=True, max_retries=None, default_retry_delay=30, time_limit=60*60)
138+
@celery.task(bind=True, retry_backoff=True, max_retries=None, default_retry_delay=30, time_limit=60 * 60)
132139
def poll(self, iri):
133140
def after_return(self, status, retval, task_id, args, kwargs, einfo):
134141
cleanup.apply_async()
135142
self.after_return = after_return
136143

137144
log = logging.getLogger(__name__)
138-
log.info(f"Polling {iri!s}")
145+
log.info(f'Polling {iri!s}')
139146

140-
r = requests.get(iri + "/overview")
147+
r = requests.get(iri + '/overview')
141148
content = r.text
142149
log.info(content)
143150
r.raise_for_status()
144151

145152
j = json.loads(content)
146-
if j['status']['@id'] == "http://etl.linkedpipes.com/resources/status/failed":
147-
log.error("Execution failed")
153+
if j['status']['@id'] == 'http://etl.linkedpipes.com/resources/status/failed':
154+
log.error('Execution failed')
148155

149156
try:
150-
r = requests.get(iri + "/logs")
157+
r = requests.get(iri + '/logs')
151158
r.raise_for_status()
152-
log.error("ETL log:\n" + r.text)
153-
except HTTPError as e:
159+
log.error('ETL log:\n' + r.text)
160+
except requests.HTTPError as e:
154161
raise EtlJobFailed(r) from e
155162

156163
raise EtlJobFailed(r)
157-
elif not (j['status']['@id'] == "http://etl.linkedpipes.com/resources/status/finished"):
158-
log.info("Execution is not finished yet")
164+
elif not (j['status']['@id'] == 'http://etl.linkedpipes.com/resources/status/finished'):
165+
log.info('Execution is not finished yet')
159166
self.retry()
160167
else:
161-
#get result uri
162-
log.info(f"Final graph:\n{str(g)!s}")
163-
result = ""
168+
# get result uri
169+
result = ''
164170
return result
165171

166172

167173
@celery.task
168174
@environment('ETL')
169175
def cleanup(iri, etl):
170176
log = logging.getLogger(__name__)
171-
log.info(f"Deleting {iri!s}")
172-
173-
r = requests.delete(f"{etl!s}/pipelines?iri={iri!s}")
177+
log.info(f'Deleting {iri!s}')
178+
179+
r = requests.delete(f'{etl!s}/pipelines?iri={iri!s}')
174180
r.raise_for_status()
175181

176-
log.info(f"Pipeline {iri!s} deleted")
182+
log.info(f'Pipeline {iri!s} deleted')
177183

178184

179185
class EtlError(Exception):

0 commit comments

Comments
 (0)