Franck Pommereau

added dirs in resources

...@@ -73,8 +73,9 @@ class WatchDog (multiprocessing.Process) : ...@@ -73,8 +73,9 @@ class WatchDog (multiprocessing.Process) :
73 shutdown.set() 73 shutdown.set()
74 74
75 class BaseHTTPSimulator (Node) : 75 class BaseHTTPSimulator (Node) :
76 - def __init__ (self, net, port=8000, respatt=[], resdict={}) : 76 + def __init__ (self, net, port=8000, respatt=[]) :
77 self.res = {} 77 self.res = {}
78 + dirs = {}
78 for cls in reversed(inspect.getmro(self.__class__)[:-2]) : 79 for cls in reversed(inspect.getmro(self.__class__)[:-2]) :
79 path = os.path.dirname(inspect.getsourcefile(cls)) 80 path = os.path.dirname(inspect.getsourcefile(cls))
80 for pattern in respatt + ["resources/*.js", 81 for pattern in respatt + ["resources/*.js",
...@@ -82,10 +83,14 @@ class BaseHTTPSimulator (Node) : ...@@ -82,10 +83,14 @@ class BaseHTTPSimulator (Node) :
82 "resources/*.html", 83 "resources/*.html",
83 "resources/alive.txt"] : 84 "resources/alive.txt"] :
84 for res in glob.glob(os.path.join(path, pattern)) : 85 for res in glob.glob(os.path.join(path, pattern)) :
85 - with open(res) as infile : 86 + if os.path.isfile(res) :
86 - self.res[os.path.basename(res)] = infile.read() 87 + with open(res) as infile :
87 - self.res.update(resdict) 88 + self.res[os.path.basename(res)] = infile.read()
88 - Node.__init__(self, r=ResourceNode(self.res)) 89 + elif os.path.isdir(res) :
90 + dirs[os.path.basename(res)] = DirNode(res)
91 + else :
92 + raise ValueError("invalid resource %r" % res)
93 + Node.__init__(self, r=ResourceNode(self.res, dirs))
89 # create HTTP server 94 # create HTTP server
90 self.port = port 95 self.port = port
91 while True : 96 while True :
......
...@@ -89,8 +89,9 @@ class DirNode (Node) : ...@@ -89,8 +89,9 @@ class DirNode (Node) :
89 return handler 89 return handler
90 90
91 class ResourceNode (Node) : 91 class ResourceNode (Node) :
92 - def __init__ (self, data) : 92 + def __init__ (self, data, dirs) :
93 self.data = data 93 self.data = data
94 + self.dirs = dirs
94 self.ct = dict((path, mimetypes.guess_type(path)[0] 95 self.ct = dict((path, mimetypes.guess_type(path)[0]
95 or "application/octet-stream") 96 or "application/octet-stream")
96 for path in self.data) 97 for path in self.data)
...@@ -101,7 +102,12 @@ class ResourceNode (Node) : ...@@ -101,7 +102,12 @@ class ResourceNode (Node) :
101 return self.data[path] 102 return self.data[path]
102 return handler 103 return handler
103 else : 104 else :
104 - raise HTTPError(httplib.NOT_FOUND) 105 + try :
106 + base, child = path.split("/", 1)
107 + return self.dirs[base][child]
108 + except :
109 + raise HTTPError(httplib.NOT_FOUND)
110 +
105 111
106 ## 112 ##
107 ## 113 ##
......