Permalink
Browse files

Remove (un)register_program_callback and replace with a signal.

Couple of small cleans and checks.
  • Loading branch information...
1 parent 97c9fb9 commit 60969be4d8167858837a0f6d1c1c131e31763b1d @charrea6 charrea6 committed May 22, 2012
Showing with 45 additions and 51 deletions.
  1. +34 −36 src/guide.py
  2. +3 −7 src/rpc.py
  3. +5 −5 test/grid.py
  4. +3 −3 test/server.py
View
@@ -47,11 +47,31 @@
# get logging object
log = logging.getLogger('epg')
-class Guide(object):
+class Guide(kaa.Object):
"""
EPG guide with db access.
"""
+ __kaasignals__ = {
+ 'program-retrieved':
+ '''
+ Emitted when a program is retrieve from the database.
+
+ .. describe:: def callback(row, extra_info, ...)
+
+ :param row: the program that has been retrieved from the database.
+ :type row: dictionary type object
+
+ :param extra_info: dictionary object to which to add additional info.
+ :type extra_info: dictionary object
+
+ Callbacks can add additional information to the program by adding
+ keys to the extra_info dictionary.
+ '''
+ }
+
+
def __init__(self, database):
+ super(Guide, self).__init__()
db_dir = os.path.dirname(database)
if db_dir and not os.path.isdir(db_dir):
os.makedirs(db_dir)
@@ -95,7 +115,6 @@ def __init__(self, database):
# List of credits (type, name, role)
credits = (list, ATTR_SIMPLE)
)
- self._program_callbacks = []
self._sync()
@@ -201,14 +220,16 @@ def search(self, channel=None, time=None, cls=Program, **kwargs):
def combine_attrs(row):
return [ row.get(a) for a in attrs ]
[ combine_attrs(row) for row in query_data ]
- extra_data = None
- if self._program_callbacks:
+
+ if len(self.signals['program-retrieved']):
extra_data = []
for row in query_data:
extra_info = {}
- for callback in self._program_callbacks:
- callback(row['id'], extra_info)
+ self.signals['program-retrieved'].emit(row, extra_info)
extra_data.append(extra_info)
+ else:
+ extra_data = None
+
if cls is None:
# return raw data:
yield query_data, extra_data
@@ -219,38 +240,13 @@ def combine_attrs(row):
def _rows_to_programs(self, cls, query_data, extra_data):
results = []
for i,row in enumerate(query_data):
- channel = self._channels_by_db_id[row['parent_id']]
- if extra_data is None:
- extra_info = None
+ if row['parent_id'] in self._channels_by_db_id:
+ channel = self._channels_by_db_id[row['parent_id']]
else:
- extra_info = extra_data[i]
- results.append(cls(channel, row, extra_info))
+ continue
+ results.append(cls(channel, row, extra_data[i] if extra_data else None))
return results
- def register_program_callback(self, callback):
- """
- Registers a callback that will be invoked when :meth:`search` is called to
- add additional information to the program, for example whether the
- program is scheduled to record, or whether it is a favorite.
-
- :param callback: Function which takes a Program id and a reference to a
- dict object to which any additional information should
- be added.
- :type callback: function(db_id, extra_info)
- """
- self._program_callbacks.append(callback)
-
- def unregister_program_callback(self, callback):
- """
- Unregisters a callback function that was registered with
- :meth:`register_program_callback`.
-
- :param callback: Function which was previously registered with
- :meth:`register_program_callback`.
- :type callback: function(db_id, extra_info)
- """
- self._progam_callbacks.remove(callback)
-
def new_channel(self, tuner_id=None, name=None, long_name=None):
"""
Returns a channel object that is not associated with the EPG.
@@ -329,7 +325,9 @@ def num_programs(self):
return self._num_programs
def to_timestamp(dt):
- 'Converts a time to a unix timestamp (seconds since epoch UTC)'
+ """
+ Converts a time to a unix timestamp (seconds since epoch UTC)
+ """
if isinstance(dt, (int, float, long)):
return dt
return kaa.dateutils.to_timestamp(dt)
View
@@ -151,9 +151,9 @@ def search(self, channel=None, time=None, cls=Program, **kwargs):
time = to_timestamp(time[0]), to_timestamp(time[1])
else:
time = to_timestamp(time)
- query_data,extra_data = yield self.channel.rpc('search', channel, time, None, **kwargs)
+ query_data, extra_data = yield self.channel.rpc('search', channel, time, None, **kwargs)
if cls is None:
- yield query_data,extra_data
+ yield query_data, extra_data
# Convert raw search result data from the server into python objects.
yield self._rows_to_programs(cls, query_data, extra_data)
@@ -203,10 +203,6 @@ def search(self, channel, time, cls, **kwargs):
return self.guide.search(channel, time, cls, **kwargs)
@kaa.rpc.expose()
- def get_grid(self, channels, start_time, end_time, cls):
- return self.guide.get_grid(channels, start_time, end_time, cls)
-
- @kaa.rpc.expose()
def get_keywords(self, associated=None, prefix=None):
return self.guide.get_keywords(associated, prefix)
@@ -225,6 +221,7 @@ def client_connected(self, client):
"""
Connect a new client to the server.
"""
+ log.info('Client connected: %s', client)
client.rpc('_sync', self.guide._channels_by_name.values(), self.guide._num_programs)
client.signals['closed'].connect(self.client_closed, client)
self._clients.append(client)
@@ -235,4 +232,3 @@ def client_closed(self, client):
"""
log.info('Client disconnected: %s', client)
self._clients.remove(client)
-
View
@@ -5,10 +5,10 @@
def local():
- def callback(id, extrainfo):
- extrainfo['test'] = 'db_id : %d' % id
+ def callback(row, extrainfo):
+ extrainfo['test'] = 'db_id : %d' % row['id']
guide = kaa.epg.load('test.db')
- guide.register_program_callback(callback)
+ guide.signals['program-retrieved'].connect(callback)
@kaa.coroutine()
@@ -37,11 +37,11 @@ def test():
print
-if 0:
+if 1:
print 'Local'
local()
-if 1:
+if 0:
print 'Remote'
remote().wait()
View
@@ -2,11 +2,11 @@
import kaa.epg
-def callback(id, extrainfo):
- extrainfo['test'] = 'db_id : %d' % id
+def callback(row, extrainfo):
+ extrainfo['test'] = 'db_id : %d' % row['id']
guide = kaa.epg.load('test.db')
-guide.register_program_callback(callback)
+guide.signals['program-retrieved'].connect(callback)
print kaa.epg.get_channels()

0 comments on commit 60969be

Please sign in to comment.