Return a default value

This commit is contained in:
Miroslav Kovar 2019-10-19 19:44:32 +02:00 committed by Lex Berezhny
parent 5501418727
commit bc04903741
3 changed files with 20 additions and 10 deletions

View file

@ -58,14 +58,15 @@ def file_reader(file_path: str):
offset += bytes_to_read
def sanitize_file_name(dirty_name: str):
def sanitize_file_name(dirty_name: str, default_file_name: str = 'lbry_download'):
file_name, ext = os.path.splitext(dirty_name)
file_name = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', file_name)
ext = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', ext)
if not file_name:
log.warning('Unable to suggest a file name for %s', dirty_name)
elif len(ext) > 1:
log.warning('Unable to sanitize file name for %s, returning default value %s', dirty_name, default_file_name)
file_name = default_file_name
if len(ext) > 1:
file_name += ext
return file_name

View file

@ -198,10 +198,19 @@ class CommandTestCase(IntegrationTestCase):
return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result']
def create_tempfile(self, data=None, prefix=None, suffix=None):
self._file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix)
self._file.write(data)
self._file.flush()
return self._file.name
file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix)
# tempfile throws FileNotFoundError when file is deleted before it's closed
def cleanup():
try:
file.close()
except FileNotFoundError:
pass
self.addCleanup(cleanup)
file.write(data)
file.flush()
return file.name
async def stream_create(self, name='hovercraft', bid='1.0', data=b'hi!', confirm=True,
prefix=None, suffix=None, **kwargs):

View file

@ -83,9 +83,9 @@ class TestStreamDescriptor(AsyncioTestCase):
self.assertEqual(sanitize_file_name('end_dot .'), 'end_dot')
self.assertEqual(sanitize_file_name('.file\0\0'), '.file')
self.assertEqual(sanitize_file_name('test n\16ame.ext'), 'test name.ext')
self.assertEqual(sanitize_file_name('COM8'), '')
self.assertEqual(sanitize_file_name('LPT2'), '')
self.assertEqual(sanitize_file_name(''), '')
self.assertEqual(sanitize_file_name('COM8.ext', default_file_name='default1'), 'default1.ext')
self.assertEqual(sanitize_file_name('LPT2', default_file_name='default2'), 'default2')
self.assertEqual(sanitize_file_name('', default_file_name=''), '')
class TestRecoverOldStreamDescriptors(AsyncioTestCase):