[rhinstaller/blivet/pulls/251 master] Use libbytesize's Size class as a base for our Size class

vpodzime installerbot-noreply at redhat.com
Mon Oct 19 07:53:05 UTC 2015


> @@ -19,229 +19,20 @@
>  #
>  # Red Hat Author(s): David Cantrell <dcantrell at redhat.com>
>  
> -import re
> -import string           # pylint: disable=deprecated-module
> -import locale
> -import sys
> -from collections import namedtuple
> -
> -from decimal import Decimal
> -from decimal import InvalidOperation
> -from decimal import ROUND_DOWN, ROUND_UP, ROUND_HALF_UP
>  import six
>  
> -from .errors import SizePlacesError
> -from .i18n import _, N_
> -from .util import stringize, unicodeize
> -
> -ROUND_DEFAULT = ROUND_HALF_UP
> -
> -# Container for size unit prefix information
> -_Prefix = namedtuple("Prefix", ["factor", "prefix", "abbr"])
> -
> -_DECIMAL_FACTOR = 10 ** 3
> -_BINARY_FACTOR = 2 ** 10
> -
> -_BYTES_SYMBOL = N_("B")
> -_BYTES_WORDS = (N_("bytes"), N_("byte"))
> -
> -# Symbolic constants for units
> -B = _Prefix(1, "", "")
> -
> -KB = _Prefix(_DECIMAL_FACTOR ** 1, N_("kilo"), N_("k"))
> -MB = _Prefix(_DECIMAL_FACTOR ** 2, N_("mega"), N_("M"))
> -GB = _Prefix(_DECIMAL_FACTOR ** 3, N_("giga"), N_("G"))
> -TB = _Prefix(_DECIMAL_FACTOR ** 4, N_("tera"), N_("T"))
> -PB = _Prefix(_DECIMAL_FACTOR ** 5, N_("peta"), N_("P"))
> -EB = _Prefix(_DECIMAL_FACTOR ** 6, N_("exa"), N_("E"))
> -ZB = _Prefix(_DECIMAL_FACTOR ** 7, N_("zetta"), N_("Z"))
> -YB = _Prefix(_DECIMAL_FACTOR ** 8, N_("yotta"), N_("Y"))
> -
> -KiB = _Prefix(_BINARY_FACTOR ** 1, N_("kibi"), N_("Ki"))
> -MiB = _Prefix(_BINARY_FACTOR ** 2, N_("mebi"), N_("Mi"))
> -GiB = _Prefix(_BINARY_FACTOR ** 3, N_("gibi"), N_("Gi"))
> -TiB = _Prefix(_BINARY_FACTOR ** 4, N_("tebi"), N_("Ti"))
> -PiB = _Prefix(_BINARY_FACTOR ** 5, N_("pebi"), N_("Pi"))
> -EiB = _Prefix(_BINARY_FACTOR ** 6, N_("exbi"), N_("Ei"))
> -ZiB = _Prefix(_BINARY_FACTOR ** 7, N_("zebi"), N_("Zi"))
> -YiB = _Prefix(_BINARY_FACTOR ** 8, N_("yobi"), N_("Yi"))
> -
> -# Categories of symbolic constants
> -_DECIMAL_PREFIXES = [KB, MB, GB, TB, PB, EB, ZB, YB]
> -_BINARY_PREFIXES = [KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB]
> -_EMPTY_PREFIX = B
> -
> -if six.PY2:
> -    _ASCIIlower_table = string.maketrans(string.ascii_uppercase, string.ascii_lowercase) # pylint: disable=no-member
> -else:
> -    _ASCIIlower_table = str.maketrans(string.ascii_uppercase, string.ascii_lowercase) # pylint: disable=no-member
> -
> -def _lowerASCII(s):
> -    """Convert a string to lowercase using only ASCII character definitions.
> -
> -       :param s: string instance to convert
> -       :type s: str or bytes
> -       :returns: lower-cased string
> -       :rtype: str
> -    """
> -
> -    # XXX: Python 3 has str.maketrans() and bytes.maketrans() so we should
> -    # ideally use one or the other depending on the type of 's'. But it turns
> -    # out we expect this function to always return string even if given bytes.
> -    if not six.PY2 and isinstance(s, bytes):
> -        s = s.decode(sys.getdefaultencoding())
> -    return s.translate(_ASCIIlower_table) # pylint: disable=no-member
> -
> -def _makeSpec(prefix, suffix, xlate, lowercase=True):
> -    """ Synthesizes a whole word from prefix and suffix.
> -
> -        :param str prefix: a prefix
> -        :param str suffixes: a suffix
> -        :param bool xlate: if True, treat as locale specific
> -        :param bool lowercase: if True, make all lowercase
> -
> -        :returns:  whole word
> -        :rtype: str
> -    """
> -    if xlate:
> -        word = (_(prefix) + _(suffix))
> -        return word.lower() if lowercase else word
> -    else:
> -        word = prefix + suffix
> -        return _lowerASCII(word) if lowercase else word
> -
> -def unitStr(unit, xlate=False):
> -    """ Return a string representation of unit.
> -
> -        :param unit: a named unit, e.g., KiB
> -        :param bool xlate: if True, translate to current locale
> -        :rtype: some kind of string type
> -        :returns: string representation of unit
> -    """
> -    return _makeSpec(unit.abbr, _BYTES_SYMBOL, xlate, lowercase=False)
> -
> -def parseUnits(spec, xlate):
> -    """ Parse a unit specification and return corresponding factor.
> -
> -        :param spec: a units specifier
> -        :type spec: any type of string like object
> -        :param bool xlate: if True, assume locale specific
> -
> -        :returns: a named constant corresponding to spec, if found
> -        :rtype: _Prefix or NoneType
> -
> -        Looks first for exact matches for a specifier, but, failing that,
> -        searches for partial matches for abbreviations.
> -
> -        Normalizes units to lowercase, e.g., MiB and mib are treated the same.
> -    """
> -    if spec == "":
> -        return B
> -
> -    if xlate:
> -        spec = spec.lower()
> -    else:
> -        spec = _lowerASCII(spec)
> -
> -    # Search for complete matches
> -    for unit in [_EMPTY_PREFIX] + _BINARY_PREFIXES + _DECIMAL_PREFIXES:
> -        if spec == _makeSpec(unit.abbr, _BYTES_SYMBOL, xlate) or \
> -           spec in (_makeSpec(unit.prefix, s, xlate) for s in _BYTES_WORDS):
> -            return unit
> -
> -    # Search for unambiguous partial match among binary abbreviations
> -    matches = [p for p in _BINARY_PREFIXES if _makeSpec(p.abbr, "", xlate).startswith(spec)]
> -    if len(matches) == 1:
> -        return matches[0]
> -
> -    return None
> -
> -def parseSpec(spec):
> -    """ Parse string representation of size.
> +from gi.repository import ByteSize
> +from gi.repository.ByteSize import ROUND_DOWN, ROUND_UP
>  
> -        :param spec: the specification of a size with, optionally, units
> -        :type spec: any type of string like object
> -        :returns: numeric value of the specification in bytes
> -        :rtype: Decimal
> +# we just need to make these objects available here
> +# pylint: disable=unused-import
> +from gi.repository.ByteSize import B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB, KB, MB, GB, TB, PB, EB, ZB, YB

Not sure what you are asking about, but the point here is to get those "objects" (integer constants actually) to the namespace of this module because they were/are part of the API and other modules import and use them.

-- 
To view this pull request on github, visit https://github.com/rhinstaller/blivet/pull/251#discussion_r42343391


More information about the anaconda-patches mailing list