This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean up tracking context -- rename to ChangesetContext, reset_changes renamed to reset, rename changes property to values, adds changes property which returns actual merged changes; adds ChangesetContext.__len__ which allows truth value calculation of context.
- Loading branch information
Showing
9 changed files
with
478 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[bumpversion] | ||
current_version = 1.29.2 | ||
current_version = 1.30.0 | ||
commit = true | ||
tag = false | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '1.29.2' | ||
__version__ = '1.30.0' | ||
|
||
from .managers import Config | ||
from .items import Item | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import collections | ||
|
||
|
||
_Change = collections.namedtuple('Change', field_names=( | ||
'old_value', 'new_value', 'old_raw_str_value', 'new_raw_str_value' | ||
)) | ||
|
||
|
||
class _ChangesetContext(object): | ||
def __init__(self, config): | ||
self.config = config | ||
self.hook = None | ||
self._changes = collections.defaultdict(list) | ||
|
||
def __enter__(self): | ||
return self.push() | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.pop() | ||
|
||
def _value_changed(self, item, old_value, new_value, old_raw_str_value, new_raw_str_value): | ||
if old_value != new_value or old_raw_str_value != new_raw_str_value: | ||
self._changes[item].append(_Change(old_value, new_value, old_raw_str_value, new_raw_str_value)) | ||
|
||
def push(self): | ||
assert self.hook is None | ||
self.hook = self.config.hooks.item_value_changed.register_hook(self._value_changed) | ||
self.config._changeset_contexts.append(self) | ||
return self | ||
|
||
def pop(self): | ||
popped = self.config._changeset_contexts.pop() | ||
assert popped is self | ||
self.config.hooks.unregister_hook(self.config.hooks.item_value_changed, self._value_changed) | ||
self.hook = None | ||
|
||
@property | ||
def values(self): | ||
""" | ||
Returns a mapping of items to their new values. The mapping includes only items whose value or raw string value | ||
has changed in the context. | ||
""" | ||
report = {} | ||
for k, k_changes in self._changes.items(): | ||
if len(k_changes) == 1: | ||
report[k] = k_changes[0].new_value | ||
elif k_changes[0].old_value != k_changes[-1].new_value: | ||
report[k] = k_changes[-1].new_value | ||
return report | ||
|
||
@property | ||
def changes(self): | ||
""" | ||
Returns a mapping of items to their effective change objects which include the old values | ||
and the new. The mapping includes only items whose value or raw string value has changed in the context. | ||
""" | ||
report = {} | ||
for k, k_changes in self._changes.items(): | ||
if len(k_changes) == 1: | ||
report[k] = k_changes[0] | ||
else: | ||
first = k_changes[0] | ||
last = k_changes[-1] | ||
if first.old_value != last.new_value or first.old_raw_str_value != last.new_raw_str_value: | ||
report[k] = _Change( | ||
first.old_value, | ||
last.new_value, | ||
first.old_raw_str_value, | ||
last.new_raw_str_value, | ||
) | ||
return report | ||
|
||
def reset(self, item=None): | ||
for k, k_changes in self._changes.items(): | ||
if item is None or k is item: | ||
k._value = k_changes[0].old_value | ||
k.raw_str_value = k_changes[0].old_raw_str_value | ||
|
||
if item is None: | ||
self._changes.clear() | ||
else: | ||
del self._changes[item] | ||
|
||
def __len__(self): | ||
""" | ||
Returns the number of items whose value or raw string value has changed in this context. | ||
""" | ||
return len(self.changes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.