Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: make database path relative to config.core.homedir #1574

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions sopel/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8
from __future__ import unicode_literals, absolute_import, print_function, division

import errno
import json
import os.path
import sys
Expand Down Expand Up @@ -87,8 +88,9 @@ class SopelDB(object):
database. It simplifies those common operations, and allows direct access
to the database, wherever the user has configured it to be.

When configured with a relative filename, it is assumed to be in the same
directory as the config."""
When configured with a relative filename, it is assumed to be in the directory
set (or defaulted to) in the core setting ``homedir``.
"""

def __init__(self, config):
# MySQL - mysql://username:password@localhost/db
Expand All @@ -98,13 +100,19 @@ def __init__(self, config):
# Handle SQLite explicitly as a default
if db_type == 'sqlite':
path = config.core.db_filename
config_dir, config_file = os.path.split(config.filename)
config_name, _ = os.path.splitext(config_file)
if path is None:
path = os.path.join(config_dir, config_name + '.db')
path = os.path.join(config.core.homedir, config.basename + '.db')
path = os.path.expanduser(path)
if not os.path.isabs(path):
path = os.path.normpath(os.path.join(config_dir, path))
path = os.path.normpath(os.path.join(config.core.homedir, path))
if not os.path.isdir(os.path.dirname(path)):
raise OSError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could bikeshed this and say we should use FileNotFoundError, but 💤. It's an easy change to make later if we really want to.

errno.ENOENT,
'Cannot create database file. '
'No such directory: "{}". Check that configuration setting '
'core.db_filename is valid'.format(os.path.dirname(path)),
path
)
self.filename = path
self.url = 'sqlite:///%s' % path
# Otherwise, handle all other database engines
Expand Down