def get(method, user): fn = Resolver().resolve(method) debug("Internal resolve to function '%s'" % (method), module="api-resolve-internal") if isinstance(fn, apimethod): return fn.as_user(user) else: glob = fn.func_globals.copy() glob.update({"user": user}) return new.function(fn.func_code, glob, argdefs=fn.func_defaults)
def walk_plugins(self, directory, apiprefix, prefix, ispkg=False): """ Go through the plugin directory and locate all modules A bit hackish, but it works """ # get all files and directories files = os.listdir(directory) # storage for modules to load modules = [] # storage for subdirectories to walk subdirs = [] for module in files: path = directory + "/" + module if os.path.isdir(path): subdirs.append((path, prefix + "." + module)) elif module == "__init__.py": ispkg = True # load __init__.py first to avoid Runtime errors modules.insert(0, ([apiprefix, prefix], path)) elif module.endswith(".py"): modules.append(([apiprefix, prefix, module[:-3]], path)) for module in modules: full_package_name = ".".join(module[0]) path = module[1] try: resolved_module = imp.load_source(full_package_name, path) lookup_name = full_package_name[len(apiprefix)+1:] self.cache[lookup_name] = resolved_module except Exception, e: if isinstance(e, SyntaxError): debug("Syntax error in '%s' on line %d" % (full_package_name, e.lineno), module="plugin-load") else: debug("Exception in '%s' %s" % (full_package_name, repr(e)), module="plugin-load") # skip further imports from submodule ispkg = False
def resolve(self, method, orig_method=None): if orig_method is None: orig_method = method try: if "." in method: (ns, method) = method.rsplit(".", 1) resolved = getattr(self.cache[ns], method) else: resolved = getattr(self.nsmain, method) except Exception, e: debug("Failure resolving call to function '%s'" % (orig_method), module="api-resolve") ex = MethodNotFoundException() ex.message = "Method not found: %s" % orig_method raise ex
def http_service(env, resp): path=env["PATH_INFO"] shortpath = path[1:].split("/") shortpath = "/" + shortpath[0] if shortpath not in services: if webroot is None: # If webroot is not defined, send a server error back resp(http_codes[501], [("Content-type", "text/html")]) return ["<h1>HTTP " + http_codes[501] + "</h1>",] else: filename = webroot + path debug(env["REQUEST_METHOD"] + " " + path, params=env["REMOTE_ADDR"], module="webserver") if os.path.isdir(filename): if filename[-1] != "/": resp(http_codes[301], [("Location", path + "/")]) debug(http_codes[301], params=filename, module="webserver") return ["<h1>HTTP "+http_codes[301]+"</h1>"] filename = os.path.dirname(filename) + "/index.html" if not os.path.isfile(filename): resp(http_codes[404], [("Content-type", "text/html")]) debug(http_codes[404], params=filename, module="webserver") return ["<h1>HTTP "+http_codes[404]+"</h1>",] (mime, enc) = mimetypes.guess_type(filename) if mime is None: mime = "application/octet-stream" resp(http_codes[200], [("Content-type", mime)]) f = file(filename,'rb') ret = [f.read(),] f.close() return ret else: try: length=int(env.get('CONTENT_LENGTH', '0')) except ValueError: length=0 postbody=env['wsgi.input'].read(length) (out, err) = services[shortpath].handleRequest(postbody, env, shortpath) resp(http_codes[err], [("Content-type", services[shortpath].content_type), ("Content-length", str(len(out)))]) return [out]
config = goldmine.config(configfile) if options["<port>"]: config["services"]["http"]["port"] = options["<port>"] if options["--engine"]: config["services"]["http"]["engine"] = options["--engine"] from goldmine.controller import Controller from goldmine.services.http import HTTPService from goldmine.protocols.json import JSONRPCProtocol from goldmine.protocols.rest import RESTProtocol info = (config["services"]["http"]["port"], config["services"]["http"]["engine"]) goldmine.debug("http_service.py starting on port %s with %s" % info, module="http-service") HTTPService( { "/service": JSONRPCProtocol(Controller), "/json": JSONRPCProtocol(Controller), "/rest": RESTProtocol(Controller), }, server=config["services"]["http"]["engine"], port=int(config["services"]["http"]["port"]), webroot=config["services"]["http"]["webroot"] )
def __storm_flushed__(self): if config().debug and "storm-save" not in config()["debug_exclude"]: debug(self.__class__.__name__, module = "storm-save", params = "\n" + pprint.pformat(self.__serialize__()))
def execute(self, method, *args, **kwargs): debug("%s.%s%s" % (method.__module__, method.__name__, str(args)), module="api-execute") return method(*args, **kwargs)
def __init__ (self): self.token = None self.resolver = Resolver() def set_token(self, token): self.token = token def get_method(self, method): resolved = self.resolver.resolve(method) try: return resolved.token(self.token) except MethodNotFoundException, e1: raise except AttributeError, e2: raise MethodNotFoundException() except UnauthorizedException, e3: debug("Unauthorized call to %s" % (method), module="api-resolve") raise def execute(self, method, *args, **kwargs): debug("%s.%s%s" % (method.__module__, method.__name__, str(args)), module="api-execute") return method(*args, **kwargs) def on_success(self): db().commit() def on_failure(self): db().rollback()