Esempio n. 1
0
 def test_basic(self):
     if is_win:
         v = strip_win32_incompat_from_path(u"C:\\foo\\<>/a")
         self.assertEqual(v, u"C:\\foo\\___a")
     else:
         v = strip_win32_incompat_from_path("/foo/<>a")
         self.assertEqual(v, "/foo/__a")
Esempio n. 2
0
 def test_basic(self):
     if is_win:
         v = strip_win32_incompat_from_path(u"C:\\foo\\<>/a")
         self.assertEqual(v, u"C:\\foo\\___a")
     else:
         v = strip_win32_incompat_from_path("/foo/<>a")
         self.assertEqual(v, "/foo/__a")
Esempio n. 3
0
    def test_types(self):
        v = strip_win32_incompat_from_path(fsnative(u""))
        self.assertTrue(isinstance(v, fsnative))
        v = strip_win32_incompat_from_path(fsnative(u"foo"))
        self.assertTrue(isinstance(v, fsnative))

        v = strip_win32_incompat_from_path(u"")
        self.assertTrue(isinstance(v, str))
        v = strip_win32_incompat_from_path(u"foo")
        self.assertTrue(isinstance(v, str))
Esempio n. 4
0
    def test_types(self):
        v = strip_win32_incompat_from_path("")
        self.assertTrue(isinstance(v, bytes))
        v = strip_win32_incompat_from_path("foo")
        self.assertTrue(isinstance(v, bytes))

        v = strip_win32_incompat_from_path(u"")
        self.assertTrue(isinstance(v, unicode))
        v = strip_win32_incompat_from_path(u"foo")
        self.assertTrue(isinstance(v, unicode))
Esempio n. 5
0
    def test_types(self):
        v = strip_win32_incompat_from_path("")
        self.assertTrue(isinstance(v, bytes))
        v = strip_win32_incompat_from_path("foo")
        self.assertTrue(isinstance(v, bytes))

        v = strip_win32_incompat_from_path(u"")
        self.assertTrue(isinstance(v, unicode))
        v = strip_win32_incompat_from_path(u"foo")
        self.assertTrue(isinstance(v, unicode))
Esempio n. 6
0
    def test_types(self):
        v = strip_win32_incompat_from_path(fsnative(u""))
        self.assertTrue(isinstance(v, fsnative))
        v = strip_win32_incompat_from_path(fsnative(u"foo"))
        self.assertTrue(isinstance(v, fsnative))

        v = strip_win32_incompat_from_path(u"")
        self.assertTrue(isinstance(v, text_type))
        v = strip_win32_incompat_from_path(u"foo")
        self.assertTrue(isinstance(v, text_type))
Esempio n. 7
0
    def _post(self, value, song, keep_extension=True):
        if value:
            assert isinstance(value, unicode)
            value = fsnative(value)

            if keep_extension:
                fn = song.get("~filename", ".")
                ext = fn[fn.rfind("."):].lower()
                val_ext = value[-len(ext):].lower()
                if not ext == val_ext:
                    value += ext.lower()

            if os.name == "nt":
                assert isinstance(value, unicode)
                value = strip_win32_incompat_from_path(value)

            value = expanduser(value)

            # Limit each path section to 255 (bytes on linux, chars on win).
            # http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
            path, ext = os.path.splitext(value)
            path = path.split(sep)
            limit = [255] * len(path)
            limit[-1] -= len(ext)
            elip = lambda (p, l): (len(p) > l and p[:l - 2] + "..") or p
            path = sep.join(map(elip, zip(path, limit)))
            value = path + ext

            if sep in value and not os.path.isabs(value):
                raise ValueError("Pattern is not rooted")
        return value
Esempio n. 8
0
    def _post(self, value, song, keep_extension=True):
        if value:
            assert isinstance(value, unicode)
            value = fsnative(value)

            if keep_extension:
                fn = song.get("~filename", ".")
                ext = fn[fn.rfind("."):].lower()
                val_ext = value[-len(ext):].lower()
                if not ext == val_ext:
                    value += ext.lower()

            if os.name == "nt":
                assert isinstance(value, unicode)
                value = strip_win32_incompat_from_path(value)

            value = expanduser(value)

            # Limit each path section to 255 (bytes on linux, chars on win).
            # http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
            path, ext = os.path.splitext(value)
            path = path.split(sep)
            limit = [255] * len(path)
            limit[-1] -= len(ext)
            elip = lambda (p, l): (len(p) > l and p[:l - 2] + "..") or p
            path = sep.join(map(elip, zip(path, limit)))
            value = path + ext

            if sep in value and not os.path.isabs(value):
                raise ValueError("Pattern is not rooted")
        return value
Esempio n. 9
0
    def _make_safe_name(self, input_path):
        """
        Make a file path safe by replacing unsafe characters.

        :param input_path: A relative Path.
        :return: The given path, with any unsafe characters replaced.
                 Returned as a string.
        """
        # Remove diacritics (accents)
        safe_filename = unicodedata.normalize('NFKD', str(input_path))
        safe_filename = u''.join(
            [c for c in safe_filename if not unicodedata.combining(c)])

        if os.name != "nt":
            # Ensure that Win32-incompatible chars are always removed.
            # On Windows, this is called during `FileFromPattern`.
            safe_filename = strip_win32_incompat_from_path(safe_filename)

        return safe_filename
Esempio n. 10
0
    def _post(self, value, song, keep_extension=True):
        if value:
            assert isinstance(value, unicode)
            value = fsnative(value)

            if keep_extension:
                fn = song.get("~filename", ".")
                ext = fn[fn.rfind("."):].lower()
                val_ext = value[-len(ext):].lower()
                if not ext == val_ext:
                    value += ext.lower()

            if os.name == "nt":
                assert isinstance(value, unicode)
                value = strip_win32_incompat_from_path(value)

            value = expanduser(value)
            value = limit_path(value)

            if sep in value and not os.path.isabs(value):
                raise ValueError("Pattern is not rooted")
        return value
Esempio n. 11
0
    def _post(self, value, song, keep_extension=True):
        if value:
            assert isinstance(value, unicode)
            value = fsnative(value)

            if keep_extension:
                fn = song.get("~filename", ".")
                ext = fn[fn.rfind(".") :].lower()
                val_ext = value[-len(ext) :].lower()
                if not ext == val_ext:
                    value += ext.lower()

            if os.name == "nt":
                assert isinstance(value, unicode)
                value = strip_win32_incompat_from_path(value)

            value = expanduser(value)
            value = limit_path(value)

            if sep in value and not os.path.isabs(value):
                raise ValueError("Pattern is not rooted")
        return value
Esempio n. 12
0
    def copy(self, parent_widget, song):
        if not self.__pattern:
            self.__set_pattern()

        target = strip_win32_incompat_from_path(self.__pattern.format(song))
        dirname = os.path.dirname(target)

        if os.path.exists(target):
            dialog = ConfirmFileReplace(parent_widget, target)
            resp = dialog.run()
            if resp == ConfirmFileReplace.RESPONSE_REPLACE:
                try:
                    # Remove the current song
                    self.__library.remove([self.__library[target]])
                except KeyError:
                    pass
            else:
                return False

        try:
            if not os.path.isdir(dirname):
                os.makedirs(dirname)
            shutil.copyfile(song['~filename'], target)

            if self['covers']:
                coverfile = os.path.join(dirname, 'folder.jpg')
                cover = app.cover_manager.get_cover(song)
                if cover and mtime(cover.name) > mtime(coverfile):
                    image = GdkPixbuf.Pixbuf.new_from_file_at_size(
                        cover.name, 200, 200)
                    image.savev(coverfile, "jpeg", [], [])

            song = copy.deepcopy(song)
            song.sanitize(target)
            self.__library.add([song])
            return song
        except (OSError, IOError, GLib.GError) as exc:
            encoding = util.get_locale_encoding()
            return str(exc).decode(encoding, 'replace')
Esempio n. 13
0
    def copy(self, parent_widget, song):
        if not self.__pattern:
            self.__set_pattern()

        target = strip_win32_incompat_from_path(self.__pattern.format(song))
        dirname = os.path.dirname(target)

        if os.path.exists(target):
            dialog = ConfirmFileReplace(parent_widget, target)
            resp = dialog.run()
            if resp == ConfirmFileReplace.RESPONSE_REPLACE:
                try:
                    # Remove the current song
                    self.__library.remove([self.__library[target]])
                except KeyError:
                    pass
            else:
                return False

        try:
            if not os.path.isdir(dirname):
                os.makedirs(dirname)
            shutil.copyfile(song['~filename'], target)

            if self['covers']:
                coverfile = os.path.join(dirname, 'folder.jpg')
                cover = app.cover_manager.get_cover(song)
                if cover and mtime(cover.name) > mtime(coverfile):
                    image = GdkPixbuf.Pixbuf.new_from_file_at_size(
                        cover.name, 200, 200)
                    image.savev(coverfile, "jpeg", [], [])

            song = copy.deepcopy(song)
            song.sanitize(target)
            self.__library.add([song])
            return song
        except (OSError, IOError, GLib.GError) as exc:
            encoding = util.get_locale_encoding()
            return str(exc).decode(encoding, 'replace')
Esempio n. 14
0
 def filter(self, original, filename):
     return strip_win32_incompat_from_path(filename)
Esempio n. 15
0
 def filter(self, original, filename):
     return strip_win32_incompat_from_path(filename)
Esempio n. 16
0
 def filter(self, original, filename):
     assert isinstance(filename, unicode)
     return fsdecode(strip_win32_incompat_from_path(fsencode(filename)))