reorganize gpod communication into PodLibrary class

--HG--
branch : device
This commit is contained in:
Adrian Sampson 2010-04-05 21:07:22 -07:00
parent 210d4f3af3
commit 4258474da8
2 changed files with 68 additions and 45 deletions

View file

@ -3,49 +3,74 @@ import os
import sys
import socket
import locale
from beets.library import Library, Item
def start_sync(pod):
# Make sure we have a version of libgpod with these
# iPhone-specific functions.
if hasattr(gpod, 'itdb_start_sync'):
gpod.itdb_start_sync(pod._itdb)
def stop_sync(pod):
if hasattr(gpod, 'itdb_stop_sync'):
gpod.itdb_stop_sync(pod._itdb)
FIELD_MAP = {
'artist': 'artist',
'title': 'title',
'BPM': 'bpm',
'genre': 'genre',
'album': 'album',
'cd_nr': 'disc',
'cds': 'disctotal',
'track_nr': 'track',
'tracks': 'tracktotal',
}
def pod_path(name):
#FIXME: os.path.expanduser('~') to get $HOME is hacky!
return os.path.join(os.path.expanduser('~'), '.gvfs', name)
class PodLibrary(Library):
def __init__(self, path):
self.db = gpod.Database(path)
def get_pod(path):
return gpod.Database(path)
@classmethod
def by_name(cls, name):
return cls(os.path.join(os.path.expanduser('~'), '.gvfs', name))
def add(pod, items):
def cbk(db, track, it, total):
print 'copying', track
start_sync(pod)
try:
for item in items:
track = pod.new_Track()
track['userdata'] = {
'transferred': 0,
'hostname': socket.gethostname(),
'charset': locale.getpreferredencoding(),
'pc_mtime': os.stat(item.path).st_mtime,
}
track._set_userdata_utf8('filename', item.path.encode())
track['artist'] = item.artist
track['title'] = item.title
track['BPM'] = item.bpm
track['genre'] = item.genre
track['album'] = item.album
track['cd_nr'] = item.disc
track['cds'] = item.disctotal
track['track_nr'] = item.track
track['tracks'] = item.tracktotal
track['tracklen'] = int(item.length * 1000)
pod.copy_delayed_files(cbk)
finally:
pod.close()
stop_sync(pod)
def _start_sync(self):
# Make sure we have a version of libgpod with these
# iPhone-specific functions.
if hasattr(gpod, 'itdb_start_sync'):
gpod.itdb_start_sync(self.db._itdb)
def _stop_sync(self):
if hasattr(gpod, 'itdb_stop_sync'):
gpod.itdb_stop_sync(self.db._itdb)
def add_items(self, items):
self._start_sync()
try:
for item in items:
track = self.db.new_Track()
track['userdata'] = {
'transferred': 0,
'hostname': socket.gethostname(),
'charset': locale.getpreferredencoding(),
'pc_mtime': os.stat(item.path).st_mtime,
}
track._set_userdata_utf8('filename', item.path.encode())
for dname, bname in FIELD_MAP.items():
track[dname] = getattr(item, bname)
track['tracklen'] = int(item.length * 1000)
self.db.copy_delayed_files()
finally:
self.db.close()
self._stop_sync()
def add(self, path):
raise NotImplementedError
def get(self, query=None):
raise NotImplementedError
def save(self):
raise NotImplementedError
# Browsing convenience.
def artists(self, query=None):
raise NotImplementedError
def albums(self, artist=None, query=None):
raise NotImplementedError
def items(self, artist=None, album=None, title=None, query=None):
raise NotImplementedError

6
bts
View file

@ -187,10 +187,8 @@ class BeetsApp(cmdln.Cmdln):
items = self.lib.items(query=q)
from beets import device
path = device.pod_path(name)
pod = device.get_pod(path)
device.add(pod, items)
pod = device.PodLibrary.by_name(name)
pod.add_items(items)
if __name__ == '__main__':
app = BeetsApp()