cc-4105. Refactored syncdb and fixed a few typos

This commit is contained in:
root 2012-07-20 15:37:13 -04:00 committed by Rudi Grinberg
parent 9964c826ab
commit 3e251997f8
6 changed files with 53 additions and 28 deletions

View File

@ -605,16 +605,13 @@ class AirtimeApiClient():
logger.error('Exception: %s', e) logger.error('Exception: %s', e)
logger.error("traceback: %s", traceback.format_exc()) logger.error("traceback: %s", traceback.format_exc())
"""
Retrive infomations needed on bootstrap time
"""
def get_bootstrap_info(self): def get_bootstrap_info(self):
"""
Retrive infomations needed on bootstrap time
"""
logger = self.logger logger = self.logger
try: try:
url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["get_bootstrap_info"]) url = self.construct_url("get_bootstrap_info")
url = url.replace("%%api_key%%", self.config["api_key"])
response = self.get_response_from_server(url) response = self.get_response_from_server(url)
response = json.loads(response) response = json.loads(response)
logger.info("Bootstrap info retrieved %s", response) logger.info("Bootstrap info retrieved %s", response)

View File

@ -9,11 +9,11 @@ class Bootstrapper(Loggable):
Bootstrapper reads all the info in the filesystem flushes organize Bootstrapper reads all the info in the filesystem flushes organize
events and watch events events and watch events
""" """
def __init__(self,db,last_ran,org_channels,watch_channels): def __init__(self,db,last_run,org_channels,watch_channels):
self.db = db self.db = db
self.org_channels = org_channels self.org_channels = org_channels
self.watch_channels = watch_channels self.watch_channels = watch_channels
self.last_ran = last_ran self.last_run = last_run
def flush_organize(self): def flush_organize(self):
""" """
@ -44,12 +44,12 @@ class Bootstrapper(Loggable):
# wasn't aware when this changes occured in the filesystem # wasn't aware when this changes occured in the filesystem
# hence it will send the correct events to sync the database # hence it will send the correct events to sync the database
# with the filesystem # with the filesystem
if os.path.getmtime(f) > self.last_ran: if os.path.getmtime(f) > self.last_run:
modded += 1 modded += 1
dispatcher.send(signal=pc.signal, sender=self, event=DeleteFile(f)) dispatcher.send(signal=pc.signal, sender=self, event=DeleteFile(f))
dispatcher.send(signal=pc.signal, sender=self, event=NewFile(f)) dispatcher.send(signal=pc.signal, sender=self, event=NewFile(f))
# Want all files in the database that are not in the filesystem # Want all files in the database that are not in the filesystem
for to_delete in self.db.exclude(songs): for to_delete in self.db.difference(songs):
for pc in self.watch_channels: for pc in self.watch_channels:
if os.path.commonprefix([pc.path, to_delete]) == pc.path: if os.path.commonprefix([pc.path, to_delete]) == pc.path:
dispatcher.send(signal=pc.signal, sender=self, event=DeleteFile(f)) dispatcher.send(signal=pc.signal, sender=self, event=DeleteFile(f))
@ -57,9 +57,8 @@ class Bootstrapper(Loggable):
deleted += 1 deleted += 1
break break
else: else:
self.logger.info("Error, could not find watch directory of would be deleted \ self.logger.info("Error, could not find watch directory of would be deleted file '%s'" % to_delete)
file '%s'" % to_delete) self.logger.info( "Flushed watch directories. (modified, deleted) = (%d, %d)"
self.logger.info("Flushed watch directories. (modified, deleted) = (%d, %d)"
% (modded, deleted) ) % (modded, deleted) )

View File

@ -2,7 +2,7 @@
import os import os
import abc import abc
from media.monitor.pure import LazyProperty from media.monitor.pure import LazyProperty
from media.monitor.monitor import Metadata from media.monitor.metadata import Metadata
class PathChannel(object): class PathChannel(object):
"""a dumb struct; python has no record types""" """a dumb struct; python has no record types"""

View File

@ -13,3 +13,6 @@ class Loggable(object):
# TODO : Clean this up # TODO : Clean this up
if not hasattr(self,"_logger"): self._logger = logging.getLogger('mediamonitor2') if not hasattr(self,"_logger"): self._logger = logging.getLogger('mediamonitor2')
return self._logger return self._logger
def get_logger():
return logging.getLogger('mediamonitor2')

View File

@ -1,13 +1,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
class SyncDB(object): from media.monitor.log import Loggable
"""
Represents the database returned by airtime_mvc. We do not use a list or some other class SyncDB(Loggable):
fixed data structure because we might want to change the internal representation for
performance reasons later on. def __init__(self, apc):
""" self.apc = apc
def __init__(self, source): dirs = self.apc.list_all_watched_dirs()
self.source = source directories = None
def has_file(self, path): try:
return True directories = dirs['dirs']
def file_mdata(self, path): except KeyError as e:
return None self.logger.error("Could not find index 'dirs' in dictionary: %s", str(dirs))
self.logger.error(e)
raise
# self.directories is a dictionary where a key is the directory and the
# value is the directory's id in the db
self.directories = dict( (v,k) for k,v in directories.iteritems() )
def list_directories(self):
return self.directories.keys()
def directory_get_files(self, directory):
print("trying to access dir id: %s" % self.directories[directory])
self.apc.list_all_db_files(self.directories[directory])

View File

@ -1,12 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# testing ground for the script # testing ground for the script
import pyinotify import pyinotify
import time
from media.monitor.listeners import OrganizeListener, StoreWatchListener from media.monitor.listeners import OrganizeListener, StoreWatchListener
from media.monitor.organizer import Organizer from media.monitor.organizer import Organizer
from media.monitor.events import PathChannel from media.monitor.events import PathChannel
from media.monitor.watchersyncer import WatchSyncer from media.monitor.watchersyncer import WatchSyncer
from media.monitor.handler import ProblemFileHandler from media.monitor.handler import ProblemFileHandler
#from media.monitor.bootstrap import Bootstrapper from media.monitor.bootstrap import Bootstrapper
from media.monitor.log import get_logger
from media.monitor.syncdb import SyncDB
from api_clients import api_client as apc
channels = { channels = {
# note that org channel still has a 'watch' path because that is the path # note that org channel still has a 'watch' path because that is the path
@ -21,6 +25,14 @@ org = Organizer(channel=channels['org'],target_path=channels['watch'].path)
watch = WatchSyncer(channel=channels['watch']) watch = WatchSyncer(channel=channels['watch'])
problem_files = ProblemFileHandler(channel=channels['badfile']) problem_files = ProblemFileHandler(channel=channels['badfile'])
apiclient = apc.AirtimeApiClient(get_logger())
raw_bootstrap = apiclient.get_bootstrap_info()
print(raw_bootstrap)
bs = Bootstrapper(db=bootstrap_db, last_run=int(time.time()), org_channels=[channels['org']], watch_channels=[channels['watch']])
bs.flush_organize()
bs.flush_watch()
# do the bootstrapping before any listening is going one # do the bootstrapping before any listening is going one
#conn = Connection('localhost', 'more', 'shit', 'here') #conn = Connection('localhost', 'more', 'shit', 'here')
#db = DBDumper(conn).dump_block() #db = DBDumper(conn).dump_block()