From ac133984a425287e02c3ef5331087f133e15dd7b Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sun, 24 Jun 2012 00:35:23 +0000 Subject: [PATCH] GEPS008: Create new module for utilities to cast types svn: r19910 --- po/POTFILES.skip | 1 + src/Utils.py | 56 -------------------------- src/gen/plug/_options.py | 4 +- src/gen/utils/Makefile.am | 1 + src/gen/utils/cast.py | 83 +++++++++++++++++++++++++++++++++++++++ src/plugins/bookreport.py | 11 +++--- 6 files changed, 92 insertions(+), 64 deletions(-) create mode 100644 src/gen/utils/cast.py diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 5e462466e..f3b998238 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -211,6 +211,7 @@ src/gen/simple/_simpledoc.py src/gen/utils/__init__.py src/gen/utils/callback.py src/gen/utils/callman.py +src/gen/utils/cast.py src/gen/utils/file.py src/gen/utils/image.py src/gen/utils/referent.py diff --git a/src/Utils.py b/src/Utils.py index da921e199..9f9e91396 100644 --- a/src/Utils.py +++ b/src/Utils.py @@ -242,62 +242,6 @@ def create_uid(self, handle=None): # # #------------------------------------------------------------------------- -def cast_to_bool(val): - if val == str(True): - return True - return False - -def get_type_converter(val): - """ - Return function that converts strings into the type of val. - """ - val_type = type(val) - if val_type in (str, unicode): - return unicode - elif val_type == int: - return int - elif val_type == float: - return float - elif val_type == bool: - return cast_to_bool - elif val_type in (list, tuple): - return list - -def type_name(val): - """ - Return the name the type of val. - - Only numbers and strings are supported. - The rest becomes strings (unicode). - """ - val_type = type(val) - if val_type == int: - return 'int' - elif val_type == float: - return 'float' - elif val_type == bool: - return 'bool' - elif val_type in (str, unicode): - return 'unicode' - return 'unicode' - -def get_type_converter_by_name(val_str): - """ - Return function that converts strings into the type given by val_str. - - Only numbers and strings are supported. - The rest becomes strings (unicode). - """ - if val_str == 'int': - return int - elif val_str == 'float': - return float - elif val_str == 'bool': - return cast_to_bool - elif val_str in ('str', 'unicode'): - return unicode - return unicode - def profile(func, *args): import hotshot.stats diff --git a/src/gen/plug/_options.py b/src/gen/plug/_options.py index 4ef41eb89..0657d3b55 100644 --- a/src/gen/plug/_options.py +++ b/src/gen/plug/_options.py @@ -51,7 +51,7 @@ except: # gramps modules # #------------------------------------------------------------------------- -import Utils +from gen.utils.cast import get_type_converter import gen #------------------------------------------------------------------------- @@ -346,7 +346,7 @@ class OptionHandler(object): bad_opts.append(option_name) continue try: - converter = Utils.get_type_converter(self.options_dict[option_name]) + converter = get_type_converter(self.options_dict[option_name]) self.options_dict[option_name] = converter(option_data) except ValueError: pass diff --git a/src/gen/utils/Makefile.am b/src/gen/utils/Makefile.am index 8928a1db9..fbb2a6fd3 100644 --- a/src/gen/utils/Makefile.am +++ b/src/gen/utils/Makefile.am @@ -11,6 +11,7 @@ pkgpython_PYTHON = \ alive.py \ callback.py \ callman.py \ + cast.py \ configmanager.py \ fallback.py \ file.py \ diff --git a/src/gen/utils/cast.py b/src/gen/utils/cast.py new file mode 100644 index 000000000..0b720590b --- /dev/null +++ b/src/gen/utils/cast.py @@ -0,0 +1,83 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2000-2007 Donald N. Allingham +# Copyright (C) 2009 Gary Burton +# Copyright (C) 2011 Tim G L Lyons +# +# 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 2 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, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# $Id$ + +""" +Utility functions to cast types +""" + +def cast_to_bool(val): + if val == str(True): + return True + return False + +def get_type_converter(val): + """ + Return function that converts strings into the type of val. + """ + val_type = type(val) + if val_type in (str, unicode): + return unicode + elif val_type == int: + return int + elif val_type == float: + return float + elif val_type == bool: + return cast_to_bool + elif val_type in (list, tuple): + return list + +def type_name(val): + """ + Return the name the type of val. + + Only numbers and strings are supported. + The rest becomes strings (unicode). + """ + val_type = type(val) + if val_type == int: + return 'int' + elif val_type == float: + return 'float' + elif val_type == bool: + return 'bool' + elif val_type in (str, unicode): + return 'unicode' + return 'unicode' + +def get_type_converter_by_name(val_str): + """ + Return function that converts strings into the type given by val_str. + + Only numbers and strings are supported. + The rest becomes strings (unicode). + """ + if val_str == 'int': + return int + elif val_str == 'float': + return float + elif val_str == 'bool': + return cast_to_bool + elif val_str in ('str', 'unicode'): + return unicode + return unicode diff --git a/src/plugins/bookreport.py b/src/plugins/bookreport.py index d442ab3fa..630fc6a0a 100644 --- a/src/plugins/bookreport.py +++ b/src/plugins/bookreport.py @@ -69,6 +69,7 @@ import gobject #------------------------------------------------------------------------- import const import Utils +from gen.utils.cast import get_type_converter_by_name, type_name from gui.listmodel import ListModel from gen.errors import FilterError, ReportError from gui.pluginmanager import GuiPluginManager @@ -461,9 +462,7 @@ class BookList(object): escape(option_name), len(options[option_name]) ) ) for list_index in range(len(option_value)): - option_type = Utils.type_name( - option_value[list_index] - ) + option_type = type_name(option_value[list_index]) value = escape(unicode(option_value[list_index])) value = value.replace('"', '"') f.write(' \n') else: - option_type = Utils.type_name(option_value) + option_type = type_name(option_value) value = escape(unicode(option_value)) value = value.replace('"', '"') f.write('