GEPS008: Create new module for utilities to cast types
svn: r19910
This commit is contained in:
parent
8b297167b1
commit
ac133984a4
@ -211,6 +211,7 @@ src/gen/simple/_simpledoc.py
|
|||||||
src/gen/utils/__init__.py
|
src/gen/utils/__init__.py
|
||||||
src/gen/utils/callback.py
|
src/gen/utils/callback.py
|
||||||
src/gen/utils/callman.py
|
src/gen/utils/callman.py
|
||||||
|
src/gen/utils/cast.py
|
||||||
src/gen/utils/file.py
|
src/gen/utils/file.py
|
||||||
src/gen/utils/image.py
|
src/gen/utils/image.py
|
||||||
src/gen/utils/referent.py
|
src/gen/utils/referent.py
|
||||||
|
56
src/Utils.py
56
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):
|
def profile(func, *args):
|
||||||
import hotshot.stats
|
import hotshot.stats
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ except:
|
|||||||
# gramps modules
|
# gramps modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import Utils
|
from gen.utils.cast import get_type_converter
|
||||||
import gen
|
import gen
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -346,7 +346,7 @@ class OptionHandler(object):
|
|||||||
bad_opts.append(option_name)
|
bad_opts.append(option_name)
|
||||||
continue
|
continue
|
||||||
try:
|
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)
|
self.options_dict[option_name] = converter(option_data)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
@ -11,6 +11,7 @@ pkgpython_PYTHON = \
|
|||||||
alive.py \
|
alive.py \
|
||||||
callback.py \
|
callback.py \
|
||||||
callman.py \
|
callman.py \
|
||||||
|
cast.py \
|
||||||
configmanager.py \
|
configmanager.py \
|
||||||
fallback.py \
|
fallback.py \
|
||||||
file.py \
|
file.py \
|
||||||
|
83
src/gen/utils/cast.py
Normal file
83
src/gen/utils/cast.py
Normal 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
|
@ -69,6 +69,7 @@ import gobject
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
import const
|
import const
|
||||||
import Utils
|
import Utils
|
||||||
|
from gen.utils.cast import get_type_converter_by_name, type_name
|
||||||
from gui.listmodel import ListModel
|
from gui.listmodel import ListModel
|
||||||
from gen.errors import FilterError, ReportError
|
from gen.errors import FilterError, ReportError
|
||||||
from gui.pluginmanager import GuiPluginManager
|
from gui.pluginmanager import GuiPluginManager
|
||||||
@ -461,9 +462,7 @@ class BookList(object):
|
|||||||
escape(option_name),
|
escape(option_name),
|
||||||
len(options[option_name]) ) )
|
len(options[option_name]) ) )
|
||||||
for list_index in range(len(option_value)):
|
for list_index in range(len(option_value)):
|
||||||
option_type = Utils.type_name(
|
option_type = type_name(option_value[list_index])
|
||||||
option_value[list_index]
|
|
||||||
)
|
|
||||||
value = escape(unicode(option_value[list_index]))
|
value = escape(unicode(option_value[list_index]))
|
||||||
value = value.replace('"', '"')
|
value = value.replace('"', '"')
|
||||||
f.write(' <listitem number="%d" type="%s" '
|
f.write(' <listitem number="%d" type="%s" '
|
||||||
@ -473,7 +472,7 @@ class BookList(object):
|
|||||||
value ) )
|
value ) )
|
||||||
f.write(' </option>\n')
|
f.write(' </option>\n')
|
||||||
else:
|
else:
|
||||||
option_type = Utils.type_name(option_value)
|
option_type = type_name(option_value)
|
||||||
value = escape(unicode(option_value))
|
value = escape(unicode(option_value))
|
||||||
value = value.replace('"', '"')
|
value = value.replace('"', '"')
|
||||||
f.write(' <option name="%s" type="%s" '
|
f.write(' <option name="%s" type="%s" '
|
||||||
@ -550,10 +549,10 @@ class BookParser(handler.ContentHandler):
|
|||||||
if attrs.has_key('length'):
|
if attrs.has_key('length'):
|
||||||
self.an_o_value = []
|
self.an_o_value = []
|
||||||
else:
|
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'])
|
self.an_o_value = converter(attrs['value'])
|
||||||
elif tag == "listitem":
|
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']))
|
self.an_o_value.append(converter(attrs['value']))
|
||||||
elif tag == "style":
|
elif tag == "style":
|
||||||
self.s = attrs['name']
|
self.s = attrs['name']
|
||||||
|
Loading…
x
Reference in New Issue
Block a user