Skip to content

Commit fa3e7f7

Browse files
committed
Merge branch 'master' into patch-1
2 parents 7c004ce + 17db5bc commit fa3e7f7

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM tiangolo/meinheld-gunicorn:python3.8
2-
MAINTAINER Michaël Dierick "[email protected]"
2+
LABEL maintainer="[email protected]"
33

44
# Gunicorn Docker config
55
ENV MODULE_NAME web
@@ -14,6 +14,7 @@ RUN chmod +x /start.sh
1414
# Template config
1515
ENV APP_ENTRYPOINT web
1616
ENV LOG_LEVEL info
17+
ENV LOG_SPARQL_ALL True
1718
ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql'
1819
ENV MU_SPARQL_UPDATEPOINT 'http://database:8890/sparql'
1920
ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application'

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Mu Python template
22

3-
Template for [mu.semte.ch](http://mu.semte.ch)-microservices written in Python3. Based on the [Flask](https://palletsprojects.com/p/flask/)-framework.
3+
Template for [mu.semte.ch](http://mu.semte.ch)-microservices written in Python3.8. Based on the [Flask](https://palletsprojects.com/p/flask/)-framework.
44

55
## Quickstart
66

77
Create a `Dockerfile` which extends the `semtech/mu-python-template`-image and set a maintainer.
88
```docker
99
FROM semtech/mu-python-template:2.0.0-beta.2
10-
LABEL maintainer="sam.landuydt@gmail.com"
10+
LABEL maintainer="maintainer@example.com"
1111
```
1212

1313
Create a `web.py` entrypoint-file. (naming of the entrypoint can be configured through `APP_ENTRYPOINT`)
@@ -300,6 +300,7 @@ my-python:
300300
```
301301

302302
### Environment variables
303+
#### General
303304

304305
- `LOG_LEVEL` takes the same options as defined in the Python [logging](https://docs.python.org/3/library/logging.html#logging-levels) module.
305306

@@ -317,7 +318,16 @@ my-python:
317318

318319
- `MU_SPARQL_TIMEOUT` is used to configure the timeout (in seconds) for SPARQL queries.
319320

321+
#### SPARQL Query Logging
322+
- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `True`)
320323

324+
- `LOG_SPARQL_QUERIES`: Log *read* queries (default: `undefined`). Overrules `LOG_SPARQL_ALL`
325+
326+
- `LOG_SPARQL_UPDATES`: Log *update* queries (default `undefined`). Overrules `LOG_SPARQL_ALL`.
327+
328+
The string "true", ignoring casing, is considered `True`. All other values are considered `False`.
329+
330+
#### Meinheld Gunicorn Docker Variables
321331
Since this template is based on the meinheld-gunicorn-docker image, all possible environment config for that image is also available for the template. See [meinheld-gunicorn-docker#environment-variables](https://github.com/tiangolo/meinheld-gunicorn-docker#environment-variables) for more info. The template configures `WEB_CONCURRENCY` in particular to `1` by default.
322332

323333
### Production

escape_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def sparql_escape_int(obj):
5353

5454
def sparql_escape_float(obj):
5555
"""Converts the given float to a SPARQL-safe RDF object string with the right RDF-datatype. """
56-
if not isinstance(obj, int):
56+
if not isinstance(obj, float):
5757
warn("You are escaping something that isn't a float with \
5858
the 'sparql_escape_float'-method. Implicit casting will occurr.")
5959
obj = str(float(obj))

helpers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr?
4343
logger.addHandler(consoleHandler)
4444

45+
LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL')
46+
LOG_SPARQL_QUERIES = os.environ.get(
47+
'LOG_SPARQL_QUERIES',
48+
default=LOG_SPARQL_ALL_VAR
49+
).lower() == 'true'
50+
LOG_SPARQL_UPDATES = os.environ.get(
51+
'LOG_SPARQL_UPDATES',
52+
default=LOG_SPARQL_ALL_VAR
53+
).lower() == 'true'
54+
4555
def generate_uuid():
4656
"""Generates a random unique user id (UUID) based on the host ID and current time"""
4757
return str(uuid.uuid1())
@@ -119,14 +129,15 @@ def validate_resource_type(expected_type, data):
119129

120130
def query(the_query):
121131
"""Execute the given SPARQL query (select/ask/construct) on the triplestore and returns the results in the given return Format (JSON by default)."""
122-
log("execute query: \n" + the_query)
123132
for header in MU_HEADERS:
124133
if header in request.headers:
125134
sparqlQuery.customHttpHeaders[header] = request.headers[header]
126135
else: # Make sure headers used for a previous query are cleared
127136
if header in sparqlQuery.customHttpHeaders:
128137
del sparqlQuery.customHttpHeaders[header]
129138
sparqlQuery.setQuery(the_query)
139+
if LOG_SPARQL_QUERIES:
140+
log("Execute query: \n" + the_query)
130141
return sparqlQuery.query().convert()
131142

132143

@@ -140,6 +151,8 @@ def update(the_query):
140151
del sparqlUpdate.customHttpHeaders[header]
141152
sparqlUpdate.setQuery(the_query)
142153
if sparqlUpdate.isSparqlUpdateRequest():
154+
if LOG_SPARQL_UPDATES:
155+
log("Execute query: \n" + the_query)
143156
sparqlUpdate.query()
144157

145158

0 commit comments

Comments
 (0)