Exemplo n.º 1
0
 def move_to_directory(self, directory):
     check_f(directory)
     if self.channelName:
         channel_name = filter_directory_name(self.channelName)
         # bug 10769: shutil and windows has problems with long
         # filenames, so we clip the directory name.
         if len(channel_name) > 80:
             channel_name = channel_name[:80]
         directory = os.path.join(directory, channel_name)
         if not os.path.exists(directory):
             try:
                 fileutil.makedirs(directory)
             except (SystemExit, KeyboardInterrupt):
                 raise
             except:
                 pass
     newfilename = os.path.join(directory, self.shortFilename)
     if newfilename == self.filename:
         return
     newfilename, fp = next_free_filename(newfilename)
     def callback():
         self.filename = newfilename
         self.update_client()
     fileutil.migrate_file(self.filename, newfilename, callback)
     fp.close()
Exemplo n.º 2
0
def migrate_download(dlid, directory):
    check_f(directory)
    try:
        download = _downloads[dlid]
    except KeyError:
        # There is no download with this id
        return

    if download.state in (u"finished", u"uploading", u"uploading-paused"):
        download.move_to_directory(directory)
Exemplo n.º 3
0
        def test_name(text):
            correct_type = FilenameType(text)
            util.check_f(correct_type)

            if sys.platform == 'win32':
                incorrect_type = str(text)
            else:
                incorrect_type = unicode(text)

            self.assertRaises(util.MiroUnicodeError, util.check_f,
                              incorrect_type)
Exemplo n.º 4
0
def check_filename_extension(filename, content_type):
    """If a filename doesn't have an extension, this tries to find a
    suitable one based on the HTTP content-type info and add it if one
    is available.
    """
    check_f(filename)
    if content_type is not None and not filetypes.is_allowed_filename(filename):
        guessed_ext = filetypes.guess_extension(content_type)
        if guessed_ext is not None:
            filename += guessed_ext
    return filename
Exemplo n.º 5
0
        def test_name(text):
            correct_type = FilenameType(text)
            util.check_f(correct_type)

            if sys.platform == 'win32':
                incorrect_type = str(text)
            else:
                incorrect_type = unicode(text)

            self.assertRaises(util.MiroUnicodeError,
                              util.check_f, incorrect_type)
Exemplo n.º 6
0
        def testName(text):
            from miro.plat.utils import FilenameType

            correctType = FilenameType(text)
            util.check_f(correctType)

            incorrectType = text
            if FilenameType == str:
                incorrectType = unicode(text)

            self.assertRaises(util.MiroUnicodeError, util.check_f, incorrectType)
Exemplo n.º 7
0
def check_filename_extension(filename, content_type):
    """If a filename doesn't have an extension, this tries to find a
    suitable one based on the HTTP content-type info and add it if one
    is available.
    """
    check_f(filename)
    if content_type is not None and not filetypes.is_allowed_filename(filename):
        guessed_ext = filetypes.guess_extension(content_type)
        if guessed_ext is not None:
            filename += guessed_ext
    return filename
Exemplo n.º 8
0
def start_new_download(url, dlid, contentType, channelName):
    """Creates a new downloader object.

    Returns id on success, None on failure.
    """
    check_u(url)
    check_u(contentType)
    if channelName:
        check_f(channelName)
    dl = create_downloader(url, contentType, dlid)
    dl.channelName = channelName
    _downloads[dlid] = dl
Exemplo n.º 9
0
    def test_check_f(self):
        def test_name(text):
            correct_type = FilenameType(text)
            util.check_f(correct_type)

            if sys.platform == 'win32':
                incorrect_type = str(text)
            else:
                incorrect_type = unicode(text)

            self.assertRaises(util.MiroUnicodeError, util.check_f,
                              incorrect_type)

        util.check_f(None)
        test_name("")
        test_name("abc.txt")
        test_name("./xyz.avi")
Exemplo n.º 10
0
    def test_check_f(self):
        def test_name(text):
            correct_type = FilenameType(text)
            util.check_f(correct_type)

            if sys.platform == 'win32':
                incorrect_type = str(text)
            else:
                incorrect_type = unicode(text)

            self.assertRaises(util.MiroUnicodeError,
                              util.check_f, incorrect_type)

        util.check_f(None)
        test_name("")
        test_name("abc.txt")
        test_name("./xyz.avi")
Exemplo n.º 11
0
    def test_check_f(self):

        def testName(text):
            from miro.plat.utils import FilenameType

            correctType = FilenameType(text)
            util.check_f(correctType)

            incorrectType = text
            if FilenameType == str:
                incorrectType = unicode(text)

            self.assertRaises(util.MiroUnicodeError, util.check_f, incorrectType)

        util.check_f(None)
        testName("")
        testName("abc.txt")
        testName("./xyz.avi")
Exemplo n.º 12
0
def next_free_filename(name):
    """Finds a filename that's unused and similar the the file we want
    to download and returns an open file handle to it.
    """ 
    check_f(name)
    mask = os.O_CREAT | os.O_EXCL | os.O_RDWR
    # Try with the name supplied.
    try:
        fd = os.open(expand_filename(name), mask)
        fp = os.fdopen(fd, 'wb')
        return expand_filename(name), fp
    except OSError:
        pass

    # Boh boh ... did't work.  Let's try to create a variant name and 
    # open that instead.
    parts = name.split('.')
    count = 1
    if len(parts) == 1:
        newname = "%s.%s" % (name, count)
        while True:
            try:
                fd = os.open(expand_filename(newname), mask)
                fp = os.fdopen(fd, 'wb')
                break
            except OSError:
                count += 1
                newname = "%s.%s" % (name, count)
                continue
    else:
        parts[-1:-1] = [str(count)]
        newname = '.'.join(parts)
        while True:
            try:
                fd = os.open(expand_filename(newname), mask)
                fp = os.fdopen(fd, 'wb')
                break
            except OSError:
                count += 1
                parts[-2] = str(count)
                newname = '.'.join(parts)
                continue
    return (expand_filename(newname), fp)
Exemplo n.º 13
0
def next_free_filename(name):
    """Finds a filename that's unused and similar the the file we want
    to download and returns an open file handle to it.
    """ 
    check_f(name)
    mask = os.O_CREAT | os.O_EXCL | os.O_RDWR
    # On Windows we need to pass in O_BINARY, fdopen() even with 'b' 
    # specified is not sufficient.
    if sys.platform == 'win32':
        mask |= os.O_BINARY

    candidates = next_free_filename_candidates(name)
    while True:
        # Try with the name supplied.
        newname = candidates.next()
        try:
            fd = os.open(expand_filename(newname), mask)
            fp = os.fdopen(fd, 'wb')
            return expand_filename(newname), fp
        except OSError:
            continue
    return (expand_filename(newname), fp)
Exemplo n.º 14
0
 def set_channel_name(self, channel_name):
     if self.channel_name is None:
         if channel_name:
             check_f(channel_name)
         self.channel_name = channel_name
Exemplo n.º 15
0
 def set_channel_name(self, channel_name):
     if self.channel_name is None:
         if channel_name:
             check_f(channel_name)
         self.channel_name = channel_name