Quellcode durchsuchen

Support yaml by --config

Kentaro Wada vor 5 Jahren
Ursprung
Commit
d0fda7de36
4 geänderte Dateien mit 26 neuen und 30 gelöschten Zeilen
  1. 9 9
      docs/man/labelme.1
  2. 4 16
      labelme/config/__init__.py
  3. 12 4
      labelme/main.py
  4. 1 1
      tests/docs_tests/man_tests/test_labelme_1.py

+ 9 - 9
docs/man/labelme.1

@@ -1,16 +1,15 @@
-.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.47.11.
-.TH LABELME "1" "December 2019" "labelme 3.18.0" "User Commands"
+.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.47.12.
+.TH LABELME "1" "January 2020" "labelme 3.18.0" "User Commands"
 .SH NAME
 .SH NAME
 labelme \- manual page for labelme 3.18.0
 labelme \- manual page for labelme 3.18.0
 .SH DESCRIPTION
 .SH DESCRIPTION
 usage: labelme [\-h] [\-\-version] [\-\-reset\-config]
 usage: labelme [\-h] [\-\-version] [\-\-reset\-config]
 .IP
 .IP
 [\-\-logger\-level {debug,info,warning,fatal,error}]
 [\-\-logger\-level {debug,info,warning,fatal,error}]
-[\-\-output OUTPUT] [\-\-config CONFIG_FILE] [\-\-nodata]
-[\-\-autosave] [\-\-nosortlabels] [\-\-flags FLAGS]
-[\-\-labelflags LABEL_FLAGS] [\-\-labels LABELS]
-[\-\-validatelabel {exact,instance}] [\-\-keep\-prev]
-[\-\-epsilon EPSILON]
+[\-\-output OUTPUT] [\-\-config CONFIG] [\-\-nodata] [\-\-autosave]
+[\-\-nosortlabels] [\-\-flags FLAGS] [\-\-labelflags LABEL_FLAGS]
+[\-\-labels LABELS] [\-\-validatelabel {exact,instance}]
+[\-\-keep\-prev] [\-\-epsilon EPSILON]
 [filename]
 [filename]
 .SS "positional arguments:"
 .SS "positional arguments:"
 .TP
 .TP
@@ -34,8 +33,9 @@ logger level
 output file or directory (if it ends with .json it is
 output file or directory (if it ends with .json it is
 recognized as file, else as directory)
 recognized as file, else as directory)
 .TP
 .TP
-\fB\-\-config\fR CONFIG_FILE
-config file (default: /Users/wkentaro/.labelmerc)
+\fB\-\-config\fR CONFIG
+config file or yaml\-format string (default:
+/Users/wkentaro/.labelmerc)
 .TP
 .TP
 \fB\-\-nodata\fR
 \fB\-\-nodata\fR
 stop storing image data to JSON file
 stop storing image data to JSON file

+ 4 - 16
labelme/config/__init__.py

@@ -55,25 +55,13 @@ def validate_config_item(key, value):
         )
         )
 
 
 
 
-def get_config(config_from_args=None, config_file=None):
-    # Configuration load order:
-    #
-    #   1. default config (lowest priority)
-    #   2. config file passed by command line argument or ~/.labelmerc
-    #   3. command line argument (highest priority)
-
+def get_config(config_specified=None):
     # 1. default config
     # 1. default config
     config = get_default_config()
     config = get_default_config()
 
 
-    # 2. config from yaml file
-    if config_file is not None and osp.exists(config_file):
-        with open(config_file) as f:
-            user_config = yaml.safe_load(f) or {}
-        update_dict(config, user_config, validate_item=validate_config_item)
-
-    # 3. command line argument
-    if config_from_args is not None:
-        update_dict(config, config_from_args,
+    # 2. command line argument or specified config file
+    if config_specified is not None:
+        update_dict(config, config_specified,
                     validate_item=validate_config_item)
                     validate_item=validate_config_item)
 
 
     return config
     return config

+ 12 - 4
labelme/main.py

@@ -42,8 +42,10 @@ def main():
     default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
     default_config_file = os.path.join(os.path.expanduser('~'), '.labelmerc')
     parser.add_argument(
     parser.add_argument(
         '--config',
         '--config',
-        dest='config_file',
-        help='config file (default: %s)' % default_config_file,
+        dest='config',
+        help='config file or yaml-format string (default: {})'.format(
+            default_config_file
+        ),
         default=default_config_file,
         default=default_config_file,
     )
     )
     # config for the gui
     # config for the gui
@@ -139,8 +141,14 @@ def main():
     reset_config = config_from_args.pop('reset_config')
     reset_config = config_from_args.pop('reset_config')
     filename = config_from_args.pop('filename')
     filename = config_from_args.pop('filename')
     output = config_from_args.pop('output')
     output = config_from_args.pop('output')
-    config_file = config_from_args.pop('config_file')
-    config = get_config(config_from_args, config_file)
+    config_file_or_yaml = config_from_args.pop('config')
+    config = yaml.safe_load(config_file_or_yaml)
+    if not isinstance(config, dict):
+        logger.info('Loading config file from: {}'.format(config))
+        with open(config) as f:
+            config = yaml.safe_load(f)
+    config.update(config_from_args)  # prioritize config_from_args
+    config = get_config(config)
 
 
     if not config['labels'] and config['validate_label']:
     if not config['labels'] and config['validate_label']:
         logger.error('--labels must be specified with --validatelabel or '
         logger.error('--labels must be specified with --validatelabel or '

+ 1 - 1
tests/docs_tests/man_tests/test_labelme_1.py

@@ -20,7 +20,7 @@ with open(man_file) as f:
 
 
 patterns_exclude = [
 patterns_exclude = [
     r'^\.TH .*',
     r'^\.TH .*',
-    r'^config file.*',
+    r'^.*/\.labelmerc\)$',
     r'^\.\\.*',
     r'^\.\\.*',
 ]
 ]