Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 1 addition & 5 deletions bibsearch/bibsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,7 @@ def main():
parser = argparse.ArgumentParser(description="bibsearch: Download, manage, and search a BibTeX database.\nUse '%(prog)s man' to get complete help.",
formatter_class=SubcommandHelpFormatter)
parser.add_argument('--version', '-V', action='version', version='%(prog)s {}'.format(VERSION))
parser.add_argument('-c', '--config_file', help="use this config file",
default=os.path.join(os.path.expanduser("~"),
'.bibsearch',
"config")
)
parser.add_argument('-c', '--config_file', help="use this config file")
parser.set_defaults(func=lambda *_ : parser.print_help())
subparsers = parser.add_subparsers(title="commands",
description="Use '%(prog)s <command> -h' to obtain additional help.",
Expand Down
13 changes: 13 additions & 0 deletions bibsearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(self, config_file: str = None):
config.read_dict(self.__class__.defaults)

# Override those defaults from the config file
if config_file is None:
config_file = self._check_for_existing_config()
config.read(config_file)

# Make available as member variables
Expand All @@ -45,6 +47,17 @@ def __init__(self, config_file: str = None):

self.macros = config["macros"]

@classmethod
def _check_for_existing_config(cls):
# the default if XDG_CONFIG_HOME isn't set is ~/.config
xdg_config_home = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config")
# check there for a config file
if os.path.exists(os.path.join(xdg_config_home, "bibsearch/config")):
config_file = os.path.join(xdg_config_home, "bibsearch/config")
# otherwise, use ~/.bibsearch/config
config_file = os.path.expanduser("~/.bibsearch/config")
return config_file

@classmethod
def get_default(cls, key: str) -> str:
return cls.defaults['bibsearch'][key]