kiwi-el8/kiwi/utils/size.py
Alberto Planas 0c095a98b1 Replace @staticmethod with @classmethod when needed
@classmethod are used in Python to represent methods that can
query and update the class (cls parameter). Is expected to be
used for metaprograming, or advanced techniques that require the
access to the class itself, before we have an instance.

@staticmethod are used to associate a function to a class. It will
not be have access to the instance (self) not the class (cls). In
other programming languages are known as class methods.

This patch replace all the @classmethod with @staticmethod when
there is not need to access to the cls parameter, because the
intention is to be used as normal functions.
2019-03-26 16:27:43 +01:00

50 lines
1.7 KiB
Python

# Copyright (c) 2015 SUSE Linux GmbH. All rights reserved.
#
# This file is part of kiwi.
#
# kiwi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kiwi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
#
import re
import math
from kiwi.exceptions import KiwiSizeError
class StringToSize(object):
"""
**Performs size convertions from strings to numbers**
"""
@staticmethod
def to_bytes(size_value):
"""
Convert the given string representig a size into the appropriate
number of bytes.
:param str size_value: a size in bytes or specified with m=MB or g=GB
:return: the number of bytes represented by size_value string
:rtype: int
"""
size_format = '^(\d+)([gGmM]{0,1})$'
size = re.search(size_format, size_value)
if not size:
raise KiwiSizeError(
'unsupported size format {0}, must match {1}'.format(
size_value, size_format
)
)
size_base = int(size.group(1))
size_unit = {'g': 3, 'm': 2}.get(size.group(2).lower())
return size_unit and size_base * math.pow(0x400, size_unit) or size_base