def _nodelist_dump(ff, nodes, clients): ''' Store the counted numbers in the api-file. Sets the key ``state`` . ``nodes`` with the node number. Leaves ``state`` . ``description`` untouched, if any already present. If empty, or the pattern ``\[[\d]+ Nodes, [\d]+ Clients\]`` is matched, the numbers in the pattern will be replaced. :param ff: running :class:`ffflash.main.FFFlash` instance :param nodes: Number of online nodes :param clients: Number of their clients :return: ``True`` if :attr:`api` was modified else ``False`` ''' if not ff.access_for('nodelist'): return False modified = [] if ff.api.pull('state', 'nodes') is not None: ff.api.push(nodes, 'state', 'nodes') ff.log('stored {} in state.nodes'.format(nodes)) modified.append(True) descr = ff.api.pull('state', 'description') if descr is not None: new = '[{} Nodes, {} Clients]'.format(nodes, clients) new_descr = (replace_text(r'(\[[\d]+ Nodes, [\d]+ Clients\])', new, descr) if descr else new) ff.api.push(new_descr, 'state', 'description') ff.log('stored {} nodes and {} clients in state.description'.format( nodes, clients)) modified.append(True) return any(modified)
def test_replace_text_empy_input(): for rx, rpl, txt in [ ('', '', ''), (r'', '', ''), ('', '', 'unchanged'), (r'', '', 'unchanged'), ('', '', None), (r'', '', None), ('', None, None), (r'', None, None), (None, None, None) ]: assert replace_text(rx, rpl, txt) == txt
def test_replace_text(): assert replace_text(r'b', 'd', 'abc') == 'adc' assert replace_text(r'b', 'd', 'abbc') == 'addc' assert replace_text(r'a(.+)f', 'g', 'abcdef') == 'g' assert replace_text(r'(a.+f)', 'g', 'abcdef') == 'g' assert replace_text(r'(a(.+)f)', 'g', 'abcdef') == 'g' assert replace_text(r'\ ', '_', 'abc def') == 'abc_def' assert replace_text(r'\d{3}', '0', 'abc123') == 'abc0'
def _nodelist_dump(ff, nodes, clients): ''' Store the counted numbers in the api-file. Sets the key ``state`` . ``nodes`` with the node number. Leaves ``state`` . ``description`` untouched, if any already present. If empty, or the pattern ``\[[\d]+ Nodes, [\d]+ Clients\]`` is matched, the numbers in the pattern will be replaced. :param ff: running :class:`ffflash.main.FFFlash` instance :param nodes: Number of online nodes :param clients: Number of their clients :return: ``True`` if :attr:`api` was modified else ``False`` ''' if not ff.access_for('nodelist'): return False modified = [] if ff.api.pull('state', 'nodes') is not None: ff.api.push(nodes, 'state', 'nodes') ff.log('stored {} in state.nodes'.format(nodes)) modified.append(True) descr = ff.api.pull('state', 'description') if descr is not None: new = '[{} Nodes, {} Clients]'.format(nodes, clients) new_descr = (replace_text( r'(\[[\d]+ Nodes, [\d]+ Clients\])', new, descr ) if descr else new) ff.api.push(new_descr, 'state', 'description') ff.log('stored {} nodes and {} clients in state.description'.format( nodes, clients )) modified.append(True) return any(modified)
def test_replace_text_returns_original_on_no_match(): for rx, rpl, txt in [ (r'a', 'b', 'c'), (r'aa', 'bb', 'cc'), (r'a{2}', 'bb', 'a'), (r'.a', 'b', 'a') ]: assert replace_text(rx, rpl, txt) == txt
def test_replace_text_empy_input(): for rx, rpl, txt in [('', '', ''), (r'', '', ''), ('', '', 'unchanged'), (r'', '', 'unchanged'), ('', '', None), (r'', '', None), ('', None, None), (r'', None, None), (None, None, None)]: assert replace_text(rx, rpl, txt) == txt
def test_replace_text_returns_original_on_no_match(): for rx, rpl, txt in [(r'a', 'b', 'c'), (r'aa', 'bb', 'cc'), (r'a{2}', 'bb', 'a'), (r'.a', 'b', 'a')]: assert replace_text(rx, rpl, txt) == txt