Fusion
The library is experimental and subject to API changes.
This module holds code common for writing cross platform apps in Python that can use different GUI frameworks (currently only used with Qt). The aim is not to make a new GUI framework, but to abstract away and experiment with some technologies and architecture patterns. It's used in the Pamet app.
Installation
pip install python-fusion
Usage
The lib helps implement something of a mix between the MVP and Redux models. And it's pretty close to the classic MVC.
-
Views (implemented as e.g. Qt widgets) have an explicitly defined state (that subclass ViewState), which can only be changed inside actions.
-
Actions are just a set of functions, and correspond to the controller in MVC, presenter in MVP, and ..actions in redux.
-
Changes to the view states are aggregated and sent back to the views asynchronously.
-
The base class for the model is
Entity. It has serialization/deserialization functionality among other utils. -
The channels library provides a pubsub mechanism, that's used by the state manager.
-
There's an action recorder class (ActionCamera), which can be used to record actions and generate test scripts. See the test suite in Pamet for reference.
Testing
A pytest test suite is available
Example
Taken from the test suite:
def test_view_state_updates_and_diffing():
main_loop = NoMainLoop()
fusion.set_main_loop(main_loop)
expected_raw_state_changes = []
children = []
children_left = []
@view_state_type
class MockViewState(ViewState):
test_field: str = 'not_set'
child_states: set = field(default_factory=set)
@action('dummy_nested')
def dummy_nested():
pass
@action('add_children') # Is called nested in create_view_state
def add_children(parent_state: MockViewState):
parent_state.test_field = 'set'
for i in range(3):
child = MockViewState()
parent_state.child_states.add(child)
children.append(child)
change = fsm.add_state(child)
expected_raw_state_changes.append(change)
change = fsm.update_state(parent_state)
expected_raw_state_changes.append(change)
@action('create_view_state')
def create_view_state():
parent_state = MockViewState()
change = fsm.add_state(parent_state)
expected_raw_state_changes.append(change)
add_children(parent_state)
dummy_nested()
return parent_state
@action('remove_child')
def remove_child(parent_state):
removed_child = parent_state.child_states.pop()
children_left.extend(list(parent_state.child_states))
change = fsm.remove_state(removed_child)
expected_raw_state_changes.append(change)
change = fsm.update_state(parent_state)
expected_raw_state_changes.append(change)
return removed_child
expected_top_level_action_functions = [create_view_state, remove_child]
completed_root_level_action_functions = []
received_raw_state_changes = []
def handle_completed_root_actions(action: ActionCall):
completed_root_level_action_functions.append(
wrapped_action_by_name(action.name))
def handle_raw_state_changes(change):
received_raw_state_changes.append(change)
def handle_state_change(change):
state: MockViewState = change.last_state()
assert change.updated.test_field
assert state.test_field == 'set'
assert set(children) == set(change.added.child_states)
actions_lib.completed_root_actions.subscribe(handle_completed_root_actions)
fsm.raw_state_changes.subscribe(handle_raw_state_changes)
parent_state = create_view_state()
subscription = fsm.state_changes_per_TLA_by_view_id.subscribe(
handle_state_change, index_val=parent_state.id)
main_loop.process_events()
removed_child = remove_child(parent_state)
def handle_state_change(change):
state: MockViewState = change.last_state()
assert set([removed_child]) == \
set(change.removed.child_states)
assert state.child_states == set(children_left)
subscription.unsubscribe()
fsm.state_changes_per_TLA_by_view_id.subscribe(handle_state_change,
index_val=parent_state.id)
main_loop.process_events()
assert completed_root_level_action_functions == \
expected_top_level_action_functions
Metadata
Release files for python-fusion 0.7.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python-fusion-0.7.5.tar.gz | 40.0 kB | Without provenance | Details for python-fusion-0.7.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| python_fusion-0.7.5-py3-none-any.whl | Python 3 | none | any | Details for python_fusion-0.7.5-py3-none-any.whl |
Total release size: 84.3 kB
Release files / python-fusion-0.7.5.tar.gz
| Download URL | python-fusion-0.7.5.tar.gz |
|---|---|
| Size | 40.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19465696b5d8a5dc0dfcb86335fce6acb644ab41186a5f8db210038e012ddc0d
|
|
BLAKE2b-256 checksum How to use checksums |
9da2376e3784b4d81d0bb1074cc711a92df37717d7c61f47e424160264df1480
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.9
|
Release files / python_fusion-0.7.5-py3-none-any.whl
| Download URL | python_fusion-0.7.5-py3-none-any.whl |
|---|---|
| Size | 44.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aff213ba131e796e9e87a89170e067f12d439a1e846175a81fce5b645ed0e0ed
|
|
BLAKE2b-256 checksum How to use checksums |
eebcc975925846d648ebcc5dd85648158403a810802509b1bf94a8e64dd0ec82
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.9
|
Security for python-fusion 0.7.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.