Example #1
0
def _nodelist_fetch(ff):
    '''
    Determines if ``--nodelist`` was a file or a url, and tries to fetch it.
    Validates nodelist to be json and to have the *version*, *nodes* and
    *updated_at* keys.

    :param ff: running :class:`ffflash.main.FFFlash` instance
    :return: the unpickled nodelist or ``False``/``None`` on error
    '''
    if not ff.access_for('nodelist'):
        return False

    ff.log('fetching nodelist {}'.format(ff.args.nodelist))

    nodelist = (load_file(ff.args.nodelist, fallback=None, as_yaml=False)
                if check_file_location(ff.args.nodelist, must_exist=True) else
                fetch_www_struct(
                    ff.args.nodelist, fallback=None, as_yaml=False))
    if not nodelist or not isinstance(nodelist, dict):
        return ff.log('could not fetch nodelist {}'.format(ff.args.nodelist),
                      level=False)

    if not all([(a in nodelist) for a in ['version', 'nodes', 'updated_at']]):
        return ff.log('this is no nodelist {}'.format(ff.args.nodelist),
                      level=False)

    ff.log('successfully fetched nodelist from {}'.format(ff.args.nodelist))
    return nodelist
Example #2
0
def _sidecar_load(ff, sidepath, fields, as_yaml):
    '''
    Loads content from ``sidepath`` if it exists, otherwise returns the values
    from the :attr:`api` instead.
    This is only done, if ``fields`` exist in :attr:`api`.

    :param ff: running :class:`ffflash.main.FFFlash` instance
    :param sidepath: full path to the sidecar
    :param fields: key-names into api-file
    :param as_yaml: load as *yaml* instead of *json*
    :return: The loaded content of ``sidepath`` or ``False``/``None`` on error
    '''
    if not ff.access_for('sidecars'):
        return False

    ff.log('searching for {}'.format('.'.join(fields)))

    apicont = ff.api.pull(*fields)
    if apicont is None:
        return ff.log('{} does not exist. skipping'.format('.'.join(fields)),
                      level=None)

    sidecont = load_file(sidepath, as_yaml=as_yaml)
    if sidecont is None:
        ff.log('sidecar {} does not exit yet. pulled data from api'.format(
            '.'.join(fields)))
        return apicont

    return merge_dicts(apicont, sidecont)
Example #3
0
def _sidecar_load(ff, sidepath, fields, as_yaml):
    '''
    Loads content from ``sidepath`` if it exists, otherwise returns the values
    from the :attr:`api` instead.
    This is only done, if ``fields`` exist in :attr:`api`.

    :param ff: running :class:`ffflash.main.FFFlash` instance
    :param sidepath: full path to the sidecar
    :param fields: key-names into api-file
    :param as_yaml: load as *yaml* instead of *json*
    :return: The loaded content of ``sidepath`` or ``False``/``None`` on error
    '''
    if not ff.access_for('sidecars'):
        return False

    ff.log('searching for {}'.format('.'.join(fields)))

    apicont = ff.api.pull(*fields)
    if apicont is None:
        return ff.log(
            '{} does not exist. skipping'.format('.'.join(fields)),
            level=None
        )

    sidecont = load_file(sidepath, as_yaml=as_yaml)
    if sidecont is None:
        ff.log('sidecar {} does not exit yet. pulled data from api'.format(
            '.'.join(fields)
        ))
        return apicont

    return merge_dicts(apicont, sidecont)
Example #4
0
def _nodelist_fetch(ff):
    '''
    Determines if ``--nodelist`` was a file or a url, and tries to fetch it.
    Validates nodelist to be json and to have the *version*, *nodes* and
    *updated_at* keys.

    :param ff: running :class:`ffflash.main.FFFlash` instance
    :return: the unpickled nodelist or ``False``/``None`` on error
    '''
    if not ff.access_for('nodelist'):
        return False

    ff.log('fetching nodelist {}'.format(ff.args.nodelist))

    nodelist = (
        load_file(ff.args.nodelist, fallback=None, as_yaml=False)
        if check_file_location(ff.args.nodelist, must_exist=True) else
        fetch_www_struct(ff.args.nodelist, fallback=None, as_yaml=False)
    )
    if not nodelist or not isinstance(nodelist, dict):
        return ff.log(
            'could not fetch nodelist {}'.format(ff.args.nodelist),
            level=False
        )

    if not all([(a in nodelist) for a in ['version', 'nodes', 'updated_at']]):
        return ff.log(
            'this is no nodelist {}'.format(ff.args.nodelist),
            level=False
        )

    ff.log('successfully fetched nodelist from {}'.format(ff.args.nodelist))
    return nodelist
Example #5
0
    def load_api(self):
        '''
        Populate :attr:`api` with :class:`ffflash.lib.api.FFApi` with content
        loaded from :attr:`location`.

        :attr:`api` is populated only once, this prevents accidental reloads.
        '''
        if (self.api is None) and self.location:
            c = load_file(self.location, as_yaml=False)
            if c:
                self.api = FFApi(c)
Example #6
0
    def load_api(self):
        '''
        Populate :attr:`api` with :class:`ffflash.lib.api.FFApi` with content
        loaded from :attr:`location`.

        :attr:`api` is populated only once, this prevents accidental reloads.
        '''
        if (self.api is None) and self.location:
            c = load_file(self.location, as_yaml=False)
            if c:
                self.api = FFApi(c)
Example #7
0
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
Example #8
0
def ly(loc, fb=None):
    return load_file(loc, fallback=fb, as_yaml=True)
Example #9
0
def lj(loc, fb=None):
    return load_file(loc, fallback=fb, as_yaml=False)