Skip to content

Commit

Permalink
Merge pull request #479 from frmdstryr/d-change-types
Browse files Browse the repository at this point in the history
Add new declarative @observe which ignores 'create' events by default
  • Loading branch information
MatthieuDartiailh committed Mar 15, 2022
2 parents ebcfec8 + db401a9 commit 0ad2793
Show file tree
Hide file tree
Showing 62 changed files with 152 additions and 151 deletions.
20 changes: 19 additions & 1 deletion enaml/core/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Event, Typed, Str
from atom.api import ChangeType, Event, Typed, Str, observe as default_observe
from atom.datastructures.api import sortedmap

from .declarative_meta import DeclarativeMeta
from .expression_engine import ExpressionEngine
from .object import Object, flag_generator, flag_property

#: Declarative observe ignores create events
OBSERVE_CHANGE_TYPES = ChangeType.ANY & ~ChangeType.CREATE


def observe(*names, change_types=OBSERVE_CHANGE_TYPES):
""" An observe decorator which ignores the created event by default.
Parameters
----------
*names
The str names of the attributes to observe on the object.
These must be of the form 'foo' or 'foo.bar'.
change_types
The flag specifying the type of changes to observe.
"""
return default_observe(*names, change_types=change_types)


def d_(member, readable=True, writable=True, final=True):
""" Mark an Atom member as bindable from Enaml syntax.
Expand Down
20 changes: 12 additions & 8 deletions enaml/core/declarative_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Atom, AtomMeta, DefaultValue, Member, Typed
from atom.api import Atom, AtomMeta, ChangeType, DefaultValue, Member, Typed

# The declarative engine only updates for these types of changes
D_CHANGE_TYPES = (
ChangeType.UPDATE | ChangeType.PROPERTY | ChangeType.EVENT | ChangeType.DELETE
)


class DeclarativeDefaultHandler(Atom):
Expand Down Expand Up @@ -60,12 +65,11 @@ def declarative_change_handler(change):
"""
# TODO think about whether this is the right place to filter on change_t
change_t = change['type']
if change_t == 'update' or change_t == 'event' or change_t == 'property':
owner = change['object']
engine = owner._d_engine
if engine is not None:
engine.write(owner, change['name'], change)
# NOTE: Filtering on change['type'] is done by atom
owner = change['object']
engine = owner._d_engine
if engine is not None:
engine.write(owner, change['name'], change)


def patch_d_member(member):
Expand All @@ -88,7 +92,7 @@ def patch_d_member(member):
new_mode = DefaultValue.CallObject_ObjectName
member.set_default_value_mode(new_mode, handler)
if metadata['d_readable']:
member.add_static_observer(declarative_change_handler)
member.add_static_observer(declarative_change_handler, D_CHANGE_TYPES)


class DeclarativeMeta(AtomMeta):
Expand Down
4 changes: 2 additions & 2 deletions enaml/core/dynamic_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Dict, List, Str, Tuple, Typed, observe
from atom.api import Dict, List, Str, Tuple, Typed

from enaml.application import ScheduledTask, schedule
from enaml.objectdict import ObjectDict

from .declarative import Declarative, d_
from .declarative import Declarative, d_, observe
from .template import Template


Expand Down
4 changes: 2 additions & 2 deletions enaml/scintilla/scintilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

from atom.api import (
Atom, Int, Constant, Enum, Event, Typed, List, ForwardTyped, Tuple,
Str, observe, set_default
Str, set_default
)
from enaml.image import Image
from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.widgets.control import Control, ProxyControl


Expand Down
4 changes: 2 additions & 2 deletions enaml/styling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"""
from collections import defaultdict

from atom.api import Atom, Str, Typed, observe
from atom.api import Atom, Str, Typed

from enaml.application import Application, deferred_call
from enaml.core.declarative import Declarative, d_
from enaml.core.declarative import Declarative, d_, observe


class Setter(Declarative):
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/abstract_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
Bool, Str, Coerced, Event, Typed, ForwardTyped, observe, set_default
Bool, Str, Coerced, Event, Typed, ForwardTyped, set_default
)

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.icon import Icon
from enaml.layout.geometry import Size

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Typed, ForwardTyped, Str, Bool, Event, observe
from atom.api import Typed, ForwardTyped, Str, Bool, Event

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.icon import Icon

from .toolkit_object import ToolkitObject, ProxyToolkitObject
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/action_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Typed, ForwardTyped, observe
from atom.api import Bool, Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .action import Action
from .toolkit_object import ToolkitObject, ProxyToolkitObject
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/bounded_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#------------------------------------------------------------------------------
from datetime import date as pydate

from atom.api import Typed, ForwardTyped, observe
from atom.api import Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .control import Control, ProxyControl

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/bounded_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#------------------------------------------------------------------------------
from datetime import datetime as pydatetime

from atom.api import Typed, ForwardTyped, observe
from atom.api import Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .control import Control, ProxyControl

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/bounded_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#------------------------------------------------------------------------------
from datetime import datetime, time as pytime

from atom.api import Typed, ForwardTyped, observe
from atom.api import Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .control import Control, ProxyControl

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/button_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, ForwardTyped, Typed, observe
from atom.api import Bool, ForwardTyped, Typed

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .toolkit_object import ToolkitObject, ProxyToolkitObject

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/color_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Typed, ForwardTyped, observe
from atom.api import Bool, Typed, ForwardTyped

from enaml.application import Application
from enaml.colors import ColorMember, Color
from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .toolkit_dialog import ToolkitDialog, ProxyToolkitDialog

Expand Down
5 changes: 2 additions & 3 deletions enaml/widgets/combo_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
Bool, List, Int, Property, Str, Typed, ForwardTyped, set_default,
observe
Bool, List, Int, Property, Str, Typed, ForwardTyped, set_default
)

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .control import Control, ProxyControl

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/constraints_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import List, ForwardTyped, Typed, observe
from atom.api import List, ForwardTyped, Typed

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.layout.constrainable import ConstrainableMixin, PolicyEnum

from .widget import Widget, ProxyWidget
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Coerced, ForwardTyped, Typed, observe, set_default
from atom.api import Bool, Coerced, ForwardTyped, Typed, set_default

from enaml.core.declarative import d_, d_func
from enaml.core.declarative import d_, d_func, observe
from enaml.layout.constrainable import ContentsConstrainableMixin
from enaml.layout.geometry import Box
from enaml.layout.layout_helpers import vbox
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/date_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Str, Typed, ForwardTyped, observe, set_default
from atom.api import Bool, Str, Typed, ForwardTyped, set_default

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .bounded_date import BoundedDate, ProxyBoundedDate

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/datetime_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Str, Typed, ForwardTyped, observe, set_default
from atom.api import Bool, Str, Typed, ForwardTyped, set_default

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .bounded_datetime import BoundedDatetime, ProxyBoundedDatetime

Expand Down
5 changes: 2 additions & 3 deletions enaml/widgets/dock_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from contextlib import contextmanager

from atom.api import (
Bool, Coerced, Enum, Typed, ForwardTyped, Str, Event, observe,
set_default
Bool, Coerced, Enum, Typed, ForwardTyped, Str, Event, set_default
)

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.layout.dock_layout import DockLayout, DockLayoutOp
from enaml.styling import StyleSheet

Expand Down
6 changes: 2 additions & 4 deletions enaml/widgets/dock_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
Coerced, Event, Str, Bool, Range, Typed, ForwardTyped, observe
)
from atom.api import Coerced, Event, Str, Bool, Range, Typed, ForwardTyped

from enaml.application import deferred_call
from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.icon import Icon
from enaml.layout.geometry import Size

Expand Down
6 changes: 2 additions & 4 deletions enaml/widgets/dock_pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
List, Enum, Str, Bool, Event, Typed, ForwardTyped, observe
)
from atom.api import List, Enum, Str, Bool, Event, Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .container import Container
from .widget import Widget, ProxyWidget
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/dual_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Enum, Int, Range, Typed, ForwardTyped, observe
from atom.api import Bool, Enum, Int, Range, Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .control import Control, ProxyControl

Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
Bool, Int, Str, Enum, List, Typed, ForwardTyped, observe, set_default
Bool, Int, Str, Enum, List, Typed, ForwardTyped, set_default
)

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.validator import Validator

from .control import Control, ProxyControl
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/file_dialog_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Bool, Enum, List, Str, Typed, ForwardTyped, observe
from atom.api import Bool, Enum, List, Str, Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .toolkit_dialog import ToolkitDialog, ProxyToolkitDialog

Expand Down
6 changes: 2 additions & 4 deletions enaml/widgets/flow_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import (
Enum, Range, Coerced, Typed, ForwardTyped, observe, set_default
)
from atom.api import Enum, Range, Coerced, Typed, ForwardTyped, set_default

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.layout.geometry import Box

from .frame import Frame, ProxyFrame, Border
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/flow_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Enum, Range, Coerced, Typed, ForwardTyped, observe
from atom.api import Enum, Range, Coerced, Typed, ForwardTyped

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe
from enaml.layout.geometry import Size

from .container import Container
Expand Down
4 changes: 2 additions & 2 deletions enaml/widgets/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Int, observe
from atom.api import Int

from enaml.layout.constrainable import ConstraintMember
from enaml.layout.layout_helpers import align, vertical, horizontal, spacer

from enaml.core.declarative import d_
from enaml.core.declarative import d_, observe

from .container import Container

Expand Down

0 comments on commit 0ad2793

Please sign in to comment.