Skip to content

Commit

Permalink
Added properties - controller.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hyades committed Jul 23, 2013
1 parent 2a50863 commit 0c75a05
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
82 changes: 78 additions & 4 deletions python-api/gstswitch/controller.py
Expand Up @@ -22,6 +22,80 @@ def __init__(
self.object_path = object_path
self.default_interface = default_interface

@property

This comment has been minimized.

Copy link
@mithro

mithro Jul 24, 2013

Collaborator

Group the @Property, @xxx.setter together.

This comment has been minimized.

Copy link
@hyades

hyades Jul 24, 2013

Author Owner

okay

def address(self):
return self._address

@property
def bus_name(self):
if self._bus_name is None:
return None
return self._bus_name

@property
def object_path(self):
return self._object_path

@property
def default_interface(self):
return self._default_interface

@address.setter
def address(self, address):
"""Set the Address
http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
"""
if not address:
raise ValueError("Address '{0}' cannot be blank")
else:
a = str(address)
if a.find(':') > 0:
self._address = a
else:
raise ValueError("""Address must follow specifications mentioned at

This comment has been minimized.

Copy link
@mithro

mithro Jul 24, 2013

Collaborator

You don't want to wrap a string like this, your string is now the same as
"Address must follow specifications mentioned at\n http://dbus.freedesktop.org/doc/dbus-specification.html#addresses"

This comment has been minimized.

Copy link
@hyades

hyades Jul 24, 2013

Author Owner

okay, will correct this

http://dbus.freedesktop.org/doc/dbus-specification.html#addresses""")

@bus_name.setter
def bus_name(self, bus_name):
"""Set the Bus Name
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus
"""
if bus_name is None:
self._bus_name = None
return
a = str(bus_name)
self._bus_name = a

@object_path.setter
def object_path(self, object_path):
"""Set the object_path
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path
"""
if not object_path:
raise ValueError("object_path '{0} cannot be blank'")
else:
a = str(object_path)
if a[0] == '/':
self._object_path = a
else:
raise ValueError("""object_path must follow specifications mentioned at
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path""")

@default_interface.setter
def default_interface(self, default_interface):
"""Set the default_interface
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface
"""
if not default_interface:
raise ValueError("default_interface '{0} cannot be blank'")
else:
a = str(default_interface)
if a.count('.') > 1:
self._default_interface = a
else:
raise ValueError("""default_interface must follow specifications mentioned at
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-interface""")

def establish_connection(self):
"""Establishes a fresh connection to the dbus
Connection stored as self.connection
Expand All @@ -30,10 +104,10 @@ def establish_connection(self):
:returns: None
"""
self.connection = Connection(
self.address,
self.bus_name,
self.object_path,
self.default_interface)
address=self.address,
bus_name=self.bus_name,
object_path=self.object_path,
default_interface=self.default_interface)

self.connection.connect_dbus()

Expand Down
77 changes: 77 additions & 0 deletions python-api/gstswitch/test_controller.py
@@ -0,0 +1,77 @@
from controller import Controller
from exception import *
import pytest
from mock import Mock, patch


class TestAddress(object):

def test_address_null(self):
address = ['', None, [], {}]
for x in address:
with pytest.raises(ValueError):
Controller(address=x)

def test_address_colon(self):
address = 'abcdefghijk'
with pytest.raises(ValueError):
Controller(address=address)

def test_address_normal(self):
address = ['unix:abstract=gstswitch', 'unix:temp=/tmp/abcd/xyz']
for x in address:
conn = Controller(address=x)
assert conn.address == x


class TestBusName(object):

def test_normal(self):
names = ['', 'abcd', 12345]
for bus in names:
conn = Controller(bus_name=bus)
assert conn.bus_name == str(bus)

def test_normal_none(self):
name = None
conn = Controller(bus_name=name)
assert conn.bus_name == name


class TestObjectPath(object):

def test_object_path_blank(self):
paths = [None, '', {}, []]
for object_path in paths:
with pytest.raises(ValueError):
Controller(object_path=object_path)

def test_object_path_slash(self):
object_path = 'a/////////'
with pytest.raises(ValueError):
Controller(object_path=object_path)

def test_object_path_normal(self):
object_path = "/info/duzy/gst/switch/SwitchController"
conn = Controller(object_path=object_path)
assert conn.object_path == object_path


class TestInterface(object):

def test_interface_none(self):
default_interface = [None, '', [], {}]
for x in default_interface:
with pytest.raises(ValueError):
Controller(default_interface=x)

def test_interface_dot(self):
default_interface = ['.', 'info.', 'info']
for x in default_interface:
with pytest.raises(ValueError):
Controller(default_interface=x)

def test_interface_normal(self):
default_interface = "info.duzy.gst.switch.SwitchControllerInterface"
conn = Controller(default_interface=default_interface)
assert default_interface == conn.default_interface

0 comments on commit 0c75a05

Please sign in to comment.