GEPS008: Create new module for utilities to cast types

svn: r19910
This commit is contained in:
Nick Hall 2012-06-24 00:35:23 +00:00
parent 8b297167b1
commit ac133984a4
6 changed files with 92 additions and 64 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -11,6 +11,7 @@ pkgpython_PYTHON = \
alive.py \
callback.py \
callman.py \
cast.py \
configmanager.py \
fallback.py \
file.py \

83
src/gen/utils/cast.py Normal file
View File

@ -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

View File

@ -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(' <listitem number="%d" type="%s" '
@ -473,7 +472,7 @@ class BookList(object):
value ) )
f.write(' </option>\n')
else:
option_type = Utils.type_name(option_value)
option_type = type_name(option_value)
value = escape(unicode(option_value))
value = value.replace('"', '&quot;')
f.write(' <option name="%s" type="%s" '
@ -550,10 +549,10 @@ class BookParser(handler.ContentHandler):
if attrs.has_key('length'):
self.an_o_value = []
else:
converter = Utils.get_type_converter_by_name(attrs['type'])
converter = get_type_converter_by_name(attrs['type'])
self.an_o_value = converter(attrs['value'])
elif tag == "listitem":
converter = Utils.get_type_converter_by_name(attrs['type'])
converter = get_type_converter_by_name(attrs['type'])
self.an_o_value.append(converter(attrs['value']))
elif tag == "style":
self.s = attrs['name']