puremvc.core package

Submodules

puremvc.core.Controller module

class puremvc.core.Controller.Controller(key: str)

Bases: IController

A Multiton IController implementation.

In PureMVC, the Controller class follows the ‘Command and Controller’ strategy, and assumes these responsibilities:

Remembering which ICommand`s are intended to handle which `INotifications.

Registering itself as an IObserver with the View for each INotification that it has an ICommand mapping for.

Creating a new instance of the proper ICommand to handle a given INotification when notified by the View.

Calling the ICommand’s execute method, passing in the INotification.

Your application must register ICommands with the Controller.

The simplest way is to subclass Facade, and use its initialize_controller method to add your registrations.

See Also

View puremvc.patterns.observer.Observer puremvc.patterns.observer.Notification puremvc.patterns.command.SimpleCommand puremvc.patterns.command.MacroCommand

MULTITON_MSG = 'Controller multiton instance for this key is already constructed!'
execute_command(notification: INotification)

Executes the specified command based on the given notification.

Parameters:

notification (INotification) – The notification to be executed.

Returns:

None

classmethod get_instance(key: str, factory: Callable[[str], IController]) IController

Controller Multiton Factory method.

Parameters:
  • key (str) – The key used to identify the instance.

  • factory (Callable[[str], IController]) – A factory function that creates a new instance of IController based on the provided key.

Returns:

The instance of IController associated with the given key.

has_command(notification_name: str) bool

Check if a Command is registered for a given Notification

Parameters:

notification_name (str) – The name of the notification to check in the commandMap.

Returns:

True if the notification_name exists in the commandMap, False otherwise.

Return type:

bool

initialize_controller()

Initialize the Multiton Controller instance.

Called automatically by the constructor.

Note that if you are using a subclass of View in your application, you should also subclass Controller and override the initialize_controller method in the following way:

def initialize_controller(self):
    self.view = MyView.get_instance(self.multitonKey, lambda: key: MyView(key))
Returns:

None

instanceMap: Dict[str, IController] = {}
instanceMapLock: allocate_lock = <unlocked _thread.lock object>

MULTITON_MSG (str): Multiton error message

register_command(notification_name: str, factory: Callable[[], ICommand])

Register a particular ICommand class as the handler for a particular INotification.

If an ICommand has already been registered to handle INotification`s with this name, it is no longer used, the new `ICommand is used instead.

The Observer for the new ICommand is only created if this is the first time an ICommand has been registered for this Notification name.

Parameters:
  • notification_name – The name of the notification.

  • factory – Callable that returns an instance of ICommand.

Returns:

None.

remove_command(notification_name: str)

Remove a previously registered ICommand to INotification mapping.

Parameters:

notification_name (str) – The name of the notification associated with the command to be removed.

Returns:

None

classmethod remove_controller(key: str)

Remove an IController instance

Parameters:

key (str) – The key to identify the controller instance to be removed.

Returns:

None

puremvc.core.Model module

class puremvc.core.Model.Model(key: str)

Bases: IModel

A Multiton IModel implementation.

In PureMVC, the Model class provides access to model objects (Proxies) by named lookup.

The Model assumes these responsibilities:

  • Maintain a cache of IProxy instances.

  • Provide methods for registering, retrieving, and removing IProxy instances.

Your application must register IProxy instances with the Model. Typically, you use an ICommand to create and register IProxy instances once the Facade has initialized the Core actors.

See Also

puremvc.patterns.proxy.Proxy puremvc.interfaces.IProxy

MULTITON_MSG = 'Model multiton instance for this key is already constructed!'
classmethod get_instance(key: str, factory: Callable[[str], IModel]) IModel

Multiton Factory method.

Parameters:
  • key (str) – A string representing the key used to retrieve the instance.

  • factory (Callable[[str], IModel]) – A factory function used to create new instances of IModel.

Returns:

An instance of IModel associated with the given key.

has_proxy(proxy_name: str) bool

Check if a Proxy is registered

Parameters:

proxy_name (str) – A string representing the name of the proxy to check.

Returns:

Returns True if the proxy exists in the proxy map, False otherwise.

Return type:

bool

initialize_model()

Initialize the Model instance.

Called automatically by the constructor, this is your opportunity to initialize the Multiton instance in your subclass without overriding the constructor.

Returns:

None

instanceMap: Dict[str, IModel] = {}
instanceMapLock: allocate_lock = <unlocked _thread.lock object>

MULTITON_MSG (str): Multiton error message

register_proxy(proxy: IProxy)

Register an IProxy with the Model.

Parameters:

proxy (IProxy) – An IProxy to be held by the Model.

Returns:

None

classmethod remove_model(key: str)

Remove an IModel instance

Parameters:

key (str) – multiton_key of IModel instance to remove

Returns:

None

remove_proxy(proxy_name: str) IProxy

Remove an IProxy from the Model.

Parameters:

proxy_name (str) – The name of the IProxy to remove.

Returns:

the IProxy that was removed from the Model

Return type:

IProxy

retrieve_proxy(proxy_name: str) IProxy

Retrieve an IProxy from the Model.

Parameters:

proxy_name (str) – The name of the proxy.

Returns:

the IProxy instance previously registered with the given proxyName.

Return type:

IProxy

puremvc.core.View module

class puremvc.core.View.View(key: str)

Bases: IView

A Multiton IView implementation.

In PureMVC, the View class assumes these responsibilities:

Maintain a cache of IMediator instances.

Provide methods for registering, retrieving, and removing IMediators.

Notifying IMediators when they are registered or removed.

Managing the observer lists for each INotification in the application.

Providing a method for attaching IObservers to an INotification’s observer list.

Providing a method for broadcasting an INotification.

Notifying the IObservers of a given INotification when it broadcast.

See Also

puremvc.patterns.mediator.Mediator puremvc.patterns.observer.Observer puremvc.patterns.observer.Notification

MULTITON_MSG = 'View multiton instance for this key is already constructed!'
classmethod get_instance(key: str, factory: Callable[[str], IView]) IView

View Multiton Factory method.

Parameters:
  • key – The key associated with the desired instance.

  • factory – A factory function that creates an instance of the desired class using the given key.

Returns:

The instance associated with the given key.

Return type:

IView

has_mediator(mediator_name: str) bool

Check if a Mediator is registered or not

Parameters:

mediator_name (str) – The name of the mediator to check.

Returns:

Returns True if the mediator exists, False otherwise.

Return type:

bool

initialize_view()

Initialize the Multiton View instance.

Called automatically by the constructor, this is your opportunity to initialize the Multiton instance in your subclass without overriding the constructor.

Returns:

None

instanceMap: Dict[str, IView] = {}
instanceMapLock: allocate_lock = <unlocked _thread.lock object>

MULTITON_MSG (str): Multiton error message

notify_observers(notification: INotification)

Notify the IObservers for a particular INotification.

All previously attached IObservers for this INotification’s list are notified and are passed a reference to the INotification in the order in which they were registered.

Parameters:

notification (INotification) – The notification to be sent to the observers.

Returns:

None

register_mediator(mediator: IMediator)

Register an IMediator instance with the View.

Registers the IMediator so that it can be retrieved by name, and further interrogates the IMediator for its INotification interests.

If the IMediator returns any INotification names to be notified about, an Observer is created encapsulating the IMediator instance’s handleNotification method and registering it as an Observer for all INotifications the IMediator is interested in.

Parameters:

mediator (IMediator) – The mediator to register.

Returns:

None

register_observer(notification_name: str, observer: IObserver)

Register an IObserver to be notified of INotifications with a given name.

Parameters:
  • notification_name (str) – The name of the notification to register the observer for.

  • observer (IObserver) – The observer object to register.

Returns:

None

remove_mediator(mediator_name: str) IMediator

Remove an IMediator from the View.

Parameters:

mediator_name (str) – The name of the mediator to be removed.

Returns:

The removed mediator instance.

Return type:

IMediator

remove_observer(notification_name: str, notify_context: Any)

Remove the observer for a given notify_context from an observer list for a given Notification name.

Parameters:
  • notification_name (str) – The name of the notification to remove the observer from.

  • notify_context (Any) – The context object of the observer to remove.

Returns:

None

classmethod remove_view(key: str)

Remove an IView instance

Parameters:

key – The key of the view to be removed.

Returns:

None

retrieve_mediator(mediator_name: str) IMediator

Retrieve an IMediator from the View.

Parameters:

mediator_name (str) – The name of the mediator to retrieve.

Returns:

The mediator with the given name.

Return type:

IMediator

Module contents