def __init__( self, path="library.blb", directory="~/Music", path_formats=((PF_KEY_DEFAULT, "$artist/$album/$track $title"),), art_filename="cover", timeout=5.0, replacements=None, item_fields=ITEM_FIELDS, album_fields=ALBUM_FIELDS, ): if path == ":memory:": self.path = path else: self.path = bytestring_path(normpath(path)) self.directory = bytestring_path(normpath(directory)) self.path_formats = path_formats self.art_filename = bytestring_path(art_filename) self.replacements = replacements self.timeout = timeout self.conn = sqlite3.connect(self.path, timeout) self.conn.row_factory = sqlite3.Row # this way we can access our SELECT results like dictionaries self._make_table("items", item_fields) self._make_table("albums", album_fields)
def __init__(self, path='library.blb', directory='~/Music', path_formats=((PF_KEY_DEFAULT, '$artist/$album/$track $title'),), art_filename='cover', timeout=5.0, replacements=None, item_fields=ITEM_FIELDS, album_fields=ALBUM_FIELDS): if path == ':memory:': self.path = path else: self.path = bytestring_path(normpath(path)) self.directory = bytestring_path(normpath(directory)) self.path_formats = path_formats self.art_filename = bytestring_path(art_filename) self.replacements = replacements self.timeout = timeout self.conn = sqlite3.connect(self.path, timeout) self.conn.row_factory = sqlite3.Row # this way we can access our SELECT results like dictionaries self._make_table('items', item_fields) self._make_table('albums', album_fields)
def __init__(self, path='library.blb', directory='~/Music', path_formats=None, art_filename='cover', item_fields=ITEM_FIELDS, album_fields=ALBUM_FIELDS): if path == ':memory:': self.path = path else: self.path = bytestring_path(normpath(path)) self.directory = bytestring_path(normpath(directory)) if path_formats is None: path_formats = {'default': '$artist/$album/$track $title'} elif isinstance(path_formats, basestring): path_formats = {'default': path_formats} self.path_formats = path_formats self.art_filename = bytestring_path(art_filename) self.conn = sqlite3.connect(self.path) self.conn.row_factory = sqlite3.Row # this way we can access our SELECT results like dictionaries self._make_table('items', item_fields) self._make_table('albums', album_fields)
def __setattr__(self, key, value): """Set the value of an album attribute.""" if key == "id": raise AttributeError("can't modify album id") elif key in ALBUM_KEYS: # Make sure paths are bytestrings. if key == "artpath" and isinstance(value, unicode): value = bytestring_path(value) # Reflect change in this object. self._record[key] = value # Store art path as a buffer. if key == "artpath" and isinstance(value, str): value = buffer(value) # Change album table. sql = "UPDATE albums SET %s=? WHERE id=?" % key self._library.conn.execute(sql, (value, self.id)) # Possibly make modification on items as well. if key in ALBUM_KEYS_ITEM: for item in self.items(): setattr(item, key, value) self._library.store(item) else: object.__setattr__(self, key, value)
def __setattr__(self, key, value): """Set the value of an album attribute.""" if key == 'id': raise AttributeError("can't modify album id") elif key in ALBUM_KEYS: # Make sure paths are bytestrings. if key == 'artpath' and isinstance(value, unicode): value = bytestring_path(value) # Reflect change in this object. self._record[key] = value # Store art path as a buffer. if key == 'artpath' and isinstance(value, str): value = buffer(value) # Change album table. sql = 'UPDATE albums SET %s=? WHERE id=?' % key self._library.conn.execute(sql, (value, self.id)) # Possibly make modification on items as well. if key in ALBUM_KEYS_ITEM: for item in self.items(): setattr(item, key, value) self._library.store(item) else: object.__setattr__(self, key, value)
def set_art(self, path): """Sets the album's cover art to the image at the given path. The image is copied into place, replacing any existing art. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart == artdest: util.soft_remove(oldart) shutil.copyfile(syspath(path), syspath(artdest)) self.artpath = artdest
def __init__(self, path='library.blb', directory='~/Music', path_formats=None, art_filename='cover', item_fields=ITEM_FIELDS, album_fields=ALBUM_FIELDS): self.path = bytestring_path(path) self.directory = bytestring_path(directory) if path_formats is None: path_formats = {'default': '$artist/$album/$track $title'} elif isinstance(path_formats, basestring): path_formats = {'default': path_formats} self.path_formats = path_formats self.art_filename = bytestring_path(art_filename) self.conn = sqlite3.connect(self.path) self.conn.row_factory = sqlite3.Row # this way we can access our SELECT results like dictionaries self._make_table('items', item_fields) self._make_table('albums', album_fields)
def art_destination(self, image, item_dir=None): """Returns a path to the destination for the album art image for the album. `image` is the path of the image that will be moved there (used for its extension). The path construction uses the existing path of the album's items, so the album must contain at least one item or item_dir must be provided. """ image = bytestring_path(image) item_dir = item_dir or self.item_dir() _, ext = os.path.splitext(image) dest = os.path.join(item_dir, self._library.art_filename + ext) return dest
def set_art(self, path): """Sets the album's cover art to the image at the given path. The image is copied into place, replacing any existing art. """ path = bytestring_path(path) oldart = self.artpath artdest = self.art_destination(path) if oldart and samefile(path, oldart): # Art already set. return elif samefile(path, artdest): # Art already in place. self.artpath = path return # Normal operation. if oldart == artdest: util.soft_remove(oldart) util.copy(path, artdest) self.artpath = artdest
def __setattr__(self, key, value): """If key is an item attribute (i.e., a column in the database), sets the record entry for that key to value. Note that to change the attribute in the database or in the file's tags, one must call store() or write(). Otherwise, performs an ordinary setattr. """ # Encode unicode paths and read buffers. if key == 'path': if isinstance(value, unicode): value = bytestring_path(value) elif isinstance(value, buffer): value = str(value) if key in ITEM_KEYS: if (not (key in self.record)) or (self.record[key] != value): # don't dirty if value unchanged self.record[key] = value self.dirty[key] = True else: super(Item, self).__setattr__(key, value)
def __setattr__(self, key, value): """If key is an item attribute (i.e., a column in the database), sets the record entry for that key to value. Note that to change the attribute in the database or in the file's tags, one must call store() or write(). Otherwise, performs an ordinary setattr. """ # Encode unicode paths and read buffers. if key == "path": if isinstance(value, unicode): value = bytestring_path(value) elif isinstance(value, buffer): value = str(value) if key in ITEM_KEYS: # If the value changed, mark the field as dirty. if (not (key in self.record)) or (self.record[key] != value): self.record[key] = value self.dirty[key] = True if key in ITEM_KEYS_WRITABLE: self.mtime = 0 # Reset mtime on dirty. else: super(Item, self).__setattr__(key, value)
def clause(self): dir_pat = self.dir_path + "%" file_blob = buffer(bytestring_path(self.file_path)) return "(path = ?) || (path LIKE ?)", (file_blob, dir_pat)
def __init__(self, lib, record): # Decode Unicode paths in database. if "artpath" in record and isinstance(record["artpath"], unicode): record["artpath"] = bytestring_path(record["artpath"]) super(Album, self).__init__(lib, record)
def clause(self): dir_pat = self.dir_path + '%' file_blob = buffer(bytestring_path(self.file_path)) return '(path = ?) || (path LIKE ?)', (file_blob, dir_pat)
def __init__(self, lib, record): # Decode Unicode paths in database. if 'artpath' in record and isinstance(record['artpath'], unicode): record['artpath'] = bytestring_path(record['artpath']) super(Album, self).__init__(lib, record)