Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2012-03-13 11:07:40 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2012-03-13 11:07:40 (GMT)
commit5099b1f4f5cca46e9efd8735106e396577f78b2e (patch)
tree430a5508e8aebb1f7a7f2f2be5a4c000e2c1cb27
parent85af88eb5e2302e146d427926d0071b2d4d7dd19 (diff)
Allow functions for typecast for properties
-rw-r--r--active_document/metadata.py5
-rwxr-xr-xtests/units/metadata.py3
2 files changed, 7 insertions, 1 deletions
diff --git a/active_document/metadata.py b/active_document/metadata.py
index e5fa565..1dfab1f 100644
--- a/active_document/metadata.py
+++ b/active_document/metadata.py
@@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
+import types
from os.path import join, exists, abspath, dirname
from gettext import gettext as _
@@ -299,7 +300,9 @@ def _convert(typecast, value):
first = iter(typecast).next()
return type(first) is not type and type(first) not in _LIST_TYPES
- if typecast in [None, str]:
+ if type(typecast) is types.FunctionType:
+ value = typecast(value)
+ elif typecast in [None, str]:
if isinstance(value, unicode):
value = value.encode('utf-8')
else:
diff --git a/tests/units/metadata.py b/tests/units/metadata.py
index a31787e..e70e2b7 100755
--- a/tests/units/metadata.py
+++ b/tests/units/metadata.py
@@ -88,6 +88,9 @@ class MetadataTest(tests.Test):
prop = metadata.Property('prop', typecast=[frozenset(['A', 'B', 'C'])])
self.assertEqual(('A', 'B', 'C'), prop.convert(['A', 'B', 'C']))
+ prop = metadata.Property('prop', typecast=lambda x: x + 1)
+ self.assertEqual(1, prop.convert(0))
+
def test_Property_reprcast(self):
prop = metadata.Property('prop', typecast=int)
self.assertEqual(['0'], prop.reprcast(0))