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()
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)
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)
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
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)
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
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")
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")
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)
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)
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