python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / client / common.py
1 # a few utilities common to sfi and sfaadmin
2
3
4
5
6 def optparse_listvalue_callback(option, opt, value, parser):
7     former = getattr(parser.values, option.dest)
8     if not former:
9         former = []
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':
13         newvalue = former
14     else:
15         newvalue = former + value.split(',')
16     setattr(parser.values, option.dest, newvalue)
17
18
19 def optparse_dictvalue_callback(option, option_string, value, parser):
20     try:
21         (k, v) = value.split('=', 1)
22         d = getattr(parser.values, option.dest)
23         d[k] = v
24     except:
25         parser.print_help()
26         sys.exit(1)
27
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)
35 #        d[k] = v
36 #####
37 # parser.add_argument ("-X", "--extra", dest='extras', default={}, action=ExtraArgAction,
38 # help="set extra flags, testbed dependent, e.g. --extra enabled=true")
39
40 ##############################
41 # these are not needed from the outside
42
43
44 def terminal_render_plural(how_many, name, names=None):
45     if not names:
46         names = "%ss" % name
47     if how_many <= 0:
48         return "No %s" % name
49     elif how_many == 1:
50         return "1 %s" % name
51     else:
52         return "%d %s" % (how_many, names)
53
54
55 def terminal_render_default(record, options):
56     print("%s (%s)" % (record['hrn'], record['type']))
57
58
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):
64         print(" [PI at %s]" %
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")))
72     else:
73         print("")
74         for key in user_keys:
75             print(8 * ' ', key.strip("\n"))
76
77
78 def terminal_render_slice(record, options):
79     print("%s (Slice)" % record['hrn'], end=' ')
80     if record.get('reg-researchers', None):
81         print(" [USERS %s]" %
82               (" and ".join(record['reg-researchers'])), end=' ')
83 #    print record.keys()
84     print("")
85
86
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=' ')
93     print("")
94
95
96 def terminal_render_node(record, options):
97     print("%s (Node)" % record['hrn'])
98
99
100 # used in sfi list
101 def terminal_render(records, options):
102     # sort records by type
103     grouped_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 = list(grouped_by_type.keys())
110     group_types.sort()
111     for type in group_types:
112         group = grouped_by_type[type]
113 #        print 20 * '-', type
114         try:
115             renderer = eval('terminal_render_' + type)
116         except:
117             renderer = terminal_render_default
118         for record in group:
119             renderer(record, options)
120
121 ####################
122
123
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