def choose_filename(self, filename, attempts=<NUM_LIT>):
new_filename = filename<EOL>for attempt in range(<NUM_LIT:2>, attempts + <NUM_LIT:1>):<EOL><INDENT>if not self.contains(new_filename):<EOL><INDENT>break<EOL><DEDENT>new_filename = alternative_filename(filename, attempt)<EOL><DEDENT>else:<EOL><INDENT>while self.contains(new_filename):<EOL><INDENT>new_filename = alternative_filename(filename)<EOL><DEDENT><DEDENT>limit = self.pathconf.get('<STR_LIT>', <NUM_LIT:0>)<EOL>if limit and limit < len(filename):<EOL><INDENT>raise FilenameTooLongError(<EOL>path=self.path, filename=filename, limit=limit)<EOL><DEDENT>abspath = os.path.join(self.path, filename)<EOL>limit = self.pathconf.get('<STR_LIT>', <NUM_LIT:0>)<EOL>if limit and limit < len(abspath):<EOL><INDENT>raise PathTooLongError(path=abspath, limit=limit)<EOL><DEDENT>return new_filename<EOL>
Get a new filename which does not colide with any entry on directory,
based on given filename.

:param filename: base filename
:type filename: str
:param attempts: number of attempts, defaults to 999
:type attempts: int
:returns: filename
:rtype: str

:raises FilenameTooLong: when filesystem filename size limit is reached
:raises PathTooLong: when OS or filesystem path size limit is reached