forked from LBRYCommunity/lbry-sdk
Return a default value
This commit is contained in:
parent
5501418727
commit
bc04903741
3 changed files with 20 additions and 10 deletions
|
@ -58,14 +58,15 @@ def file_reader(file_path: str):
|
||||||
offset += bytes_to_read
|
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, ext = os.path.splitext(dirty_name)
|
||||||
file_name = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', file_name)
|
file_name = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', file_name)
|
||||||
ext = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', ext)
|
ext = re.sub(RE_ILLEGAL_FILENAME_CHARS, '', ext)
|
||||||
|
|
||||||
if not file_name:
|
if not file_name:
|
||||||
log.warning('Unable to suggest a file name for %s', dirty_name)
|
log.warning('Unable to sanitize file name for %s, returning default value %s', dirty_name, default_file_name)
|
||||||
elif len(ext) > 1:
|
file_name = default_file_name
|
||||||
|
if len(ext) > 1:
|
||||||
file_name += ext
|
file_name += ext
|
||||||
|
|
||||||
return file_name
|
return file_name
|
||||||
|
|
|
@ -198,10 +198,19 @@ class CommandTestCase(IntegrationTestCase):
|
||||||
return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result']
|
return json.loads(jsonrpc_dumps_pretty(value, ledger=self.ledger))['result']
|
||||||
|
|
||||||
def create_tempfile(self, data=None, prefix=None, suffix=None):
|
def create_tempfile(self, data=None, prefix=None, suffix=None):
|
||||||
self._file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix)
|
file = tempfile.NamedTemporaryFile(prefix=prefix, suffix=suffix)
|
||||||
self._file.write(data)
|
|
||||||
self._file.flush()
|
# tempfile throws FileNotFoundError when file is deleted before it's closed
|
||||||
return self._file.name
|
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,
|
async def stream_create(self, name='hovercraft', bid='1.0', data=b'hi!', confirm=True,
|
||||||
prefix=None, suffix=None, **kwargs):
|
prefix=None, suffix=None, **kwargs):
|
||||||
|
|
|
@ -83,9 +83,9 @@ class TestStreamDescriptor(AsyncioTestCase):
|
||||||
self.assertEqual(sanitize_file_name('end_dot .'), 'end_dot')
|
self.assertEqual(sanitize_file_name('end_dot .'), 'end_dot')
|
||||||
self.assertEqual(sanitize_file_name('.file\0\0'), '.file')
|
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('test n\16ame.ext'), 'test name.ext')
|
||||||
self.assertEqual(sanitize_file_name('COM8'), '')
|
self.assertEqual(sanitize_file_name('COM8.ext', default_file_name='default1'), 'default1.ext')
|
||||||
self.assertEqual(sanitize_file_name('LPT2'), '')
|
self.assertEqual(sanitize_file_name('LPT2', default_file_name='default2'), 'default2')
|
||||||
self.assertEqual(sanitize_file_name(''), '')
|
self.assertEqual(sanitize_file_name('', default_file_name=''), '')
|
||||||
|
|
||||||
|
|
||||||
class TestRecoverOldStreamDescriptors(AsyncioTestCase):
|
class TestRecoverOldStreamDescriptors(AsyncioTestCase):
|
||||||
|
|
Loading…
Reference in a new issue