|
1 | | -# Copyright 2013 OpenStack Foundation |
2 | | -# |
3 | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
4 | 2 | # not use this file except in compliance with the License. You may obtain |
5 | 3 | # a copy of the License at |
|
13 | 11 | # under the License. |
14 | 12 | # |
15 | 13 |
|
16 | | -"""argparse Custom Actions""" |
17 | | - |
18 | | -import argparse |
19 | | - |
20 | | -from openstackclient.i18n import _ |
21 | | - |
22 | | - |
23 | | -class KeyValueAction(argparse.Action): |
24 | | - """A custom action to parse arguments as key=value pairs |
25 | | -
|
26 | | - Ensures that ``dest`` is a dict |
27 | | - """ |
28 | | - |
29 | | - def __call__(self, parser, namespace, values, option_string=None): |
30 | | - # Make sure we have an empty dict rather than None |
31 | | - if getattr(namespace, self.dest, None) is None: |
32 | | - setattr(namespace, self.dest, {}) |
33 | | - |
34 | | - # Add value if an assignment else remove it |
35 | | - if '=' in values: |
36 | | - getattr(namespace, self.dest, {}).update([values.split('=', 1)]) |
37 | | - else: |
38 | | - msg = _("Expected 'key=value' type, " |
39 | | - "but got: %s") % (str(values)) |
40 | | - raise argparse.ArgumentTypeError(msg) |
41 | | - |
42 | | - |
43 | | -class MultiKeyValueAction(argparse.Action): |
44 | | - """A custom action to parse arguments as key1=value1,key2=value2 pairs |
45 | | -
|
46 | | - Ensure that ``dest`` is a list. The list will finally contain multiple |
47 | | - dicts, with key=value pairs in them. |
48 | | -
|
49 | | - NOTE: The arguments string should be a comma separated key-value pairs. |
50 | | - And comma(',') and equal('=') may not be used in the key or value. |
51 | | - """ |
52 | | - |
53 | | - def __init__(self, option_strings, dest, nargs=None, |
54 | | - required_keys=None, optional_keys=None, **kwargs): |
55 | | - """Initialize the action object, and parse customized options |
56 | | -
|
57 | | - Required keys and optional keys can be specified when initializing |
58 | | - the action to enable the key validation. If none of them specified, |
59 | | - the key validation will be skipped. |
60 | | -
|
61 | | - :param required_keys: a list of required keys |
62 | | - :param optional_keys: a list of optional keys |
63 | | - """ |
64 | | - if nargs: |
65 | | - raise ValueError("Parameter 'nargs' is not allowed, but got %s" |
66 | | - % nargs) |
67 | | - |
68 | | - super(MultiKeyValueAction, self).__init__(option_strings, |
69 | | - dest, **kwargs) |
70 | | - |
71 | | - # required_keys: A list of keys that is required. None by default. |
72 | | - if required_keys and not isinstance(required_keys, list): |
73 | | - raise TypeError("'required_keys' must be a list") |
74 | | - self.required_keys = set(required_keys or []) |
75 | | - |
76 | | - # optional_keys: A list of keys that is optional. None by default. |
77 | | - if optional_keys and not isinstance(optional_keys, list): |
78 | | - raise TypeError("'optional_keys' must be a list") |
79 | | - self.optional_keys = set(optional_keys or []) |
80 | | - |
81 | | - def __call__(self, parser, namespace, values, metavar=None): |
82 | | - # Make sure we have an empty list rather than None |
83 | | - if getattr(namespace, self.dest, None) is None: |
84 | | - setattr(namespace, self.dest, []) |
85 | | - |
86 | | - params = {} |
87 | | - for kv in values.split(','): |
88 | | - # Add value if an assignment else raise ArgumentTypeError |
89 | | - if '=' in kv: |
90 | | - params.update([kv.split('=', 1)]) |
91 | | - else: |
92 | | - msg = _("Expected key=value pairs separated by comma, " |
93 | | - "but got: %s") % (str(kv)) |
94 | | - raise argparse.ArgumentTypeError(msg) |
95 | | - |
96 | | - # Check key validation |
97 | | - valid_keys = self.required_keys | self.optional_keys |
98 | | - if valid_keys: |
99 | | - invalid_keys = [k for k in params if k not in valid_keys] |
100 | | - if invalid_keys: |
101 | | - msg = _("Invalid keys %(invalid_keys)s specified.\n" |
102 | | - "Valid keys are: %(valid_keys)s.") |
103 | | - raise argparse.ArgumentTypeError( |
104 | | - msg % {'invalid_keys': ', '.join(invalid_keys), |
105 | | - 'valid_keys': ', '.join(valid_keys)} |
106 | | - ) |
107 | | - |
108 | | - if self.required_keys: |
109 | | - missing_keys = [k for k in self.required_keys if k not in params] |
110 | | - if missing_keys: |
111 | | - msg = _("Missing required keys %(missing_keys)s.\n" |
112 | | - "Required keys are: %(required_keys)s.") |
113 | | - raise argparse.ArgumentTypeError( |
114 | | - msg % {'missing_keys': ', '.join(missing_keys), |
115 | | - 'required_keys': ', '.join(self.required_keys)} |
116 | | - ) |
117 | | - |
118 | | - # Update the dest dict |
119 | | - getattr(namespace, self.dest, []).append(params) |
120 | | - |
121 | | - |
122 | | -class RangeAction(argparse.Action): |
123 | | - """A custom action to parse a single value or a range of values |
124 | | -
|
125 | | - Parses single integer values or a range of integer values delimited |
126 | | - by a colon and returns a tuple of integers: |
127 | | - '4' sets ``dest`` to (4, 4) |
128 | | - '6:9' sets ``dest`` to (6, 9) |
129 | | - """ |
130 | | - |
131 | | - def __call__(self, parser, namespace, values, option_string=None): |
132 | | - range = values.split(':') |
133 | | - if len(range) == 0: |
134 | | - # Nothing passed, return a zero default |
135 | | - setattr(namespace, self.dest, (0, 0)) |
136 | | - elif len(range) == 1: |
137 | | - # Only a single value is present |
138 | | - setattr(namespace, self.dest, (int(range[0]), int(range[0]))) |
139 | | - elif len(range) == 2: |
140 | | - # Range of two values |
141 | | - if int(range[0]) <= int(range[1]): |
142 | | - setattr(namespace, self.dest, (int(range[0]), int(range[1]))) |
143 | | - else: |
144 | | - msg = (_("Invalid range, %(range0)s is not " |
145 | | - "less than %(range1)s") |
146 | | - % {'range0': range[0], 'range1': range[1]}) |
147 | | - raise argparse.ArgumentError(self, msg) |
148 | | - else: |
149 | | - # Too many values |
150 | | - msg = _("Invalid range, too many values") |
151 | | - raise argparse.ArgumentError(self, msg) |
| 14 | +# NOTE(dtroyer): This file is deprecated in Jun 2016, remove after 4.x release |
| 15 | +# or Jun 2017. |
152 | 16 |
|
| 17 | +import sys |
153 | 18 |
|
154 | | -class NonNegativeAction(argparse.Action): |
155 | | - """A custom action to check whether the value is non-negative or not |
| 19 | +from osc_lib.cli.parseractions import * # noqa |
156 | 20 |
|
157 | | - Ensures the value is >= 0. |
158 | | - """ |
159 | 21 |
|
160 | | - def __call__(self, parser, namespace, values, option_string=None): |
161 | | - if int(values) >= 0: |
162 | | - setattr(namespace, self.dest, values) |
163 | | - else: |
164 | | - msg = (_("%s expected a non-negative integer") |
165 | | - % (str(option_string))) |
166 | | - raise argparse.ArgumentTypeError(msg) |
| 22 | +sys.stderr.write( |
| 23 | + "WARNING: %s is deprecated and will be removed after Jun 2017. " |
| 24 | + "Please use osc_lib.cli.parseractions\n" % __name__ |
| 25 | +) |
0 commit comments