Author: dmeyer
Date: Wed Apr 11 17:37:09 2007
New Revision: 2614
Modified:
trunk/beacon/bin/beacon
trunk/beacon/src/server/monitor.py
trunk/beacon/src/server/parser.py
trunk/beacon/src/server/server.py
Log:
minor cleanup
Modified: trunk/beacon/bin/beacon
==============================================================================
--- trunk/beacon/bin/beacon (original)
+++ trunk/beacon/bin/beacon Wed Apr 11 17:37:09 2007
@@ -1,4 +1,34 @@
#!/usr/bin/python
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# beacon
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# -----------------------------------------------------------------------------
+# kaa.beacon - A virtual filesystem with metadata
+# Copyright (C) 2006-2007 Dirk Meyer
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer: Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file AUTHORS for a complete list of authors.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
+# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# -----------------------------------------------------------------------------
# python imports
import os
@@ -359,8 +389,22 @@
# start garbage collector
kaa.notifier.Timer(garbage_collect).start(10)
if shutdown:
+
+ shutdown_timer = 3
+ def autoshutdown_poll():
+ global shutdown_timer
+ if server.has_clients():
+ shutdown_timer = 3
+ return True
+ shutdown_timer -= 1
+ if not shutdown_timer:
+ log.info('beacon timeout')
+ sys.exit(0)
+ return True
+
log.info('set autoshutdown timer to 3 seconds')
- server.autoshutdown(3)
+ kaa.notifier.Timer(autoshutdown_poll).start(1)
+
if detach:
# release waiting main process
daemonize_lock.release(0)
Modified: trunk/beacon/src/server/monitor.py
==============================================================================
--- trunk/beacon/src/server/monitor.py (original)
+++ trunk/beacon/src/server/monitor.py Wed Apr 11 17:37:09 2007
@@ -6,7 +6,7 @@
#
# -----------------------------------------------------------------------------
# kaa.beacon.server - A virtual filesystem with metadata
-# Copyright (C) 2006 Dirk Meyer
+# Copyright (C) 2006-2007 Dirk Meyer
#
# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
# Maintainer: Dirk Meyer <[EMAIL PROTECTED]>
@@ -46,18 +46,6 @@
# get logging object
log = logging.getLogger('beacon.monitor')
-class Notification(object):
- def __init__(self, client, id):
- self.rpc = client.rpc
- self.id = id
-
- def __call__(self, *args, **kwargs):
- try:
- self.rpc('notify', self.id, *args, **kwargs)
- except IOError:
- pass
-
-
class Master(object):
"""
Master Monitor. This monitor will connect to the db and will call all
@@ -104,17 +92,18 @@
self.monitors.append((monitor, [ False, [] ]))
return len(changes) > 0 or force
-_master = None
class Monitor(object):
"""
Monitor query for changes and call the client.
"""
+
+ _master = None
+
def __init__(self, client, db, server, id, query):
- global _master
log.info('create new monitor %s' % id)
self.id = id
- self.notify_client = Notification(client, self.id)
+ self._client = client
self._server = server
self._db = db
self._query = query
@@ -122,17 +111,27 @@
self._running = True
self._check_changes = []
self.items = self._db.query(**self._query)
- if not _master:
- _master = Master(db)
- _master.connect(self)
+ if not Monitor._master:
+ Monitor._master = Master(db)
+ Monitor._master.connect(self)
if self.items and isinstance(self.items[0], Item):
- self._initial_scan(True)
+ self._initial_scan()
# FIXME: how to get updates on directories not monitored by
# inotify? Maybe poll the dirs when we have a query with
# dirname it it?
+ def notify_client(self, *args, **kwargs):
+ """
+ Send notify rpc to client.
+ """
+ try:
+ self._client.rpc('notify', self.id, *args, **kwargs)
+ except IOError:
+ pass
+
+
def check(self, changes):
"""
This function compares the last query result with the current db status
@@ -202,7 +201,7 @@
@yield_execution(0.01)
- def _initial_scan(self, first_call):
+ def _initial_scan(self):
"""
Start scanning the current list of items if they need to be updated.
With a full structure covered by inotify, there should be not changes.
@@ -220,7 +219,7 @@
if i._beacon_changed():
changed.append(i)
- if not changed and first_call:
+ if not changed:
# no changes but it was our first call. Tell the client that
# everything is checked
self.notify_client('checked')
@@ -233,10 +232,6 @@
self._checking = False
yield False
- if not first_call and len(changed) > 10:
- # do not wait to send the changed signal, it may take a while.
- self.notify_client('changed', True)
-
for pos, item in enumerate(changed):
self.notify_client('progress', pos+1, len(changed), item.url)
parser.parse(self._db, item)
@@ -257,8 +252,7 @@
# Set new check timer. This should not be needed, but just in
# case :)
OneShotTimer(self.check, []).start(0.5)
- if first_call:
- self.notify_client('checked')
+ self.notify_client('checked')
self._checking = False
yield False
Modified: trunk/beacon/src/server/parser.py
==============================================================================
--- trunk/beacon/src/server/parser.py (original)
+++ trunk/beacon/src/server/parser.py Wed Apr 11 17:37:09 2007
@@ -245,6 +245,7 @@
if not metadata.get('title'):
# try to set a good title
+ # FIXME: if a file is renamed later, this tite should also update
title = get_title(item._beacon_data['name'])
metadata['title'] = str_to_unicode(title)
@@ -264,7 +265,7 @@
db.delete_object(item._beacon_id)
item._beacon_id = None
db.commit()
-
+
# Note: the items are not updated yet, the changes are still in
# the queue and will be added to the db on commit.
@@ -300,15 +301,13 @@
type = 'track_%s' % metadata.get('type').lower()
for track in metadata.tracks:
db.add_object(type, name=str(track.trackno),
- parent=item._beacon_id,
- media=item._beacon_media._beacon_id[1],
- mtime=0,
- metadata=track)
+ parent=item._beacon_id,
+ media=item._beacon_media._beacon_id[1],
+ mtime=0, metadata=track)
db.commit()
-
+
if store:
db.commit()
log.info('scan %s (%0.3f)' % (item, time.time() - t1))
-
return produced_load
Modified: trunk/beacon/src/server/server.py
==============================================================================
--- trunk/beacon/src/server/server.py (original)
+++ trunk/beacon/src/server/server.py Wed Apr 11 17:37:09 2007
@@ -6,7 +6,7 @@
#
# -----------------------------------------------------------------------------
# kaa.beacon.server - A virtual filesystem with metadata
-# Copyright (C) 2006 Dirk Meyer
+# Copyright (C) 2006-2007 Dirk Meyer
#
# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
# Maintainer: Dirk Meyer <[EMAIL PROTECTED]>
@@ -160,6 +160,10 @@
self.monitor_dir(dir.replace('$(HOME)', os.environ.get('HOME')))
+ # -------------------------------------------------------------
+ # client handling
+ # -------------------------------------------------------------
+
def client_connect(self, client):
"""
Connect a new client to the server.
@@ -184,30 +188,45 @@
m.stop()
self._clients.remove(client_info)
+ def has_clients(self):
+ """
+ Return if clients are connected.
+ """
+ return len(self._clients) > 0
+
+
+ # -------------------------------------------------------------
+ # hardware monitor callbacks
+ # -------------------------------------------------------------
- def autoshutdown(self, timeout):
+ def media_changed(self, media):
"""
- Start autoshutdown.
+ Media mountpoint changed or added.
"""
- if hasattr(self, '_autoshutdown_timer'):
- return
- self._autoshutdown_timer = timeout
- Timer(self._autoshutdown, timeout).start(1)
+ for id, client, monitors in self._clients:
+ client.rpc('device.changed', media.id, media.prop)
+ if not media.crawler:
+ if not media.get('block.device'):
+ log.info('start crawler for /')
+ media.crawler = Crawler(self._db, use_inotify=True)
+ self._db.signals['changed'].emit([media._beacon_id])
- def _autoshutdown(self, timeout):
+ def media_removed(self, media):
"""
- Timer callback for autoshutdown.
+ Media mountpoint removed.
"""
- if len(self._clients) > 0:
- self._autoshutdown_timer = timeout
- return True
- self._autoshutdown_timer -= 1
- if self._autoshutdown_timer == 0:
- log.info('beacon timeout')
- sys.exit(0)
- return True
+ for id, client, monitors in self._clients:
+ client.rpc('device.removed', media.id)
+ self._db.signals['changed'].emit([media._beacon_id])
+ if media.crawler:
+ media.crawler.stop()
+ media.crawler = None
+
+ # -------------------------------------------------------------
+ # client RPC API
+ # -------------------------------------------------------------
@kaa.rpc.expose('db.register_file_type_attrs')
def register_file_type_attrs(self, name, **kwargs):
@@ -300,30 +319,6 @@
log.error('unable to find monitor %s:%s', client_id, request_id)
- def media_changed(self, media):
- """
- Media mountpoint changed or added.
- """
- for id, client, monitors in self._clients:
- client.rpc('device.changed', media.id, media.prop)
- if not media.crawler:
- if not media.get('block.device'):
- log.info('start crawler for /')
- media.crawler = Crawler(self._db, use_inotify=True)
- self._db.signals['changed'].emit([media._beacon_id])
-
-
- def media_removed(self, media):
- """
- Media mountpoint removed.
- """
- for id, client, monitors in self._clients:
- client.rpc('device.removed', media.id)
- self._db.signals['changed'].emit([media._beacon_id])
- if media.crawler:
- media.crawler.stop()
- media.crawler = None
-
@kaa.rpc.expose('item.update')
def update(self, items):
"""
@@ -336,6 +331,9 @@
@kaa.rpc.expose('item.request')
def request(self, filename):
+ """
+ Request item data.
+ """
self._db.commit()
data = self._db.query(filename=filename)
items = []
@@ -351,11 +349,17 @@
@kaa.rpc.expose('beacon.shutdown')
def shutdown(self):
+ """
+ Shutdown beacon.
+ """
sys.exit(0)
@kaa.rpc.expose('media.eject')
def eject(self, id):
+ """
+ Eject media with the given id
+ """
dev = medialist.get(id)
if not dev:
log.error('eject: no device %s' % id)
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog