def __init__(self, config, section, key): self.key = key """Section key or key modifier.""" data = dict(config._config.items(section)) self.ignored_actions = set() """List of ignored actions.""" if 'ignore_actions' in data: ignored_actions = data.get('ignore_actions', '').split(', ') self.ignored_actions.update(ignored_actions) self.ignored_actions.update(config.ignored_actions) if 'grid' in self.ignored_actions: self.ignored_actions.update(['grid_width', 'grid_height']) self.gravity = Gravity.parse(data.get('gravity', '')) """Gravity.""" self.direction = Gravity.parse(data.get('direction', '')) """Direction of window movement.""" if not self.direction: self.direction = self.gravity self.position = Gravity.parse(data.get('position', '')) """Position position on :ref:`workarea`.""" if not self.position: self.position = self.gravity elif self.position and not self.gravity: self.gravity = self.position self.size = Size.parse(data.get('widths', ''), data.get('heights', '')) """Window size(s)."""
def test_set_position(self): geo = Geometry(0, 0, 100, 200) self.assertEqual(geo.x, 0) self.assertEqual(geo.y, 0) # set postion geo.set_position(10, 10) self.assertEqual(geo.x, 10) self.assertEqual(geo.y, 10) # set position relative to gravity geo.set_position(10, 10, Gravity(0, 0)) self.assertEqual(geo.x, 10) self.assertEqual(geo.y, 10) geo.set_position(110, 210, Gravity(1, 1)) self.assertEqual(geo.x, 10) self.assertEqual(geo.y, 10) geo.set_position(60, 110, Gravity(0.5, 0.5)) self.assertEqual(geo.x, 10) self.assertEqual(geo.y, 10)
class DummyWindow(object): """Dummy Window object, only geometry information is needed.""" gravity = Gravity(0.5, 0.5) def __init__(self, window, position, size, gravity): self.extents = window.extents self.desktop = window.desktop self.id = window.id self.geometry = Geometry(position.x, position.y, min(size.width), min(size.height), gravity)
def __init__(self, config, section, key): self.key = key data = dict(config._config.items(section)) self.ignored = set() if 'ignore_actions' in data: self.ignored.update(data.get('ignore_actions', '').split(', ')) self.ignored.update(config.ignored) if 'grid' in self.ignored: self.ignored.update(['grid_width', 'grid_height']) self.gravity = Gravity.parse(data.get('gravity', '')) self.direction = Gravity.parse(data.get('direction', '')) if not self.direction: self.direction = self.gravity self.position = Gravity.parse(data.get('position', '')) if not self.position: self.position = self.gravity elif self.position and not self.gravity: self.gravity = self.position self.size = Size.parse(data.get('widths', ''), data.get('heights', ''))
def gravity_callback(option, opt_str, value, parser): """Parse gravity, direction, position options to Gravity.""" largs_callback(option, opt_str, value, parser) try: gravity = Gravity.parse(value) except ValueError: msg = 'option %s: error parsing Gravity value: %s' % (opt_str, value) raise optparse.OptionValueError(msg) setattr(parser.values, option.dest, gravity) if option.dest == 'gravity' and not parser.values.position: setattr(parser.values, 'position', gravity) if option.dest == 'gravity' and not parser.values.direction: setattr(parser.values, 'direction', gravity) if option.dest == 'position' and not parser.values.gravity: setattr(parser.values, 'gravity', gravity)
def test_constructor(self): geo = Geometry(100, 150, 20, 30) self.assertEqual(geo.x2, 120) self.assertEqual(geo.y2, 180) # relative to gravity geo = Geometry(100, 150, 20, 30, Gravity(1, 1)) self.assertEqual(geo.x2, 100) self.assertEqual(geo.y2, 150) self.assertEqual(geo.x, 80) self.assertEqual(geo.y, 120) # conversion to int geo = Geometry(1.1, 2.9, 10.5, 15.2) self.assertEqual(geo.x, 1) self.assertEqual(geo.y, 2) self.assertEqual(geo.width, 10) self.assertEqual(geo.height, 15)
def test_equal(self): self.assertEqual(Gravity(1, 0), self.TOP_RIGHT) self.assertEqual(Gravity(1.0, 0.0), self.TOP_RIGHT) self.assertEqual(Gravity(0.5, 0.5), self.MIDDLE)
def setUp(self): self.TOP = Gravity(0.5, 0) self.BOTTOM = Gravity(0.5, 1) self.RIGHT = Gravity(1, 0.5) self.LEFT = Gravity(0, 0.5) self.MIDDLE = Gravity(0.5, 0.5) self.TOP_LEFT = Gravity(0, 0) self.BOTTOM_RIGHT = Gravity(1, 1) self.TOP_RIGHT = Gravity(1, 0) self.BOTTOM_LEFT = Gravity(0, 1)
class GravityTests(unittest.TestCase): def setUp(self): self.TOP = Gravity(0.5, 0) self.BOTTOM = Gravity(0.5, 1) self.RIGHT = Gravity(1, 0.5) self.LEFT = Gravity(0, 0.5) self.MIDDLE = Gravity(0.5, 0.5) self.TOP_LEFT = Gravity(0, 0) self.BOTTOM_RIGHT = Gravity(1, 1) self.TOP_RIGHT = Gravity(1, 0) self.BOTTOM_LEFT = Gravity(0, 1) def test_equal(self): self.assertEqual(Gravity(1, 0), self.TOP_RIGHT) self.assertEqual(Gravity(1.0, 0.0), self.TOP_RIGHT) self.assertEqual(Gravity(0.5, 0.5), self.MIDDLE) def test_is_direction__top(self): self.assertTrue(self.TOP.is_top) self.assertTrue(not self.TOP.is_bottom) self.assertTrue(not self.TOP.is_left) self.assertTrue(not self.TOP.is_right) self.assertTrue(not self.TOP.is_middle) self.assertTrue(not self.TOP.is_diagonal) def test_is_direction__bottom(self): self.assertTrue(not self.BOTTOM.is_top) self.assertTrue(self.BOTTOM.is_bottom) self.assertTrue(not self.BOTTOM.is_left) self.assertTrue(not self.BOTTOM.is_right) self.assertTrue(not self.BOTTOM.is_middle) self.assertTrue(not self.BOTTOM.is_diagonal) def test_is_direction__left(self): self.assertTrue(not self.LEFT.is_top) self.assertTrue(not self.LEFT.is_bottom) self.assertTrue(self.LEFT.is_left) self.assertTrue(not self.LEFT.is_right) self.assertTrue(not self.LEFT.is_middle) self.assertTrue(not self.LEFT.is_diagonal) def test_is_direction__right(self): self.assertTrue(not self.RIGHT.is_top) self.assertTrue(not self.RIGHT.is_bottom) self.assertTrue(not self.RIGHT.is_left) self.assertTrue(self.RIGHT.is_right) self.assertTrue(not self.RIGHT.is_middle) self.assertTrue(not self.RIGHT.is_diagonal) def test_is_direction__middle(self): self.assertTrue(self.MIDDLE.is_top) self.assertTrue(self.MIDDLE.is_bottom) self.assertTrue(self.MIDDLE.is_left) self.assertTrue(self.MIDDLE.is_right) self.assertTrue(self.MIDDLE.is_middle) self.assertTrue(not self.MIDDLE.is_diagonal) def test_is_direction__middle_left(self): self.assertTrue(not self.BOTTOM_LEFT.is_top) self.assertTrue(self.BOTTOM_LEFT.is_bottom) self.assertTrue(self.BOTTOM_LEFT.is_left) self.assertTrue(not self.BOTTOM_LEFT.is_right) self.assertTrue(not self.BOTTOM_LEFT.is_middle) self.assertTrue(self.BOTTOM_LEFT.is_diagonal) def test_is_direction__top_right(self): self.assertTrue(self.TOP_RIGHT.is_top) self.assertTrue(not self.TOP_RIGHT.is_bottom) self.assertTrue(not self.TOP_RIGHT.is_left) self.assertTrue(self.TOP_RIGHT.is_right) self.assertTrue(not self.TOP_RIGHT.is_middle) self.assertTrue(self.TOP_RIGHT.is_diagonal) def test_invert__top(self): self.assertNotEqual(self.TOP.invert(), self.TOP) self.assertEqual(self.TOP.invert(), self.BOTTOM) self.assertEqual(self.TOP.invert(vertical=False), self.TOP) self.assertEqual(self.TOP.invert(horizontal=False), self.BOTTOM) def test_invert__bottom(self): self.assertNotEqual(self.BOTTOM.invert(), self.BOTTOM) self.assertEqual(self.BOTTOM.invert(), self.TOP) self.assertEqual(self.BOTTOM.invert(vertical=False), self.BOTTOM) self.assertEqual(self.BOTTOM.invert(horizontal=False), self.TOP) def test_invert__left(self): self.assertNotEqual(self.LEFT.invert(), self.LEFT) self.assertEqual(self.LEFT.invert(), self.RIGHT) self.assertEqual(self.LEFT.invert(vertical=False), self.RIGHT) self.assertEqual(self.LEFT.invert(horizontal=False), self.LEFT) def test_invert__right(self): self.assertNotEqual(self.RIGHT.invert(), self.RIGHT) self.assertEqual(self.RIGHT.invert(), self.LEFT) self.assertEqual(self.RIGHT.invert(vertical=False), self.LEFT) self.assertEqual(self.RIGHT.invert(horizontal=False), self.RIGHT) def test_invert__middle(self): self.assertEqual(self.MIDDLE.invert(), self.MIDDLE) self.assertEqual(self.MIDDLE.invert(vertical=False), self.MIDDLE) self.assertEqual(self.MIDDLE.invert(horizontal=False), self.MIDDLE) def test_invert__bottom_left(self): self.assertEqual(self.BOTTOM_LEFT.invert(), self.TOP_RIGHT) self.assertNotEqual(self.BOTTOM_LEFT.invert(), self.BOTTOM_LEFT) self.assertEqual(self.BOTTOM_LEFT.invert(vertical=False), self.BOTTOM_RIGHT) self.assertEqual(self.BOTTOM_LEFT.invert(horizontal=False), self.TOP_LEFT) self.assertEqual(self.BOTTOM_LEFT.invert(vertical=False, horizontal=False), self.BOTTOM_LEFT) def test_invert__top_right(self): self.assertEqual(self.TOP_RIGHT.invert(), self.BOTTOM_LEFT) self.assertNotEqual(self.TOP_RIGHT.invert(), self.TOP_RIGHT) self.assertEqual(self.TOP_RIGHT.invert(vertical=False), self.TOP_LEFT) self.assertEqual(self.TOP_RIGHT.invert(horizontal=False), self.BOTTOM_RIGHT) self.assertEqual(self.TOP_RIGHT.invert(vertical=False, horizontal=False), self.TOP_RIGHT) def test_parse(self): self.assertEqual(Gravity.parse(''), None) self.assertEqual(Gravity.parse('TOP'), self.TOP) self.assertEqual(Gravity.parse('UP'), self.TOP) self.assertEqual(Gravity.parse('N'), self.TOP) self.assertEqual(Gravity.parse('FULL, 0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('FULL, 0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1, 0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1,0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1.0, 0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1.0,0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('HALF, HALF'), self.MIDDLE) self.assertEqual(Gravity.parse('HALF, 0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('HALF, 1.0/2'), self.MIDDLE) self.assertEqual(Gravity.parse('0.5, 1.0-0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('0.25*2, 2.0/2-0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('QUARTER*2, FULL/2'), self.MIDDLE) self.assertRaises(ValueError, Gravity.parse, 'top') self.assertRaises(ValueError, Gravity.parse, 'slkfsk') self.assertRaises(ValueError, Gravity.parse, '1.0') self.assertRaises(ValueError, Gravity.parse, '1,2,3')
def test_parse(self): self.assertEqual(Gravity.parse(''), None) self.assertEqual(Gravity.parse('TOP'), self.TOP) self.assertEqual(Gravity.parse('UP'), self.TOP) self.assertEqual(Gravity.parse('N'), self.TOP) self.assertEqual(Gravity.parse('FULL, 0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('FULL, 0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1, 0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1,0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1.0, 0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('1.0,0.0'), self.TOP_RIGHT) self.assertEqual(Gravity.parse('HALF, HALF'), self.MIDDLE) self.assertEqual(Gravity.parse('HALF, 0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('HALF, 1.0/2'), self.MIDDLE) self.assertEqual(Gravity.parse('0.5, 1.0-0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('0.25*2, 2.0/2-0.5'), self.MIDDLE) self.assertEqual(Gravity.parse('QUARTER*2, FULL/2'), self.MIDDLE) self.assertRaises(ValueError, Gravity.parse, 'top') self.assertRaises(ValueError, Gravity.parse, 'slkfsk') self.assertRaises(ValueError, Gravity.parse, '1.0') self.assertRaises(ValueError, Gravity.parse, '1,2,3')