Removed invalid logic and added additional error handling
This commit is contained in:
parent
90fa593be7
commit
47989941d8
1 changed files with 18 additions and 6 deletions
|
@ -1,14 +1,24 @@
|
||||||
import Logging
|
import ESock
|
||||||
|
|
||||||
BROADCAST = 0
|
BROADCAST = 0
|
||||||
ROUTE = 1
|
ROUTE = 1
|
||||||
SOCK = 2
|
SOCK = 2
|
||||||
|
|
||||||
|
|
||||||
class InvalidRouteSetup(AttributeError):
|
class InvalidRouteSetup(AttributeError):
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
super(AttributeError, self).__init__(msg)
|
super(AttributeError, self).__init__(msg)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidRouteLength(AttributeError):
|
||||||
|
def __init__(self, msg):
|
||||||
|
super(AttributeError, self).__init__(msg)
|
||||||
|
|
||||||
|
|
||||||
class ServerRoute:
|
class ServerRoute:
|
||||||
|
REQUIRED = []
|
||||||
|
PATCHED = False
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -18,27 +28,29 @@ class ServerRoute:
|
||||||
def start(self, handler):
|
def start(self, handler):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def stop(self, handler):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ClientRoute:
|
class ClientRoute:
|
||||||
def run(self, data, handler):
|
def run(self, data, handler):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def create_routes(routes, handler):
|
def create_routes(routes, handler):
|
||||||
routes = routes.copy()
|
routes = routes.copy()
|
||||||
reverse_routes = {}
|
reverse_routes = {}
|
||||||
for prefix in routes:
|
for prefix in routes:
|
||||||
|
if len(prefix) > ESock.MAX_ROUTE_LENGTH:
|
||||||
|
raise InvalidRouteLength("'%s' is too long (maximum: %d)" % (prefix, ESock.MAX_ROUTE_LENGTH))
|
||||||
if type(routes[prefix]) is tuple or type(routes[prefix]) is list:
|
if type(routes[prefix]) is tuple or type(routes[prefix]) is list:
|
||||||
routes[prefix] = routes[prefix][0](**routes[prefix][1])
|
routes[prefix] = routes[prefix][0](**routes[prefix][1])
|
||||||
reverse_routes[routes[prefix]] = prefix
|
reverse_routes[routes[prefix]] = prefix
|
||||||
for prefix in routes:
|
for prefix in routes:
|
||||||
attrs = dir(routes[prefix])
|
attrs = dir(routes[prefix])
|
||||||
if "REQUIRED" in attrs:
|
if not routes[prefix].PATCHED:
|
||||||
for required in type(routes[prefix]).REQUIRED:
|
for required in routes[prefix].REQUIRED:
|
||||||
if required == BROADCAST:
|
if required == BROADCAST:
|
||||||
routes[prefix].broadcast = handler.broadcast
|
routes[prefix].broadcast = handler.broadcast
|
||||||
elif required == ROUTE:
|
elif required == ROUTE:
|
||||||
routes[prefix].route = reverse_routes[routes[prefix]]
|
routes[prefix].route = reverse_routes[routes[prefix]]
|
||||||
|
routes[prefix].PATCHED = True
|
||||||
|
routes[prefix].start(handler)
|
||||||
return routes
|
return routes
|
Reference in a new issue