[ocrfeeder] Use Python logging instead of printing



commit 4c2a6a305bedc2a6aacc76dbde767707d76b778e
Author: Joaquim Rocha <me joaquimrocha com>
Date:   Sat Mar 19 21:17:36 2016 +0100

    Use Python logging instead of printing
    
    These changes add a new module log.py which provides a new debug message
    that uses Python's logging and replaces the previous debug function that
    simply printed the messages.
    
    The use of the previous debug message is also updated to the new one.
    
    gb#758797

 src/ocrfeeder/feeder/documentGeneration.py |    2 +-
 src/ocrfeeder/feeder/imageManipulation.py  |    2 +-
 src/ocrfeeder/feeder/layoutAnalysis.py     |    2 +-
 src/ocrfeeder/feeder/ocrEngines.py         |    7 +++--
 src/ocrfeeder/studio/project.py            |    2 +-
 src/ocrfeeder/studio/studioBuilder.py      |    3 +-
 src/ocrfeeder/studio/widgetModeler.py      |   14 +++++++++-
 src/ocrfeeder/studio/widgetPresenter.py    |    5 ++-
 src/ocrfeeder/util/Makefile.am             |    3 +-
 src/ocrfeeder/util/asyncworker.py          |    2 +-
 src/ocrfeeder/util/configuration.py        |    3 +-
 src/ocrfeeder/util/graphics.py             |    3 +-
 src/ocrfeeder/util/lib.py                  |    5 +---
 src/ocrfeeder/util/log.py                  |   39 ++++++++++++++++++++++++++++
 14 files changed, 73 insertions(+), 19 deletions(-)
---
diff --git a/src/ocrfeeder/feeder/documentGeneration.py b/src/ocrfeeder/feeder/documentGeneration.py
index 8f4b817..fa76060 100644
--- a/src/ocrfeeder/feeder/documentGeneration.py
+++ b/src/ocrfeeder/feeder/documentGeneration.py
@@ -27,7 +27,7 @@ from ocrfeeder.util import TEXT_TYPE, IMAGE_TYPE, ALIGN_LEFT, ALIGN_RIGHT, ALIGN
     ALIGN_FILL
 from ocrfeeder.util.configuration import ConfigurationManager
 from ocrfeeder.util.graphics import getImagePrintSize
-from ocrfeeder.util.lib import debug
+from ocrfeeder.util.log import debug
 from gi.repository import Pango
 from reportlab.pdfgen import canvas
 from reportlab.lib import units
diff --git a/src/ocrfeeder/feeder/imageManipulation.py b/src/ocrfeeder/feeder/imageManipulation.py
index 63957f6..9b08e32 100644
--- a/src/ocrfeeder/feeder/imageManipulation.py
+++ b/src/ocrfeeder/feeder/imageManipulation.py
@@ -18,7 +18,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ###########################################################################
 
-from ocrfeeder.util.lib import debug
+from ocrfeeder.util.log import debug
 
 import gettext
 from PIL import Image, ImageDraw
diff --git a/src/ocrfeeder/feeder/layoutAnalysis.py b/src/ocrfeeder/feeder/layoutAnalysis.py
index 80770e2..a2b4edc 100644
--- a/src/ocrfeeder/feeder/layoutAnalysis.py
+++ b/src/ocrfeeder/feeder/layoutAnalysis.py
@@ -18,7 +18,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ###########################################################################
 
-from ocrfeeder.util.lib import debug
+from ocrfeeder.util.log import debug
 from ocrfeeder.util import graphics
 from ocrfeeder.util.constants import OCRFEEDER_DEBUG, DTP
 from ocrfeeder.studio.dataHolder import DataBox
diff --git a/src/ocrfeeder/feeder/ocrEngines.py b/src/ocrfeeder/feeder/ocrEngines.py
index cf662d1..c43c8fc 100644
--- a/src/ocrfeeder/feeder/ocrEngines.py
+++ b/src/ocrfeeder/feeder/ocrEngines.py
@@ -26,6 +26,7 @@ import xml.etree.ElementTree as ET
 from xml.parsers.expat import ExpatError
 from ocrfeeder.studio.dataHolder import TEXT_TYPE, IMAGE_TYPE
 from ocrfeeder.util import lib
+from ocrfeeder.util.log import debug
 IMAGE_ARGUMENT = '$IMAGE'
 FILE_ARGUMENT = '$FILE'
 LANGUAGE_ARGUMENT = '$LANG'
@@ -209,7 +210,7 @@ class OcrEnginesManager:
                 favorite_engine_exists = favorite_engine_exists or \
                     self.configuration_manager.favorite_engine == engine.name
         if not len(self.ocr_engines):
-            lib.debug("Warning: no engines found!")
+            debug("Warning: no engines found!")
         elif not favorite_engine_exists:
             self.configuration_manager.favorite_engine = self.ocr_engines[0][0].name
         engines_needing_update = {'auto': [],
@@ -250,10 +251,10 @@ class OcrEnginesManager:
         try:
             engine = Engine(**arguments)
         except TypeError, exception:
-            lib.debug('Error when unserializing engine: %s' % exception.message)
+            debug('Error when unserializing engine: %s' % exception.message)
             engine = None
         except WrongSettingsForEngine, we:
-            lib.debug("Cannot load engine at %s: %s" %( xml_file_name, str(we)))
+            debug("Cannot load engine at %s: %s" %( xml_file_name, str(we)))
             engine = None
         else:
             engine.temporary_folder = self.configuration_manager.TEMPORARY_FOLDER
diff --git a/src/ocrfeeder/studio/project.py b/src/ocrfeeder/studio/project.py
index b02fc17..d091a07 100644
--- a/src/ocrfeeder/studio/project.py
+++ b/src/ocrfeeder/studio/project.py
@@ -19,7 +19,7 @@
 ###########################################################################
 
 from dataHolder import PageData, DataBox, TextData
-from ocrfeeder.util.lib import debug
+from ocrfeeder.util.log import debug
 from ocrfeeder.util.configuration import ConfigurationManager
 from xml.dom import minidom
 import os.path
diff --git a/src/ocrfeeder/studio/studioBuilder.py b/src/ocrfeeder/studio/studioBuilder.py
index 8f0ae3d..6ab2404 100644
--- a/src/ocrfeeder/studio/studioBuilder.py
+++ b/src/ocrfeeder/studio/studioBuilder.py
@@ -32,6 +32,7 @@ from ocrfeeder.feeder.ocrEngines import Engine, OcrEnginesManager
 from ocrfeeder.feeder.documentGeneration import DocumentGeneratorManager
 from ocrfeeder.util.configuration import ConfigurationManager
 from ocrfeeder.util.asyncworker import AsyncItem
+from ocrfeeder.util.log import debug
 from optparse import OptionParser
 import gettext
 import locale
@@ -410,7 +411,7 @@ class Studio:
         ocr_dialog.destroy()
 
     def warnNoOCREngines(self):
-        lib.debug('No OCR engines found')
+        debug('No OCR engines found')
         dialog = Gtk.MessageDialog(self.main_window,
                                    Gtk.DialogFlags.MODAL |
                                    Gtk.DialogFlags.DESTROY_WITH_PARENT,
diff --git a/src/ocrfeeder/studio/widgetModeler.py b/src/ocrfeeder/studio/widgetModeler.py
index 9a002f7..4818ad1 100644
--- a/src/ocrfeeder/studio/widgetModeler.py
+++ b/src/ocrfeeder/studio/widgetModeler.py
@@ -26,7 +26,8 @@ from ocrfeeder.feeder.layoutAnalysis import *
 from project import ProjectSaver, ProjectLoader
 from ocrfeeder.util import graphics, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, \
      ALIGN_FILL, PAPER_SIZES
-from ocrfeeder.util.lib import debug, getNonExistingFileName, unpaperImage
+from ocrfeeder.util.lib import getNonExistingFileName, unpaperImage
+from ocrfeeder.util.log import debug, warning
 from ocrfeeder.util.configuration import ConfigurationManager
 from ocrfeeder.util import constants
 from ocrfeeder.util.asyncworker import AsyncItem
@@ -316,6 +317,17 @@ class ImageReviewer_Controler:
         return image_reviewer
 
     def addImages(self, image_path_list):
+        paths = []
+        for path in image_path_list:
+            if os.path.exists(path):
+                paths.append(path)
+            else:
+                warning('Could not load image "%s": does not exist' % path)
+
+        if not paths:
+            return
+
+        image_path_list = paths
         item_list = []
         temp_dir = self.configuration_manager.TEMPORARY_FOLDER
         image_path_list = graphics.convertMultiImagesInList(image_path_list,
diff --git a/src/ocrfeeder/studio/widgetPresenter.py b/src/ocrfeeder/studio/widgetPresenter.py
index 139f2d0..a5f6e4b 100644
--- a/src/ocrfeeder/studio/widgetPresenter.py
+++ b/src/ocrfeeder/studio/widgetPresenter.py
@@ -24,6 +24,7 @@ from ocrfeeder.util import lib, PAPER_SIZES
 from ocrfeeder.util.configuration import ConfigurationManager
 from ocrfeeder.util.asyncworker import AsyncWorker
 from ocrfeeder.util.constants import *
+from ocrfeeder.util.log import debug
 from ocrfeeder.util.graphics import convertPixbufToImage
 from enchant.checker import SpellChecker
 from PIL import Image
@@ -979,7 +980,7 @@ class UnpaperDialog(Gtk.Dialog):
         try:
             thumbnail_image = Image.open(image_path)
         except Exception, exception:
-            lib.debug(exception.message)
+            debug(exception.message)
             return
         thumbnail_image.thumbnail((150, 200), Image.ANTIALIAS)
         image_thumbnail_path = lib.getNonExistingFileName(name + '_thumb.png')
@@ -1359,7 +1360,7 @@ class PreferencesDialog(Gtk.Dialog):
                 self.unpaper_images.get_active()
         index = self.engines_combo.get_active()
         if index != -1:
-            lib.debug('ACTIVE INDEX: %s %s' % (index, self.ocr_engines[index][0].name))
+            debug('ACTIVE INDEX: %s %s' % (index, self.ocr_engines[index][0].name))
             self.configuration_manager.setFavoriteEngine(self.ocr_engines[index][0].name)
 
     def __makeGeneralPreferences(self, *args):
diff --git a/src/ocrfeeder/util/Makefile.am b/src/ocrfeeder/util/Makefile.am
index 43c7e2e..f49b534 100644
--- a/src/ocrfeeder/util/Makefile.am
+++ b/src/ocrfeeder/util/Makefile.am
@@ -6,7 +6,8 @@ ocrfeeder_util_PYTHON = \
        configuration.py \
        constants.py \
        graphics.py \
-       lib.py
+       lib.py \
+       log.py
 
 DISTCLEANFILES = constants.py
 
diff --git a/src/ocrfeeder/util/asyncworker.py b/src/ocrfeeder/util/asyncworker.py
index b58ae59..c42b1e2 100644
--- a/src/ocrfeeder/util/asyncworker.py
+++ b/src/ocrfeeder/util/asyncworker.py
@@ -25,7 +25,7 @@
 from threading import Thread
 import Queue
 from gi.repository import GLib
-from lib import debug
+from log import debug
 
 class AsyncItem(object):
 
diff --git a/src/ocrfeeder/util/configuration.py b/src/ocrfeeder/util/configuration.py
index bb594c5..6feb9a1 100644
--- a/src/ocrfeeder/util/configuration.py
+++ b/src/ocrfeeder/util/configuration.py
@@ -19,7 +19,8 @@
 ###########################################################################
 
 from ocrfeeder.feeder.ocrEngines import Engine
-from ocrfeeder.util.lib import getExecPath, debug
+from ocrfeeder.util.lib import getExecPath
+from ocrfeeder.util.log import debug
 from ocrfeeder.util.constants import OCRFEEDER_COMPACT_NAME, USER_CONFIG_DIR
 import tempfile
 import shutil
diff --git a/src/ocrfeeder/util/graphics.py b/src/ocrfeeder/util/graphics.py
index 270e6a3..1fc27f2 100644
--- a/src/ocrfeeder/util/graphics.py
+++ b/src/ocrfeeder/util/graphics.py
@@ -19,7 +19,8 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ###########################################################################
 
-from lib import debug, getNonExistingFileName
+from lib import getNonExistingFileName
+from log import debug
 from PIL import Image
 from gi.repository import GdkPixbuf
 import math
diff --git a/src/ocrfeeder/util/lib.py b/src/ocrfeeder/util/lib.py
index 7d3bbf6..73b03b9 100644
--- a/src/ocrfeeder/util/lib.py
+++ b/src/ocrfeeder/util/lib.py
@@ -30,6 +30,7 @@ import sane
 import tempfile
 import locale
 from lxml import etree
+from log import debug
 
 def getIconOrLabel(icon_name, label_text, icon_size = Gtk.IconSize.SMALL_TOOLBAR):
     icon = Gtk.Image()
@@ -100,10 +101,6 @@ def getStandardDeviation(list_of_values):
     new_average = sum(op_list) / float(number_of_values)
     return math.sqrt(new_average)
 
-def debug(*args):
-    if OCRFEEDER_DEBUG:
-        print 'OCRFEEDER DEBUG :::::: ', args
-
 def getExecPath(exec_name):
     real_exec_name = None
     if os.path.isfile(exec_name) and os.access(exec_name, os.X_OK):
diff --git a/src/ocrfeeder/util/log.py b/src/ocrfeeder/util/log.py
new file mode 100644
index 0000000..a4fba4a
--- /dev/null
+++ b/src/ocrfeeder/util/log.py
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+###########################################################################
+#    OCRFeeder - The complete OCR suite
+#    Copyright (C) 2013 Joaquim Rocha <me joaquimrocha com>
+#    Copyright (C) 2009-2012 Igalia, S.L.
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+###########################################################################
+
+import logging
+from constants import OCRFEEDER_COMPACT_NAME
+
+logger = logging.getLogger(OCRFEEDER_COMPACT_NAME)
+LOG_FORMAT = "%(asctime)-15s %(levelname)s: %(message)s"
+logging.basicConfig(format=LOG_FORMAT)
+
+def debug(*args):
+    if OCRFEEDER_DEBUG:
+        logger.setLevel(logging.DEBUG)
+    logger.debug(*args)
+
+info = logger.info
+warning = logger.warning
+error = logger.error
+critical = logger.critical
+log = logger.log
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]