-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a FactoryHelper constraint helper
This constraints helper allows the user to define a function (or any callable) which will be invoked as-needed to generate the constraints for the layout.
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2014, Nucleic Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#------------------------------------------------------------------------------ | ||
from atom.api import Callable, Tuple, Dict | ||
|
||
from .constraint_helper import ConstraintHelper | ||
|
||
|
||
class FactoryHelper(ConstraintHelper): | ||
""" A constraint helper which delegates to a factory callable. | ||
""" | ||
#: The callable object which will generate the list of constraints. | ||
#: It will be passed the constraints widget owner as the first | ||
#: argument, and should return the list of generated constraints. | ||
factory = Callable() | ||
|
||
#: Additional positional arguments to pass to the callable. | ||
args = Tuple() | ||
|
||
#: Additional keyword arguments to pass to the callable. | ||
kwargs = Dict() | ||
|
||
def __init__(self, factory, *args, **kwargs): | ||
""" Initialize a FactoryHelper. | ||
Parameters | ||
---------- | ||
factory : callable | ||
The callable object which generates the constraints. | ||
*args | ||
Additional positional arguments to pass to the factory. | ||
**kwargs | ||
Additional keyword arguments to pass to the factory. | ||
""" | ||
self.factory = factory | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def constraints(self, component): | ||
""" Generate the constraints using the underlying factory. | ||
This method will automatically expand ConstraintHelper objects | ||
which are generated by the factory. | ||
""" | ||
factory = self.factory | ||
if factory is None: | ||
return [] | ||
cns = [] # inline `expand_constraints` | ||
for cn in factory(component, *self.args, **self.kwargs): | ||
if isinstance(cn, ConstraintHelper): | ||
cns.extend(cn.create_constraints(component)) | ||
elif cn is not None: | ||
cns.append(cn) | ||
return cns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters