[Zodb-checkins] CVS: ZODB3/ZConfig - datatypes.py:1.1.2.23
Barry Warsaw
barry@wooz.org
Fri, 3 Jan 2003 13:54:50 -0500
Update of /cvs-repository/ZODB3/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv26668
Modified Files:
Tag: zconfig-schema-devel-branch
datatypes.py
Log Message:
SuffixMultiplier: A generic class to support specifications like 12h,
53MB. Used in the new byte-size type and time-interval types.
=== ZODB3/ZConfig/datatypes.py 1.1.2.22 => 1.1.2.23 ===
--- ZODB3/ZConfig/datatypes.py:1.1.2.22 Fri Jan 3 01:04:25 2003
+++ ZODB3/ZConfig/datatypes.py Fri Jan 3 13:54:47 2003
@@ -276,6 +276,29 @@
l.append('')
return l
+
+class SuffixMultiplier:
+ # d is a dictionary of suffixes to integer multipliers. If no suffixes
+ # match, default is the multiplier. Matches are case insensitive. Return
+ # values are in the fundamental unit.
+ def __init__(self, d, default=1):
+ self._d = d
+ self._default = default
+ # all keys must be the same size
+ self._keysz = None
+ for k in d.keys():
+ if self._keysz is None:
+ self._keysz = len(k)
+ else:
+ assert self._keysz == len(k)
+
+ def __call__(self, v):
+ v = v.lower()
+ for s, m in self._d.items():
+ if v[-self._keysz:] == s:
+ return int(v[:-self._keysz]) * m
+ return int(v) * self._default
+
stock_datatypes = {
"boolean": asBoolean,
"identifier": IdentifierConversion().__call__,
@@ -291,11 +314,20 @@
"socket-address":socket_address,
"ipaddr-or-hostname":IpaddrOrHostname().__call__,
"existing-directory":existing_directory,
- "existing-path":existing_path,
- "existing-file":existing_file,
- "existing-dirpath":existing_dirpath,
- "constructor":constructor_conversion,
- "key-value":space_sep_key_value_conversion,
+ "existing-path": existing_path,
+ "existing-file": existing_file,
+ "existing-dirpath": existing_dirpath,
+ "constructor": constructor_conversion,
+ "key-value": space_sep_key_value_conversion,
+ "byte-size": SuffixMultiplier({'kb': 1024,
+ 'mb': 1024*1024,
+ 'gb': 1024*1024*1024,
+ }).__call__,
+ "time-interval": SuffixMultiplier({'s': 1,
+ 'm': 60,
+ 'h': 60*60,
+ 'd': 60*60*24,
+ }).__call__,
}
class Registry: