提交 9bfbdc7c 编写于 作者: K Kentaro Wada

Fix the order of loading configuration

Close https://github.com/wkentaro/labelme/issues/149

1. default config (lowest priority)
2. config file passed by command line argument or ~/.labelmerc
3. command line argument (highest priority)
上级 d66ad415
import os
import os.path as osp import os.path as osp
import yaml import yaml
...@@ -41,29 +40,36 @@ def validate_config_item(key, value): ...@@ -41,29 +40,36 @@ def validate_config_item(key, value):
def get_config(config_from_args=None, config_file=None): def get_config(config_from_args=None, config_file=None):
# default config # Configuration load order:
#
# 1. default config (lowest priority)
# 2. config file passed by command line argument or ~/.labelmerc
# 3. command line argument (highest priority)
# 1. default config
config = get_default_config() config = get_default_config()
if config_from_args is not None: # save default config to ~/.labelmerc
update_dict(config, config_from_args, home = osp.expanduser('~')
validate_item=validate_config_item) default_config_file = osp.join(home, '.labelmerc')
if not osp.exists(default_config_file):
try:
with open(config_file, 'w') as f:
yaml.safe_dump(config, f, default_flow_style=False)
except Exception:
logger.warn('Failed to save config: {}'.format(config_file))
save_config_file = False # 2. config from yaml file
if config_file is None: if config_file is None:
home = os.path.expanduser('~') config_file = default_config_file
config_file = os.path.join(home, '.labelmerc') if osp.exists(config_file):
save_config_file = True
if os.path.exists(config_file):
with open(config_file) as f: with open(config_file) as f:
user_config = yaml.load(f) or {} user_config = yaml.load(f) or {}
update_dict(config, user_config, validate_item=validate_config_item) update_dict(config, user_config, validate_item=validate_config_item)
if save_config_file: # 3. command line argument
try: if config_from_args is not None:
with open(config_file, 'w') as f: update_dict(config, config_from_args,
yaml.safe_dump(config, f, default_flow_style=False) validate_item=validate_config_item)
except Exception:
logger.warn('Failed to save config: {}'.format(config_file))
return config return config
...@@ -3,6 +3,7 @@ import shutil ...@@ -3,6 +3,7 @@ import shutil
import tempfile import tempfile
import labelme.app import labelme.app
import labelme.config
import labelme.testing import labelme.testing
...@@ -33,7 +34,9 @@ def test_MainWindow_annotate_jpg(qtbot): ...@@ -33,7 +34,9 @@ def test_MainWindow_annotate_jpg(qtbot):
filename) filename)
output = osp.join(tmp_dir, 'apc2016_obj3.json') output = osp.join(tmp_dir, 'apc2016_obj3.json')
win = labelme.app.MainWindow(filename=filename, output=output) config = labelme.config.get_default_config()
win = labelme.app.MainWindow(
config=config, filename=filename, output=output)
qtbot.addWidget(win) qtbot.addWidget(win)
win.show() win.show()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册