Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow getting configuration from environment variables #1422

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 1.27.1

### Improvements
- Read config files from the environment variables ([#1422](../../pull/1422))

## 1.27.0

### Features
Expand Down
5 changes: 5 additions & 0 deletions docs/config_options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ As an example, configuration parameters can be set via python code like::

large_image.config.setConfig('max_small_image_size', 8192)

Configuration from Environment
------------------------------

All configuration parameters can be specified as environment parameters by prefixing their uppercase names with ``LARGE_IMAGE_``. For instance, ``LARGE_IMAGE_CACHE_BACKEND=python`` specifies the cache backend. If the values can be decoded as json, they will be parsed as such. That is, numerical values will be parsed as numbers; to parse them as strings, surround them with double quotes.

Configuration within the Girder Plugin
--------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion large_image/cache_util/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import pickle
import threading
import uuid

Expand Down Expand Up @@ -92,7 +93,7 @@ def wrapper(self, *args, **kwargs):
return self.cache[k]
except KeyError:
pass # key not found
except ValueError:
except (ValueError, pickle.UnpicklingError):
# this can happen if a different version of python wrote the record
pass
v = func(self, *args, **kwargs)
Expand Down
12 changes: 12 additions & 0 deletions large_image/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import logging
import os
from typing import cast

try:
Expand Down Expand Up @@ -65,6 +67,16 @@ def getConfig(key=None, default=None):
"""
if key is None:
return ConfigValues
envKey = f'LARGE_IMAGE_{key.replace(".", "_").upper()}'
if envKey in os.environ:
value = os.environ[envKey]
if value == '__default__':
return default
try:
value = json.loads(value)
except ValueError:
pass
return value
return ConfigValues.get(key, default)


Expand Down