[Checkins] SVN: cipher.configstore/trunk/ - Store `None` values as `<<<###NONE###>>>` so we can load them again as `None`
Adam Groszer
cvs-admin at zope.org
Fri Nov 23 15:23:58 UTC 2012
Log message for revision 128426:
- Store `None` values as `<<<###NONE###>>>` so we can load them again as `None`
Note: `None` values will not be passed to converters.
Changed:
U cipher.configstore/trunk/CHANGES.txt
U cipher.configstore/trunk/src/cipher/configstore/configstore.py
U cipher.configstore/trunk/src/cipher/configstore/configstore.txt
-=-
Modified: cipher.configstore/trunk/CHANGES.txt
===================================================================
--- cipher.configstore/trunk/CHANGES.txt 2012-11-23 15:09:12 UTC (rev 128425)
+++ cipher.configstore/trunk/CHANGES.txt 2012-11-23 15:23:58 UTC (rev 128426)
@@ -5,8 +5,12 @@
1.3.1 (unreleased)
------------------
+- Store `None` values as `<<<###NONE###>>>` so we can load them again as `None`
+ Note: `None` values will not be passed to converters.
+
- Fixed a bug where invalid Choice values broke dumping.
+
1.3.0 (2012-10-05)
------------------
Modified: cipher.configstore/trunk/src/cipher/configstore/configstore.py
===================================================================
--- cipher.configstore/trunk/src/cipher/configstore/configstore.py 2012-11-23 15:09:12 UTC (rev 128425)
+++ cipher.configstore/trunk/src/cipher/configstore/configstore.py 2012-11-23 15:23:58 UTC (rev 128426)
@@ -33,6 +33,8 @@
log = logging.getLogger(__name__)
novalue = object()
+NONE_VALUE_MARKER = u'<<<###NONE###>>>' # make this pretty unique
+BLANKLINE_MARKER = '\n<BLANKLINE>\n'
def stringify(s):
@@ -117,7 +119,7 @@
return datetime.timedelta(seconds=h * 3600 + m * 60 + s)
def load_type_Text(self, value, field):
- return value.replace('\n<BLANKLINE>\n', '\n\n')
+ return value.replace(BLANKLINE_MARKER, '\n\n')
def load_type_Choice(self, unicode, field):
if not unicode.strip():
@@ -154,8 +156,11 @@
# If we do not have a converter then it is probably meant to be.
return False
try:
- value = converter(
- unicode(config.get(self.section, name), 'UTF-8'))
+ value = unicode(config.get(self.section, name), 'UTF-8')
+ if value == NONE_VALUE_MARKER:
+ value = None
+ else:
+ value = converter(value)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
zope.schema.ValidationError, ValueError):
# Ignore fields that fail validation, since the field might
@@ -198,19 +203,19 @@
))
def dump_type_Time(self, value, field):
- return value.strftime('%H:%M') if value is not None else ''
+ return value.strftime('%H:%M')
def dump_type_Timedelta(self, value, field):
- return str(value) if value is not None else ''
+ return str(value)
def dump_type_Text(self, value, field):
if not value:
return ''
value = value.replace('\r\n', '\n')
- return value.replace('\n\n', '\n<BLANKLINE>\n')
+ return value.replace('\n\n', BLANKLINE_MARKER)
def dump_type_Choice(self, value, field):
- if value is None or value == '':
+ if value == '':
return ''
bound_field = field.bind(self.context)
try:
@@ -219,7 +224,7 @@
return ''
def dump_type_Tuple(self, value, field):
- return self.listValueSeparator.join(value) if value else ''
+ return self.listValueSeparator.join(value)
dump_type_List = dump_type_Tuple
dump_type_Set = dump_type_Tuple
@@ -242,7 +247,10 @@
return
if value is novalue:
value = getattr(self.context, name)
- unicode_value = converter(value)
+ if value is None:
+ unicode_value = NONE_VALUE_MARKER
+ else:
+ unicode_value = converter(value)
assert unicode_value is not None, "%r wasn't supposed to return None!" % converter
config.set(self.section, name, unicode_value.encode('UTF-8'))
Modified: cipher.configstore/trunk/src/cipher/configstore/configstore.txt
===================================================================
--- cipher.configstore/trunk/src/cipher/configstore/configstore.txt 2012-11-23 15:09:12 UTC (rev 128425)
+++ cipher.configstore/trunk/src/cipher/configstore/configstore.txt 2012-11-23 15:23:58 UTC (rev 128426)
@@ -42,7 +42,7 @@
[generic]
firstName = Stephan
lastName = Richter
- nickname =
+ nickname = <<<###NONE###>>>
We can now load the config again, overwriting any existing data.
@@ -58,8 +58,8 @@
u'Stephan'
>>> stephan2.lastName
u'Richter'
- >>> stephan2.nickname
- u''
+ >>> print stephan2.nickname
+ None
Note that after the loading process an ``ObjectModifiedEvent`` event is
created. Let's write a simple subscriber to the event and see what we get:
@@ -119,7 +119,7 @@
[generic]
firstName = Stephan
lastName = Richter
- nickname =
+ nickname = <<<###NONE###>>>
<BLANKLINE>
[address]
zip = 01754
@@ -159,7 +159,7 @@
[IPerson]
firstName = Stephan
lastName = RICHTER
- nickname =
+ nickname = <<<###NONE###>>>
<BLANKLINE>
[address]
zip = 01754
@@ -240,7 +240,7 @@
[IPerson]
firstName = Stephan
lastName = RICHTER
- nickname =
+ nickname = <<<###NONE###>>>
<BLANKLINE>
[address]
zip = 01754
@@ -316,7 +316,7 @@
[general]
firstName = Stephan
lastName = Richter
- nickname =
+ nickname = <<<###NONE###>>>
<BLANKLINE>
[address]
zip = 01754
@@ -339,8 +339,8 @@
u'Stephan'
>>> stephan2.lastName
u'Richter'
- >>> stephan2.nickname
- u''
+ >>> print stephan2.nickname
+ None
Field Support
-------------
@@ -426,8 +426,6 @@
>>> store.load_type_List('one, two, three', field)
['one', 'two', 'three']
- >>> store.dump_type_List(None, field)
- ''
>>> store.load_type_List('', field)
[]
@@ -442,8 +440,6 @@
>>> store.load_type_Tuple('one, two, three', field)
('one', 'two', 'three')
- >>> store.dump_type_Tuple(None, field)
- ''
>>> store.load_type_Tuple('', field)
()
@@ -458,7 +454,5 @@
>>> store.load_type_Set('one, two, three', field)
set(['three', 'two', 'one'])
- >>> store.dump_type_Set(None, field)
- ''
>>> store.load_type_Set('', field)
set([])
More information about the checkins
mailing list