Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_point_is_relative(self):
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
point_abs = Point(size_px, size_px2)
point_mix = Point(size_percent, size_px)
point_rel = Point(size_percent, size_percent2)
self.assertFalse(point_abs.is_relative())
self.assertFalse(point_mix.is_relative())
self.assertTrue(point_rel.is_relative())
def test_stretch_is_relative(self):
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
stretch_abs = Stretch(size_px, size_px2)
stretch_mix = Stretch(size_percent, size_px)
stretch_rel = Stretch(size_percent, size_percent2)
self.assertFalse(stretch_abs.is_relative())
self.assertFalse(stretch_mix.is_relative())
self.assertTrue(stretch_rel.is_relative())
def test_layout_is_relative(self):
empty_layout = Layout()
self.assertTrue(empty_layout.is_relative())
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
point_abs = Point(size_px, size_px2)
point_rel = Point(size_percent, size_percent2)
stretch_abs = Stretch(size_px, size_px2)
stretch_rel = Stretch(size_percent, size_percent2)
layout_abs = Layout(
origin=point_abs,
extent=stretch_abs,
padding=None
)
def test_size_is_valid(self):
with self.assertRaises(TypeError):
Size()
with self.assertRaises(ValueError):
Size(None, None)
def test_layout_is_relative(self):
empty_layout = Layout()
self.assertTrue(empty_layout.is_relative())
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
point_abs = Point(size_px, size_px2)
point_rel = Point(size_percent, size_percent2)
stretch_abs = Stretch(size_px, size_px2)
stretch_rel = Stretch(size_percent, size_percent2)
layout_abs = Layout(
origin=point_abs,
extent=stretch_abs,
padding=None
)
def test_padding_is_relative(self):
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_px3 = Size(30, UnitEnum.PIXEL)
size_px4 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
size_percent3 = Size(30, UnitEnum.PERCENT)
size_percent4 = Size(30, UnitEnum.PERCENT)
padding_abs = Padding(size_px, size_px2, size_px3, size_px4)
padding_mix = Padding(size_px, size_px2, size_px3, size_percent)
padding_rel = Padding(
size_percent, size_percent2, size_percent3, size_percent4)
self.assertFalse(padding_abs.is_relative())
self.assertFalse(padding_mix.is_relative())
self.assertTrue(padding_rel.is_relative())
def test_point_is_relative(self):
size_px = Size(30, UnitEnum.PIXEL)
size_px2 = Size(30, UnitEnum.PIXEL)
size_percent = Size(30, UnitEnum.PERCENT)
size_percent2 = Size(30, UnitEnum.PERCENT)
point_abs = Point(size_px, size_px2)
point_mix = Point(size_percent, size_px)
point_rel = Point(size_percent, size_percent2)
self.assertFalse(point_abs.is_relative())
self.assertFalse(point_mix.is_relative())
self.assertTrue(point_rel.is_relative())
def from_xml_attribute(cls, attribute):
"""Instantiate the class from a value of the type "4px" or "5%"
or any number concatenated with a measuring unit (member of UnitEnum)
:type attribute: unicode
"""
horizontal, vertical = six.text_type(attribute).split(' ')
horizontal = Size.from_string(horizontal)
vertical = Size.from_string(vertical)
return cls(horizontal, vertical)
def __sub__(self, other):
if self.unit == other.unit:
return Size(self.value - other.value, self.unit)
else:
raise ValueError("The sizes should have the same measure units.")
def fit_to_screen(self):
"""
If extent is not set or if origin + extent > 100%, (re)calculate it
based on origin. It is a pycaption fix for caption files that are
technically valid but contain inconsistent settings that may cause
long captions to be cut out of the screen.
ATTENTION: This must be called on relativized objects (such as the one
returned by as_percentage_of). All units are presumed to be percentages.
"""
if self.origin:
# Calculated values to be used if replacement is needed
diff_horizontal = Size(100 - self.origin.x.value, UnitEnum.PERCENT)
diff_vertical = Size(100 - self.origin.y.value, UnitEnum.PERCENT)
if not self.extent:
# Extent is not set, use the calculated values
new_extent = Stretch(diff_horizontal, diff_vertical)
else:
# Extent is set but may have inconsistent values,
# e.g. origin="35% 25%" extent="80% 80%", which would cause
# captions to end horizontally at 115% and vertically at 105%,
# which would result in them being cut out of the screen.
# In this case, the horizontal and vertical values are
# corrected so that origin + extent = 100%.
bottom_right = self.origin.add_stretch(self.extent)
found_absolute_unit = False
if bottom_right.x.unit != UnitEnum.PERCENT:
found_absolute_unit = True