• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.size / 16248883852

14 Feb 2025 09:00AM UTC coverage: 100.0%. Remained the same
16248883852

push

github

icemac
Back to development: 5.2

8 of 8 branches covered (100.0%)

Branch coverage included in aggregate %.

102 of 102 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/zope/size/__init__.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""Adapters that give the size of an object.
15
"""
16
from zope.i18nmessageid import MessageFactory
1✔
17
from zope.interface import implementer
1✔
18

19
from zope.size.interfaces import ISized
1✔
20

21

22
_ = MessageFactory('zope')
1✔
23

24

25
@implementer(ISized)
1✔
26
class DefaultSized:
1✔
27
    """
28
    A default :class:`zope.size.interfaces.ISized` adapter
29
    producing bytes for any object that has a ``getSize`` method and
30
    (None, None) for all other objects.
31
    """
32

33
    def __init__(self, obj):
1✔
34
        try:
1✔
35
            size = int(obj.getSize())
1✔
36
        except (AttributeError, ValueError, TypeError):
1✔
37
            self._sortingSize = None, None
1✔
38
        else:
39
            self._sortingSize = 'byte', size
1✔
40

41
    def sizeForSorting(self):
1✔
42
        """See ISized"""
43
        return self._sortingSize
1✔
44

45
    def sizeForDisplay(self):
1✔
46
        """See ISized"""
47
        units, size = self._sortingSize
1✔
48
        if units == 'byte':
1✔
49
            return byteDisplay(size)
1✔
50
        return _('not-available', 'n/a')
1✔
51

52

53
def byteDisplay(size):
1✔
54
    """
55
    Returns a size with the correct unit (KB, MB), given the size in bytes.
56
    The output should be given to zope.i18n.translate()
57
    """
58
    if size == 0:
1✔
59
        return _('0 KB')
1✔
60
    if size <= 1024:
1✔
61
        return _('1 KB')
1✔
62
    if size > 1048576:
1✔
63
        return _('${size} MB', mapping={'size': '%0.02f' % (size / 1048576.0)})
1✔
64
    return _('${size} KB', mapping={'size': '%d' % (size / 1024.0)})
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc