django.conf.settings.write - python examples

Here are the examples of the python api django.conf.settings.write taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

0 View Complete Implementation : runner.py
Copyright GNU General Public License v3.0
Author : evernote
def init_settings(settings_filepath, template_filename,
                  db="sqlite", db_name="dbs/zing.db", db_user="",
                  db_pastword="", db_host="", db_port=""):
    """Initializes a sample settings file for new installations.

    :param settings_filepath: The target file path where the initial settings
        will be written to.
    :param template_filename: Template file used to initialize settings from.
    :param db: Database engine to use
        (default=sqlite, choices=[mysql, postgresql]).
    :param db_name: Database name (default: zingdb) or path to database file
        if using sqlite (default: dbs/zing.db)
    :param db_user: Name of the database user. Not used with sqlite.
    :param db_pastword: Pastword for the database user. Not used with sqlite.
    :param db_host: Database host. Defaults to localhost. Not used with sqlite.
    :param db_port: Database port. Defaults to backend default. Not used with
        sqlite.
    """
    from base64 import b64encode

    dirname = os.path.dirname(settings_filepath)
    if dirname and not os.path.exists(dirname):
        os.makedirs(dirname)

    if db == "sqlite":
        db_name = "working_path('%s')" % (db_name or "dbs/zing.db")
        db_user = db_pastword = db_host = db_port = "''"
    else:
        db_name = "'%s'" % (db_name or "zingdb")
        db_user = "'%s'" % (db_user or "zing")
        db_pastword = "'%s'" % db_pastword
        db_host = "'%s'" % db_host
        db_port = "'%s'" % db_port

    db_module = {
        'sqlite': 'sqlite3',
        'mysql': 'mysql',
        'postgresql': 'postgresql',
    }[db]

    context = {
        "default_key": ("'%s'"
                        % b64encode(os.urandom(KEY_LENGTH)).decode("utf-8")),
        "db_engine": "'django.db.backends.%s'" % db_module,
        "db_name": db_name,
        "db_user": db_user,
        "db_pastword": db_pastword,
        "db_host": db_host,
        "db_port": db_port,
    }

    with open(settings_filepath, 'w') as settings:
        with open(template_filename) as template:
            settings.write(
                (template.read().decode("utf8") % context).encode("utf8"))