Skip to content

Commit

Permalink
Made connection.py pythonic and removed dbus.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hyades committed Jul 18, 2013
1 parent e11aaea commit 82cd61b
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 151 deletions.
Binary file added python-api/gstswitch/3000 2013-07-18 222427.dat
Binary file not shown.
61 changes: 42 additions & 19 deletions python-api/gstswitch/connection.py
Expand Up @@ -3,22 +3,45 @@
from dbus import DBus
from gi.repository import Gio, GLib

CONNECTION_FLAGS = Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT


class Connection(DBus):
"""Class which makes all remote object class
"""Class which makes all remote object class.
Deals with lower level connection and remote method invoking
:default bus-address: unix:abstract=gstswitch
:param: None
"""

def __init__(self):
def __init__(
self,
address="unix:abstract=gstswitch",
bus_name=None,
object_path="/info/duzy/gst/switch/SwitchController",
default_interface="info.duzy.gst.switch.SwitchControllerInterface"):

super(Connection, self).__init__()
self.set_address("unix:abstract=gstswitch")
self.set_busname(None)
self.set_objectpath("/info/duzy/gst/switch/SwitchController")
self.set_default_interface("info.duzy.gst.switch.SwitchControllerInterface")
# self.connection = self.connect_dbus()
self._address = address
self._bus_name = bus_name
self._object_path = object_path
self._default_interface = default_interface

def connect_dbus(self):
"""Make a new connection using the parameters belonging to the class
to the gst-switch-srv over dbus.
Sets the self.connection
:params: None
:returns: Nothing
"""
connection = Gio.DBusConnection.new_for_address_sync(
self._address,
CONNECTION_FLAGS,
None,
None)
self.connection = connection

def get_compose_port(self):
"""get_compose_port(out i port);
Expand All @@ -30,7 +53,7 @@ def get_compose_port(self):
args = None
connection = self.get_connection()
port = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'get_compose_port',
self._bus_name, self._object_path, self._default_interface, 'get_compose_port',
args, GLib.VariantType.new("(i)"), Gio.DBusCallFlags.NONE, -1,
None)
return port
Expand All @@ -45,7 +68,7 @@ def get_encode_port(self):
args = None
connection = self.get_connection()
port = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'get_encode_port',
self._bus_name, self._object_path, self._default_interface, 'get_encode_port',
args, GLib.VariantType.new("(i)"), Gio.DBusCallFlags.NONE, -1,
None)
return port
Expand All @@ -60,7 +83,7 @@ def get_audio_port(self):
args = None
connection = self.get_connection()
port = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'get_audio_port',
self._bus_name, self._object_path, self._default_interface, 'get_audio_port',
args, GLib.VariantType.new("(i)"), Gio.DBusCallFlags.NONE, -1,
None)
return port
Expand All @@ -75,7 +98,7 @@ def get_preview_ports(self):
args = None
connection = self.get_connection()
ports = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'get_preview_ports',
self._bus_name, self._object_path, self._default_interface, 'get_preview_ports',
args, GLib.VariantType.new("(s)"), Gio.DBusCallFlags.NONE, -1,
None)
return ports
Expand All @@ -91,7 +114,7 @@ def set_composite_mode(self, mode):
args = GLib.Variant('(i)', (mode,))
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'set_composite_mode',
self._bus_name, self._object_path, self._default_interface, 'set_composite_mode',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -108,7 +131,7 @@ def set_encode_mode(self, channel):
args = GLib.Variant('(i)', (channel,))
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'set_encode_mode',
self._bus_name, self._object_path, self._default_interface, 'set_encode_mode',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -123,7 +146,7 @@ def new_record(self):
args = None
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'new_record',
self._bus_name, self._object_path, self._default_interface, 'new_record',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -145,7 +168,7 @@ def adjust_pip(self, dx, dy, dw, dh):
args = GLib.Variant('(iiii)', (dx, dy, dw, dh,))
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'adjust_pip',
self._bus_name, self._object_path, self._default_interface, 'adjust_pip',
args, GLib.VariantType.new("(u)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -163,7 +186,7 @@ def switch(self, channel, port):
args = GLib.Variant('(ii)', (channel, port,))
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'switch',
self._bus_name, self._object_path, self._default_interface, 'switch',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -185,7 +208,7 @@ def click_video(self, x, y, fw, fh):
args = GLib.Variant('(iiii)', (x, y, fw, fh,))
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'click_video',
self._bus_name, self._object_path, self._default_interface, 'click_video',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -200,7 +223,7 @@ def mark_face(self, faces):
args = GLib.Variant('a(iiii)', faces)
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'mark_face',
self._bus_name, self._object_path, self._default_interface, 'mark_face',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
Expand All @@ -215,7 +238,7 @@ def mark_tracking(self, faces):
args = GLib.Variant('a(iiii)', faces)
connection = self.get_connection()
result = connection.call_sync(
self.get_busname(), self.get_objectpath(), 'info.duzy.gst.switch.SwitchControllerInterface', 'mark_tracking',
self._bus_name, self._object_path, self._default_interface, 'mark_tracking',
args, GLib.VariantType.new("(b)"), Gio.DBusCallFlags.NONE, -1,
None)
return result
21 changes: 18 additions & 3 deletions python-api/gstswitch/controller.py
Expand Up @@ -5,12 +5,22 @@

class Controller(object):
"""A Class to control all interactions with the gst-switch-srv over dbus.
Provides the interface for interactions
Provides the interface for higher level interactions
:param: None
"""
def __init__(self):
def __init__(
self,
address="unix:abstract=gstswitch",
bus_name=None,
object_path="/info/duzy/gst/switch/SwitchController",
default_interface="info.duzy.gst.switch.SwitchControllerInterface"):

super(Controller, self).__init__()
self.address = "unix:abstract=gstswitch"
self.bus_name = None
self.object_path = "/info/duzy/gst/switch/SwitchController"
self.default_interface = "info.duzy.gst.switch.SwitchControllerInterface"

def establish_connection(self):
"""Establishes a fresh connection to the dbus
Expand All @@ -19,7 +29,12 @@ def establish_connection(self):
:param: None
:returns: None
"""
self.connection = Connection()
self.connection = Connection(
self.address,
self.bus_name,
self.object_path,
self.default_interface)

self.connection.connect_dbus()

def get_compose_port(self):
Expand Down
98 changes: 0 additions & 98 deletions python-api/gstswitch/dbus.py

This file was deleted.

27 changes: 12 additions & 15 deletions python-api/gstswitch/server.py
Expand Up @@ -134,20 +134,20 @@ def get_record_file(self):

def set_executable_path(self, path):
"""Sets the path where all executables
gst-switch-srv, gst-launch-1.0, wtc are located
gst-switch-srv, gst-launch-1.0, etc are located
:param path: Path where exceutables are present
:returns: nothing
"""
if type(path) != str:
raise TypeError("path should be of type str")
if len(str) == 0:
if len(path) == 0:
raise ValueError("path should be a string of length more than 0")
self.PATH = path

def get_executable_path(self, path):
def get_executable_path(self):
"""Sets the path where all executables
gst-switch-srv, gst-launch-1.0, wtc are located
gst-switch-srv, gst-launch-1.0, etc are located
:param path: Path where exceutables are present
:returns: nothing
Expand Down Expand Up @@ -179,10 +179,7 @@ def run(self):
self.proc = None
self.pid = -1
print "Starting server"
try:
self.proc = self._run_process()
except:
raise RuntimeError("cannot creating gst-switch-srv process")
self.proc = self._run_process()
if self.proc is None:
raise RuntimeError("cannot create gst-switch-srv process: No process created")
if self.proc.pid < 0:
Expand All @@ -201,14 +198,14 @@ def _run_process(self):
--video-input-port=%s \
--audio-input-port=%s \
--control-port=%s \
--record=%s """ % (self.get_video_port(), self.get_audio_port(), self.get_control_port(), self.get_video_port())
--record=%s """ % (self.get_video_port(),
self.get_audio_port(),
self.get_control_port(),
self.get_video_port())
proc = self._start_process(cmd)
print "process:", proc
if proc is None:
print 'ERROR: Server unable to create process'
pass
raise RuntimeError("unable to create process: %s" % (proc))
else:
print 'Created process with PID:%s', str(proc.pid)
return proc

def _start_process(self, cmd):
Expand All @@ -218,8 +215,8 @@ def _start_process(self, cmd):
:returns: process created
"""
print 'Creating process %s' % (cmd)
with open(os.devnull, 'w') as tempf:
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
tempf = open(os.devnull, 'w')
process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False)
return process

def terminate(self):
Expand Down

0 comments on commit 82cd61b

Please sign in to comment.