Quellcode durchsuchen

Use logger and config in app.py

Kentaro Wada vor 7 Jahren
Ursprung
Commit
38ac17f145
6 geänderte Dateien mit 66 neuen und 62 gelöschten Zeilen
  1. 9 0
      labelme/__init__.py
  2. 7 46
      labelme/app.py
  3. 1 9
      labelme/canvas.py
  4. 44 3
      labelme/config/__init__.py
  5. 2 2
      labelme/labelDialog.py
  6. 3 2
      labelme/shape.py

+ 9 - 0
labelme/__init__.py

@@ -1,4 +1,13 @@
 # flake8: noqa
 
+import logging
+
+
+__appname__ = 'labelme'
+
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__appname__)
+
+
 from labelme import testing
 from labelme import utils

+ 7 - 46
labelme/app.py

@@ -1,6 +1,5 @@
 import argparse
 import functools
-import logging
 import os.path
 import re
 import sys
@@ -12,13 +11,13 @@ from qtpy import QtCore
 from qtpy.QtCore import Qt
 from qtpy import QtGui
 from qtpy import QtWidgets
-import yaml
 
 QT5 = QT_VERSION[0] == '5'
 
+from labelme import __appname__
 from labelme.canvas import Canvas
 from labelme.colorDialog import ColorDialog
-from labelme.config import default_config
+from labelme.config import get_config
 from labelme.labelDialog import LabelDialog
 from labelme.labelFile import LabelFile
 from labelme.labelFile import LabelFileError
@@ -27,6 +26,7 @@ from labelme.lib import fmtShortcut
 from labelme.lib import newAction
 from labelme.lib import newIcon
 from labelme.lib import struct
+from labelme import logger
 from labelme.shape import DEFAULT_FILL_COLOR
 from labelme.shape import DEFAULT_LINE_COLOR
 from labelme.shape import Shape
@@ -34,13 +34,6 @@ from labelme.toolBar import ToolBar
 from labelme.zoomWidget import ZoomWidget
 
 
-__appname__ = 'labelme'
-
-
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__appname__)
-
-
 # FIXME
 # - [medium] Set max zoom value to something big enough for FitWidth/Window
 
@@ -133,10 +126,13 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
 
     def __init__(self, filename=None, output=None, store_data=True,
                  labels=None, sort_labels=True, auto_save=False,
-                 validate_label=None):
+                 validate_label=None, config=None):
         super(MainWindow, self).__init__()
         self.setWindowTitle(__appname__)
 
+        if config is None:
+            config = get_config()
+
         # Whether we need to save or not.
         self.dirty = False
 
@@ -225,8 +221,6 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
                              QtWidgets.QDockWidget.DockWidgetFloatable)
         self.dock.setFeatures(self.dock.features() ^ self.dockFeatures)
 
-        config = self.getConfig()
-
         # Actions
         action = functools.partial(newAction, self)
         shortcuts = config['shortcuts']
@@ -472,39 +466,6 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
 
     # Support Functions
 
-    def getConfig(self):
-        # shortcuts for actions
-        home = os.path.expanduser('~')
-        config_file = os.path.join(home, '.labelmerc')
-
-        # default config
-        config = default_config.copy()
-
-        def update_dict(target_dict, new_dict):
-            for key, value in new_dict.items():
-                if key not in target_dict:
-                    logger.warn('Skipping unexpected key in config: {}'
-                                .format(key))
-                    continue
-                if isinstance(target_dict[key], dict) and \
-                        isinstance(value, dict):
-                    update_dict(target_dict[key], value)
-                else:
-                    target_dict[key] = value
-
-        if os.path.exists(config_file):
-            user_config = yaml.load(open(config_file)) or {}
-            update_dict(config, user_config)
-
-        # save config
-        try:
-            yaml.safe_dump(config, open(config_file, 'w'),
-                           default_flow_style=False)
-        except Exception:
-            warnings.warn('Failed to save config: {}'.format(config_file))
-
-        return config
-
     def noShapes(self):
         return not self.labelList.itemsToShapes
 

+ 1 - 9
labelme/canvas.py

@@ -1,7 +1,3 @@
-from __future__ import print_function
-
-import sys
-
 from qtpy import QT_VERSION
 from qtpy import QtCore
 from qtpy import QtGui
@@ -220,11 +216,7 @@ class Canvas(QtWidgets.QWidget):
         if ev.button() == QtCore.Qt.LeftButton:
             if self.drawing():
                 if self.current:
-                    try:
-                        self.current.addPoint(self.line[1])
-                    except Exception as e:
-                        print(e, file=sys.stderr)
-                        return
+                    self.current.addPoint(self.line[1])
                     self.line[0] = self.current[-1]
                     if self.current.isClosed():
                         self.finalise()

+ 44 - 3
labelme/config/__init__.py

@@ -1,9 +1,50 @@
+import os
 import os.path as osp
 
 import yaml
 
+from labelme import logger
+
 
 here = osp.dirname(osp.abspath(__file__))
-config_file = osp.join(here, 'default_config.yaml')
-default_config = yaml.load(open(config_file))
-del here, config_file
+
+
+def update_dict(target_dict, new_dict):
+    for key, value in new_dict.items():
+        if key not in target_dict:
+            logger.warn('Skipping unexpected key in config: {}'
+                        .format(key))
+            continue
+        if isinstance(target_dict[key], dict) and \
+                isinstance(value, dict):
+            update_dict(target_dict[key], value)
+        else:
+            target_dict[key] = value
+
+
+def get_default_config():
+    config_file = osp.join(here, 'default_config.yaml')
+    config = yaml.load(open(config_file))
+    return config
+
+
+def get_config():
+    # default config
+    config = get_default_config()
+
+    # shortcuts for actions
+    home = os.path.expanduser('~')
+    config_file = os.path.join(home, '.labelmerc')
+
+    if os.path.exists(config_file):
+        user_config = yaml.load(open(config_file)) or {}
+        update_dict(config, user_config)
+
+    # save config
+    try:
+        yaml.safe_dump(config, open(config_file, 'w'),
+                       default_flow_style=False)
+    except Exception:
+        logger.warn('Failed to save config: {}'.format(config_file))
+
+    return config

+ 2 - 2
labelme/labelDialog.py

@@ -5,8 +5,8 @@ from qtpy import QtWidgets
 
 QT5 = QT_VERSION[0] == '5'
 
-from .lib import labelValidator
-from .lib import newIcon
+from labelme.lib import labelValidator
+from labelme.lib import newIcon
 
 
 # TODO(unknown):

+ 3 - 2
labelme/shape.py

@@ -1,6 +1,7 @@
 from qtpy import QtGui
 
-from .lib import distance
+from labelme.lib import distance
+from labelme import logger
 
 
 # TODO(unknown):
@@ -54,7 +55,7 @@ class Shape(object):
             self.line_color = line_color
 
     def close(self):
-        assert len(self.points) > 2, 'Polygon should be created with points >2'
+        logger.warn('Polygon should be created with points >2')
         self._closed = True
 
     def addPoint(self, point):