1 # a few utilities common to sfi and sfaadmin
3 from __future__ import print_function
6 def optparse_listvalue_callback(option, opt, value, parser):
7 former = getattr(parser.values, option.dest)
10 # support for using e.g. sfi update -t slice -x the.slice.hrn -r none
11 # instead of -r '' which is painful and does not pass well through ssh
12 if value.lower() == 'none':
15 newvalue = former + value.split(',')
16 setattr(parser.values, option.dest, newvalue)
19 def optparse_dictvalue_callback(option, option_string, value, parser):
21 (k, v) = value.split('=', 1)
22 d = getattr(parser.values, option.dest)
28 # a code fragment that could be helpful for argparse which unfortunately is
29 # available with 2.7 only, so this feels like too strong a requirement for the client side
30 # class ExtraArgAction (argparse.Action):
31 # def __call__ (self, parser, namespace, values, option_string=None):
32 # would need a try/except of course
33 # (k, v) = values.split('=')
34 # d = getattr(namespace, self.dest)
37 # parser.add_argument ("-X", "--extra", dest='extras', default={}, action=ExtraArgAction,
38 # help="set extra flags, testbed dependent, e.g. --extra enabled=true")
40 ##############################
41 # these are not needed from the outside
44 def terminal_render_plural(how_many, name, names=None):
52 return "%d %s" % (how_many, names)
55 def terminal_render_default(record, options):
56 print("%s (%s)" % (record['hrn'], record['type']))
59 def terminal_render_user(record, options):
60 print("%s (User)" % record['hrn'], end=' ')
61 if options.verbose and record.get('email', None):
62 print("email='{}'".format(record['email']), end=' ')
63 if record.get('reg-pi-authorities', None):
65 (" and ".join(record['reg-pi-authorities'])), end=' ')
66 if record.get('reg-slices', None):
67 print(" [IN slices %s]" %
68 (" and ".join(record['reg-slices'])), end=' ')
69 user_keys = record.get('reg-keys', [])
70 if not options.verbose:
71 print(" [has %s]" % (terminal_render_plural(len(user_keys), "key")))
75 print(8 * ' ', key.strip("\n"))
78 def terminal_render_slice(record, options):
79 print("%s (Slice)" % record['hrn'], end=' ')
80 if record.get('reg-researchers', None):
82 (" and ".join(record['reg-researchers'])), end=' ')
87 def terminal_render_authority(record, options):
88 print("%s (Authority)" % record['hrn'], end=' ')
89 if options.verbose and record.get('name'):
90 print("name='{}'".format(record['name']))
91 if record.get('reg-pis', None):
92 print(" [PIS %s]" % (" and ".join(record['reg-pis'])), end=' ')
96 def terminal_render_node(record, options):
97 print("%s (Node)" % record['hrn'])
101 def terminal_render(records, options):
102 # sort records by type
104 for record in records:
105 type = record['type']
106 if type not in grouped_by_type:
107 grouped_by_type[type] = []
108 grouped_by_type[type].append(record)
109 group_types = grouped_by_type.keys()
111 for type in group_types:
112 group = grouped_by_type[type]
113 # print 20 * '-', type
115 renderer = eval('terminal_render_' + type)
117 renderer = terminal_render_default
119 renderer(record, options)
124 def filter_records(type, records):
125 filtered_records = []
126 for record in records:
127 if (record['type'] == type) or (type == "all"):
128 filtered_records.append(record)
129 return filtered_records