make the PodLibrary interface class more consistent; remove more back-references

from Item to Library

(including removing the delete() method, which was unused)

--HG--
branch : device
This commit is contained in:
Adrian Sampson 2010-04-06 11:07:57 -07:00
parent 4d1944f939
commit c7f98ccde1
4 changed files with 40 additions and 72 deletions

View file

@ -20,6 +20,7 @@ FIELD_MAP = {
class PodLibrary(BaseLibrary):
def __init__(self, path):
self.db = gpod.Database(path)
self.syncing = False
@classmethod
def by_name(cls, name):
@ -28,17 +29,21 @@ class PodLibrary(BaseLibrary):
def _start_sync(self):
# Make sure we have a version of libgpod with these
# iPhone-specific functions.
if self.syncing:
return
if hasattr(gpod, 'itdb_start_sync'):
gpod.itdb_start_sync(self.db._itdb)
self.syncing = True
def _stop_sync(self):
if not self.syncing:
return
if hasattr(gpod, 'itdb_stop_sync'):
gpod.itdb_stop_sync(self.db._itdb)
self.syncing = False
def add_items(self, items):
def add(self, item):
self._start_sync()
try:
for item in items:
track = self.db.new_Track()
track['userdata'] = {
'transferred': 0,
@ -51,18 +56,13 @@ class PodLibrary(BaseLibrary):
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
self._stop_sync()
gpod.itdb_write(self.pod._itdb, None)
# Browsing convenience.
def artists(self, query=None):

View file

@ -142,6 +142,17 @@ class Item(object):
self._fill_record(values)
self._clear_dirty()
@classmethod
def from_path(cls, path, library=None):
"""Creates a new item from the media file at the specified path. Sets
the item's library (but does not add the item) if library is
specified.
"""
i = cls({})
i.read(path)
i.library = library
return i
def _fill_record(self, values):
self.record = {}
for key in item_keys:
@ -156,8 +167,7 @@ class Item(object):
self.dirty[key] = False
def __repr__(self):
return 'Item(' + repr(self.record) + \
', library=' + repr(self.library) + ')'
return 'Item(' + repr(self.record) + ')'
#### item field accessors ####
@ -284,27 +294,6 @@ class Item(object):
# Either copying or moving succeeded, so update the stored path.
self.path = dest
def delete(self):
"""Deletes the item from the filesystem. If the item is located
in the library directory, any empty parent directories are trimmed.
Also calls remove(), deleting the appropriate row from the database.
As with move(), library.save() should almost certainly be called after
invoking this (although store() should not).
"""
os.unlink(self.path)
self.remove()
@classmethod
def from_path(cls, path, library=None):
"""Creates a new item from the media file at the specified path. Sets
the item's library (but does not add the item) if library is
specified.
"""
i = cls({})
i.read(path)
i.library = library
return i

5
bts
View file

@ -188,8 +188,11 @@ class BeetsApp(cmdln.Cmdln):
from beets import device
pod = device.PodLibrary.by_name(name)
pod.add_items(items)
for item in items:
pod.add(item)
pod.save()
if __name__ == '__main__':
app = BeetsApp()
sys.exit(app.main())

View file

@ -71,30 +71,6 @@ class MoveTest(unittest.TestCase):
self.i.move()
self.assertEqual(self.i.path, beets.library._normpath(self.dest))
class DeleteTest(unittest.TestCase):
def setUp(self):
# make a temporary file
self.path = join('rsrc', 'temp.mp3')
shutil.copy(join('rsrc', 'full.mp3'), self.path)
# add it to a temporary library
self.lib = beets.library.Library(':memory:')
self.i = beets.library.Item.from_path(self.path)
self.i.add(self.lib)
def tearDown(self):
# make sure the temp file is gone
if os.path.exists(self.path):
os.remove(self.path)
def test_delete_deletes_file(self):
self.i.delete()
self.assertTrue(not os.path.exists(self.path))
def test_delete_removes_from_db(self):
self.i.delete()
c = self.lib.conn.execute('select * from items where 1')
self.assertEqual(c.fetchone(), None)
class WalkTest(unittest.TestCase):
def setUp(self):
# create a directory structure for testing