Skip to content

Commit

Permalink
-Item stffix is stripped from item names
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Jul 11, 2020
1 parent b3cf3cc commit 9a0fb75
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
23 changes: 18 additions & 5 deletions gaphor/core/modeling/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from gaphor.core.modeling.coremodel import PackageableElement
from gaphor.core.modeling.element import Id, RepositoryProtocol
from gaphor.core.modeling.event import DiagramItemCreated
from gaphor.core.modeling.presentation import Presentation
from gaphor.core.modeling.stylesheet import StyleSheet
from gaphor.core.styling import FontStyle, Style, StyleNode, TextAlign

Expand Down Expand Up @@ -69,12 +70,21 @@ class DrawContext:
dropzone: bool


# From https://www.python.org/dev/peps/pep-0616/
def removesuffix(self: str, suffix: str, /) -> str:
# suffix='' should not call self[:-0].
if suffix and self.endswith(suffix):
return self[: -len(suffix)]
else:
return self[:]


class StyledDiagram:
def __init__(self, diagram: Diagram, view: Optional[gaphas.View] = None):
self.diagram = diagram
self.view = view

def local_name(self) -> str:
def name(self) -> str:
return "diagram"

def parent(self):
Expand All @@ -99,13 +109,14 @@ class StyledItem:
pseudo-classes for the item (focus, hover, etc.)
"""

def __init__(self, item: gaphas.Item, view: Optional[gaphas.View] = None):
def __init__(self, item: Presentation, view: Optional[gaphas.View] = None):
assert item.canvas
self.item = item
self.canvas = item.canvas
self.view = view

def local_name(self) -> str:
return type(self.item).__name__.lower()
def name(self) -> str:
return removesuffix(type(self.item).__name__, "Item").lower()

def parent(self) -> Union[StyledItem, StyledDiagram]:
parent = self.canvas.get_parent(self.item)
Expand Down Expand Up @@ -254,7 +265,9 @@ def create(self, type, parent=None, subject=None):
return self.create_as(type, str(uuid.uuid1()), parent, subject)

def create_as(self, type, id, parent=None, subject=None):
if not (type and issubclass(type, gaphas.Item)):
if not (
type and issubclass(type, gaphas.Item) and issubclass(type, Presentation)
):
raise TypeError(
f"Type {type} can not be added to a diagram as it is not a diagram item"
)
Expand Down
2 changes: 1 addition & 1 deletion gaphor/core/styling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class StyleNode(Protocol):
def local_name(self) -> str:
def name(self) -> str:
...

def parent(self) -> Optional[StyleNode]:
Expand Down
4 changes: 2 additions & 2 deletions gaphor/core/styling/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def compile_compound_selector(selector: parser.CompoundSelector):


@compile_node.register
def compile_local_name_selector(selector: parser.LocalNameSelector):
return lambda el: el.local_name() == selector.lower_local_name
def compile_name_selector(selector: parser.LocalNameSelector):
return lambda el: el.name() == selector.lower_local_name


def ancestors(el):
Expand Down
16 changes: 8 additions & 8 deletions gaphor/core/styling/tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class Node:
def __init__(self, local_name, parent=None, children=None, attributes={}, state=()):
self._local_name = local_name
def __init__(self, name, parent=None, children=None, attributes={}, state=()):
self._name = name
self._parent = parent
self._children = children or []
self._attributes = attributes
Expand All @@ -16,8 +16,8 @@ def __init__(self, local_name, parent=None, children=None, attributes={}, state=
for c in self._children:
c._parent = self

def local_name(self):
return self._local_name
def name(self):
return self._name

def parent(self):
return self._parent
Expand All @@ -36,8 +36,8 @@ def test_node_test_object_parent_child():
c = Node("child")
p = Node("parent", children=[c])

assert c.local_name() == "child"
assert p.local_name() == "parent"
assert c.name() == "child"
assert p.name() == "parent"
assert c.parent() is p
assert c in p.children()

Expand All @@ -46,8 +46,8 @@ def test_node_test_object_child_parent():
p = Node("parent")
c = Node("child", parent=p)

assert c.local_name() == "child"
assert p.local_name() == "parent"
assert c.name() == "child"
assert p.name() == "parent"
assert c.parent() is p
assert c in p.children()

Expand Down

0 comments on commit 9a0fb75

Please sign in to comment.