예제 #1
0
def test_check_file_location(tmpdir):
    ne = tmpdir.join('does_not_exist')
    de = tmpdir.ensure('does_exist')

    assert check_file_location(str(ne), must_exist=False) == str(ne)
    assert check_file_location(str(ne), must_exist=True) is None

    assert check_file_location(str(de), must_exist=False) == str(de)
    assert check_file_location(str(de), must_exist=True) == str(de)

    assert tmpdir.remove() is None
예제 #2
0
def test_check_file_location_on_folders(tmpdir):
    pf = tmpdir.join('parent')
    ne = pf.join('does_not_exist')

    assert tmpdir.listdir() == []

    assert check_file_location(str(ne), must_exist=False) is None
    assert check_file_location(str(ne), must_exist=True) is None

    assert check_file_location(str(pf), must_exist=False) == str(pf)
    assert check_file_location(str(pf), must_exist=True) is None

    assert tmpdir.remove() is None
예제 #3
0
파일: sidecars.py 프로젝트: spookey/ffflash
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)
예제 #4
0
파일: rankfile.py 프로젝트: spookey/ffflash
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
예제 #5
0
def write_file(location, data):
    '''
    Write string data into files

    :param location: filename where to write to
    :param data: content to write into ``filename``
    :return: ``data`` if successful
    '''
    location = check_file_location(location)
    if location and (data is not None):
        with c_open(location, 'w', encoding='utf-8') as wl:
            wl.write(data)
            return data
예제 #6
0
def write_file(location, data):
    '''
    Write string data into files

    :param location: filename where to write to
    :param data: content to write into ``filename``
    :return: ``data`` if successful
    '''
    location = check_file_location(location)
    if location and (data is not None):
        with c_open(location, 'w', encoding='utf-8') as wl:
            wl.write(data)
            return data
예제 #7
0
def read_file(location, fallback=None):
    '''
    Read string data from files

    :param location: filename where to write to
    :param fallback: data to return in case of read failure
    :return: read data from ``location`` if successful else ``fallback``
    '''
    location = check_file_location(location, must_exist=True)
    if location:
        with c_open(location, 'r', encoding='utf-8') as rl:
            data = rl.read()
            if data is not None:
                return data
    return fallback
예제 #8
0
def read_file(location, fallback=None):
    '''
    Read string data from files

    :param location: filename where to write to
    :param fallback: data to return in case of read failure
    :return: read data from ``location`` if successful else ``fallback``
    '''
    location = check_file_location(location, must_exist=True)
    if location:
        with c_open(location, 'r', encoding='utf-8') as rl:
            data = rl.read()
            if data is not None:
                return data
    return fallback
예제 #9
0
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)
예제 #10
0
def test_check_file_location_empty():
    assert check_file_location('', must_exist=True) is None
    assert check_file_location('', must_exist=False) is None