1 # a few utilities common to sfi and sfaadmin
3 def optparse_listvalue_callback(option, opt, value, parser):
4 former=getattr(parser.values,option.dest)
5 if not former: former=[]
6 # support for using e.g. sfi update -t slice -x the.slice.hrn -r none
7 # instead of -r '' which is painful and does not pass well through ssh
8 if value.lower()=='none':
11 newvalue=former+value.split(',')
12 setattr(parser.values, option.dest, newvalue)
14 def optparse_dictvalue_callback (option, option_string, value, parser):
16 (k,v)=value.split('=',1)
17 d=getattr(parser.values, option.dest)
23 # a code fragment that could be helpful for argparse which unfortunately is
24 # available with 2.7 only, so this feels like too strong a requirement for the client side
25 #class ExtraArgAction (argparse.Action):
26 # def __call__ (self, parser, namespace, values, option_string=None):
27 # would need a try/except of course
28 # (k,v)=values.split('=')
29 # d=getattr(namespace,self.dest)
32 #parser.add_argument ("-X","--extra",dest='extras', default={}, action=ExtraArgAction,
33 # help="set extra flags, testbed dependent, e.g. --extra enabled=true")
35 ##############################
36 # these are not needed from the outside
37 def terminal_render_plural (how_many, name,names=None):
38 if not names: names="%ss"%name
39 if how_many<=0: return "No %s"%name
40 elif how_many==1: return "1 %s"%name
41 else: return "%d %s"%(how_many,names)
43 def terminal_render_default (record,options):
44 print "%s (%s)" % (record['hrn'], record['type'])
45 def terminal_render_user (record, options):
46 print "%s (User)"%record['hrn'],
47 if options.verbose and record.get('email', None):
48 print "email='{}'".format(record['email']),
49 if record.get('reg-pi-authorities', None):
50 print " [PI at %s]"%(" and ".join(record['reg-pi-authorities'])),
51 if record.get('reg-slices', None):
52 print " [IN slices %s]"%(" and ".join(record['reg-slices'])),
53 user_keys=record.get('reg-keys',[])
54 if not options.verbose:
55 print " [has %s]"%(terminal_render_plural(len(user_keys),"key"))
58 for key in user_keys: print 8*' ',key.strip("\n")
60 def terminal_render_slice (record, options):
61 print "%s (Slice)"%record['hrn'],
62 if record.get('reg-researchers', None):
63 print " [USERS %s]"%(" and ".join(record['reg-researchers'])),
66 def terminal_render_authority (record, options):
67 print "%s (Authority)"%record['hrn'],
68 if options.verbose and record.get('name'):
69 print "name='{}'".format(record['name'])
70 if record.get('reg-pis', None):
71 print " [PIS %s]"%(" and ".join(record['reg-pis'])),
73 def terminal_render_node (record, options):
74 print "%s (Node)"%record['hrn']
78 def terminal_render (records, options):
79 # sort records by type
81 for record in records:
83 if type not in grouped_by_type:
84 grouped_by_type[type]=[]
85 grouped_by_type[type].append(record)
86 group_types = grouped_by_type.keys()
88 for type in group_types:
89 group = grouped_by_type[type]
90 # print 20 * '-', type
91 try: renderer = eval('terminal_render_' + type)
92 except: renderer = terminal_render_default
94 renderer(record, options)
97 def filter_records(type, records):
99 for record in records:
100 if (record['type'] == type) or (type == "all"):
101 filtered_records.append(record)
102 return filtered_records