def create(parser, options, args): """Create the database tables of the application If the ``--drop`` option is on, delete the existing tables before to re-create them If the ``--no-populate`` option is off, call the populate function (if it exists) after the creation of the tables In: - ``parser`` -- the optparse.OptParser object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines : application name """ for database_settings, populate in read_options(options.debug, args, parser.error): database.set_metadata(*database_settings) with database.session.begin(): if options.drop: database_settings[0].drop_all() database_settings[0].create_all() if options.populate and populate: reference.load_object(populate)[0]()
def get_database(conf, debug): """Read the database settings The location of the metadata object is read from the configuration file In: - ``conf`` -- the ``ConfigObj`` object, created from the configuration file - ``debug`` -- debug mode for the database engine Return: - the tuple: - metadata object - database uri - database debug mode - database engines settings """ metadata = conf.get("metadata") if not conf["activated"] or not metadata: return None # Import the metadata object metadata = reference.load_object(metadata)[0] # All the parameters, of the [database] section, with an unknown name are # given to the database engine engine_conf = dict( [(k, v) for (k, v) in conf.items() if k not in ("uri", "activated", "metadata", "debug", "populate")] ) return (metadata, conf["uri"], debug, engine_conf)
def get_database(conf, debug): """Read the database settings The location of the metadata object is read from the configuration file In: - ``conf`` -- the ``ConfigObj`` object, created from the configuration file - ``debug`` -- debug mode for the database engine Return: - the tuple: - metadata object - database uri - database debug mode - database engines settings """ metadata = conf.get('metadata') if not conf['activated'] or not metadata: return None # Import the metadata object metadata = reference.load_object(metadata)[0] # All the parameters, of the [database] section, with an unknown name are # given to the database engine engine_conf = {k: v for k, v in conf.items() if k not in ('uri', 'activated', 'metadata', 'debug', 'populate')} return metadata, conf['uri'], debug, engine_conf
def run(parser, options, args): """launch an object In: - ``parser`` -- the ``optparse.OptParser`` object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines The unique argument is the path of the object to launch. The path syntax is described into the module ``nagare.admin.reference``. For example, ``/tmp/counter.py:Counter`` is the path to the class ``Counter`` of the module ``tmp.counter.py`` """ if len(args) != 2: parser.error('Bad number of arguments') if 'nagare_reloaded' not in os.environ: return reloader.restart_with_monitor() # With the ``serve-module`` command, the automatic reloader is always activated reloader.install(excluded_directories=(pkg_resources.get_default_cache(),)) # Load the object if os.path.sep in args[0]: path = 'file ' + args[0] else: path = 'python ' + args[0] app = reference.load_object(path)[0] # Wrap it into a WSGIApp app = wsgi.create_WSGIApp(app) # Always use the standalone publisher (Python HTTP server) publishers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.publishers')]) publisher = publishers['standalone'].load()() wsgi_pipe = debugged_app(app) if options.debug else app publisher.register_application(args[0], args[1], app, wsgi_pipe) app.set_config('', {'application': {'redirect_after_post': False, 'name': args[1], 'always_html': True}}, None) app.set_publisher(publisher) # Always use the standalone session manager (in memory sessions) sessions_managers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.sessions')]) sessions_manager = sessions_managers['standalone'].load()() app.set_sessions_manager(sessions_manager) # Set the application logger level to DEBUG log.configure({'logger': {'level': 'DEBUG'}}, args[1]) log.activate() log.set_logger('nagare.application.' + args[1]) # The static contents of the framework are served by the standalone server publisher.register_static('nagare', lambda path, r=pkg_resources.Requirement.parse('nagare'): get_file_from_package(r, path)) # Launch the object publisher.serve(None, dict(host=options.host, port=options.port), None)
def set_config(self, filename, conf, error): """Read the configuration parameters In: - ``filename`` -- the path to the configuration file - ``conf`` -- the ``ConfigObj`` object, created from the configuration file - ``error`` -- the function to call in case of configuration errors """ conf = {k: v for k, v in conf.iteritems() if k in self.spec} conf = configobj.ConfigObj(conf, configspec=self.spec) config.validate(filename, conf, error) self.states_history = conf['states_history'] self.security_cookie_name = conf['security_cookie_name'] pickler = reference.load_object(conf['pickler'])[0] unpickler = reference.load_object(conf['unpickler'])[0] serializer = reference.load_object(conf['serializer'])[0] self.serializer = serializer(pickler, unpickler) return conf
def create(parser, options, args): """Create the database tables of the application If the ``--drop`` option is on, delete the existing tables before to re-create them If the ``--no-populate`` option is off, call the populate function (if it exists) after the creation of the tables In: - ``parser`` -- the optparse.OptParser object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines : application name """ for (database_settings, populate) in read_options(options.debug, args, parser.error): database.set_metadata(*database_settings) with database.session.begin(): if options.drop: database_settings[0].drop_all() database_settings[0].create_all() if options.populate and populate: reference.load_object(populate)[0]()
def read_application(cfgfile, error): """Read the configuration file for the application and create the application object In: - ``cfgfile`` -- name of a registered application or path to an application configuration file - ``error`` -- the function to call in case of configuration errors Return: - a tuple: - name of the application configuration file - the application object - the distribution of the application - a ``ConfigObj`` of the application parameters """ if not os.path.isfile(cfgfile): # The name of a registered application is given, find its configuration file # Get all the registered applications apps = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points("nagare.applications")]) # Get the application app = apps.get(cfgfile) if app is None: error("application '%s' not found (use 'nagare-admin serve' to see the available applications)" % cfgfile) # From the directory of the application, get its configuration file requirement = pkg_resources.Requirement.parse(app.dist.project_name) cfgfile = pkg_resources.resource_filename(requirement, os.path.join("conf", cfgfile + ".cfg")) # Read the application configuration file aconf = read_application_options(cfgfile, error) # From the path of the application, create the application object (app, dist) = reference.load_object(aconf["application"]["path"]) defaults = dict(here='string(default="%s")' % os.path.abspath(os.path.dirname(cfgfile))) if dist is not None: defaults["root"] = 'string(default="%s")' % dist.location # Re-read the application configuration, with some substitution variables aconf = read_application_options(cfgfile, error, defaults) return (cfgfile, app, dist, aconf)
def create_wsgi_pipe(app, options, config_filename, config, error): """Wrap the application into one or more WSGI "middlewares" to create a WSGI pipe In: - ``app`` -- the application - ``options`` -- options in the command line - ``config_filename`` -- the path to the configuration file - ``config`` -- the ``ConfigObj`` object, created from the configuration file - ``error`` -- the function to call in case of configuration errors Return: - the wsgi pipe """ if options.debug or config['application']['debug']: app = debugged_app(app) wsgi_pipe = config['application']['wsgi_pipe'] if not wsgi_pipe: return app return reference.load_object(wsgi_pipe)[0](app, options, config_filename, config, error)
def set_config(self, config_filename, conf, error): """Process the configuration file In: - ``config_filename`` -- the path to the configuration file - ``config`` -- the ``ConfigObj`` object, created from the configuration file - ``error`` -- the function to call in case of configuration errors """ conf = configobj.ConfigObj(conf, configspec=configobj.ConfigObj(WSGIApp.spec)) config.validate(config_filename, conf, error) self.allow_extensions = conf['navigator']['allow_extensions'] self.nagare_sources = conf['navigator']['nagare_sources'] self.editor_config = dict([(k, str(v).lower() if isinstance(v, bool) else v) for (k, v) in conf['editor'].items()]) # Create and configure the security manager # ----------------------------------------- self.security = reference.load_object(conf['security']['manager'])[0]() self.security.set_config(config_filename, conf['security'], error) super(WSGIApp, self).set_config(config_filename, conf, error)
def __init__(self, app_title, app_banner, theme, assets_manager_service): cls = load_object(self.config['schema'])[0] self.ldap_engine = cls(self.config) self.error_message = '' self.assets_manager = assets_manager_service