finished new add(); importing now works

--HG--
extra : convert_revision : svn%3A41726ec3-264d-0410-9c23-a9f1637257cc/trunk%4040
This commit is contained in:
adrian.sampson 2008-07-03 21:09:05 +00:00
parent af66c04d5d
commit e69152e958
3 changed files with 63 additions and 16 deletions

View file

@ -66,6 +66,20 @@ def _ancestry(path):
out.insert(0, path) out.insert(0, path)
return out return out
def _walk_files(path):
"""Like os.walk, but only yields the files in the directory tree. The full
pathnames to the files (under path) are given. Also, if path is a file,
_walk_files just yields that."""
if os.path.isfile(path):
yield path
else:
for root, dirs, files in os.walk(path):
for filebase in files:
yield os.path.join(root, filebase)
class Item(object): class Item(object):
def __init__(self, values, library=None): def __init__(self, values, library=None):
@ -112,7 +126,7 @@ class Item(object):
#### interaction with the database #### #### interaction with the database ####
def load(self, fetch_id=None): def load(self, load_id=None):
"""Refresh the item's metadata from the library database. If fetch_id """Refresh the item's metadata from the library database. If fetch_id
is not specified, use the current item's id.""" is not specified, use the current item's id."""
@ -195,7 +209,7 @@ class Item(object):
if read_path is None: if read_path is None:
read_path = self.path read_path = self.path
f = MediaFile(read_path) f = MediaFile(read_path)
for key in metadata_keys: for key in metadata_keys:
self.record[key] = getattr(f, key) self.record[key] = getattr(f, key)
self.record['path'] = read_path # don't use self.path because there's self.record['path'] = read_path # don't use self.path because there's
@ -230,7 +244,7 @@ class Item(object):
# build the mapping for substitution in the path template, beginning # build the mapping for substitution in the path template, beginning
# with the values from the database # with the values from the database
mapping = {} mapping = {}
for key in item_keys: for key in metadata_keys:
value = self.record[key] value = self.record[key]
# sanitize the value for inclusion in a path: # sanitize the value for inclusion in a path:
# replace / and leading . with _ # replace / and leading . with _
@ -539,17 +553,18 @@ class Library(object):
#### main interface #### #### main interface ####
def add(self, path): def add(self, path, copy=False):
"""Add a file to the library or recursively search a directory and add """Add a file to the library or recursively search a directory and add
all its contents.""" all its contents. If copy is True, copy files to their destination in
the library directory while adding."""
for root, dirs, files in os.walk(path): for f in _walk_files(path):
for filebase in files: try:
filepath = os.path.join(root, filebase) i = Item.from_path(_normpath(f), self)
try: if copy:
Item.from_path(_normpath(filepath), self) i.move(copy=True)
except FileTypeError: except FileTypeError:
_log(filepath + ' of unknown type, skipping') _log(f + ' of unknown type, skipping')
def get(self, query=None): def get(self, query=None):
"""Returns a ResultIterator to the items matching query, which may be """Returns a ResultIterator to the items matching query, which may be

3
bts.py
View file

@ -16,7 +16,8 @@ def ls(lib, criteria):
def imp(lib, paths): def imp(lib, paths):
for path in paths: for path in paths:
pass lib.add(path, copy=True)
lib.save()
if __name__ == "__main__": if __name__ == "__main__":
# parse options # parse options

View file

@ -116,19 +116,50 @@ correct_dicts = {
'comments': u'', 'comments': u'',
'bpm': 0, 'bpm': 0,
'comp': False 'comp': False
},
# empty.mp3 has had its ID3 tag deleted with mp3info -d
'empty': {
'title': u'',
'artist': u'',
'album': u'',
'genre': u'',
'composer': u'',
'grouping': u'',
'year': 0,
'track': 0,
'maxtrack': 0,
'disc': 0,
'maxdisc': 0,
'lyrics': u'',
'comments': u'',
'bpm': 0,
'comp': False
} }
} }
def suite_for_file(path, correct_dict):
s = unittest.TestSuite()
for field in correct_dict.keys():
s.addTest(MakeReadingTest(path, correct_dict, field)())
s.addTest(MakeWritingTest(path, correct_dict, field)())
return s
def suite(): def suite():
s = unittest.TestSuite() s = unittest.TestSuite()
# General tests.
for kind in ('m4a', 'mp3'): for kind in ('m4a', 'mp3'):
for tagset in ('full', 'partial', 'min'): for tagset in ('full', 'partial', 'min'):
path = 'rsrc' + os.sep + tagset + '.' + kind path = 'rsrc' + os.sep + tagset + '.' + kind
correct_dict = correct_dicts[tagset] correct_dict = correct_dicts[tagset]
for field in correct_dict.keys(): s.addTest(suite_for_file(path, correct_dict))
s.addTest(MakeReadingTest(path, correct_dict, field)())
s.addTest(MakeWritingTest(path, correct_dict, field)()) # Special test for missing ID3 tag.
s.addTest(suite_for_file('rsrc' + os.sep + 'empty.mp3',
correct_dicts['empty']))
return s return s
if __name__ == '__main__': if __name__ == '__main__':