This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from dephell/unapply-markers
Unapply markers
- Loading branch information
Showing
9 changed files
with
107 additions
and
18 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# external | ||
from dephell_markers import Markers, OrMarker | ||
|
||
|
||
class MarkerTracker: | ||
def __init__(self): | ||
self._markers = dict() | ||
|
||
@property | ||
def markers(self) -> Markers: | ||
if len(self._markers) == 1: | ||
return next(iter(self._markers.values())) | ||
container = Markers() | ||
markers = [m._marker for m in self._markers.values() if m._marker] | ||
container._marker = OrMarker(*markers) | ||
return container | ||
|
||
def apply(self, *, source, markers) -> 'MarkerTracker': | ||
if not markers: | ||
return self | ||
if type(source) is not str: | ||
source = source.name | ||
if source in self._markers: | ||
raise ValueError('marker for given source already added') | ||
if type(markers) is str: | ||
markers = Markers(markers) | ||
self._markers[source] = markers | ||
return self | ||
|
||
def merge(self, other: 'MarkerTracker') -> None: | ||
for source, marker in other._markers.items(): | ||
self._markers[source] = marker | ||
|
||
def unapply(self, source) -> None: | ||
if type(source) is not str: | ||
source = source.name | ||
if source in self._markers: | ||
del self._markers[source] | ||
|
||
def __getattr__(self, name): | ||
if name not in dir(Markers): | ||
raise AttributeError(name) | ||
return getattr(self.markers, name) | ||
|
||
def __bool__(self) -> bool: | ||
return any(marker for marker in self._markers.values()) | ||
|
||
def __str__(self) -> str: | ||
return str(self.markers) | ||
|
||
def __repr__(self): | ||
markers = map(repr, self._markers.values()) | ||
return '{}({})'.format(type(self).__name__, ', '.join(markers)) |
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,22 @@ | ||
# project | ||
from dephell.models import MarkerTracker | ||
|
||
|
||
def test_apply(): | ||
mt = MarkerTracker() | ||
mt.apply(source='sname', markers='python_version >= "3.5"') | ||
assert len(mt._markers) == 1 | ||
assert str(mt._markers['sname']) == 'python_version >= "3.5"' | ||
|
||
|
||
def test_str(): | ||
mt = MarkerTracker() | ||
mt.apply(source='sname', markers='python_version >= "3.5"') | ||
assert str(mt) == 'python_version >= "3.5"' | ||
|
||
|
||
def test_bool(): | ||
mt = MarkerTracker() | ||
assert bool(mt) is False | ||
mt.apply(source='sname', markers='python_version >= "3.5"') | ||
assert bool(mt) is True |
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