diff options
author | Daniil Baturin <daniil@vyos.io> | 2021-12-08 00:12:54 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-08 00:12:54 +0700 |
commit | 7af98b0bb9190f37b5d27da6ffc8343a7138106b (patch) | |
tree | b6c84f22dcd5b910c6da2bd6775fcaaf163f9c4a /src/validators | |
parent | fc7fd28724088761eccc1c601dda2381e65ee949 (diff) | |
parent | 45d2429aa5d2ffafacdc5d9d00b7097169592427 (diff) | |
download | vyos-1x-7af98b0bb9190f37b5d27da6ffc8343a7138106b.tar.gz vyos-1x-7af98b0bb9190f37b5d27da6ffc8343a7138106b.zip |
Merge pull request #1102 from dmbaturin/T3006
T3006: add a range validator
Diffstat (limited to 'src/validators')
-rwxr-xr-x | src/validators/range | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/validators/range b/src/validators/range new file mode 100755 index 000000000..d4c25f3c4 --- /dev/null +++ b/src/validators/range @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import re +import sys +import argparse + +class MalformedRange(Exception): + pass + +def validate_range(value, min=None, max=None): + try: + lower, upper = re.match(r'^(\d+)-(\d+)$', value).groups() + + lower, upper = int(lower), int(upper) + + if int(lower) > int(upper): + raise MalformedRange("the lower bound exceeds the upper bound".format(value)) + + if min is not None: + if lower < min: + raise MalformedRange("the lower bound must not be less than {}".format(min)) + + if max is not None: + if upper > max: + raise MalformedRange("the upper bound must not be greater than {}".format(max)) + + except (AttributeError, ValueError): + raise MalformedRange("range syntax error") + +parser = argparse.ArgumentParser(description='Range validator.') +parser.add_argument('--min', type=int, action='store') +parser.add_argument('--max', type=int, action='store') +parser.add_argument('value', action='store') + +if __name__ == '__main__': + args = parser.parse_args() + + try: + validate_range(args.value, min=args.min, max=args.max) + except MalformedRange as e: + print("Incorrect range '{}': {}".format(args.value, e)) + sys.exit(1) |