Example #1
0
 def do_GET(self):
     path, args = parse_url(self.path)
     if not path:
         path = ["index"]
     name_path = path[0].replace(".", "_")
     if len(path) > 1:
         rest = os.path.sep.join(path[1:])
     else:
         rest = None
     method_to_call = getattr(self, name_path, None)
     if method_to_call is None or not getattr(method_to_call, "exposed", None):
         exec_meth = getattr(self.exported_methods, name_path, None)
         if exec_meth is None:
             self.send_error(404, "File %s not found" % path)
         else:
             self.serve_data("text/json", json.write(exec_meth(**args)), True)
     else:
         if rest:
             outp = method_to_call(rest, **args)
         else:
             outp = method_to_call(**args)
         if isinstance(outp, (str, unicode)):
             self.serve_data("text/html", outp)
         elif isinstance(outp, tuple):
             self.serve_data(*outp)
         else:
             raise ValueError("Don't know how to serve %s" % (outp,))
Example #2
0
 def do_GET(self):
     path, args = parse_url(self.path)
     if not path:
         path = ["index"]
     name_path = path[0].replace(".", "_")
     if len(path) > 1:
         rest = os.path.sep.join(path[1:])
     else:
         rest = None
     method_to_call = getattr(self, name_path, None)
     if method_to_call is None or not getattr(method_to_call, 'exposed',
                                              None):
         exec_meth = getattr(self.exported_methods, name_path, None)
         if exec_meth is None:
             self.send_error(404, "File %s not found" % path)
         else:
             self.serve_data('text/json', json.write(exec_meth(**args)),
                             True)
     else:
         if rest:
             outp = method_to_call(rest, **args)
         else:
             outp = method_to_call(**args)
         if isinstance(outp, (str, unicode)):
             self.serve_data('text/html', outp)
         elif isinstance(outp, tuple):
             self.serve_data(*outp)
         else:
             raise ValueError("Don't know how to serve %s" % (outp, ))
Example #3
0
    def traverse(self, path, orgpath):
        """ traverse path relative to self

            'path' is the path requested by the client, split on '/', but
            relative from the current object: parent Collection items may have
            removed items (they will have, actually, unless 'self' is the root
            of the website) from the beginning on traversal to 'self'

            path is split on '/', the first item is removed and used to do
            a lookup on self, if that fails a 404 is raised, if successful
            the item is used to continue traversal (if the object found is
            a Collection type) or to handle the request (if the object found
            is a callable with .exposed set to True)

            if path equals '', a lookup for 'index' is done

            can be overridden in subclasses to implement different path
            handling (PATH_INFO-like stuff)
        """
        name = path.pop(0)
        name = name.replace(".", "_")
        resource = getattr(self, name, None)
        if not resource:
            raise HTTPError(404)
        return lambda **args: ("text/json", json.write(resource(**args)))
Example #4
0
    def traverse(self, path, orgpath):
        """ traverse path relative to self

            'path' is the path requested by the client, split on '/', but
            relative from the current object: parent Collection items may have
            removed items (they will have, actually, unless 'self' is the root
            of the website) from the beginning on traversal to 'self'

            path is split on '/', the first item is removed and used to do
            a lookup on self, if that fails a 404 is raised, if successful
            the item is used to continue traversal (if the object found is
            a Collection type) or to handle the request (if the object found
            is a callable with .exposed set to True)

            if path equals '', a lookup for 'index' is done

            can be overridden in subclasses to implement different path
            handling (PATH_INFO-like stuff)
        """
        name = path.pop(0)
        name = name.replace(".", "_")
        resource = getattr(self, name, None)
        if not resource:
            raise HTTPError(404)
        return lambda **args: ('text/json', json.write(resource(**args)))