Skip to content

Commit

Permalink
Merge pull request #159 from girder/static-root-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba authored Oct 29, 2021
2 parents a9fcd39 + 4de4a43 commit cd884a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions composed_configuration/_static.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pathlib import Path
from typing import Type

from configurations import values

from ._base import ComposedConfiguration, ConfigMixin
from ._values import DirectoryPathValue


class StaticFileMixin(ConfigMixin):
Expand All @@ -26,12 +25,14 @@ def BASE_DIR(self) -> Path: # noqa: N802

@property
def STATIC_ROOT(self): # noqa: N802
# Django staticfiles creates any intermediate directories which don't exist
# TODO: allow from env?
return values.PathValue(
return DirectoryPathValue(
str(Path(self.BASE_DIR) / 'staticfiles'),
environ=False,
check_exists=False,
# Django staticfiles creates any intermediate directories which don't exist, but do
# so at the setting level to prevent warnings
ensure_exists=True,
# Disable late_binding, to make this return an actual str, not a Value, since some
# "os" module functions (which are called with this) do strict nominal type checking.
late_binding=False,
Expand Down
19 changes: 19 additions & 0 deletions composed_configuration/_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os.path

from configurations import values


class DirectoryPathValue(values.PathValue):
"""A PathValue requiring that its path is a directory, optionally creating it if necessary."""

def __init__(self, *args, **kwargs):
self.ensure_exists: bool = kwargs.pop('ensure_exists', False)
super().__init__(*args, **kwargs)

def setup(self, name: str) -> str:
value = super().setup(name)
if os.path.exists(value) and not os.path.isdir(value):
raise ValueError(f'Path {repr(value)} is not a directory.')
if self.ensure_exists:
os.makedirs(value, exist_ok=True)
return value

0 comments on commit cd884a3

Please sign in to comment.