def test_save_ascii_keys_as_bytes_on_py3(self): i = AudioFile.__new__(list(formats.types)[0]) dict.__setitem__(i, u"foo", u"bar") data = dump_audio_files([i], process=True) if PY3: items = load_audio_files(data, process=False) assert isinstance(list(items[0].keys())[0], bytes)
def test_sanitize_py2_fixup(self): if PY3: return old = dict.__new__(AudioFile) dict.__init__(old, { b"foo": b"bar", u"öäü": b"bla", "~#num": u"1", "~#num2": u"1.25", "~#num3": u"bla", "~filename": u"text", "~mountpoint": b"bytes", "~somethingdifferent": b"hello", }) fixed = { b"foo": u"bar", u"öäü": u"bla", "~#num": 1, "~#num2": 1.25, "~#num3": 0, "~filename": fsnative(u"text"), "~mountpoint": fsnative(u"bytes"), "~somethingdifferent": u"hello", } data = dump_audio_files([old]) new = load_audio_files(data) assert dict(new[0]) == fixed for v1, v2 in zip(sorted(new[0].values()), sorted(fixed.values())): assert type(v1) is type(v2)
def test_dump_audio_files(self): data = dump_audio_files(self.instances, process=False) items = load_audio_files(data, process=False) assert len(items) == len(self.instances) for a, b in zip(items, self.instances): a = dict(a) b = dict(b) for key in a: assert b[key] == a[key] for key in b: assert b[key] == a[key]
def test_sanitize_py2_normal(self): if PY3: return af = AudioFile({ b"foo": u"bar", u"öäü": u"bla", "~#num": 1, "~#num2": long(2), "~#num3": 1.25, "~filename": fsnative(u"filename"), "~mountpoint": fsnative(u"mount"), "~somethingdifferent": u"hello", }) data = dump_audio_files([af]) new = load_audio_files(data) assert dict(new[0]) == dict(af)
def save(self, filename=None): """Save the library to the given filename, or the default if `None`""" if filename is None: filename = self.filename print_d("Saving contents to %r." % filename, self) try: dirname = os.path.dirname(filename) mkdir(dirname) with atomic_save(filename, "wb") as fileobj: fileobj.write(dump_audio_files(self.get_content())) # unhandled SerializationError, shouldn't happen -> better # not replace the library file with nothing except EnvironmentError: print_w("Couldn't save library to path: %r" % filename) else: self.dirty = False
def save(self, filename=None): """Save the library to the given filename, or the default if `None`""" if filename is None: filename = self.filename print_d("Saving contents to %r." % filename, self) try: dirname = os.path.dirname(filename) mkdir(dirname) with atomic_save(filename, "wb") as fileobj: fileobj.write(dump_audio_files(self.get_content())) except SerializationError: # Can happen when we try to pickle while the library is being # modified, like in the periodic 15min save. # Ignore, as it should try again later or on program exit. util.print_exc() except EnvironmentError: print_w("Couldn't save library to path: %r" % filename) else: self.dirty = False
def test_dump_empty(self): data = dump_audio_files([]) assert load_audio_files(data) == []