[Pycodeocr-main] SF.net SVN: pycodeocr:[50] trunk
Status: Beta
Brought to you by:
drtrigon
From: <drt...@us...> - 2011-04-27 19:43:48
|
Revision: 50 http://pycodeocr.svn.sourceforge.net/pycodeocr/?rev=50&view=rev Author: drtrigon Date: 2011-04-27 19:43:41 +0000 (Wed, 27 Apr 2011) Log Message: ----------- group the various modules into one single package 'utils' Modified Paths: -------------- trunk/PyCodeOCR.py Added Paths: ----------- trunk/utils/ trunk/utils/__init__.py trunk/utils/checksum.py trunk/utils/installbundle.py trunk/utils/ocr.py trunk/utils/requirements.py Removed Paths: ------------- trunk/checksum.py trunk/installbundle.py trunk/requirements.py Modified: trunk/PyCodeOCR.py =================================================================== --- trunk/PyCodeOCR.py 2011-04-27 19:01:54 UTC (rev 49) +++ trunk/PyCodeOCR.py 2011-04-27 19:43:41 UTC (rev 50) @@ -41,12 +41,14 @@ # python standard modules import os, re, sys, subprocess, time, gobject, string, struct +# PyCodeOCR python standard utils (modules) +import utils + # check and install requirements #print "To check and install requirements run: 'python %s --install-deps'" % sys.argv[0] -import requirements if "--install-deps" in sys.argv: - requirements.check_all_pycodeocr_requirements() + utils.requirements.check_all_pycodeocr_requirements() sys.exit() @@ -56,7 +58,7 @@ import gtk, gtk.glade import gobject -from checksum import modulo10 +from utils.checksum import modulo10 # PIL image library and it's SANE bindings import Image, ImageDraw @@ -76,9 +78,9 @@ print "To check and install requirements run: 'python %s --install-deps'" % sys.argv[0] pakages = [] # gocr -paths = { "gocr": requirements.which("gocr"), - "dmtxread": requirements.which("dmtxread"), - "pdf417decode.exe": requirements.which("pdf417decode.exe"), } +paths = { "gocr": utils.requirements.which("gocr"), + "dmtxread": utils.requirements.which("dmtxread"), + "pdf417decode.exe": utils.requirements.which("pdf417decode.exe"), } if os.path.exists(paths["gocr"]): pakages.append( "gocr" ) print "gocr found." @@ -612,7 +614,7 @@ if (op_mode == self.MDE['invoices']): # 0: invoices self.progress(4./max_steps, "Character correction...") print "*** " * 10 - data = self.char_correction(data) + data = utils.ocr.char_correction(data) self.refresh() if self.__stop: return @@ -775,68 +777,6 @@ self.progressbar1.set_text(text) self.refresh() - ## Character correction after recogition (on basis that there should be numbers and few special chars). - # - # @test Add unittest to check correct operation of this important module/function. - # - # @todo May be this important function should be placed into a separate module, - # similar to @ref checksum. - # - def char_correction(self, data): - data = re.sub("\n", "", data) - print data - corrections = [ - # Zero - ("O", "0"), ("D", "0"), ("U", "0"), ("Q", "0"), ("\(\)", "0"), ("G", "0"), ("o", "0"), - # Two - ("Z", "2"), - # Three - ("\?>", "3"), - # Four - ("l\.", "4"), ("A", "4"), ("l»", "4"), ("h", "4"), ("i»", "4"), ("l\xe2\x80\xa2", "4"), ("&", "4"), - # Five - ("S", "5"), - # Six - ("c>", "6"), - # Seven - #("\?", "7"), - # One (critical, therefore done as last; has also Overlapps e.g. with Four) - (chr(226)+chr(128)+chr(153)+"I", "1"), - (chr(226)+chr(128)+chr(153)+"\|", "1"), - (chr(226)+chr(128)+chr(152)+"\|", "1"), - (chr(226)+chr(128)+chr(152)+"I", "1"), - (chr(39)+"I", "1"), - ("I", "1"), - (chr(153)+"l", "1"), - (chr(39)+"l", "1"), - ("l", "1"), - (chr(39)+"!", "1"), - (chr(39)+"\|", "1"), - ("\|", "1"), - ("'\]", "1"), - ("'\}", "1"), - (r'\\', "1"), - # non-numbers - (" ", ""), - ("\xe2", ""), - ("\x80", ""), - ("\xbb", ""), - ("\x99", ""), - ("\x98", ""), - ("\x5f", ""), - ("\x60", ""), - ("'", ""), - #('\xbb', 187)('\x99', 153)('\x98', 152) - ("\+", "+ "), - ("\x92", "1"), # post correction - ] - for item in corrections: - olddata = data - data = re.sub(item[0], item[1], data) - if not (data == olddata): print 'subst: "%s" => "%s"' % (item[0], item[1]) - - return data - ## Run external shell command/application. class RunExternal: Deleted: trunk/checksum.py =================================================================== --- trunk/checksum.py 2011-04-27 19:01:54 UTC (rev 49) +++ trunk/checksum.py 2011-04-27 19:43:41 UTC (rev 50) @@ -1,47 +0,0 @@ -## -# @file checksum.py -# @package checksum -# @authors laserb, drtrigon -# @version 1.1 -# @brief Modulo 10 checksum for e-banking codes. -# -# @test Add unittest to check correct operation of this important module. -# -# $Id$ -# -# @section Description -# -# Modulo 10 checksum for e-banking codes -# - -class modulo10: - # thanks to http://www.hosang.ch/modulo10.aspx - # much more details http://www.sic.ch/de/dl_tkicch_dta.pdf ( page 51 ) - # @see http://www.bundesbank.de/download/zahlungsverkehr/zv_pz201012.pdf - # @see http://www.bundesbank.de/zahlungsverkehr/zahlungsverkehr_pruefziffernberechnung.php - def __init__(self, amount, account, reference): - self.tabelle = [0,9,4,6,8,2,7,1,3,5] - - self.account = account - self.reference = reference - - self.checksum_b = self._checksum(amount) - self.checksum_k = self._checksum(account) - self.checksum_r = self._checksum(reference) - self.checksum = self.checksum_b and self.checksum_k and self.checksum_r - - try: - self.amount = str(int(amount[2:-1])/100.) - except ValueError: - self.amount = amount + " (error)" - - def _run(self, nr): - self.uebertrag = 0 - # iterate over each character - for i in range(len(nr)): - self.uebertrag = self.tabelle[(self.uebertrag + int(nr[i]) )%10] - - return (10-self.uebertrag)%10 - - def _checksum(self, value): - return (int(value[-1]) == self._run(value[:-1]) ) Deleted: trunk/installbundle.py =================================================================== --- trunk/installbundle.py 2011-04-27 19:01:54 UTC (rev 49) +++ trunk/installbundle.py 2011-04-27 19:43:41 UTC (rev 50) @@ -1,196 +0,0 @@ -## -# @file installbundle.py -# @package installbundle -# @authors University of Utah (VisTrails), drtrigon -# @version 1.1 -# @brief Module with utilities to try and install a bundle if possible. -# -# $Id$ -# -# @section Description -# -# Module with utilities to try and install a bundle if possible. -# -############################################################################ -## -## Copyright (C) 2006-2010 University of Utah. All rights reserved. -## -## This file is part of VisTrails. -## -## This file may be used under the terms of the GNU General Public -## License version 2.0 as published by the Free Software Foundation -## and appearing in the file LICENSE.GPL included in the packaging of -## this file. Please review the following to ensure GNU General Public -## Licensing requirements will be met: -## http://www.opensource.org/licenses/gpl-license.php -## -## If you are unsure which license is appropriate for your use (for -## instance, you are interested in developing a commercial derivative -## of VisTrails), please contact us at vis...@sc.... -## -## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -## -## Changed 2011 by drtrigon. Source from 'core.bundles.installbundle' -## http://downloads.sourceforge.net/project/vistrails/vistrails/nightly/vistrails-src-nightly.tar.gz -## -## This file was modified to be used as part of PyCodeOCR. -## -############################################################################ -# - -"""Module with utilities to try and install a bundle if possible.""" - -import os - -############################################################################## - -def linux_ubuntu_install(package_name): - qt = has_qt() - hide_splash_if_necessary() - - if qt: - cmd = core.system.vistrails_root_directory() - cmd += '/core/bundles/linux_ubuntu_install.py' - else: - cmd = 'apt-get install -y' - - if type(package_name) == str: - cmd += ' ' + package_name - elif type(package_name) == list: - for package in package_name: - if type(package) != str: - raise TypeError("Expected string or list of strings") - cmd += ' ' + package - - if qt: - sucmd = guess_graphical_sudo() + " '" + cmd + "'" - else: - debug.warning("PyCodeOCR wants to install package(s) '%s'" % - package_name) - sucmd = "sudo " + cmd - - result = os.system(sucmd) - - return (result == 0) # 0 indicates success - -def linux_fedora_install(package_name): - qt = has_qt() - hide_splash_if_necessary() - if qt: - cmd = core.system.vistrails_root_directory() - cmd += '/core/bundles/linux_fedora_install.py' - else: - cmd = 'yum -y install' - - if type(package_name) == str: - cmd += ' ' + package_name - elif type(package_name) == list: - for package in package_name: - if type(package) != str: - raise TypeError("Expected string or list of strings") - cmd += ' ' + package - - if qt: - sucmd = guess_graphical_sudo() + " " + cmd - else: - debug.warning(("PyCodeOCR wants to install package(s) '%s' through " - "_sudo_. Make sure you are a sudoer.") % package_name) - sucmd = "sudo " + cmd - - debug.warning("EXECUTING: sucmd") - result = os.system(sucmd) - debug.warning("RETURN VALUE: %s" % result) - return (result == 0) - -def show_question(which_files): - qt = has_qt() - if qt: - import gui.utils - if type(which_files) == str: - which_files = [which_files] - v = gui.utils.show_question("Required packages missing", - "One or more required packages are missing: " + - " ".join(which_files) + - ". PyCodeOCR can " + - "automaticallly install them. " + - "If you click OK, PyCodeOCR will need "+ - "administrator privileges, and you " + - "might be asked for the administrator password.", - buttons=[gui.utils.OK_BUTTON, - gui.utils.CANCEL_BUTTON], - default=gui.utils.OK_BUTTON) - return v == gui.utils.OK_BUTTON - else: - print "Required package missing" - print ("A required package is missing, but PyCodeOCR can " + - "automatically install it. " + - "If you say Yes, PyCodeOCR will need "+ - "administrator privileges, and you" + - "might be asked for the administrator password.") - print "Give PyCodeOCR permission to try to install package? (y/N)" - v = raw_input().upper() - return v == 'Y' or v == 'YES' - - -def install(dependency_dictionary): - """Tries to import a python module. If unsuccessful, tries to install -the appropriate bundle and then reimport. py_import tries to be smart -about which system it runs on.""" - - # Ugly fix to avoid circular import - distro = guess_system() - if not dependency_dictionary.has_key(distro): - return False - else: - files = dependency_dictionary[distro] - if show_question(files): - callable_ = getattr(installbundle, - distro.replace('-', '_') + '_install') - return callable_(files) - else: - return False - -############################################################################## - -## Guesses the operation system. -# -# Replaces original VisTrails 'core.bundles.utils.guess_system'. -# -# @since (added for PyCodeOCR) -# -def guess_system(): - import platform - #print os.name, platform.platform() - return ("%s-%s" % (platform.system(), platform.dist()[0])).lower() - -## Dummy function, returns False. -# -# Replaces original VisTrails 'has_qt'. -# -# @since (added for PyCodeOCR) -# -def has_qt(): - return False - -import installbundle - -## Dummy function, just passes. -# -# Replaces original VisTrails 'hide_splash_if_necessary'. -# -# @since (added for PyCodeOCR) -# -def hide_splash_if_necessary(): - pass - -## Debug warning output handler. -# -# Replaces original VisTrails 'core.debug.warning'. -# -# @since (added for PyCodeOCR) -# -class dbg(): - def warning(self, *args): - print "".join(args) -debug = dbg() Deleted: trunk/requirements.py =================================================================== --- trunk/requirements.py 2011-04-27 19:01:54 UTC (rev 49) +++ trunk/requirements.py 2011-04-27 19:43:41 UTC (rev 50) @@ -1,241 +0,0 @@ -## -# @file requirements.py -# @package requirements -# @authors University of Utah (VisTrails), drtrigon -# @version 1.1 -# @brief Online test presence of runtime components. -# -# $Id$ -# -# @section Description -# -# module that allows online inspection of environment to test presence of -# runtime components such as binaries, libraries, other python modules, etc. -# -############################################################################ -## -## Copyright (C) 2006-2010 University of Utah. All rights reserved. -## -## This file is part of VisTrails. -## -## This file may be used under the terms of the GNU General Public -## License version 2.0 as published by the Free Software Foundation -## and appearing in the file LICENSE.GPL included in the packaging of -## this file. Please review the following to ensure GNU General Public -## Licensing requirements will be met: -## http://www.opensource.org/licenses/gpl-license.php -## -## If you are unsure which license is appropriate for your use (for -## instance, you are interested in developing a commercial derivative -## of VisTrails), please contact us at vis...@sc.... -## -## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE -## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -## -## Changed 2011 by drtrigon. Source from 'core.requirements' -## http://downloads.sourceforge.net/project/vistrails/vistrails/nightly/vistrails-src-nightly.tar.gz -## -## This file was modified to be used as part of PyCodeOCR. -## -############################################################################ -# - -"""module that allows online inspection of environment to test presence of -runtime components such as binaries, libraries, other python modules, etc.""" - -import sys - -############################################################################## - -def python_module_exists(module_name): - """python_module_exists(module_name): Boolean. -Returns if python module of given name can be safely imported.""" - - try: - sys.modules[module_name] - return True - except KeyError: - pass - try: - __import__(module_name) - return True - except ImportError: - return False - - -def executable_file_exists(filename): - """executable_file_exists(filename): Boolean. -Returns if certain file is in current path and is executable.""" - result = executable_is_in_path(filename) - if result == "": - result = executable_is_in_pythonpath(filename) - return result != "" - -# FIXME: Add documentation. - -def require_python_module(module_name): - if not python_module_exists(module_name): - raise MissingRequirement(module_name) - -def require_executable(filename): - if not executable_file_exists(filename): - raise MissingRequirement(filename) - -def check_pyqt4(): - # checks for the presence of pyqt4, which is more important than the rest, - # since using pyqt requires a qapplication. - try: - require_python_module('PyQt4.QtGui') - require_python_module('PyQt4.QtOpenGL') - except MissingRequirement: - r = core.bundles.installbundle.install({'linux-ubuntu': ['python-qt4', - 'python-qt4-gl', - 'python-qt4-sql']}) - if not r: - raise - - -def check_all_vistrails_requirements(): - pass - - # check scipy -# try: -# require_python_module('scipy') -# except MissingRequirement: -# r = core.bundles.installbundle.install({'linux-ubuntu': 'python-scipy'}) -# if not r: -# raise - - -############################################################################## - -class MissingRequirement(Exception): - """Raise this exception in packages when necessary items are missing.""" - def __init__(self, req): - self.requirement = req - def __str__(self): - return "Missing Requirement: %s" % self.requirement - -############################################################################## - -import installbundle -import os - - -## Shows the full path of (shell) commands. -# -# According to shell command 'which', look also at: -# @see http://unixhelp.ed.ac.uk/CGI/man-cgi?which -# @see http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python -# -# @since (added for PyCodeOCR) -# -def which(program, search_path=None): - is_exe = lambda fpath: os.path.exists(fpath) and os.access(fpath, os.X_OK) - #is_exe = lambda fpath: os.path.exists(fpath) - - fpath, fname = os.path.split(program) - if fpath: - if is_exe(program): - return program - else: - if not search_path: - search_path = os.environ["PATH"].split(os.pathsep) - search_path += [ '.' ] # add local path - for path in search_path: - exe_file = os.path.join(path, program) - if is_exe(exe_file): - return exe_file - return "" - -## Checks if executable is in system PATH. -# -# Replaces original VisTrails 'core.system.executable_is_in_path'. -# -# @since (added for PyCodeOCR) -# -def executable_is_in_path(filename): - #print os.system(filename) - return which(filename) - -## Checks if executable is in system PYTHONPATH. -# -# Replaces original VisTrails 'core.system.executable_is_in_pythonpath'. -# -# @since (added for PyCodeOCR) -# -def executable_is_in_pythonpath(filename): - return which(filename, search_path=os.environ.get("PYTHONPATH", None)) - - -## Checks if pygtk libraries are installed. -# -# @since (added for PyCodeOCR) -# -# @todo Check if further dependencies/packages are needed...? -# -def check_pygtk(): - # checks for the presence of pygtk, which is more important than the rest. - try: - require_python_module('pygtk') - #pygtk.require('2.0') - require_python_module('gtk') - require_python_module('gtk.glade') - require_python_module('gobject') - except MissingRequirement: - r = core.bundles.installbundle.install({'linux-fedora': ['pygtk2']}) - # other deps? - if not r: - raise - -## Checks if sane libraries are installed. -# -# @since (added for PyCodeOCR) -# -# @todo Check if further dependencies/packages are needed...? -# -def check_sane(): - try: - require_python_module('sane') - except MissingRequirement: - r = core.bundles.installbundle.install({'linux-fedora': ['python-imaging-sane']}) - # other deps? - if not r: - raise - -## Checks if optional libraries are installed. -# -# @since (added for PyCodeOCR) -# -def check_optionals(): - try: - require_executable("gocr") - except MissingRequirement: - r = installbundle.install({'linux-fedora': ['gocr'], - 'linux-ubuntu': ['gocr']}) - - try: - #require_python_module('pydmtx') - require_executable("dmtxread") - except MissingRequirement: - #r = installbundle.install({'linux-fedora': ['python-libdmtx']}) - r = installbundle.install({'linux-fedora': ['libdmtx-utils'], - 'linux-ubuntu': ['libdmtx-utils']}) - - -## Checks if all libraries whether needed or optional are installed. -# -# @since (added for PyCodeOCR) -# -def check_all_pycodeocr_requirements(): - print "Checking all PyCodeOCR requirements:" - - # needed - print " * NEEDED 'pygtk2'" - check_pygtk() - print " * NEEDED 'sane' (scanner)" - check_sane() - - # optional - print " * OPTIONAL 'gocr', 'dmtxread'" - check_optionals() Added: trunk/utils/__init__.py =================================================================== --- trunk/utils/__init__.py (rev 0) +++ trunk/utils/__init__.py 2011-04-27 19:43:41 UTC (rev 50) @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +## +# @file __init__.py +# @namespace utils +# @authors drtrigon +# @version 1.1 +# @brief This is the utils package with important functions. +# +# $Id: checksum.py -1 $ +# +# @section Description +# +# This is the utils package with important functions. +# + +import utils.checksum +import utils.requirements +import utils.ocr Property changes on: trunk/utils/__init__.py ___________________________________________________________________ Added: svn:eol-style + native Copied: trunk/utils/checksum.py (from rev 49, trunk/checksum.py) =================================================================== --- trunk/utils/checksum.py (rev 0) +++ trunk/utils/checksum.py 2011-04-27 19:43:41 UTC (rev 50) @@ -0,0 +1,47 @@ +## +# @file utils/checksum.py +# @namespace utils::checksum +# @authors laserb, drtrigon +# @version 1.1 +# @brief Modulo 10 checksum for e-banking codes. +# +# @test Add unittest to check correct operation of this important module. +# +# $Id$ +# +# @section Description +# +# Modulo 10 checksum for e-banking codes +# + +class modulo10: + # thanks to http://www.hosang.ch/modulo10.aspx + # much more details http://www.sic.ch/de/dl_tkicch_dta.pdf ( page 51 ) + # @see http://www.bundesbank.de/download/zahlungsverkehr/zv_pz201012.pdf + # @see http://www.bundesbank.de/zahlungsverkehr/zahlungsverkehr_pruefziffernberechnung.php + def __init__(self, amount, account, reference): + self.tabelle = [0,9,4,6,8,2,7,1,3,5] + + self.account = account + self.reference = reference + + self.checksum_b = self._checksum(amount) + self.checksum_k = self._checksum(account) + self.checksum_r = self._checksum(reference) + self.checksum = self.checksum_b and self.checksum_k and self.checksum_r + + try: + self.amount = str(int(amount[2:-1])/100.) + except ValueError: + self.amount = amount + " (error)" + + def _run(self, nr): + self.uebertrag = 0 + # iterate over each character + for i in range(len(nr)): + self.uebertrag = self.tabelle[(self.uebertrag + int(nr[i]) )%10] + + return (10-self.uebertrag)%10 + + def _checksum(self, value): + return (int(value[-1]) == self._run(value[:-1]) ) Copied: trunk/utils/installbundle.py (from rev 46, trunk/installbundle.py) =================================================================== --- trunk/utils/installbundle.py (rev 0) +++ trunk/utils/installbundle.py 2011-04-27 19:43:41 UTC (rev 50) @@ -0,0 +1,196 @@ +## +# @file utils/installbundle.py +# @namespace utils::installbundle +# @authors University of Utah (VisTrails), drtrigon +# @version 1.1 +# @brief Module with utilities to try and install a bundle if possible. +# +# $Id$ +# +# @section Description +# +# Module with utilities to try and install a bundle if possible. +# +############################################################################ +## +## Copyright (C) 2006-2010 University of Utah. All rights reserved. +## +## This file is part of VisTrails. +## +## This file may be used under the terms of the GNU General Public +## License version 2.0 as published by the Free Software Foundation +## and appearing in the file LICENSE.GPL included in the packaging of +## this file. Please review the following to ensure GNU General Public +## Licensing requirements will be met: +## http://www.opensource.org/licenses/gpl-license.php +## +## If you are unsure which license is appropriate for your use (for +## instance, you are interested in developing a commercial derivative +## of VisTrails), please contact us at vis...@sc.... +## +## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +## +## Changed 2011 by drtrigon. Source from 'core.bundles.installbundle' +## http://downloads.sourceforge.net/project/vistrails/vistrails/nightly/vistrails-src-nightly.tar.gz +## +## This file was modified to be used as part of PyCodeOCR. +## +############################################################################ +# + +"""Module with utilities to try and install a bundle if possible.""" + +import os + +############################################################################## + +def linux_ubuntu_install(package_name): + qt = has_qt() + hide_splash_if_necessary() + + if qt: + cmd = core.system.vistrails_root_directory() + cmd += '/core/bundles/linux_ubuntu_install.py' + else: + cmd = 'apt-get install -y' + + if type(package_name) == str: + cmd += ' ' + package_name + elif type(package_name) == list: + for package in package_name: + if type(package) != str: + raise TypeError("Expected string or list of strings") + cmd += ' ' + package + + if qt: + sucmd = guess_graphical_sudo() + " '" + cmd + "'" + else: + debug.warning("PyCodeOCR wants to install package(s) '%s'" % + package_name) + sucmd = "sudo " + cmd + + result = os.system(sucmd) + + return (result == 0) # 0 indicates success + +def linux_fedora_install(package_name): + qt = has_qt() + hide_splash_if_necessary() + if qt: + cmd = core.system.vistrails_root_directory() + cmd += '/core/bundles/linux_fedora_install.py' + else: + cmd = 'yum -y install' + + if type(package_name) == str: + cmd += ' ' + package_name + elif type(package_name) == list: + for package in package_name: + if type(package) != str: + raise TypeError("Expected string or list of strings") + cmd += ' ' + package + + if qt: + sucmd = guess_graphical_sudo() + " " + cmd + else: + debug.warning(("PyCodeOCR wants to install package(s) '%s' through " + "_sudo_. Make sure you are a sudoer.") % package_name) + sucmd = "sudo " + cmd + + debug.warning("EXECUTING: sucmd") + result = os.system(sucmd) + debug.warning("RETURN VALUE: %s" % result) + return (result == 0) + +def show_question(which_files): + qt = has_qt() + if qt: + import gui.utils + if type(which_files) == str: + which_files = [which_files] + v = gui.utils.show_question("Required packages missing", + "One or more required packages are missing: " + + " ".join(which_files) + + ". PyCodeOCR can " + + "automaticallly install them. " + + "If you click OK, PyCodeOCR will need "+ + "administrator privileges, and you " + + "might be asked for the administrator password.", + buttons=[gui.utils.OK_BUTTON, + gui.utils.CANCEL_BUTTON], + default=gui.utils.OK_BUTTON) + return v == gui.utils.OK_BUTTON + else: + print "Required package missing" + print ("A required package is missing, but PyCodeOCR can " + + "automatically install it. " + + "If you say Yes, PyCodeOCR will need "+ + "administrator privileges, and you" + + "might be asked for the administrator password.") + print "Give PyCodeOCR permission to try to install package? (y/N)" + v = raw_input().upper() + return v == 'Y' or v == 'YES' + + +def install(dependency_dictionary): + """Tries to import a python module. If unsuccessful, tries to install +the appropriate bundle and then reimport. py_import tries to be smart +about which system it runs on.""" + + # Ugly fix to avoid circular import + distro = guess_system() + if not dependency_dictionary.has_key(distro): + return False + else: + files = dependency_dictionary[distro] + if show_question(files): + callable_ = getattr(installbundle, + distro.replace('-', '_') + '_install') + return callable_(files) + else: + return False + +############################################################################## + +## Guesses the operation system. +# +# Replaces original VisTrails 'core.bundles.utils.guess_system'. +# +# @since (added for PyCodeOCR) +# +def guess_system(): + import platform + #print os.name, platform.platform() + return ("%s-%s" % (platform.system(), platform.dist()[0])).lower() + +## Dummy function, returns False. +# +# Replaces original VisTrails 'has_qt'. +# +# @since (added for PyCodeOCR) +# +def has_qt(): + return False + +import installbundle + +## Dummy function, just passes. +# +# Replaces original VisTrails 'hide_splash_if_necessary'. +# +# @since (added for PyCodeOCR) +# +def hide_splash_if_necessary(): + pass + +## Debug warning output handler. +# +# Replaces original VisTrails 'core.debug.warning'. +# +# @since (added for PyCodeOCR) +# +class dbg(): + def warning(self, *args): + print "".join(args) +debug = dbg() Added: trunk/utils/ocr.py =================================================================== --- trunk/utils/ocr.py (rev 0) +++ trunk/utils/ocr.py 2011-04-27 19:43:41 UTC (rev 50) @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +## +# @file utils/ocr.py +# @namespace utils::ocr +# @authors drtrigon +# @version 1.1 +# @brief Character recogition (OCR) functions. +# +# $Id$ +# +# @section Description +# +# Character recogition (OCR) functions. +# + +# python standard modules +import re + +## Character correction after recogition (on basis that there should be numbers and few special chars). +# +# @test Add unittest to check correct operation of this important module/function. +# +def char_correction(data): + data = re.sub("\n", "", data) + print data + corrections = [ + # Zero + ("O", "0"), ("D", "0"), ("U", "0"), ("Q", "0"), ("\(\)", "0"), ("G", "0"), ("o", "0"), + # Two + ("Z", "2"), + # Three + ("\?>", "3"), + # Four + ("l\.", "4"), ("A", "4"), ("l»", "4"), ("h", "4"), ("i»", "4"), ("l\xe2\x80\xa2", "4"), ("&", "4"), + # Five + ("S", "5"), + # Six + ("c>", "6"), + # Seven + #("\?", "7"), + # One (critical, therefore done as last; has also Overlapps e.g. with Four) + (chr(226)+chr(128)+chr(153)+"I", "1"), + (chr(226)+chr(128)+chr(153)+"\|", "1"), + (chr(226)+chr(128)+chr(152)+"\|", "1"), + (chr(226)+chr(128)+chr(152)+"I", "1"), + (chr(39)+"I", "1"), + ("I", "1"), + (chr(153)+"l", "1"), + (chr(39)+"l", "1"), + ("l", "1"), + (chr(39)+"!", "1"), + (chr(39)+"\|", "1"), + ("\|", "1"), + ("'\]", "1"), + ("'\}", "1"), + (r'\\', "1"), + # non-numbers + (" ", ""), + ("\xe2", ""), + ("\x80", ""), + ("\xbb", ""), + ("\x99", ""), + ("\x98", ""), + ("\x5f", ""), + ("\x60", ""), + ("'", ""), + #('\xbb', 187)('\x99', 153)('\x98', 152) + ("\+", "+ "), + ("\x92", "1"), # post correction + ] + for item in corrections: + olddata = data + data = re.sub(item[0], item[1], data) + if not (data == olddata): print 'subst: "%s" => "%s"' % (item[0], item[1]) + + return data Property changes on: trunk/utils/ocr.py ___________________________________________________________________ Added: svn:keywords + Id Added: svn:eol-style + native Copied: trunk/utils/requirements.py (from rev 49, trunk/requirements.py) =================================================================== --- trunk/utils/requirements.py (rev 0) +++ trunk/utils/requirements.py 2011-04-27 19:43:41 UTC (rev 50) @@ -0,0 +1,241 @@ +## +# @file utils/requirements.py +# @namespace utils::requirements +# @authors University of Utah (VisTrails), drtrigon +# @version 1.1 +# @brief Online test presence of runtime components. +# +# $Id$ +# +# @section Description +# +# module that allows online inspection of environment to test presence of +# runtime components such as binaries, libraries, other python modules, etc. +# +############################################################################ +## +## Copyright (C) 2006-2010 University of Utah. All rights reserved. +## +## This file is part of VisTrails. +## +## This file may be used under the terms of the GNU General Public +## License version 2.0 as published by the Free Software Foundation +## and appearing in the file LICENSE.GPL included in the packaging of +## this file. Please review the following to ensure GNU General Public +## Licensing requirements will be met: +## http://www.opensource.org/licenses/gpl-license.php +## +## If you are unsure which license is appropriate for your use (for +## instance, you are interested in developing a commercial derivative +## of VisTrails), please contact us at vis...@sc.... +## +## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +## +## Changed 2011 by drtrigon. Source from 'core.requirements' +## http://downloads.sourceforge.net/project/vistrails/vistrails/nightly/vistrails-src-nightly.tar.gz +## +## This file was modified to be used as part of PyCodeOCR. +## +############################################################################ +# + +"""module that allows online inspection of environment to test presence of +runtime components such as binaries, libraries, other python modules, etc.""" + +import sys + +############################################################################## + +def python_module_exists(module_name): + """python_module_exists(module_name): Boolean. +Returns if python module of given name can be safely imported.""" + + try: + sys.modules[module_name] + return True + except KeyError: + pass + try: + __import__(module_name) + return True + except ImportError: + return False + + +def executable_file_exists(filename): + """executable_file_exists(filename): Boolean. +Returns if certain file is in current path and is executable.""" + result = executable_is_in_path(filename) + if result == "": + result = executable_is_in_pythonpath(filename) + return result != "" + +# FIXME: Add documentation. + +def require_python_module(module_name): + if not python_module_exists(module_name): + raise MissingRequirement(module_name) + +def require_executable(filename): + if not executable_file_exists(filename): + raise MissingRequirement(filename) + +def check_pyqt4(): + # checks for the presence of pyqt4, which is more important than the rest, + # since using pyqt requires a qapplication. + try: + require_python_module('PyQt4.QtGui') + require_python_module('PyQt4.QtOpenGL') + except MissingRequirement: + r = core.bundles.installbundle.install({'linux-ubuntu': ['python-qt4', + 'python-qt4-gl', + 'python-qt4-sql']}) + if not r: + raise + + +def check_all_vistrails_requirements(): + pass + + # check scipy +# try: +# require_python_module('scipy') +# except MissingRequirement: +# r = core.bundles.installbundle.install({'linux-ubuntu': 'python-scipy'}) +# if not r: +# raise + + +############################################################################## + +class MissingRequirement(Exception): + """Raise this exception in packages when necessary items are missing.""" + def __init__(self, req): + self.requirement = req + def __str__(self): + return "Missing Requirement: %s" % self.requirement + +############################################################################## + +import installbundle +import os + + +## Shows the full path of (shell) commands. +# +# According to shell command 'which', look also at: +# @see http://unixhelp.ed.ac.uk/CGI/man-cgi?which +# @see http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python +# +# @since (added for PyCodeOCR) +# +def which(program, search_path=None): + is_exe = lambda fpath: os.path.exists(fpath) and os.access(fpath, os.X_OK) + #is_exe = lambda fpath: os.path.exists(fpath) + + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + if not search_path: + search_path = os.environ["PATH"].split(os.pathsep) + search_path += [ '.' ] # add local path + for path in search_path: + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + return "" + +## Checks if executable is in system PATH. +# +# Replaces original VisTrails 'core.system.executable_is_in_path'. +# +# @since (added for PyCodeOCR) +# +def executable_is_in_path(filename): + #print os.system(filename) + return which(filename) + +## Checks if executable is in system PYTHONPATH. +# +# Replaces original VisTrails 'core.system.executable_is_in_pythonpath'. +# +# @since (added for PyCodeOCR) +# +def executable_is_in_pythonpath(filename): + return which(filename, search_path=os.environ.get("PYTHONPATH", None)) + + +## Checks if pygtk libraries are installed. +# +# @since (added for PyCodeOCR) +# +# @todo Check if further dependencies/packages are needed...? +# +def check_pygtk(): + # checks for the presence of pygtk, which is more important than the rest. + try: + require_python_module('pygtk') + #pygtk.require('2.0') + require_python_module('gtk') + require_python_module('gtk.glade') + require_python_module('gobject') + except MissingRequirement: + r = core.bundles.installbundle.install({'linux-fedora': ['pygtk2']}) + # other deps? + if not r: + raise + +## Checks if sane libraries are installed. +# +# @since (added for PyCodeOCR) +# +# @todo Check if further dependencies/packages are needed...? +# +def check_sane(): + try: + require_python_module('sane') + except MissingRequirement: + r = core.bundles.installbundle.install({'linux-fedora': ['python-imaging-sane']}) + # other deps? + if not r: + raise + +## Checks if optional libraries are installed. +# +# @since (added for PyCodeOCR) +# +def check_optionals(): + try: + require_executable("gocr") + except MissingRequirement: + r = installbundle.install({'linux-fedora': ['gocr'], + 'linux-ubuntu': ['gocr']}) + + try: + #require_python_module('pydmtx') + require_executable("dmtxread") + except MissingRequirement: + #r = installbundle.install({'linux-fedora': ['python-libdmtx']}) + r = installbundle.install({'linux-fedora': ['libdmtx-utils'], + 'linux-ubuntu': ['libdmtx-utils']}) + + +## Checks if all libraries whether needed or optional are installed. +# +# @since (added for PyCodeOCR) +# +def check_all_pycodeocr_requirements(): + print "Checking all PyCodeOCR requirements:" + + # needed + print " * NEEDED 'pygtk2'" + check_pygtk() + print " * NEEDED 'sane' (scanner)" + check_sane() + + # optional + print " * OPTIONAL 'gocr', 'dmtxread'" + check_optionals() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |