File: sanitizerservice.py

package info (click to toggle)
gaphor 0.13.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,692 kB
  • ctags: 2,971
  • sloc: python: 19,981; xml: 247; makefile: 54; sh: 40
file content (44 lines) | stat: -rw-r--r-- 1,375 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
The Sanitize module is dedicated to adapters (stuff) that keeps
the model clean and in sync with diagrams.
"""

from zope import interface
from zope import component
from gaphor import UML
from gaphor.UML.interfaces import IElementDeleteEvent
from gaphor.interfaces import IService


class SanitizerService(object):
    """
    Does some background cleanup jobs, such as removing elements from the
    model that have no presentations (and should have some).
    """
    interface.implements(IService)

    def __init__(self):
        pass

    def init(self, app):
        self._app = app
        app.register_handler(self._unlink_on_presentation_delete)

    def shutdown(self):
        self._app.unregister_handler(self._unlink_on_presentation_delete)
        
    @component.adapter(UML.Presentation, IElementDeleteEvent)
    def _unlink_on_presentation_delete(self, item, event):
        """
        Unlink the model element if no more presentations link to the `item`'s
        subject or the to-be-deleted item is the only item currently linked.
        """
        subject = item.subject
        if subject:
            presentation = subject.presentation
            if not presentation or \
                    (len(presentation) == 1 and presentation[0] is item):
                subject.unlink()


# vim:sw=4:et:ai