Commits

Szymon Wróblewski committed 7201ab7

selecting adapters updated

Comments (0)

Files changed (5)

pygame_network/__init__.py

 import logging
-from importlib import import_module
 import message
 import syncobject
+import network
+import serialization
 from handler import Handler
+from network import Server, Client
 
 _logger = logging.getLogger(__name__)
 _network_module = None
 register = message.message_factory.register
 
 
-class Client(object):
-    def __new__(cls, *args, **kwargs):
-        "Create instance of Client class depending on selected network adapter"
-        b = cls.__bases__
-        if Client in b:  # creation by inheritance
-            i = b.index(Client) + 1
-            cls.__bases__ = b[:i] + (_network_module.Client,) + b[i:]
-            return super(cls, cls).__new__(cls, *args, **kwargs)
-        else:  # direct object creation
-            # can't assign to __bases__ - bugs.python.org/issue672115
-            return _network_module.Client(*args, **kwargs)
-
-
-class Server(object):
-    def __new__(cls, *args, **kwargs):
-        "Create instance of Server class depending on selected network adapter"
-        b = cls.__bases__
-        if Server in b:  # creation by inheritance
-            i = b.index(Server) + 1
-            cls.__bases__ = b[:i] + (_network_module.Server,) + b[i:]
-            return super(Server, cls).__new__(cls, *args, **kwargs)
-        else:  # direct object creation
-            # can't assign to __bases__ - bugs.python.org/issue672115
-            return _network_module.Server(*args, **kwargs)
-
-
-def _find_adapter(a_type, names):
-    """Find and return first found adapter
-
-    a_type - adapter type
-    names - string or list of strings with names of libraries
-    """
-    if isinstance(names, basestring):
-        names = (names,)
-    for name in names:
-        a_name = name + '_adapter'
-        try:
-            _logger.debug("Trying to import %s.%s", a_type, a_name)
-            return import_module('.'.join((__name__, a_type, a_name)))
-        except ImportError as e:
-            _logger.debug("%s: %s", e.__class__.__name__, e.message)
-        finally:
-            _logger.info("Using %s %s module", name, a_type)
-    else:
-        _logger.critical("Can't find any %s module", a_type)
-
-
 def init(events=False, event_val=1, logging_lvl=logging.INFO,
-         network=('enet',), serialization=('msgpack', 'json')):
+         network_m=('enet',), serialization_m=('msgpack', 'json')):
     """Initialize network library.
 
     events - allow sending Pygame events (default False)
     event_val - set event ID as event_val + pygame.USEREVENT (default 1)
     logging_lvl - level of logging messages (default logging.INFO, None to skip
                   initializing logging module
-    network - string or list of strings with names of network library adapters,
-              first available will be used
-    serialization - string or list of strings with names of serialization
+    network_m - string or list of strings with names of network
+              library adapters, first available will be used
+    serialization_m - string or list of strings with names of serialization
                     library adapters, first available will be used
 
     Note: Because of the dynamic loading of network library adapter, Client,
         logging.basicConfig(level=logging_lvl,
                             format='%(asctime)-8s %(levelname)-8s %(message)s',
                             datefmt='%H:%M:%S')
-    _network_module = _find_adapter('network', network)
-    _serialization_module = _find_adapter('serialization', serialization)
-    message.s_lib = _serialization_module
+    network.select_adapter(network_m)
+    if network._selected_adapter is not None:
+        _logger.info("Using %s",
+            network._selected_adapter.__name__.split('.')[-1])
+    else:
+        _logger.critical("Can't find any network module")
+    serialization.select_adapter(serialization_m)
+    if serialization._selected_adapter is not None:
+        _logger.info("Using %s",
+            serialization._selected_adapter.__name__.split('.')[-1])
+    else:
+        _logger.critical("Can't find any serialization module")
     if events:
         _logger.info("Enabling pygame events")
         import event

pygame_network/_utils.py

+import logging
+from importlib import import_module
+
+_logger = logging.getLogger(__name__)
+
+def select_adapter(a_type, names):
+    """Return first found adapter
+
+    a_type - adapter type
+    names - string or list of strings with names of libraries
+    """
+    if isinstance(names, basestring):
+        names = (names,)
+    for name in names:
+        a_name = name + '_adapter'
+        try:
+            return import_module('.'.join((a_type, a_name)))
+        except ImportError as e:
+            _logger.debug("%s: %s", e.__class__.__name__, e.message)

pygame_network/message.py

 import logging
 from collections import namedtuple
 from weakref import WeakKeyDictionary, WeakValueDictionary
+import serialization
 
 __all__ = ('MessageFactory', 'MessageError')
 _logger = logging.getLogger(__name__)
-s_lib = None
 
 
 class MessageError(Exception):
         """
         type_id = self._message_params[message.__class__][0]
         message = (type_id,) + message
-        data = s_lib.pack(message)
+        data = serialization.pack(message)
         _logger.debug("Packing message (length: %d)", len(data))
         return data
 
         self._frozen = True
 
     def reset_context(self, context):
-        context._unpacker = s_lib.unpacker()
+        context._unpacker = serialization.unpacker()
 
     def _process_message(self, message):
         try:
         """
         _logger.debug("Unpacking message (length: %d)", len(data))
         try:
-            message = s_lib.unpack(data)
+            message = serialization.unpack(data)
         except:
             _logger.error('Data corrupted')
             self._reset_unpacker()  # prevent from corrupting next data
                 ids = self._message_types.keys()
                 ids.sort()
                 l = list()
-                l.append(s_lib.__name__)
+                l.append(serialization._selected_adapter.__name__)
                 for i in ids:
                     p = self._message_types[i]
                     l.append((i, p.__name__, p._fields))

pygame_network/network/__init__.py

+from .. import _utils
+
+_selected_adapter = None
+
+
+def select_adapter(names):
+    global _selected_adapter
+    _selected_adapter = _utils.select_adapter(__name__, names)
+
+
+class Client(object):
+    def __new__(cls, *args, **kwargs):
+        "Create instance of Client class depending on selected network adapter"
+        b = cls.__bases__
+        if Client in b:  # creation by inheritance
+            i = b.index(Client) + 1
+            cls.__bases__ = b[:i] + (_selected_adapter.Client,) + b[i:]
+            return super(cls, cls).__new__(cls, *args, **kwargs)
+        else:  # direct object creation
+            # can't assign to __bases__ - bugs.python.org/issue672115
+            return _selected_adapter.Client(*args, **kwargs)
+
+
+class Server(object):
+    def __new__(cls, *args, **kwargs):
+        "Create instance of Server class depending on selected network adapter"
+        b = cls.__bases__
+        if Server in b:  # creation by inheritance
+            i = b.index(Server) + 1
+            cls.__bases__ = b[:i] + (_selected_adapter.Server,) + b[i:]
+            return super(Server, cls).__new__(cls, *args, **kwargs)
+        else:  # direct object creation
+            # can't assign to __bases__ - bugs.python.org/issue672115
+            return _selected_adapter.Server(*args, **kwargs)

pygame_network/serialization/__init__.py

+from .. import _utils
+
+_selected_adapter = None
+pack = unpack = unpacker = None
+
+
+def select_adapter(names):
+    global _selected_adapter, pack, unpack, unpacker
+    _selected_adapter = _utils.select_adapter(__name__, names)
+    if _selected_adapter is not None:
+        pack = _selected_adapter.pack
+        unpack = _selected_adapter.unpack
+        unpacker = _selected_adapter.unpacker