Merge pull request #139 from nakato/DBURI
Use properties in config for pass-through compat vars
This commit is contained in:
commit
701cadcb46
|
@ -164,7 +164,7 @@ def error_handler(e):
|
||||||
|
|
||||||
def create_app(config=None):
|
def create_app(config=None):
|
||||||
app = Application(__name__)
|
app = Application(__name__)
|
||||||
app.config.from_object('realms.config')
|
app.config.from_object('realms.config.conf')
|
||||||
app.url_map.converters['regex'] = RegexConverter
|
app.url_map.converters['regex'] = RegexConverter
|
||||||
app.url_map.strict_slashes = False
|
app.url_map.strict_slashes = False
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ import pip
|
||||||
import time
|
import time
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
config = config.conf
|
||||||
|
|
||||||
# called to discover commands in modules
|
# called to discover commands in modules
|
||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
|
|
|
@ -1,65 +1,14 @@
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
|
||||||
from realms.lib.util import in_vagrant
|
from realms.lib.util import in_vagrant
|
||||||
|
|
||||||
|
|
||||||
def update(data):
|
class Config(object):
|
||||||
conf = read()
|
|
||||||
conf.update(data)
|
|
||||||
return save(data)
|
|
||||||
|
|
||||||
|
|
||||||
def read():
|
|
||||||
conf = dict()
|
|
||||||
|
|
||||||
for k, v in os.environ.items():
|
|
||||||
if k.startswith('REALMS_'):
|
|
||||||
conf[k[7:]] = v
|
|
||||||
|
|
||||||
loc = get_path()
|
|
||||||
|
|
||||||
if loc:
|
|
||||||
with open(loc) as f:
|
|
||||||
conf.update(json.load(f))
|
|
||||||
|
|
||||||
for k in ['APP_PATH', 'USER_HOME']:
|
|
||||||
if k in conf:
|
|
||||||
del conf[k]
|
|
||||||
|
|
||||||
return conf
|
|
||||||
|
|
||||||
|
|
||||||
def save(conf):
|
|
||||||
loc = get_path(check_write=True)
|
|
||||||
with open(loc, 'w') as f:
|
|
||||||
f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')).strip() + '\n')
|
|
||||||
return loc
|
|
||||||
|
|
||||||
|
|
||||||
def get_path(check_write=False):
|
|
||||||
"""Find config path
|
|
||||||
"""
|
|
||||||
for loc in os.curdir, os.path.expanduser("~"), "/etc/realms-wiki":
|
|
||||||
if not loc:
|
|
||||||
continue
|
|
||||||
path = os.path.join(loc, "realms-wiki.json")
|
|
||||||
if os.path.isfile(path):
|
|
||||||
# file exists
|
|
||||||
if not check_write:
|
|
||||||
# Don't care if I can write
|
|
||||||
return path
|
|
||||||
|
|
||||||
if os.access(path, os.W_OK):
|
|
||||||
# Has write access, ok!
|
|
||||||
return path
|
|
||||||
elif os.path.isdir(loc) and check_write:
|
|
||||||
# dir exists file doesn't
|
|
||||||
if os.access(loc, os.W_OK):
|
|
||||||
# can write file
|
|
||||||
return path
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
urlparse = urlparse
|
||||||
|
|
||||||
APP_PATH = os.path.abspath(os.path.dirname(__file__) + "/../..")
|
APP_PATH = os.path.abspath(os.path.dirname(__file__) + "/../..")
|
||||||
USER_HOME = os.path.abspath(os.path.expanduser("~"))
|
USER_HOME = os.path.abspath(os.path.expanduser("~"))
|
||||||
|
@ -69,10 +18,6 @@ PIDFILE = "/tmp/realms-wiki.pid"
|
||||||
|
|
||||||
ENV = 'DEV'
|
ENV = 'DEV'
|
||||||
|
|
||||||
DEBUG = True
|
|
||||||
ASSETS_DEBUG = True
|
|
||||||
SQLALCHEMY_ECHO = False
|
|
||||||
|
|
||||||
HOST = "0.0.0.0"
|
HOST = "0.0.0.0"
|
||||||
PORT = 5000
|
PORT = 5000
|
||||||
BASE_URL = 'http://localhost'
|
BASE_URL = 'http://localhost'
|
||||||
|
@ -117,25 +62,21 @@ DB_URI = 'sqlite:////tmp/wiki.db'
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
|
|
||||||
|
# Valid options: simple, redis, memcached
|
||||||
CACHE_TYPE = 'simple'
|
CACHE_TYPE = 'simple'
|
||||||
|
|
||||||
# Redis
|
|
||||||
# CACHE_TYPE = 'redis'
|
|
||||||
CACHE_REDIS_HOST = '127.0.0.1'
|
CACHE_REDIS_HOST = '127.0.0.1'
|
||||||
CACHE_REDIS_PORT = 6379
|
CACHE_REDIS_PORT = 6379
|
||||||
CACHE_REDIS_DB = '0'
|
CACHE_REDIS_DB = '0'
|
||||||
|
|
||||||
# Memcached
|
|
||||||
# CACHE_TYPE = 'memcached'
|
|
||||||
CACHE_MEMCACHED_SERVERS = ['127.0.0.1:11211']
|
CACHE_MEMCACHED_SERVERS = ['127.0.0.1:11211']
|
||||||
|
|
||||||
SEARCH_TYPE = 'simple' # simple is not good for large wikis
|
# Valid options: simple, elasticsearch, woosh
|
||||||
|
SEARCH_TYPE = 'simple'
|
||||||
|
|
||||||
# SEARCH_TYPE = 'elasticsearch'
|
|
||||||
ELASTICSEARCH_URL = 'http://127.0.0.1:9200'
|
ELASTICSEARCH_URL = 'http://127.0.0.1:9200'
|
||||||
ELASTICSEARCH_FIELDS = ["name"]
|
ELASTICSEARCH_FIELDS = ["name"]
|
||||||
|
|
||||||
# SEARCH_TYPE = 'whoosh'
|
|
||||||
WHOOSH_INDEX = '/tmp/whoosh'
|
WHOOSH_INDEX = '/tmp/whoosh'
|
||||||
WHOOSH_LANGUAGE = 'en'
|
WHOOSH_LANGUAGE = 'en'
|
||||||
|
|
||||||
|
@ -172,37 +113,106 @@ WIKI_LOCKED_PAGES = []
|
||||||
ROOT_ENDPOINT = 'wiki.page'
|
ROOT_ENDPOINT = 'wiki.page'
|
||||||
|
|
||||||
# Used by Flask-Login
|
# Used by Flask-Login
|
||||||
LOGIN_DISABLED = ALLOW_ANON
|
@property
|
||||||
|
def LOGIN_DISABLED(self):
|
||||||
|
return self.ALLOW_ANON
|
||||||
|
|
||||||
# Depreciated variable name
|
# Depreciated variable name
|
||||||
LOCKED = WIKI_LOCKED_PAGES[:]
|
@property
|
||||||
|
def LOCKED(self):
|
||||||
|
return self.WIKI_LOCKED_PAGES[:]
|
||||||
|
|
||||||
if BASE_URL.endswith('/'):
|
@property
|
||||||
BASE_URL = BASE_URL[:-1]
|
def SQLALCHEMY_DATABASE_URI(self):
|
||||||
|
return self.DB_URI
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URI = DB_URI
|
@property
|
||||||
|
def _url(self):
|
||||||
|
return urlparse(self.BASE_URL)
|
||||||
|
|
||||||
_url = urlparse(BASE_URL)
|
@property
|
||||||
RELATIVE_PATH = _url.path
|
def RELATIVE_PATH(self):
|
||||||
|
return self._url.path
|
||||||
|
|
||||||
if in_vagrant():
|
|
||||||
# sendfile doesn't work well with Virtualbox shared folders
|
|
||||||
USE_X_SENDFILE = False
|
USE_X_SENDFILE = False
|
||||||
|
|
||||||
if ENV != "DEV":
|
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
ASSETS_DEBUG = False
|
ASSETS_DEBUG = False
|
||||||
SQLALCHEMY_ECHO = False
|
SQLALCHEMY_ECHO = False
|
||||||
|
|
||||||
MODULES = ['wiki', 'search', 'auth']
|
MODULES = ['wiki', 'search', 'auth']
|
||||||
|
|
||||||
globals().update(read())
|
def __init__(self):
|
||||||
|
for k, v in self.read().iteritems():
|
||||||
|
setattr(self, k, v)
|
||||||
|
if hasattr(self, 'AUTH_LOCAL_ENABLE'):
|
||||||
|
self.MODULES.append('auth.local')
|
||||||
|
if hasattr(self, 'OAUTH'):
|
||||||
|
self.MODULES.append('auth.oauth')
|
||||||
|
if hasattr(self, 'LDAP'):
|
||||||
|
self.MODULES.append('auth.ldap')
|
||||||
|
if in_vagrant():
|
||||||
|
self.USE_X_SENDFILE = False
|
||||||
|
if self.ENV == "DEV":
|
||||||
|
self.DEBUG = True
|
||||||
|
self.ASSETS_DEBUG = True
|
||||||
|
self.SQLALCHEMY_ECHO = True
|
||||||
|
self.USE_X_SENDFILE = False
|
||||||
|
|
||||||
if globals().get('AUTH_LOCAL_ENABLE'):
|
def update(self, data):
|
||||||
MODULES.append('auth.local')
|
conf = self.read()
|
||||||
|
conf.update(data)
|
||||||
|
return self.save(data)
|
||||||
|
|
||||||
if globals().get('OAUTH'):
|
def read(self):
|
||||||
MODULES.append('auth.oauth')
|
conf = dict()
|
||||||
|
|
||||||
if globals().get('LDAP'):
|
for k, v in os.environ.items():
|
||||||
MODULES.append('auth.ldap')
|
if k.startswith('REALMS_'):
|
||||||
|
conf[k[7:]] = v
|
||||||
|
|
||||||
|
loc = self.get_path()
|
||||||
|
|
||||||
|
if loc:
|
||||||
|
with open(loc) as f:
|
||||||
|
conf.update(json.load(f))
|
||||||
|
|
||||||
|
if 'BASE_URL' in conf and conf['BASE_URL'].endswith('/'):
|
||||||
|
conf['BASE_URL'] = conf['BASE_URL'][:-1]
|
||||||
|
|
||||||
|
for k in ['APP_PATH', 'USER_HOME']:
|
||||||
|
if k in conf:
|
||||||
|
del conf[k]
|
||||||
|
|
||||||
|
return conf
|
||||||
|
|
||||||
|
def save(self, conf):
|
||||||
|
loc = self.get_path(check_write=True)
|
||||||
|
with open(loc, 'w') as f:
|
||||||
|
f.write(json.dumps(conf, sort_keys=True, indent=4, separators=(',', ': ')).strip() + '\n')
|
||||||
|
return loc
|
||||||
|
|
||||||
|
def get_path(self, check_write=False):
|
||||||
|
"""Find config path
|
||||||
|
"""
|
||||||
|
for loc in os.curdir, os.path.expanduser("~"), "/etc/realms-wiki":
|
||||||
|
if not loc:
|
||||||
|
continue
|
||||||
|
path = os.path.join(loc, "realms-wiki.json")
|
||||||
|
if os.path.isfile(path):
|
||||||
|
# file exists
|
||||||
|
if not check_write:
|
||||||
|
# Don't care if I can write
|
||||||
|
return path
|
||||||
|
|
||||||
|
if os.access(path, os.W_OK):
|
||||||
|
# Has write access, ok!
|
||||||
|
return path
|
||||||
|
elif os.path.isdir(loc) and check_write:
|
||||||
|
# dir exists file doesn't
|
||||||
|
if os.access(loc, os.W_OK):
|
||||||
|
# can write file
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
conf = Config()
|
||||||
|
|
|
@ -5,6 +5,8 @@ from flask_oauthlib.client import OAuth
|
||||||
from realms import config
|
from realms import config
|
||||||
from ..models import BaseUser
|
from ..models import BaseUser
|
||||||
|
|
||||||
|
config = config.conf
|
||||||
|
|
||||||
oauth = OAuth()
|
oauth = OAuth()
|
||||||
|
|
||||||
users = {}
|
users = {}
|
||||||
|
|
Loading…
Reference in a new issue