Login

Settings file switcher

Author:
anton
Posted:
March 31, 2008
Language:
Python
Version:
.96
Score:
6 (after 6 ratings)

Changes settings file according to server hostname.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import socket, platform
hostname = socket.gethostname().replace('.','_').replace('-', '_')

try:
  import os
  path = "settings_files/%s.py" % hostname
  path = os.path.join(os.path.dirname(__file__), path)
  execfile(path)
except ImportError, e:
  import sys
  sys.stderr.write("Unable to read settings_files/%s.py\n" % hostname)
  sys.exit(1)

More like this

  1. Form field with fixed value by roam 1 week, 5 days ago
  2. New Snippet! by Antoliny0919 2 weeks, 4 days ago
  3. Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months, 1 week ago
  4. get_object_or_none by azwdevops 6 months, 4 weeks ago
  5. Mask sensitive data from logger by agusmakmun 8 months, 3 weeks ago

Comments

itavor (on April 3, 2008):

A variation:

import socket
hostname = socket.gethostname().replace('-', '_').split('.')
while True:
    try:
        exec 'from host_settings.%s import *' % '_'.join(hostname)
        break
    except ImportError:
        hostname = hostname[:-1]

Lets you use shorter names - for example, mysite.py for mysite.com.au, or www_mysite.py for www.mysite.com.

#

Please login first before commenting.