[Zope3-checkins] CVS: Zope3/src/zope/app/size - __init__.py:1.1
interfaces.py:1.1 tests.py:1.1
Philipp von Weitershausen
philikon at philikon.de
Wed Mar 3 06:04:06 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/size
In directory cvs.zope.org:/tmp/cvs-serv13819/size
Added Files:
__init__.py interfaces.py tests.py
Log Message:
Made zope.app.size a package containing interfaces and tests besides the
implementation.
=== Added File Zope3/src/zope/app/size/__init__.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Adapters that give the size of an object.
$Id: __init__.py,v 1.1 2004/03/03 11:04:03 philikon Exp $
"""
from zope.app.i18n import ZopeMessageIDFactory as _
from zope.app.size.interfaces import ISized
from zope.interface import implements
__metaclass__ = type
class DefaultSized:
implements(ISized)
def __init__(self, obj):
try:
size = int(obj.getSize())
except (AttributeError, ValueError, TypeError):
self._sortingSize = None, None
else:
self._sortingSize = 'byte', size
def sizeForSorting(self):
"""See ISized"""
return self._sortingSize
def sizeForDisplay(self):
"""See ISized"""
units, size = self._sortingSize
if units == 'byte':
return byteDisplay(size)
return _('not-available', 'n/a')
def byteDisplay(size):
if size == 0:
return _('0 KB')
if size <= 1024:
return _('1 KB')
if size > 1048576:
size_str = _('${size} MB')
size_str.mapping = {'size': '%0.02f' % (size / 1048576.0)}
return size_str
size_str = _('${size} KB')
size_str.mapping = {'size': '%d' % (size / 1024.0)}
return size_str
=== Added File Zope3/src/zope/app/size/interfaces.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Interfaces that give the size of an object.
$Id: interfaces.py,v 1.1 2004/03/03 11:04:03 philikon Exp $
"""
from zope.interface import Interface
# basic units:
# 'byte'
# 'item' for example, number of subobjects for a folder
# None for unsized things
# 'line' for source-code like things
class ISized(Interface):
def sizeForSorting():
"""Returns a tuple (basic_unit, amount)
Used for sorting among different kinds of sized objects.
'amount' need only be sortable among things that share the
same basic unit."""
def sizeForDisplay():
"""Returns a string giving the size.
"""
=== Added File Zope3/src/zope/app/size/tests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import unittest
from zope.app.size.interfaces import ISized
class DummyObject:
def __init__(self, size):
self._size = size
def getSize(self):
return self._size
class Test(unittest.TestCase):
def testImplementsISized(self):
from zope.app.size import DefaultSized
sized = DefaultSized(object())
self.assert_(ISized.isImplementedBy(sized))
def testSizeWithBytes(self):
from zope.app.size import DefaultSized
obj = DummyObject(1023)
sized = DefaultSized(obj)
self.assertEqual(sized.sizeForSorting(), ('byte', 1023))
self.assertEqual(sized.sizeForDisplay(), u'1 KB')
def testSizeWithNone(self):
from zope.app.size import DefaultSized
obj = DummyObject(None)
sized = DefaultSized(obj)
self.assertEqual(sized.sizeForSorting(), (None, None))
self.assertEqual(sized.sizeForDisplay(), u'not-available')
def testSizeNotAvailable(self):
from zope.app.size import DefaultSized
sized = DefaultSized(object())
self.assertEqual(sized.sizeForSorting(), (None, None))
self.assertEqual(sized.sizeForDisplay(), u'not-available')
def testVariousSizes(self):
from zope.app.size import DefaultSized
sized = DefaultSized(DummyObject(0))
self.assertEqual(sized.sizeForSorting(), ('byte', 0))
self.assertEqual(sized.sizeForDisplay(), u'0 KB')
sized = DefaultSized(DummyObject(1))
self.assertEqual(sized.sizeForSorting(), ('byte', 1))
self.assertEqual(sized.sizeForDisplay(), u'1 KB')
sized = DefaultSized(DummyObject(2048))
self.assertEqual(sized.sizeForSorting(), ('byte', 2048))
self.assertEqual(sized.sizeForDisplay(), u'${size} KB')
self.assertEqual(sized.sizeForDisplay().mapping, {'size': '2'})
sized = DefaultSized(DummyObject(2000000))
self.assertEqual(sized.sizeForSorting(), ('byte', 2000000))
self.assertEqual(sized.sizeForDisplay(), u'${size} MB')
self.assertEqual(sized.sizeForDisplay().mapping, {'size': '1.91'})
def test_byteDisplay(self):
from zope.app.size import byteDisplay
self.assertEqual(byteDisplay(0), u'0 KB')
self.assertEqual(byteDisplay(1), u'1 KB')
self.assertEqual(byteDisplay(2048), u'${size} KB')
self.assertEqual(byteDisplay(2048).mapping, {'size': '2'})
self.assertEqual(byteDisplay(2000000), u'${size} MB')
self.assertEqual(byteDisplay(2000000).mapping, {'size': '1.91'})
def test_suite():
loader = unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
More information about the Zope3-Checkins
mailing list