1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

Modified app to use a settings file.

This allows users to change the Solr URL and other things in one spot.
This commit is contained in:
Samuel Lai 2012-08-18 21:39:17 +10:00
parent 46100e7f01
commit c1a5382622
5 changed files with 74 additions and 46 deletions

View File

@ -24,6 +24,7 @@ import iso8601
import html5lib
from stackdump.models import Site, Badge, Comment, User
from stackdump import settings
# STATIC VARIABLES
BOTTLE_ROOT = os.path.abspath(os.path.dirname(sys.argv[0]))
@ -158,7 +159,7 @@ def uses_solr(fn):
'''
def init_solr():
if not hasattr(thread_locals, 'solr_conn'):
thread_locals.solr_conn = Solr("http://localhost:8983/solr/")
thread_locals.solr_conn = Solr(settings.SOLR_URL)
if not fn:
init_solr()
@ -181,8 +182,7 @@ def uses_db(fn):
'''
def init_db():
if not hasattr(thread_locals, 'db_conn'):
db_path = os.path.abspath(os.path.join(BOTTLE_ROOT, '../../../data/stackdump.sqlite'))
conn_str = 'sqlite://' + db_path
conn_str = settings.DATABASE_CONN_STR
thread_locals.db_conn = sqlhub.threadConnection = connectionForURI(conn_str)
if not fn:
@ -387,7 +387,7 @@ def view_question_redirect(site_key, question_id):
Redirects users from the long-form, proper URLs to the shorter one used
by Stackdump.
'''
redirect('%s%s/%s' % (settings['APP_URL_ROOT'], site_key, question_id))
redirect('%s%s/%s' % (settings.APP_URL_ROOT, site_key, question_id))
# END WEB REQUEST METHODS
@ -422,9 +422,9 @@ def render_template(template_path, context=None):
def get_template_settings():
template_settings = { }
keys = settings.get('TEMPLATE_SETTINGS', [ ])
keys = settings.TEMPLATE_SETTINGS
for k in keys:
template_settings[k] = settings.get(k, None)
template_settings[k] = getattr(settings, k, None)
return template_settings
@ -755,7 +755,7 @@ def rewrite_result(result):
The JSON must have been decoded first.
'''
app_url_root = settings.get('APP_URL_ROOT', '/')
app_url_root = settings.APP_URL_ROOT
# get a list of all the site base URLs
sites = list(Site.select())
@ -787,25 +787,15 @@ if __name__ == '__main__':
if os.environ.get('BOTTLE_CHILD', 'false') == 'true':
print('Serving media from: %s' % MEDIA_ROOT)
# load the settings file
__import__('settings')
if 'settings' in sys.modules.keys():
settings = sys.modules.get('settings')
settings = dict([ (k, getattr(settings, k)) for k in dir(settings) if not k.startswith('__') ])
else:
print('No settings file found; using defaults.')
settings = { }
if settings.get('DEBUG', False):
debug(True)
debug(settings.DEBUG)
# run the server!
server = settings.get('SERVER_ADAPTER', 'wsgiref')
server = settings.SERVER_ADAPTER
run(
server=server,
host=settings.get('SERVER_HOST', '0.0.0.0'),
port=settings.get('SERVER_PORT', 8080),
host=settings.SERVER_HOST,
port=settings.SERVER_PORT,
reloader=True
)

View File

@ -20,6 +20,7 @@ from sqlobject.styles import DefaultStyle
from pysolr import Solr
from stackdump.models import Site, Badge, Comment, User
from stackdump import settings
try:
# For Python < 2.6 or people using a newer version of simplejson
@ -568,17 +569,15 @@ if not os.path.exists(xml_root):
print('The given XML root path does not exist.')
sys.exit(1)
db_path = os.path.abspath(os.path.join(script_dir, '../../../../data/stackdump.sqlite'))
# connect to the database
print('Connecting to the database...')
conn_str = 'sqlite://' + db_path
conn_str = settings.DATABASE_CONN_STR
sqlhub.processConnection = connectionForURI(conn_str)
print('Connected.\n')
# connect to solr
print('Connecting to solr...')
solr = Solr("http://localhost:8983/solr/")
solr = Solr(settings.SOLR_URL)
print('Connected.\n')
# ensure required tables exist

View File

@ -12,17 +12,16 @@ from sqlobject import sqlhub, connectionForURI
from pysolr import Solr
from stackdump.models import Site
from stackdump import settings
script_dir = os.path.dirname(sys.argv[0])
# FUNCTIONS
def list_sites():
# connect to the data sources
db_path = os.path.abspath(os.path.join(script_dir, '../../../../data/stackdump.sqlite'))
# connect to the database
print('Connecting to the database...')
conn_str = 'sqlite://' + db_path
conn_str = settings.DATABASE_CONN_STR
sqlhub.processConnection = connectionForURI(conn_str)
print('Connected.\n')
@ -36,17 +35,15 @@ def list_sites():
def delete_site(site_key):
# connect to the data sources
db_path = os.path.abspath(os.path.join(script_dir, '../../../../data/stackdump.sqlite'))
# connect to the database
print('Connecting to the database...')
conn_str = 'sqlite://' + db_path
conn_str = settings.DATABASE_CONN_STR
sqlhub.processConnection = connectionForURI(conn_str)
print('Connected.\n')
# connect to solr
print('Connecting to solr...')
solr = Solr("http://localhost:8983/solr/")
solr = Solr(settings.SOLR_URL)
print('Connected.\n')
site_name = None

View File

@ -0,0 +1,35 @@
# This is the default settings file for stackdump.
#
# DO NOT CHANGE THIS FILE.
#
# Change the settings.py file; those settings will override the defaults in this
# file.
#
# This file is just like any other Python file, except the local variables form
# the settings dictionary.
DEBUG = False
# see http://bottlepy.org/docs/dev/tutorial.html#multi-threaded-server
SERVER_ADAPTER = 'cherrypy'
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8080
SOLR_URL = 'http://localhost:8983/solr/'
import os
DATABASE_CONN_STR = 'sqlite://%s/../../../data/stackdump.sqlite' % os.path.dirname(__file__)
# if the website is hosted under a subpath, specify it here. It must end with a
# slash.
APP_URL_ROOT = '/'
# number of comments to show before the rest are hidden behind a 'click to show'
# link
NUM_OF_DEFAULT_COMMENTS = 3
# settings that are available in templates
TEMPLATE_SETTINGS = [
'APP_URL_ROOT',
'NUM_OF_DEFAULT_COMMENTS'
]

View File

@ -1,25 +1,32 @@
# This is the settings file for stackdump.
#
# It is modelled after the Django settings file. This file is just like any
# other Python file, except the local variables form the settings dictionary.
# Uncomment lines from this file to override any of the default settings.
#
# This file is just like any other Python file, except the local variables form
# the settings dictionary.
DEBUG = True
# DO NOT remove this line - this line loads the default settings. Stackdump will
# not work without the default settings.
from default_settings import *
#DEBUG = False
# see http://bottlepy.org/docs/dev/tutorial.html#multi-threaded-server
SERVER_ADAPTER = 'cherrypy'
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8080
#SERVER_ADAPTER = 'cherrypy'
#SERVER_HOST = '0.0.0.0'
#SERVER_PORT = 8080
# uncomment if the default host and port for Solr is different.
#SOLR_URL = 'http://localhost:8983/solr/'
# uncomment if the database for Stackdump is not the default SQLite one
#import os
#DATABASE_CONN_STR = 'sqlite://%s/../../../data/stackdump.sqlite' % os.path.dirname(__file__)
# if the website is hosted under a subpath, specify it here. It must end with a
# slash.
APP_URL_ROOT = '/'
#APP_URL_ROOT = '/'
# number of comments to show before the rest are hidden behind a 'click to show'
# link
NUM_OF_DEFAULT_COMMENTS = 3
# settings that are available in templates
TEMPLATE_SETTINGS = [
'APP_URL_ROOT',
'NUM_OF_DEFAULT_COMMENTS'
]
#NUM_OF_DEFAULT_COMMENTS = 3