Skip to content

Commit

Permalink
rework for eliminating lib 'collections'
Browse files Browse the repository at this point in the history
  • Loading branch information
JK-de committed Feb 22, 2018
1 parent 0f51fcb commit baa4589
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
22 changes: 16 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def _httpHandlerTestPost(httpClient, httpResponse) :
content = content )


@MicroWebSrv.route('/edit/<index>') # <IP>/edit/123 -> args.index=123
@MicroWebSrv.route('/edit/<index>/abc/<foo>') # <IP>/edit/123/abc/bar -> args.index=123 args.foo='bar'
@MicroWebSrv.route('/edit') # <IP>/edit -> args=None
def _httpHandlerEditWithArgs(httpClient, httpResponse, args=None) :
@MicroWebSrv.route('/edit/<index>') # <IP>/edit/123 -> args['index']=123
@MicroWebSrv.route('/edit/<index>/abc/<foo>') # <IP>/edit/123/abc/bar -> args['index']=123 args['foo']='bar'
@MicroWebSrv.route('/edit') # <IP>/edit -> args={}
def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :
content = """\
<!DOCTYPE html>
<html lang=en>
Expand All @@ -68,10 +68,20 @@ def _httpHandlerEditWithArgs(httpClient, httpResponse, args=None) :
<title>TEST EDIT</title>
</head>
<body>
<h1>EDIT item with {} arguments and index = {}</h1>
"""
content += "<h1>EDIT item with {} variable arguments</h1>"\
.format(len(args))

if 'index' in args :
content += "<p>index = {}</p>".format(args['index'])

if 'foo' in args :
content += "<p>foo = {}</p>".format(args['foo'])

content += """
</body>
</html>
""".format(len(args) if args else 'no', args.index if args else 'default')
"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
Expand Down
23 changes: 14 additions & 9 deletions microWebSrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from _thread import start_new_thread
import socket
import gc
import collections
import re

try :
Expand All @@ -22,6 +21,15 @@
except :
pass

class MicroWebSrvRoute :
def __init__(self, route, method, func, routeArgNames, routeRegex) :
self.route = route
self.method = method
self.func = func
self.routeArgNames = routeArgNames
self.routeRegex = routeRegex


class MicroWebSrv :

# ============================================================================
Expand Down Expand Up @@ -180,7 +188,6 @@ def __init__( self,
self.WebSocketThreaded = True
self.AcceptWebSocketCallback = None

routeHandlersCls = collections.namedtuple('routeHandlers', 'route method func routeArgNames routeRegex')
self._routeHandlers = []
routeHandlers += self._docoratedRouteHandlers
for route, method, func in routeHandlers :
Expand All @@ -198,7 +205,7 @@ def __init__( self,
# -> '/users/(\w*)/addresses/(\w*)/test/(\w*)$'
routeRegex = re.compile(routeRegex)

self._routeHandlers.append(routeHandlersCls(route, method, func, routeArgNames, routeRegex))
self._routeHandlers.append(MicroWebSrvRoute(route, method, func, routeArgNames, routeRegex))

# ============================================================================
# ===( Server Process )=======================================================
Expand Down Expand Up @@ -267,20 +274,18 @@ def GetRouteHandler(self, resUrl, method) :
resUrl = resUrl[:-1]
method = method.upper()
for rh in self._routeHandlers :
if len(rh) == 5 and rh.method == method :
if rh.method == method :
m = rh.routeRegex.match(resUrl)
if m : # found matching route?
if rh.routeArgNames :
routeArgValues = []
for i in range(len(rh.routeArgNames)) :
routeArgs = {}
for i, name in enumerate(rh.routeArgNames) :
value = m.group(i+1)
try :
value = int(value)
except :
pass
routeArgValues.append(value)
routeArgCls = collections.namedtuple('routeArg', rh.routeArgNames)
routeArgs = routeArgCls(*routeArgValues)
routeArgs[name] = value
return (rh.func, routeArgs)
else :
return (rh.func, None)
Expand Down

0 comments on commit baa4589

Please sign in to comment.