Coverage for manila/share/drivers/nexenta/utils.py: 97%
22 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2019 Nexenta by DDN, Inc.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16import re
18from oslo_utils import units
21def str2size(s, scale=1024):
22 """Convert size-string.
24 String format: <value>[:space:]<B | K | M | ...> to bytes.
26 :param s: size-string
27 :param scale: base size
28 """
29 if not s:
30 return 0
31 if isinstance(s, int):
32 return s
34 match = re.match(r'^([\.\d]+)\s*([BbKkMmGgTtPpEeZzYy]?)', s)
35 if match is None:
36 raise ValueError('Invalid value: %s' % s)
37 groups = match.groups()
38 value = float(groups[0])
39 suffix = len(groups) > 1 and groups[1].upper() or 'B'
40 types = ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
41 for i, t in enumerate(types): 41 ↛ exitline 41 didn't return from function 'str2size' because the loop on line 41 didn't complete
42 if suffix == t:
43 return float(value * pow(scale, i))
46def str2gib_size(s):
47 """Covert size-string to size in gigabytes."""
48 size_in_bytes = str2size(s)
49 return size_in_bytes // units.Gi
52def bytes_to_gb(size):
53 return float(size) / units.Gi