Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 9407070

Browse files
author
arnikz
committed
Clean & format code
1 parent eae9c38 commit 9407070

2 files changed

Lines changed: 318 additions & 321 deletions

File tree

fdp-api/python/fdp.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,119 +17,119 @@
1717
#
1818
#
1919
# FAIR Data Point (FDP) exposes the following endpoints (URL paths):
20-
# [ /, /doc, /doc/ ] = Redirect to the API documentation (Swagger UI)
21-
# /fdp = returns FDP metadata
22-
# /catalog/{catalogID} = returns catalog metadata (default: catalog-01)
23-
# /dataset/{datasetID} = returns dataset metadata (default: breedb)
24-
# /distribution/{distributionID} = returns distribution metadata (default: breedb-sparql)
20+
# [ /, /doc, /doc/ ] = Redirects to the API documentation
21+
# /fdp = Returns FDP metadata
22+
# /catalog/{catalogID} = Returns catalog metadata (default: catalog-01)
23+
# /dataset/{datasetID} = Returns dataset metadata (default: breedb)
24+
# /distribution/{distributionID} = Returns distribution metadata
25+
# (default: breedb-sparql)
2526
#
2627
# This services makes use of:
27-
# Data Catalog Vocabulary (DCAT, http://www.w3.org/TR/vocab-dcat/)
28-
# Dublin Core Metadata Terms (DCMI, http://dublincore.org/documents/dcmi-terms/)
29-
# DBpedia (DBPEDIA, http://dbpedia.org/resource/)
28+
# Data Catalog Vocabulary, http://www.w3.org/TR/vocab-dcat/
29+
# Dublin Core Metadata Terms, http://dublincore.org/documents/dcmi-terms/
30+
# DBpedia, http://dbpedia.org/resource/)
3031
#
3132

32-
__author__ = 'Arnold Kuzniar'
33+
__author__ = 'Arnold Kuzniar'
3334
__version__ = '0.5.0'
34-
__status__ = 'beta'
35+
__status__ = 'beta'
3536
__license__ = 'Apache License, Version 2.0'
3637

3738

38-
from bottle import get, run, static_file, redirect, response, request, opt, install
39+
from bottle import get, run, static_file, redirect, response, request, opt, \
40+
install
3941
from metadata import FAIRConfigReader, FAIRGraph, FDPath
4042
from datetime import datetime
4143
from functools import wraps
4244
from logging import getLogger, FileHandler, INFO
4345

4446

45-
doc_dir = 'doc' # Swagger UI files
46-
47-
# log HTTP requests into file in Common Log Format
48-
log_file = 'access.log'
47+
# log HTTP requests
4948
logger = getLogger(__name__)
5049
logger.setLevel(INFO)
51-
fh = FileHandler(log_file)
50+
fh = FileHandler('access.log')
5251
fh.setLevel(INFO)
5352
logger.addHandler(fh)
5453

5554
def logHttpRequests(fn):
55+
"""Log HTTP requests into log file using Common Log Format"""
5656
@wraps(fn)
5757
def _log_to_logger(*args, **kwargs):
5858
request_time = datetime.now().strftime("%d/%b/%Y %H:%M:%S")
59-
logger.info('%s - - [%s] "%s %s %s" %d' % (request.remote_addr,
60-
request_time,
61-
request.method,
62-
request.urlparts.path,
63-
request.get('SERVER_PROTOCOL'),
64-
response.status_code))
59+
logger.info('%s - - [%s] "%s %s %s" %d' % (
60+
request.remote_addr,
61+
request_time,
62+
request.method,
63+
request.urlparts.path,
64+
request.get('SERVER_PROTOCOL'),
65+
response.status_code))
6566
return fn(*args, **kwargs)
6667
return _log_to_logger
6768

6869
install(logHttpRequests)
6970

70-
# populate FAIR metadata from default config file
71+
# populate FAIR metadata from config file
7172
reader = FAIRConfigReader()
7273
scheme = 'http'
7374
host = opt.bind # pass host:[port] through the command-line -b option
74-
base_uri = '%s://%s' % (scheme, host)
75+
base_uri = '{}://{}'.format(scheme, host)
7576
g = FAIRGraph(base_uri)
7677

7778
for triple in reader.getTriples():
78-
g.setMetadata(triple)
79+
g.setMetadata(triple)
7980

8081

81-
# HTTP response: FAIR metadata in RDF and JSON-LD formats
8282
def httpResponse(graph, uri):
83-
accept_header = request.headers.get('Accept')
84-
fmt = 'turtle' # default RDF serialization
85-
mime_types = {
86-
'text/turtle' : 'turtle',
87-
'application/rdf+xml' : 'xml',
88-
'application/ld+json' : 'json-ld',
89-
'application/n-triples' : 'nt'
90-
}
91-
92-
if accept_header in mime_types:
83+
"""HTTP response: FAIR metadata in RDF and JSON-LD formats"""
84+
accept_header = request.headers.get('Accept')
85+
fmt = 'turtle' # default RDF serialization
86+
mime_types = {
87+
'text/turtle' : 'turtle',
88+
'application/rdf+xml' : 'xml',
89+
'application/ld+json' : 'json-ld',
90+
'application/n-triples' : 'nt'
91+
}
92+
93+
if accept_header in mime_types:
9394
fmt = mime_types[accept_header]
9495

95-
serialized_graph = graph.serialize(uri, fmt)
96+
serialized_graph = graph.serialize(uri, fmt)
9697

97-
if serialized_graph is None:
98-
response.status = 404 # web resource not found
99-
return
98+
if serialized_graph is None:
99+
response.status = 404 # web resource not found
100+
return
100101

101-
response.content_type = 'text/plain'
102-
response.set_header('Allow', 'GET')
102+
response.content_type = 'text/plain'
103+
response.set_header('Allow', 'GET')
103104

104-
return serialized_graph
105+
return serialized_graph
105106

106107

107108
# HTTP request handlers
108109
@get(['/', '/doc', '/doc/'])
109110
def defaultPage():
110-
redirect('/doc/index.html')
111+
redirect('/doc/index.html')
111112

112113
@get(FDPath('doc', '<fname:path>'))
113114
def sourceDocFiles(fname):
114-
return static_file(fname, root=doc_dir)
115+
return static_file(fname, root='doc')
115116

116117
@get(FDPath('fdp'))
117118
def getFdpMetadata(graph=g):
118-
return httpResponse(graph, graph.fdpURI())
119+
return httpResponse(graph, graph.fdpURI())
119120

120121
@get(FDPath('cat', '<catalog_id>'))
121122
def getCatalogMetadata(catalog_id, graph=g):
122-
return httpResponse(graph, graph.catURI(catalog_id))
123+
return httpResponse(graph, graph.catURI(catalog_id))
123124

124125
@get(FDPath('dat', '<dataset_id>'))
125126
def getDatasetMetadata(dataset_id, graph=g):
126-
return httpResponse(graph, graph.datURI(dataset_id))
127+
return httpResponse(graph, graph.datURI(dataset_id))
127128

128129
@get(FDPath('dist', '<distribution_id>'))
129130
def getDistributionMetadata(distribution_id, graph=g):
130-
return httpResponse(graph, graph.distURI(distribution_id))
131+
return httpResponse(graph, graph.distURI(distribution_id))
131132

132133

133134
if __name__ == '__main__':
134-
run(server='wsgiref')
135-
135+
run(server='wsgiref')

0 commit comments

Comments
 (0)