X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fclient%2Fcandidates.py;h=c36e19919e098f1cc67c1d22beeabda22e7dd83c;hb=04a3f20dc71bf8b3f96b1e3172623aa346a638a7;hp=830e24d8bbe70b707ffbd648f102fb018bd940a8;hpb=fad16c7d54b658b37a9b42fbee47b0d4f51cb8ec;p=sfa.git diff --git a/sfa/client/candidates.py b/sfa/client/candidates.py index 830e24d8..c36e1991 100644 --- a/sfa/client/candidates.py +++ b/sfa/client/candidates.py @@ -1,52 +1,64 @@ from __future__ import print_function -### utility to match command-line args to names +# utility to match command-line args to names + + class Candidates: - def __init__ (self, names): - self.names=names + + def __init__(self, names): + self.names = names # is an input string acceptable for one of the known names? + @staticmethod - def fits (input, name): - return name.find(input)==0 + def fits(input, name): + return name.find(input) == 0 # returns one of the names if the input name has a unique match # or None otherwise - def only_match (self, input): - if input in self.names: return input - matches=[ name for name in self.names if Candidates.fits(input,name) ] - if len(matches)==1: return matches[0] - else: return None - -#################### minimal test -candidates_specs=[ -('create delete reset resources slices start status stop version create_gid', - [ ('ver','version'), - ('r',None), - ('re',None), - ('res',None), - ('rese','reset'), - ('reset','reset'), - ('reso','resources'), - ('sli','slices'), - ('st',None), - ('sta',None), - ('stop','stop'), - ('a',None), - ('cre',None), - ('create','create'), - ('create_','create_gid'), - ('create_g','create_gid'), - ('create_gi','create_gid'), - ('create_gid','create_gid'), -]) + + def only_match(self, input): + if input in self.names: + return input + matches = [name for name in self.names if Candidates.fits(input, name)] + if len(matches) == 1: + return matches[0] + else: + return None + +# minimal test +candidates_specs = [ + ('create delete reset resources slices start status stop version create_gid', + [('ver', 'version'), + ('r', None), + ('re', None), + ('res', None), + ('rese', 'reset'), + ('reset', 'reset'), + ('reso', 'resources'), + ('sli', 'slices'), + ('st', None), + ('sta', None), + ('stop', 'stop'), + ('a', None), + ('cre', None), + ('create', 'create'), + ('create_', 'create_gid'), + ('create_g', 'create_gid'), + ('create_gi', 'create_gid'), + ('create_gid', 'create_gid'), + ]) ] -def test_candidates (): + +def test_candidates(): for (names, tuples) in candidates_specs: - names=names.split() - for (input,expected) in tuples: - got=Candidates(names).only_match(input) - if got==expected: print('.', end=' ') - else: print('X FAIL','names[',names,'] input',input,'expected',expected,'got',got) + names = names.split() + for (input, expected) in tuples: + got = Candidates(names).only_match(input) + if got == expected: + print('.', end=' ') + else: + print('X FAIL', 'names[', names, '] input', + input, 'expected', expected, 'got', got) if __name__ == '__main__': test_candidates()