changeset 142:7e4287d9b3bb

Changes to make it easier to deploy: - Local config can go in `.wikirc.local`. - Remove path dependencies from `WikiParameters`. - More configurability in the SQL database backend.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 10 Dec 2013 13:49:03 -0800
parents 957b269c6dfc
children 391dee401693
files wikked/db/sql.py wikked/fs.py wikked/indexer/native.py wikked/resources/defaults.cfg wikked/web.py wikked/wiki.py
diffstat 6 files changed, 41 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/db/sql.py	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/db/sql.py	Tue Dec 10 13:49:03 2013 -0800
@@ -116,15 +116,16 @@
     """
     schema_version = 3
 
-    def __init__(self, db_path):
+    def __init__(self):
         Database.__init__(self)
-        self.db_path = db_path
         self.engine = None
 
     def initDb(self, wiki):
         self.wiki = wiki
 
-        engine_url = 'sqlite:///' + self.db_path
+        url_params = {'root': wiki.root}
+        engine_url = wiki.config.get('wiki', 'database_url') % url_params
+        logger.info("Using database from URL: %s" % engine_url)
         self.engine = create_engine(engine_url, convert_unicode=True)
         self.session = scoped_session(sessionmaker(
                 autocommit=False,
@@ -134,22 +135,17 @@
         Base.query = self.session.query_property()
 
         create_schema = False
-        if self.db_path != 'sqlite:///:memory:':
-            if not os.path.exists(os.path.dirname(self.db_path)):
-                # No database on disk... create one.
-                logger.debug("Creating SQL database at: %s" % self.db_path)
+        if engine_url != 'sqlite:///:memory:':
+            # The existing schema is outdated, re-create it.
+            schema_version = self._getSchemaVersion()
+            if schema_version < self.schema_version:
+                logger.debug(
+                        "SQL database is outdated (got version %s), will re-create.",
+                        schema_version)
                 create_schema = True
             else:
-                # The existing schema is outdated, re-create it.
-                schema_version = self._getSchemaVersion()
-                if schema_version < self.schema_version:
-                    logger.debug(
-                            "SQL database is outdated (got version %s), will re-create.",
-                            schema_version)
-                    create_schema = True
-                else:
-                    logger.debug(
-                            "SQL database has up-to-date schema.")
+                logger.debug(
+                        "SQL database has up-to-date schema.")
         else:
             create_schema = True
         if create_schema:
--- a/wikked/fs.py	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/fs.py	Tue Dec 10 13:49:03 2013 -0800
@@ -45,7 +45,7 @@
         self.page_extensions = list(set(
             itertools.chain(*wiki.formatters.itervalues())))
 
-        self.excluded += wiki.parameters.getSpecialFilenames()
+        self.excluded += wiki.getSpecialFilenames()
         self.excluded += wiki.scm.getSpecialFilenames()
 
         self.default_extension = wiki.config.get('wiki', 'default_extension')
--- a/wikked/indexer/native.py	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/indexer/native.py	Tue Dec 10 13:49:03 2013 -0800
@@ -12,11 +12,11 @@
 
 
 class WhooshWikiIndex(WikiIndex):
-    def __init__(self, store_dir):
+    def __init__(self):
         WikiIndex.__init__(self)
-        self.store_dir = store_dir
 
     def initIndex(self, wiki):
+        self.store_dir = os.path.join(wiki.root, '.wiki', 'index')
         if not os.path.isdir(self.store_dir):
             logger.debug("Creating new index in: " + self.store_dir)
             os.makedirs(self.store_dir)
--- a/wikked/resources/defaults.cfg	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/resources/defaults.cfg	Tue Dec 10 13:49:03 2013 -0800
@@ -4,4 +4,5 @@
 naming_policy=capitalize
 main_page=Main Page
 templates_dir=Templates
+database_url=sqlite:///%(root)s/.wiki/wiki.db
 
--- a/wikked/web.py	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/web.py	Tue Dec 10 13:49:03 2013 -0800
@@ -5,7 +5,10 @@
 from utils import find_wiki_root
 
 # Create the main app.
-app = Flask("wikked.web")
+app = Flask(
+        'wikked',
+        static_folder='build',
+        static_url_path='/')
 app.config.from_object('wikked.settings')
 app.config.from_envvar('WIKKED_SETTINGS', silent=True)
 
@@ -13,11 +16,17 @@
 # Setup some config defaults.
 app.config.setdefault('SQL_DEBUG', False)
 app.config.setdefault('SQL_COMMIT_ON_TEARDOWN', False)
+app.config.setdefault('WIKI_ROOT', None)
+app.config.setdefault('UPDATE_WIKI_ON_START', True)
 
 
 # Find the wiki root, and further configure the app if there's a
 # config file in there.
-wiki_root = find_wiki_root()
+wiki_root = app.config['WIKI_ROOT']
+if wiki_root is None:
+    wiki_root = find_wiki_root()
+if wiki_root is None:
+    raise Exception("Can't find the wiki root to use.")
 config_path = os.path.join(wiki_root, '.wiki', 'app.cfg')
 if os.path.isfile(config_path):
     app.config.from_pyfile(config_path)
@@ -28,10 +37,7 @@
     from werkzeug import SharedDataMiddleware
     import os
     app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
-      '/': os.path.join(
-          os.path.dirname(os.path.dirname(__file__)),
-          'build'),
-      '/files': os.path.join(wiki_root)
+      '/files': os.path.join(wiki_root, '_files')
     })
 
 
--- a/wikked/wiki.py	Tue Dec 10 13:47:37 2013 -0800
+++ b/wikked/wiki.py	Tue Dec 10 13:49:03 2013 -0800
@@ -38,18 +38,14 @@
 
         self.formatters = self.getFormatters()
 
-        self.config_path = os.path.join(self.root, '.wikirc')
-        self.index_path = os.path.join(self.root, '.wiki', 'index')
-        self.db_path = os.path.join(self.root, '.wiki', 'wiki.db')
-
     def fs_factory(self, config):
         return FileSystem(self.root)
 
     def index_factory(self, config):
-        return WhooshWikiIndex(self.index_path)
+        return WhooshWikiIndex()
 
     def db_factory(self, config):
-        return SQLDatabase(self.db_path)
+        return SQLDatabase()
 
     def scm_factory(self, config):
         try:
@@ -71,10 +67,6 @@
         else:
             raise InitializationError("No such source control: " + scm_type)
 
-    def getSpecialFilenames(self):
-        yield self.config_path
-        yield os.path.join(self.root, '.wiki')
-
     def getFormatters(self):
         formatters = {passthrough_formatter: ['txt', 'html']}
         self.tryAddFormatter(formatters, 'markdown', 'markdown', ['md', 'mdown', 'markdown'])
@@ -105,6 +97,7 @@
 
         logger.debug("Initializing wiki.")
 
+
         self.parameters = parameters
         self.config = self._loadConfig(parameters)
         self.main_page_url = '/' + self.config.get('wiki', 'main_page').strip('/')
@@ -253,6 +246,11 @@
         """
         return self.scm.getHistory(limit=limit)
 
+    def getSpecialFilenames(self):
+        yield os.path.join(self.root, '.wikirc')
+        yield os.path.join(self.root, '.wikirc.local')
+        yield os.path.join(self.root, '.wiki')
+
     def _cachePages(self, only_urls=None):
         logger.debug("Caching extended page data...")
         if only_urls:
@@ -266,11 +264,14 @@
     def _loadConfig(self, parameters):
         # Merge the default settings with any settings provided by
         # the parameters.
+        config_path = os.path.join(parameters.root, '.wikirc')
+        local_config_path = config_path + '.local'
         default_config_path = os.path.join(
             os.path.dirname(__file__), 'resources', 'defaults.cfg')
+
         config = SafeConfigParser()
         config.readfp(open(default_config_path))
-        config.read([parameters.config_path])
+        config.read([config_path, local_config_path])
         return config