def test_check_file_extension(): for ext in ['txt', '.txt', 'TXT', '.TXT', 'tXt', '.tXt', 'TxT', '.TxT']: assert check_file_extension('file.txt', ext) == ('file', '.txt') assert check_file_extension('file.json', 'txt', 'yaml') == (None, None) assert check_file_extension('file.json', 'txt', 'yaml', 'json') == ('file', '.json')
def test_check_file_extension_dir(tmpdir): assert check_file_extension(str(tmpdir.join('file.txt')), 'txt') == ('file', '.txt') assert check_file_extension(str(tmpdir.join('file.txt')), 'json') == (None, None) assert check_file_extension(str(tmpdir.join('file.txt'))) == (None, None) assert check_file_extension(str(tmpdir.join('out')), 'txt') == (None, None) assert tmpdir.remove() is None
def test_check_file_extension(): for ext in ['txt', '.txt', 'TXT', '.TXT', 'tXt', '.tXt', 'TxT', '.TxT']: assert check_file_extension('file.txt', ext) == ('file', '.txt') assert check_file_extension( 'file.json', 'txt', 'yaml' ) == (None, None) assert check_file_extension( 'file.json', 'txt', 'yaml', 'json' ) == ('file', '.json')
def test_check_file_extension_dir(tmpdir): assert check_file_extension( str(tmpdir.join('file.txt')), 'txt' ) == ('file', '.txt') assert check_file_extension( str(tmpdir.join('file.txt')), 'json' ) == (None, None) assert check_file_extension( str(tmpdir.join('file.txt')) ) == (None, None) assert check_file_extension( str(tmpdir.join('out')), 'txt' ) == (None, None) assert tmpdir.remove() is None
def _sidecar_path(ff, sc): ''' Check passed sidecars for valid paths, format (*json* or *yaml*) and for valid filenames (no double dots). :param ff: running :class:`ffflash.main.FFFlash` instance :param sc: sidecar as passed by user :return: Tuple of either (``False``, ``None``, ``None``) on error or: * normalized and full path to ``sc`` * unvalidated key-names into api-file * ``True`` if ``sc`` is a *yaml* file, ``False`` if it's *json* ''' ff.log('handling sidecar {}'.format(sc)) sidepath = check_file_location(sc) if not sidepath: return ff.log('sidecar {} is either a folder, or parent folder does ' 'not exist yet skipping'.format(sc), level=False), None, None name, ext = check_file_extension(sidepath, 'yaml', 'json') if not all([name, ext]): return ff.log('sidecar {} is neither json nor yaml'.format(sc), level=False), None, None fields = name.split('.') if not all([f for f in fields]): return ff.log('sidecar {} {} name is invalid ' '(check for double dots)'.format(sc, name), level=False), None, None ff.log('sidecar path {} is valid. got {}'.format(sidepath, fields)) return sidepath, fields, (True if ext == '.yaml' else False)
def test_check_file_extension_no_ext(): assert check_file_extension('file') == (None, None) assert check_file_extension('file', '') == (None, None) assert check_file_extension('file.txt') == (None, None) assert check_file_extension('file.txt', '') == (None, None) assert check_file_extension('file.txt', '.') == (None, None) assert check_file_extension('file.txt', '..') == (None, None) assert check_file_extension('file.txt', 'file') == (None, None) assert check_file_extension('file', 'file') == (None, None)
def _rankfile_load(ff): ''' Load either existing ``rankfile`` from disk, or create empty stub if one does not exist yet. Path and extension (*json*) get validated. :param ff: running :class:`ffflash.main.FFFlash` instance :return: Tuple of either (``False``, ``None``) on error or: * validated path to the ``rankfile`` * ``rankfile`` content ''' if not ff.access_for('rankfile'): return (False, None) ff.log('handling rankfile {}'.format(ff.args.rankfile)) rankfile = check_file_location(ff.args.rankfile, must_exist=False) if not rankfile: return ff.log( 'wrong path for rankfile {}'.format(ff.args.rankfile), level=False ), None if not all(check_file_extension(rankfile, 'json')): return ff.log( 'rankfile {} is no json'.format(rankfile), level=False ), None ranks = load_file(rankfile, fallback={ 'updated_at': 'never', 'nodes': [] }, as_yaml=False) if not ranks or not isinstance(ranks, dict): return ff.log( 'could not load rankfile {}'.format(rankfile), level=False ), None if not all([(a in ranks) for a in ['nodes', 'updated_at']]): return ff.log( 'this is no rankfile {}'.format(rankfile), level=False ), None lranks = len(ranks.get('nodes', 0)) ff.log(( 'creating new rankfile {}'.format(rankfile) if lranks == 0 else 'loaded {} nodes'.format(lranks) )) return rankfile, ranks
def _sidecar_path(ff, sc): ''' Check passed sidecars for valid paths, format (*json* or *yaml*) and for valid filenames (no double dots). :param ff: running :class:`ffflash.main.FFFlash` instance :param sc: sidecar as passed by user :return: Tuple of either (``False``, ``None``, ``None``) on error or: * normalized and full path to ``sc`` * unvalidated key-names into api-file * ``True`` if ``sc`` is a *yaml* file, ``False`` if it's *json* ''' ff.log('handling sidecar {}'.format(sc)) sidepath = check_file_location(sc) if not sidepath: return ff.log( 'sidecar {} is either a folder, or parent folder does ' 'not exist yet skipping'.format(sc), level=False ), None, None name, ext = check_file_extension(sidepath, 'yaml', 'json') if not all([name, ext]): return ff.log( 'sidecar {} is neither json nor yaml'.format(sc), level=False ), None, None fields = name.split('.') if not all([f for f in fields]): return ff.log( 'sidecar {} {} name is invalid ' '(check for double dots)'.format(sc, name), level=False ), None, None ff.log('sidecar path {} is valid. got {}'.format(sidepath, fields)) return sidepath, fields, (True if ext == '.yaml' else False)