Skip to content

Commit

Permalink
config: don't assume ~/.sopel as dotdir
Browse files Browse the repository at this point in the history
Infer config directory from given config filename.

Resolves sopel-irc#1243
  • Loading branch information
dgw authored and kwaaak committed Mar 25, 2019
1 parent d0b79ee commit eda5c49
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions sopel/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ def _modules(self):


def _wizard(section, config=None):
dotdir = os.path.expanduser('~/.sopel')
dotdir = os.path.dirname(config) if config is not None else os.path.expanduser('~/.sopel')
configpath = os.path.join(dotdir, ((config or 'default.cfg') + ('.cfg' if config and not config.endswith('.cfg') else '')))
if section == 'all':
_create_config(configpath)
elif section == 'mod':
_check_dir(False)
_check_dir(dotdir, False)
if not os.path.isfile(configpath):
print("No config file found." +
" Please make one before configuring these options.")
Expand All @@ -228,15 +228,14 @@ def _wizard(section, config=None):
config._modules()


def _check_dir(create=True):
dotdir = os.path.join(os.path.expanduser('~'), '.sopel')
if not os.path.isdir(dotdir):
def _check_dir(path=os.path.expanduser('~/.sopel'), create=True):
if not os.path.isdir(path):
if create:
print('Creating a config directory at ~/.sopel...')
print('Creating a config directory at {}...'.format(path))
try:
os.makedirs(dotdir)
os.makedirs(path)
except Exception as e:
print('There was a problem creating %s:' % dotdir, file=sys.stderr)
print('There was a problem creating %s:' % path, file=sys.stderr)
print('%s, %s' % (e.__class__, str(e)), file=sys.stderr)
print('Please fix this and then run Sopel again.', file=sys.stderr)
sys.exit(1)
Expand All @@ -246,7 +245,7 @@ def _check_dir(create=True):


def _create_config(configpath):
_check_dir()
_check_dir(os.path.dirname(configpath))
print("Please answer the following questions" +
" to create your configuration file:\n")
try:
Expand Down

0 comments on commit eda5c49

Please sign in to comment.