Debian Codegraph @

lib/ClusterShell/CLI/Utils.py

raw symbols SCIP
1 #
2 # Copyright (C) 2010-2015 CEA/DAM
3 # Copyright (C) 2018 Stephane Thiell <[email protected]>
4 #
5 # This file is part of ClusterShell.
6 #
7 # ClusterShell is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # ClusterShell is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with ClusterShell; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 """
22 CLI utility functions
23 """
24
25 from ClusterShell.RangeSet import RangeSet
26
27
28 def parse_fold_axis(axis):
29 """Translate a 1-indexed command-line --axis value to a 0-indexed
30 fold_axis suitable for NodeSet.fold_axis or Defaults.fold_axis."""
31 if not axis.startswith('-'):
32 # axis are 1-indexed on the command line (0 ignored)
33 return tuple(x - 1 for x in RangeSet(axis).intiter() if x > 0)
34 # negative axis index (only single number supported)
35 return [int(axis)]
36
37
38 (KIBI, MEBI, GIBI, TEBI) = (1024.0, 1024.0 ** 2, 1024.0 ** 3, 1024.0 ** 4)
39
40 def human_bi_bytes_unit(value):
41 """
42 Format numerical `value` to display it using human readable unit with
43 binary prefix like (KiB, MiB, GiB, ...).
44 """
45 if value >= TEBI:
46 fmt = "%.2f TiB" % (value / TEBI)
47 elif value >= GIBI:
48 fmt = "%.2f GiB" % (value / GIBI)
49 elif value >= MEBI:
50 fmt = "%.2f MiB" % (value / MEBI)
51 elif value >= KIBI:
52 fmt = "%.2f KiB" % (value / KIBI)
53 else:
54 fmt = "%d B" % value
55 return fmt
56
57 def nodeset_cmpkey(nodeset):
58 """We want larger nodeset first, then sorted by first node index."""
59 return -len(nodeset), nodeset[0]
60
61 def bufnodeset_cmpkey(buf):
62 """Helper to get nodeset compare key from a buffer (buf, nodeset)"""
63 return nodeset_cmpkey(buf[1])