__init__.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import os.path as osp
  3. import yaml
  4. from labelme import logger
  5. here = osp.dirname(osp.abspath(__file__))
  6. def update_dict(target_dict, new_dict):
  7. for key, value in new_dict.items():
  8. if key not in target_dict:
  9. logger.warn('Skipping unexpected key in config: {}'
  10. .format(key))
  11. continue
  12. if isinstance(target_dict[key], dict) and \
  13. isinstance(value, dict):
  14. update_dict(target_dict[key], value)
  15. else:
  16. target_dict[key] = value
  17. def get_default_config():
  18. config_file = osp.join(here, 'default_config.yaml')
  19. config = yaml.load(open(config_file))
  20. return config
  21. def get_config():
  22. # default config
  23. config = get_default_config()
  24. # shortcuts for actions
  25. home = os.path.expanduser('~')
  26. config_file = os.path.join(home, '.labelmerc')
  27. if os.path.exists(config_file):
  28. user_config = yaml.load(open(config_file)) or {}
  29. update_dict(config, user_config)
  30. # save config
  31. try:
  32. yaml.safe_dump(config, open(config_file, 'w'),
  33. default_flow_style=False)
  34. except Exception:
  35. logger.warn('Failed to save config: {}'.format(config_file))
  36. return config