Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #220 from enthought/feature-resource
Browse files Browse the repository at this point in the history
Feature resource
  • Loading branch information
sccolbert committed Dec 10, 2012
2 parents 4416d07 + cc1c382 commit 77d5ca3
Show file tree
Hide file tree
Showing 101 changed files with 3,529 additions and 2,243 deletions.
148 changes: 56 additions & 92 deletions enaml/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from itertools import count
import logging
from threading import Lock
import uuid


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -149,7 +148,6 @@ def __init__(self, factories):
"""
self._all_factories = []
self._named_factories = {}
self._sessions = {}
self._task_heap = []
self._counter = count()
self._heap_lock = Lock()
Expand Down Expand Up @@ -182,20 +180,67 @@ def _next_task(self):
# Abstract API
#--------------------------------------------------------------------------
@abstractmethod
def socket(self, session_id):
""" Get the ActionSocketInterface for a session.
def start_session(self, name):
""" Start a new session of the given name.
This method will create a new session object for the requested
session type and return the new session_id. If the session name
is invalid, an exception will be raised.
Parameters
----------
name : str
The name of the session to start.
Returns
-------
result : str
The unique identifier for the created session.
"""
raise NotImplementedError

@abstractmethod
def end_session(self, session_id):
""" End the session with the given session id.
This method will close down the existing session. If the session
id is not valid, an exception will be raised.
Parameters
----------
session_id : str
The string identifier for the session which will use the
created action socket.
The unique identifier for the session to close.
"""
raise NotImplementedError

@abstractmethod
def session(self, session_id):
""" Get the session for the given session id.
Parameters
----------
session_id : str
The unique identifier for the session to retrieve.
Returns
-------
result : ActionSocketInterface
An implementor of ActionSocketInterface which can be used
by Enaml Session instances for messaging.
result : Session or None
The session object with the given id, or None if the id
does not correspond to an active session.
"""
raise NotImplementedError

@abstractmethod
def sessions(self):
""" Get the currently active sessions for the application.
Returns
-------
result : list
The list of currently active sessions for the application.
"""
raise NotImplementedError
Expand Down Expand Up @@ -364,98 +409,17 @@ def discover(self):
]
return info

def session(self, session_id):
""" Get the session for the given session id.
Parameters
----------
session_id : str
The unique identifier for the session to retrieve.
Returns
-------
result : Session or None
The session object with the given id, or None if the id
does not correspond to an active session.
"""
return self._sessions.get(session_id)

def start_session(self, name):
""" Start a new session of the given name.
This method will create a new session object for the requested
session type and return the new session_id. If the session name
is invalid, an exception will be raised.
Parameters
----------
name : str
The name of the session to start.
Returns
-------
result : str
The unique identifier for the created session.
"""
if name not in self._named_factories:
raise ValueError('Invalid session name')
factory = self._named_factories[name]
session = factory()
session_id = uuid.uuid4().hex
self._sessions[session_id] = session
session.open(session_id, self.socket(session_id))
return session_id

def end_session(self, session_id):
""" End the session with the given session id.
This method will close down the existing session. If the session
id is not valid, an exception will be raised.
Parameters
----------
session_id : str
The unique identifier for the session to close.
"""
if session_id not in self._sessions:
raise ValueError('Invalid session id')
session = self._sessions.pop(session_id)
session.close()

def snapshot(self, session_id):
""" Get a snapshot of the Session with the given session_id.
Parameters
----------
session_id : str
The unique identifier for the given session.
Returns
-------
result : list
A list of snapshot dictionaries for the given session.
"""
session = self._sessions.get(session_id)
if session is None:
raise ValueError('Invalid session id')
return session.snapshot()

def destroy(self):
""" Destroy this application instance.
Once an application is created, it must be destroyed before a
new application can be instantiated.
"""
for session in self._sessions.itervalues():
session.close()
for session in self.sessions():
self.end_session(session.session_id)
self._all_factories = []
self._named_factories = {}
self._sessions = {}
Application._instance = None


Expand Down
63 changes: 23 additions & 40 deletions enaml/core/abstract_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,60 @@
#------------------------------------------------------------------------------
from abc import ABCMeta, abstractmethod

from enaml.signaling import Signal

class AbstractExpression(object):
""" An abstract interface definition for creating expressions.
class AbstractListener(object):
""" An interface definition for creating attribute listeners.
Listeners are registered with `Declarative` instances using the
`bind_listener` method to track changes to their attributes.
Expressions are registered with `Declarative` instances using the
`bind_expression` method to provide computed attribute values.
"""
__metaclass__ = ABCMeta

@abstractmethod
def value_changed(self, obj, name, old, new):
""" Called when the attribute on the object has changed.
def eval(self, obj, name):
""" Evaluate and return the results of the expression.
Parameters
----------
obj : Declarative
The Declarative object which owns the attribute.
The declarative object which owns the expression.
name : str
The name of the attribute which changed.
old : object
The old value of the attribute.
new : object
The new value of the attribute.
The attribute name on `obj` for which this expression is
providing the value.
"""
raise NotImplementedError


class AbstractExpression(object):
""" An abstract interface definition for creating expressions.
class AbstractListener(object):
""" An interface definition for creating attribute listeners.
Expressions are registered with `Declarative` instances using the
`bind_expression` method to provide computed attribute values.
Listeners are registered with `Declarative` instances using the
`bind_listener` method to track changes to their attributes.
"""
__metaclass__ = ABCMeta

@abstractmethod
def eval(self, obj, name):
""" Evaluate and return the results of the expression.
def value_changed(self, obj, name, old, new):
""" Called when the attribute on the object has changed.
Parameters
----------
obj : Declarative
The declarative object which owns the expression.
The Declarative object which owns the attribute.
name : str
The attribute name on `obj` for which this expression is
providing the value.
"""
raise NotImplementedError

The name of the attribute which changed.
class AbstractListenableExpression(AbstractExpression):
""" An abstract interface definition for creating listenable
expressions.
old : object
The old value of the attribute.
Listenable expressions are registered with `Declarative` instances
using the `bind_expression` method to provide dynamically computed
attribute values at runtime.
new : object
The new value of the attribute.
"""
#: An Enaml Signal emitted by the expression when it becomes invalid.
#: The payload of the signal will be the name that was passed to the
#: `eval` method during the last expression evaluation.
invalidated = Signal()
"""
raise NotImplementedError

5 changes: 1 addition & 4 deletions enaml/core/compiler_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def _make_enamldef_helper_(name, base, builder):
msg = "can't derive enamldef from '%s'"
raise TypeError(msg % base)
decl_cls = EnamlDef(name, (base,), dct)
# The list of builder functions is created in reverse order, with
# the oldest builder appearing first in the list, so that newer
# builders will override the effects of older builders.
decl_cls._builders = base._builders + [builder]
decl_cls._builders = base._builders + (builder,)
return decl_cls

Loading

0 comments on commit 77d5ca3

Please sign in to comment.