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

Commit

Permalink
Clean & format code
Browse files Browse the repository at this point in the history
  • Loading branch information
arnikz committed Dec 5, 2017
1 parent eae9c38 commit 9407070
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 321 deletions.
102 changes: 51 additions & 51 deletions fdp-api/python/fdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,119 +17,119 @@
#
#
# FAIR Data Point (FDP) exposes the following endpoints (URL paths):
# [ /, /doc, /doc/ ] = Redirect to the API documentation (Swagger UI)
# /fdp = returns FDP metadata
# /catalog/{catalogID} = returns catalog metadata (default: catalog-01)
# /dataset/{datasetID} = returns dataset metadata (default: breedb)
# /distribution/{distributionID} = returns distribution metadata (default: breedb-sparql)
# [ /, /doc, /doc/ ] = Redirects to the API documentation
# /fdp = Returns FDP metadata
# /catalog/{catalogID} = Returns catalog metadata (default: catalog-01)
# /dataset/{datasetID} = Returns dataset metadata (default: breedb)
# /distribution/{distributionID} = Returns distribution metadata
# (default: breedb-sparql)
#
# This services makes use of:
# Data Catalog Vocabulary (DCAT, http://www.w3.org/TR/vocab-dcat/)
# Dublin Core Metadata Terms (DCMI, http://dublincore.org/documents/dcmi-terms/)
# DBpedia (DBPEDIA, http://dbpedia.org/resource/)
# Data Catalog Vocabulary, http://www.w3.org/TR/vocab-dcat/
# Dublin Core Metadata Terms, http://dublincore.org/documents/dcmi-terms/
# DBpedia, http://dbpedia.org/resource/)
#

__author__ = 'Arnold Kuzniar'
__author__ = 'Arnold Kuzniar'
__version__ = '0.5.0'
__status__ = 'beta'
__status__ = 'beta'
__license__ = 'Apache License, Version 2.0'


from bottle import get, run, static_file, redirect, response, request, opt, install
from bottle import get, run, static_file, redirect, response, request, opt, \
install
from metadata import FAIRConfigReader, FAIRGraph, FDPath
from datetime import datetime
from functools import wraps
from logging import getLogger, FileHandler, INFO


doc_dir = 'doc' # Swagger UI files

# log HTTP requests into file in Common Log Format
log_file = 'access.log'
# log HTTP requests
logger = getLogger(__name__)
logger.setLevel(INFO)
fh = FileHandler(log_file)
fh = FileHandler('access.log')
fh.setLevel(INFO)
logger.addHandler(fh)

def logHttpRequests(fn):
"""Log HTTP requests into log file using Common Log Format"""
@wraps(fn)
def _log_to_logger(*args, **kwargs):
request_time = datetime.now().strftime("%d/%b/%Y %H:%M:%S")
logger.info('%s - - [%s] "%s %s %s" %d' % (request.remote_addr,
request_time,
request.method,
request.urlparts.path,
request.get('SERVER_PROTOCOL'),
response.status_code))
logger.info('%s - - [%s] "%s %s %s" %d' % (
request.remote_addr,
request_time,
request.method,
request.urlparts.path,
request.get('SERVER_PROTOCOL'),
response.status_code))
return fn(*args, **kwargs)
return _log_to_logger

install(logHttpRequests)

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

for triple in reader.getTriples():
g.setMetadata(triple)
g.setMetadata(triple)


# HTTP response: FAIR metadata in RDF and JSON-LD formats
def httpResponse(graph, uri):
accept_header = request.headers.get('Accept')
fmt = 'turtle' # default RDF serialization
mime_types = {
'text/turtle' : 'turtle',
'application/rdf+xml' : 'xml',
'application/ld+json' : 'json-ld',
'application/n-triples' : 'nt'
}

if accept_header in mime_types:
"""HTTP response: FAIR metadata in RDF and JSON-LD formats"""
accept_header = request.headers.get('Accept')
fmt = 'turtle' # default RDF serialization
mime_types = {
'text/turtle' : 'turtle',
'application/rdf+xml' : 'xml',
'application/ld+json' : 'json-ld',
'application/n-triples' : 'nt'
}

if accept_header in mime_types:
fmt = mime_types[accept_header]

serialized_graph = graph.serialize(uri, fmt)
serialized_graph = graph.serialize(uri, fmt)

if serialized_graph is None:
response.status = 404 # web resource not found
return
if serialized_graph is None:
response.status = 404 # web resource not found
return

response.content_type = 'text/plain'
response.set_header('Allow', 'GET')
response.content_type = 'text/plain'
response.set_header('Allow', 'GET')

return serialized_graph
return serialized_graph


# HTTP request handlers
@get(['/', '/doc', '/doc/'])
def defaultPage():
redirect('/doc/index.html')
redirect('/doc/index.html')

@get(FDPath('doc', '<fname:path>'))
def sourceDocFiles(fname):
return static_file(fname, root=doc_dir)
return static_file(fname, root='doc')

@get(FDPath('fdp'))
def getFdpMetadata(graph=g):
return httpResponse(graph, graph.fdpURI())
return httpResponse(graph, graph.fdpURI())

@get(FDPath('cat', '<catalog_id>'))
def getCatalogMetadata(catalog_id, graph=g):
return httpResponse(graph, graph.catURI(catalog_id))
return httpResponse(graph, graph.catURI(catalog_id))

@get(FDPath('dat', '<dataset_id>'))
def getDatasetMetadata(dataset_id, graph=g):
return httpResponse(graph, graph.datURI(dataset_id))
return httpResponse(graph, graph.datURI(dataset_id))

@get(FDPath('dist', '<distribution_id>'))
def getDistributionMetadata(distribution_id, graph=g):
return httpResponse(graph, graph.distURI(distribution_id))
return httpResponse(graph, graph.distURI(distribution_id))


if __name__ == '__main__':
run(server='wsgiref')

run(server='wsgiref')
Loading

0 comments on commit 9407070

Please sign in to comment.