Ver Fonte

Fix handling the command line argument to app

Kentaro Wada há 7 anos atrás
pai
commit
f9964425e4
2 ficheiros alterados com 27 adições e 24 exclusões
  1. 16 10
      labelme/app.py
  2. 11 14
      labelme/config/__init__.py

+ 16 - 10
labelme/app.py

@@ -1237,8 +1237,14 @@ def main():
                         help='show version')
     parser.add_argument('filename', nargs='?', help='image or label filename')
     parser.add_argument('--output', '-O', '-o', help='output label name')
-    parser.add_argument('--config', dest='config_file', help='config file')
-    # config
+    default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
+    parser.add_argument(
+        '--config',
+        dest='config_file',
+        default=default_config_file,
+        help='config file (default: %s)' % default_config_file,
+    )
+    # config for the gui
     parser.add_argument('--nodata', dest='store_data', action='store_false',
                         help='stop storing image data to JSON file')
     parser.add_argument('--autosave', dest='auto_save', action='store_true',
@@ -1257,13 +1263,7 @@ def main():
         print('{0} {1}'.format(__appname__, __version__))
         sys.exit(0)
 
-    if args.labels is None:
-        if args.validate_label is not None:
-            logger.error('--labels must be specified with --validatelabel or '
-                         'validate_label: true in the config file '
-                         '(ex. ~/.labelmerc).')
-            sys.exit(1)
-    else:
+    if args.labels is not None:
         if os.path.isfile(args.labels):
             with codecs.open(args.labels, 'r', encoding='utf-8') as f:
                 args.labels = [l.strip() for l in f if l.strip()]
@@ -1275,12 +1275,18 @@ def main():
     filename = config_from_args.pop('filename')
     output = config_from_args.pop('output')
     config_file = config_from_args.pop('config_file')
-    # drop the default config
+    # drop the unspecified config
     for k, v in list(config_from_args.items()):
         if v is None:
             config_from_args.pop(k)
     config = get_config(config_from_args, config_file)
 
+    if not config['labels'] and config['validate_label']:
+        logger.error('--labels must be specified with --validatelabel or '
+                     'validate_label: true in the config file '
+                     '(ex. ~/.labelmerc).')
+        sys.exit(1)
+
     app = QtWidgets.QApplication(sys.argv)
     app.setApplicationName(__appname__)
     app.setWindowIcon(newIcon('icon'))

+ 11 - 14
labelme/config/__init__.py

@@ -1,4 +1,5 @@
 import os.path as osp
+import shutil
 
 import yaml
 
@@ -30,6 +31,15 @@ def get_default_config():
     config_file = osp.join(here, 'default_config.yaml')
     with open(config_file) as f:
         config = yaml.load(f)
+
+    # save default config to ~/.labelmerc
+    user_config_file = osp.join(osp.expanduser('~'), '.labelmerc')
+    if not osp.exists(user_config_file):
+        try:
+            shutil.copy(config_file, user_config_file)
+        except Exception:
+            logger.warn('Failed to save config: {}'.format(user_config_file))
+
     return config
 
 
@@ -49,21 +59,8 @@ def get_config(config_from_args=None, config_file=None):
     # 1. default config
     config = get_default_config()
 
-    # save default config to ~/.labelmerc
-    home = osp.expanduser('~')
-    default_config_file = osp.join(home, '.labelmerc')
-    if not osp.exists(default_config_file):
-        try:
-            with open(default_config_file, 'w') as f:
-                yaml.safe_dump(config, f, default_flow_style=False)
-        except Exception:
-            logger.warn('Failed to save config: {}'
-                        .format(default_config_file))
-
     # 2. config from yaml file
-    if config_file is None:
-        config_file = default_config_file
-    if osp.exists(config_file):
+    if config_file is not None and osp.exists(config_file):
         with open(config_file) as f:
             user_config = yaml.load(f) or {}
         update_dict(config, user_config, validate_item=validate_config_item)