Debian Codegraph @

lib/ClusterShell/Engine/Factory.py

raw symbols SCIP
1 #
2 # Copyright (C) 2009-2016 CEA/DAM
3 # Copyright (C) 2016 Stephane Thiell <[email protected]>
4 #
5 # This file is part of ClusterShell.
6 #
7 # ClusterShell is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # ClusterShell is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with ClusterShell; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 # Available event engines
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 # in order or preference
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 # User overriding engine selection
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