Cleanup unused util functions
There are a lot of util funtions that are not used neither in Horizon, nor in plugins. This patch removes them. Change-Id: I987b39d620a037834cb2d05a234a1ad089276108
This commit is contained in:
parent
0b7e231ea5
commit
26eebd4abb
@ -1,74 +0,0 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from horizon.test import helpers as test
|
||||
from horizon.utils import units
|
||||
|
||||
|
||||
class UnitsTests(test.TestCase):
|
||||
def test_is_supported(self):
|
||||
self.assertTrue(units.is_supported('MB'))
|
||||
self.assertTrue(units.is_supported('min'))
|
||||
self.assertFalse(units.is_supported('KWh'))
|
||||
self.assertFalse(units.is_supported('unknown_unit'))
|
||||
|
||||
def test_is_larger(self):
|
||||
self.assertTrue(units.is_larger('KB', 'B'))
|
||||
self.assertTrue(units.is_larger('MB', 'B'))
|
||||
self.assertTrue(units.is_larger('GB', 'B'))
|
||||
self.assertTrue(units.is_larger('TB', 'B'))
|
||||
self.assertTrue(units.is_larger('GB', 'MB'))
|
||||
self.assertFalse(units.is_larger('B', 'KB'))
|
||||
self.assertFalse(units.is_larger('MB', 'GB'))
|
||||
|
||||
self.assertTrue(units.is_larger('min', 's'))
|
||||
self.assertTrue(units.is_larger('hr', 'min'))
|
||||
self.assertTrue(units.is_larger('hr', 's'))
|
||||
self.assertFalse(units.is_larger('s', 'min'))
|
||||
|
||||
def test_convert(self):
|
||||
self.assertEqual(units.convert(4096, 'MB', 'GB'), (4, 'GB'))
|
||||
self.assertEqual(units.convert(4, 'GB', 'MB'), (4096, 'MB'))
|
||||
|
||||
self.assertEqual(units.convert(1.5, 'hr', 'min'), (90, 'min'))
|
||||
self.assertEqual(units.convert(12, 'hr', 'day'), (0.5, 'day'))
|
||||
|
||||
def test_normalize(self):
|
||||
self.assertEqual(units.normalize(1, 'B'), (1, 'B'))
|
||||
self.assertEqual(units.normalize(1000, 'B'), (1000, 'B'))
|
||||
self.assertEqual(units.normalize(1024, 'B'), (1, 'KB'))
|
||||
self.assertEqual(units.normalize(1024 * 1024, 'B'), (1, 'MB'))
|
||||
self.assertEqual(units.normalize(10 * 1024 ** 3, 'B'), (10, 'GB'))
|
||||
self.assertEqual(units.normalize(1000 * 1024 ** 4, 'B'), (1000, 'TB'))
|
||||
self.assertEqual(units.normalize(1024, 'KB'), (1, 'MB'))
|
||||
self.assertEqual(units.normalize(1024 ** 2, 'KB'), (1, 'GB'))
|
||||
self.assertEqual(units.normalize(10 * 1024, 'MB'), (10, 'GB'))
|
||||
self.assertEqual(units.normalize(0.5, 'KB'), (512, 'B'))
|
||||
self.assertEqual(units.normalize(0.0001, 'MB'), (104.9, 'B'))
|
||||
|
||||
self.assertEqual(units.normalize(1, 's'), (1, 's'))
|
||||
self.assertEqual(units.normalize(120, 's'), (2, 'min'))
|
||||
self.assertEqual(units.normalize(3600, 's'), (60, 'min'))
|
||||
self.assertEqual(units.normalize(3600 * 24, 's'), (24, 'hr'))
|
||||
self.assertEqual(units.normalize(10 * 3600 * 24, 's'), (10, 'day'))
|
||||
self.assertEqual(units.normalize(90, 'min'), (90, 'min'))
|
||||
self.assertEqual(units.normalize(150, 'min'), (2.5, 'hr'))
|
||||
self.assertEqual(units.normalize(60 * 24, 'min'), (24, 'hr'))
|
||||
self.assertEqual(units.normalize(0.5, 'day'), (12, 'hr'))
|
||||
self.assertEqual(units.normalize(10800000000000, 'ns'), (3, 'hr'))
|
||||
self.assertEqual(units.normalize(14, 'day'), (2, 'week'))
|
||||
self.assertEqual(units.normalize(91, 'day'), (3, 'month'))
|
||||
self.assertEqual(units.normalize(18, 'month'), (18, 'month'))
|
||||
self.assertEqual(units.normalize(24, 'month'), (2, 'year'))
|
||||
|
||||
self.assertEqual(units.normalize(1, 'unknown_unit'),
|
||||
(1, 'unknown_unit'))
|
@ -1,147 +0,0 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import decimal
|
||||
|
||||
import pint
|
||||
|
||||
from horizon.utils import functions
|
||||
|
||||
# Mapping of units from Ceilometer to Pint
|
||||
INFORMATION_UNITS = (
|
||||
('B', 'byte'),
|
||||
('KB', 'Kibyte'),
|
||||
('MB', 'Mibyte'),
|
||||
('GB', 'Gibyte'),
|
||||
('TB', 'Tibyte'),
|
||||
('PB', 'Pibyte'),
|
||||
('EB', 'Eibyte'),
|
||||
)
|
||||
|
||||
TIME_UNITS = ('ns', 's', 'min', 'hr', 'day', 'week', 'month', 'year')
|
||||
|
||||
|
||||
ureg = pint.UnitRegistry()
|
||||
|
||||
|
||||
def is_supported(unit):
|
||||
"""Returns a bool indicating whether the unit specified is supported."""
|
||||
return unit in functions.get_keys(INFORMATION_UNITS) + TIME_UNITS
|
||||
|
||||
|
||||
def is_larger(unit_1, unit_2):
|
||||
"""Returns a boolean indicating whether unit_1 is larger than unit_2.
|
||||
|
||||
E.g:
|
||||
|
||||
>>> is_larger('KB', 'B')
|
||||
True
|
||||
>>> is_larger('min', 'day')
|
||||
False
|
||||
"""
|
||||
unit_1 = functions.value_for_key(INFORMATION_UNITS, unit_1)
|
||||
unit_2 = functions.value_for_key(INFORMATION_UNITS, unit_2)
|
||||
|
||||
return ureg.parse_expression(unit_1) > ureg.parse_expression(unit_2)
|
||||
|
||||
|
||||
def convert(value, source_unit, target_unit, fmt=False):
|
||||
"""Converts value from source_unit to target_unit.
|
||||
|
||||
Returns a tuple containing the converted value and target_unit.
|
||||
Having fmt set to True causes the value to be formatted to 1 decimal digit
|
||||
if it's a decimal or be formatted as integer if it's an integer.
|
||||
|
||||
E.g:
|
||||
|
||||
>>> convert(2, 'hr', 'min')
|
||||
(120.0, 'min')
|
||||
>>> convert(2, 'hr', 'min', fmt=True)
|
||||
(120, 'min')
|
||||
>>> convert(30, 'min', 'hr', fmt=True)
|
||||
(0.5, 'hr')
|
||||
"""
|
||||
orig_target_unit = target_unit
|
||||
source_unit = functions.value_for_key(INFORMATION_UNITS, source_unit)
|
||||
target_unit = functions.value_for_key(INFORMATION_UNITS, target_unit)
|
||||
|
||||
q = ureg.Quantity(value, source_unit)
|
||||
q = q.to(ureg.parse_expression(target_unit))
|
||||
value = functions.format_value(q.magnitude) if fmt else q.magnitude
|
||||
return value, orig_target_unit
|
||||
|
||||
|
||||
def normalize(value, unit):
|
||||
"""Converts the value so that it belongs to some expected range.
|
||||
|
||||
Returns the new value and new unit.
|
||||
|
||||
E.g:
|
||||
|
||||
>>> normalize(1024, 'KB')
|
||||
(1, 'MB')
|
||||
>>> normalize(90, 'min')
|
||||
(1.5, 'hr')
|
||||
>>> normalize(1.0, 'object')
|
||||
(1, 'object')
|
||||
"""
|
||||
if value < 0:
|
||||
raise ValueError('Negative value: %s %s.' % (value, unit))
|
||||
|
||||
if unit in functions.get_keys(INFORMATION_UNITS):
|
||||
return _normalize_information(value, unit)
|
||||
elif unit in TIME_UNITS:
|
||||
return _normalize_time(value, unit)
|
||||
else:
|
||||
# Unknown unit, just return it
|
||||
return functions.format_value(value), unit
|
||||
|
||||
|
||||
def _normalize_information(value, unit):
|
||||
value = decimal.Decimal(str(value))
|
||||
|
||||
while value < 1:
|
||||
prev_unit = functions.previous_key(INFORMATION_UNITS, unit)
|
||||
if prev_unit is None:
|
||||
break
|
||||
value, unit = convert(value, unit, prev_unit)
|
||||
|
||||
while value >= 1024:
|
||||
next_unit = functions.next_key(INFORMATION_UNITS, unit)
|
||||
if next_unit is None:
|
||||
break
|
||||
value, unit = convert(value, unit, next_unit)
|
||||
|
||||
return functions.format_value(value), unit
|
||||
|
||||
|
||||
def _normalize_time(value, unit):
|
||||
# Normalize time by converting to next higher unit when value is
|
||||
# at least 2 units
|
||||
value, unit = convert(value, unit, 's')
|
||||
|
||||
if value >= 120:
|
||||
value, unit = convert(value, 's', 'min')
|
||||
|
||||
if value >= 120:
|
||||
value, unit = convert(value, 'min', 'hr')
|
||||
|
||||
if value >= 48:
|
||||
value, unit = convert(value, 'hr', 'day')
|
||||
|
||||
if value >= 730:
|
||||
value, unit = convert(value, 'day', 'year')
|
||||
elif value >= 62:
|
||||
value, unit = convert(value, 'day', 'month')
|
||||
elif value >= 14:
|
||||
value, unit = convert(value, 'day', 'week')
|
||||
|
||||
return functions.format_value(value), unit
|
@ -30,8 +30,6 @@ from six.moves.urllib import parse as urlparse
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
_TOKEN_TIMEOUT_MARGIN = getattr(settings, 'TOKEN_TIMEOUT_MARGIN', 0)
|
||||
|
||||
"""
|
||||
We need the request object to get the user, so we'll slightly modify the
|
||||
existing django.contrib.auth.get_user method. To do so we update the
|
||||
|
@ -13,21 +13,8 @@
|
||||
from django.core import validators
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from openstack_dashboard.api import swift
|
||||
|
||||
|
||||
no_slash_validator = validators.RegexValidator(r'^(?u)[^/]+$',
|
||||
_("Slash is not an allowed "
|
||||
"character."),
|
||||
code="noslash")
|
||||
no_begin_or_end_slash = validators.RegexValidator(r'^[^\/](?u).+[^\/]$',
|
||||
_("Slash is not allowed at "
|
||||
"the beginning or end of "
|
||||
"your string."),
|
||||
code="nobeginorendslash")
|
||||
|
||||
|
||||
def wrap_delimiter(name):
|
||||
if name and not name.endswith(swift.FOLDER_DELIMITER):
|
||||
return name + swift.FOLDER_DELIMITER
|
||||
return name
|
||||
|
@ -10,7 +10,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from django.template.defaultfilters import filesizeformat
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from horizon import exceptions
|
||||
@ -102,32 +101,3 @@ def get_available_images(request, project_id=None, images_cache=None):
|
||||
image_ids.append(image.id)
|
||||
final_images.append(image)
|
||||
return final_images
|
||||
|
||||
|
||||
def image_field_data(request, include_empty_option=False):
|
||||
"""Returns a list of tuples of all images.
|
||||
|
||||
Generates a sorted list of images available. And returns a list of
|
||||
(id, name) tuples.
|
||||
|
||||
:param request: django http request object
|
||||
:param include_empty_option: flag to include a empty tuple in the front of
|
||||
the list
|
||||
|
||||
:return: list of (id, name) tuples
|
||||
|
||||
"""
|
||||
try:
|
||||
images = get_available_images(request, request.user.project_id)
|
||||
except Exception:
|
||||
exceptions.handle(request, _('Unable to retrieve images'))
|
||||
images.sort(key=lambda c: c.name)
|
||||
images_list = [('', _('Select Image'))]
|
||||
for image in images:
|
||||
image_label = u"{} ({})".format(image.name, filesizeformat(image.size))
|
||||
images_list.append((image.id, image_label))
|
||||
|
||||
if not images:
|
||||
return [("", _("No images available")), ]
|
||||
|
||||
return images_list
|
||||
|
@ -70,16 +70,6 @@ def sort_flavor_list(request, flavors, with_menu_label=True):
|
||||
return []
|
||||
|
||||
|
||||
def availability_zone_list(request):
|
||||
"""Utility method to retrieve a list of availability zones."""
|
||||
try:
|
||||
return api.nova.availability_zone_list(request)
|
||||
except Exception:
|
||||
exceptions.handle(request,
|
||||
_('Unable to retrieve Nova availability zones.'))
|
||||
return []
|
||||
|
||||
|
||||
def server_group_list(request):
|
||||
"""Utility method to retrieve a list of server groups."""
|
||||
try:
|
||||
|
@ -1,29 +0,0 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from horizon import exceptions
|
||||
|
||||
from openstack_dashboard import api
|
||||
|
||||
|
||||
def availability_zone_list(request):
|
||||
"""Utility method to retrieve a list of availability zones."""
|
||||
try:
|
||||
if api.cinder.extension_supported(request, 'AvailabilityZones'):
|
||||
return api.cinder.availability_zone_list(request)
|
||||
return []
|
||||
except Exception:
|
||||
exceptions.handle(request,
|
||||
_('Unable to retrieve volumes availability zones.'))
|
||||
return []
|
Loading…
Reference in New Issue
Block a user