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

Fix/make project config immutable #246

Merged
merged 7 commits into from
Nov 28, 2019
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.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Fixed

- Attempting to create a linked view for a Project on Windows now raises an informative error message.

Deprecated
++++++++++

- In-memory modification of the project configuration, to be removed in 2.0 (#246).

Removed
+++++++

Expand Down
23 changes: 21 additions & 2 deletions signac/contrib/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ..core.jsondict import JSONDict
from ..core.h5store import H5StoreManager
from .collection import Collection
from ..common.config import load_config
from ..common.config import load_config, Config
from ..sync import sync_projects
from .job import Job
from .hashing import calc_id
Expand Down Expand Up @@ -95,6 +95,25 @@ def find_job_ids(self, filter=None, doc_filter=None):
return self._collection._find(filter)


class _ProjectConfig(Config):
"""Extends the project config to make it immutable."""
def __init__(self, *args, **kwargs):
self._mutable = True
super(_ProjectConfig, self).__init__(*args, **kwargs)
self._mutable = False

def __setitem__(self, key, value):
if not self._mutable:
warnings.warn("Modifying the project configuration after project "
"initialization is deprecated as of version 1.3 and "
"will be removed in version 2.0.",
DeprecationWarning)

from packaging import version
assert version.parse(__version__) < version.parse("2.0")
return super(_ProjectConfig, self).__setitem__(key, value)


class Project(object):
"""The handle on a signac project.

Expand All @@ -120,7 +139,7 @@ class Project(object):
def __init__(self, config=None):
if config is None:
config = load_config()
self._config = config
self._config = _ProjectConfig(config)

# Ensure that the project id is configured.
self.get_id()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def test_root_directory(self):
def test_workspace_directory(self):
self.assertEqual(self._tmp_wd, self.project.workspace())

def test_config_modification(self):
# In-memory modification of the project configuration is
# deprecated as of 1.3, and will be removed in version 2.0.
# This unit test should reflect that change beginning 2.0,
# and check that the project configuration is immutable.
with warnings.catch_warnings(record=True) as w:
self.project.config['foo'] = 'bar'
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, DeprecationWarning)

def test_workspace_directory_with_env_variable(self):
os.environ['SIGNAC_ENV_DIR_TEST'] = self._tmp_wd
self.project.config['workspace_dir'] = '${SIGNAC_ENV_DIR_TEST}'
Expand Down