def __init__(self, config, opts, args): """ Initialization :Parameters: - `config`: Configuration - `opts`: Command line options - `args`: Positioned command line arguments :Types: - `config`: `wtf.config.Config` - `opts`: ``optparse.OptionContainer`` - `args`: ``list`` """ self.config, self.opts, self.args = config, opts, args self._resolver = _util.load_dotted(config.app.resolver)( config, opts, args ) self._request = _util.load_dotted( config.app('request', 'wtf.app.request.Request')) self._response = _util.load_dotted( config.app('response', 'wtf.app.response.Response')) if 'codec' in config.app and 'cookie' in config.app.codec: cookie_codec = config.app.codec.cookie.encode('ascii') else: cookie_codec = 'wtf.app.cookie.DefaultCookie' self._addenv = { 'wtf.codec.cookie': _util.load_dotted(cookie_codec)(config, opts, args)(), }
def factory(cls, config, opts, args): """ Call the main application factory based on the configuration :Parameters: - `config`: Configuration - `opts`: Command line options - `args`: Positioned command line arguments :Types: - `config`: `wtf.config.Config` - `opts`: ``optparse.OptionContainer`` - `args`: ``list`` :return: New Application instance :rtype: `Application` """ from wtf import services, util # BEWARE: Order is important here - first the services, then the app. # This is because the app modules may depend on initialized services # on top level. manager = services.init(config, opts, args, config.wtf("services", ())) app = manager.apply(util.load_dotted(config.wtf.application)(config, opts, args)) return cls(manager, app)
def driver(): """ Get this module :Return: The module :Rtype: ``module`` """ return _wtf_util.load_dotted(__name__)
def __init__(self, config, opts, args): """ :See: `wtf.services.ServiceInterface.__init__` """ if 'session' in config and not config.session('enable', True): self._storage = None else: if 'session' in config and 'storage' in config.session: storage = config.session.storage else: storage = 'wtf.app.services.session_storage.cookie.Cookie' self._storage = _util.load_dotted(storage)(config, opts, args)
def frommodule(cls, base, isfile=False): """ Determine a resource relative to a module :Parameters: - `base`: The combined module and path. Like: ``wtf.app.sample:static``. This resolves to the static subdirectory of the wtf.app.sample package. If the last part is a module, the directory is treated as parallel to the module, Like: ``wtf.app.sample.index:static`` resolves to the static subdir of wtf.app.sample, too; parallel to index.py. - `isfile`: Does it refer to a file? :Types: - `base`: ``unicode`` - `isfile`: Does it refer to a file? :return: New resource instance :rtype: `Resource` :Exceptions: - `ImportError`: Module could not be imported - `BuiltinModuleError`: Module doesn't have a __file__ attribute - `UnicodeError`: Recoding according to the locale failed. """ base = unicode(base) tup = base.split(u':', 1) if len(tup) == 1: modname, reldir = base, None else: modname, reldir = tup modname = modname.encode('ascii') mod = _util.load_dotted(modname) try: modfile = mod.__file__ except AttributeError: raise BuiltinModuleError( "Cannot take builtin module %r as relative path reference" % (modname,) ) tup = [_os.path.normpath(_os.path.dirname( modfile.decode(cls._encoding).encode('utf-8')))] if reldir is not None: reldir = _os.path.normpath(reldir.encode('utf-8')) root = _os.path.normpath('/') while reldir.startswith(root): reldir = reldir[1:] tup.append(reldir) return cls(_os.path.join(*tup).decode('utf-8'), isfile=isfile)
def init(config, opts, args, services, module='__svc__'): """ Initialize services The function can only be called once (because the module will be only initialized once) :Parameters: - `config`: Configuration - `opts`: Command line options - `args`: Positioned command line arguments - `services`: List of services to initialize. The list items can either be classes (which are instanciated) or strings containing dotted class names (which will be loaded and instanciated). Service classes must implement the `ServiceInterface`. - `module`: Dotted module name, where global services are put into :Types: - `config`: `wtf.config.Config` - `opts`: ``optparse.OptionContainer`` - `args`: ``list`` - `services`: ``iterable`` - `module`: ``str`` :return: Service manager :rtype: `ServiceManager` """ _, fresh = _util.make_dotted(module) assert fresh, "Services already initialized" module, manager = module.split('.'), ServiceManager() for service in services: if isinstance(service, basestring): service = _util.load_dotted(str(service)) service = service(config, opts, args) manager.add(service) svc = service.global_service() if svc is not None: name, svc = svc name = module + name.split('.') if len(name) > 1: (prename, _), name = _util.make_dotted( '.'.join(name[:-1])), name[-1] if getattr(prename, name, None) is not None: raise ServiceError("%s.%s already exists" % (prename, name)) setattr(prename, name, svc) manager.finalize() return manager