DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
toolbox.py
Go to the documentation of this file.
1from __future__ import print_function
2
3import click
4from click import echo, style, secho
5from .click_texttable import Texttable
6
7# ------------------------------------------------------------------------------
8class IntRange(click.types.IntParamType):
9 """A parameter that works similar to :data:`click.INT` but restricts
10 the value to fit into a range. The default behavior is to fail if the
11 value falls outside the range, but it can also be silently clamped
12 between the two edges.
13
14 See :ref:`ranges` for an example.
15 """
16 name = 'integer range'
17
18 def __init__(self, min=None, max=None, clamp=False):
19 self.min = min
20 self.max = max
21 self.clamp = clamp
22
23 def convert(self, value, param, ctx):
24
25 if type(value) == str:
26 if value.startswith('0x'):
27 base = 16
28 elif value.startswith('0o'):
29 bae = 8
30 elif value.startswith('0b'):
31 base = 2
32 else:
33 base = 10
34 rv = int(value, base)
35 else:
36 rv = int(value)
37 if self.clamp:
38 if self.min is not None and rv < self.min:
39 return self.min
40 if self.max is not None and rv > self.max:
41 return self.max
42 if self.min is not None and rv < self.min or \
43 self.max is not None and rv > self.max:
44 if self.min is None:
45 self.fail('%s is bigger than the maximum valid value '
46 '%s.' % (rv, self.max), param, ctx)
47 elif self.max is None:
48 self.fail('%s is smaller than the minimum valid value '
49 '%s.' % (rv, self.min), param, ctx)
50 else:
51 self.fail('%s is not in the valid range of %s to %s.'
52 % (rv, self.min, self.max), param, ctx)
53 return rv
54
55 def __repr__(self):
56 return 'IntRange(%r, %r)' % (self.min, self.max)
57# ------------------------------------------------------------------------------
58
59# ------------------------------------------------------------------------------
60def split(ctx, param, value):
61 if value is None:
62 return []
63
64 return value.split(',')
65# ------------------------------------------------------------------------------
66
67# ------------------------------------------------------------------------------
68def __str2int__( value ):
69 if value.startswith('0x'):
70 base = 16
71 elif value.startswith('0o'):
72 bae = 8
73 elif value.startswith('0b'):
74 base = 2
75 else:
76 base = 10
77 return int(value, base)
78
79def split_ints(ctx, param, value):
80
81 sep = ','
82 dash = '-'
83
84 if value is None:
85 return []
86
87 numbers = []
88 for item in value.split(sep):
89 nums = item.split(dash)
90 if len(nums) == 1:
91 # single entry
92 numbers.append(__str2int__(item))
93 elif len(nums) == 2:
94 # range
95 i, j = __str2int__(nums[0]), __str2int__(nums[1])
96 if i > j:
97 click.ClickException('Invalid interval '+item)
98 numbers.extend(list(range(i,j+1)))
99 else:
100 click.ClickException('Malformed option (comma separated list expected): {}'.format(value))
101
102 return numbers
103# ------------------------------------------------------------------------------
convert(self, value, param, ctx)
Definition toolbox.py:23
__init__(self, min=None, max=None, clamp=False)
Definition toolbox.py:18
split(ctx, param, value)
Definition toolbox.py:60
__str2int__(value)
Definition toolbox.py:68
split_ints(ctx, param, value)
Definition toolbox.py:79