Skip to content

Commit

Permalink
Merge branch 'main' into absolute-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Sep 5, 2024
2 parents 84d57ed + 3e5874a commit 11e28ea
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
5 changes: 4 additions & 1 deletion bw2data/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from pathlib import Path
from typing import Optional

import wrapt
from bw_processing import safe_filename
from peewee import SQL, BooleanField, DoesNotExist, Model, TextField
from platformdirs import PlatformDirs
import wrapt

from bw2data import config
from bw2data.filesystem import create_dir
from bw2data.signals import project_changed, project_created
from bw2data.sqlite import PickleField, SubstitutableDatabase
from bw2data.utils import maybe_path

Expand Down Expand Up @@ -196,6 +197,7 @@ def set_current(self, name, writable=True, update=True):
self.dataset = ProjectDataset.get(ProjectDataset.name == self._project_name)
self._reset_meta()
self._reset_sqlite3_databases()
project_changed.send(self.dataset)

if not writable:
self.read_only = True
Expand Down Expand Up @@ -263,6 +265,7 @@ def create_project(self, name=None, **kwargs):
self.dataset = ProjectDataset.create(
data=kwargs, name=name, full_hash=full_hash
)
project_created.send(self.dataset)
create_dir(self.dir)
for dir_name in self._basic_directories:
create_dir(self.dir / dir_name)
Expand Down
20 changes: 20 additions & 0 deletions bw2data/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from blinker import signal


project_changed = signal('bw2data.project_changed', doc="""
Emitted when project changed, after redirecting any SQLite database references.
Expected inputs:
* `bw2data.projects.ProjectDataset` instance
No expected return value.
""")

project_created = signal('bw2data.project_created', doc="""
Emitted when project created, but before switching to that project, and before any filesystem ops.
Expected inputs:
* `bw2data.projects.ProjectDataset` instance
No expected return value.
""")
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
# dependencies as strings with quotes, e.g. "foo"
# You can add version requirements like "foo>2.0"
"blinker",
"bw2parameters",
"bw_processing>=0.9.5",
"fsspec",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from bw2data.project import ProjectDataset, projects
from bw2data.tests import bw2test
from blinker import signal


class SignalCatcher:
def __call__(self, arg):
self.arg = arg


@bw2test
def test_project_changed_signal():
subscriber = SignalCatcher()
signal('bw2data.project_changed').connect(subscriber)
projects.set_current("foo")

assert isinstance(subscriber.arg, ProjectDataset)
assert subscriber.arg.name == 'foo'


@bw2test
def test_project_created_signal():
subscriber = SignalCatcher()
signal('bw2data.project_created').connect(subscriber)
projects.set_current("foo")

assert isinstance(subscriber.arg, ProjectDataset)
assert subscriber.arg.name == 'foo'

0 comments on commit 11e28ea

Please sign in to comment.