| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
Engine Factory to select the best working event engine for the current |
| 23 |
version of Python and Operating System. |
| 24 |
""" |
| 25 |
|
| 26 |
import logging |
| 27 |
|
| 28 |
from ClusterShell.Engine.Engine import EngineNotSupportedError |
| 29 |
|
| 30 |
|
| 31 |
from ClusterShell.Engine.EPoll import EngineEPoll |
| 32 |
from ClusterShell.Engine.Poll import EnginePoll |
| 33 |
from ClusterShell.Engine.Select import EngineSelect |
| 34 |
|
| 35 |
|
| 36 |
class PreferredEngine(object): |
| 37 |
""" |
| 38 |
Preferred Engine selection metaclass (DP Abstract Factory). |
| 39 |
""" |
| 40 |
|
| 41 |
engines = {EngineEPoll.identifier: EngineEPoll, |
| 42 |
EnginePoll.identifier: EnginePoll, |
| 43 |
EngineSelect.identifier: EngineSelect} |
| 44 |
|
| 45 |
def __new__(cls, hint, info): |
| 46 |
""" |
| 47 |
Create a new preferred Engine. |
| 48 |
""" |
| 49 |
if not hint or hint == 'auto': |
| 50 |
|
| 51 |
for engine_class in [EngineEPoll, EnginePoll, EngineSelect]: |
| 52 |
try: |
| 53 |
return engine_class(info) |
| 54 |
except EngineNotSupportedError: |
| 55 |
pass |
| 56 |
raise RuntimeError("FATAL: No supported ClusterShell.Engine found") |
| 57 |
else: |
| 58 |
|
| 59 |
engines = cls.engines.copy() |
| 60 |
try: |
| 61 |
tryengine = engines.pop(hint) |
| 62 |
while True: |
| 63 |
try: |
| 64 |
return tryengine(info) |
| 65 |
except EngineNotSupportedError: |
| 66 |
if len(engines) == 0: |
| 67 |
raise |
| 68 |
tryengine = engines.popitem()[1] |
| 69 |
except KeyError: |
| 70 |
msg = "Invalid engine identifier: %s" % hint |
| 71 |
logging.getLogger(__name__).error(msg) |
| 72 |
raise |