Skip to content

Commit be19267

Browse files
author
Dean Troyer
committed
osc-lib: parseractions
Leave parseractions.py and test_parseractions.py as a sanity check during the deprecation period. Change-Id: I1a7469b6d872284e0276502a1a287bc0b87f8f83
1 parent 59dffb9 commit be19267

27 files changed

Lines changed: 37 additions & 175 deletions

doc/source/plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ the plugin commands:
152152
.. code-block:: python
153153
154154
# osc-lib interfaces available to plugins:
155+
from osc_lib.cli import parseractions
155156
from osc_lib import exceptions
156157
from osc_lib import logs
157158
from osc_lib import utils
158159
159160
# OSC common interfaces available to plugins:
160161
from openstackclient.common import command
161-
from openstackclient.common import parseractions
162162
163163
164164
class DeleteMypluginobject(command.Command):
Lines changed: 8 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Copyright 2013 OpenStack Foundation
2-
#
31
# Licensed under the Apache License, Version 2.0 (the "License"); you may
42
# not use this file except in compliance with the License. You may obtain
53
# a copy of the License at
@@ -13,154 +11,15 @@
1311
# under the License.
1412
#
1513

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.
15216

17+
import sys
15318

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
15620

157-
Ensures the value is >= 0.
158-
"""
15921

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+
)

openstackclient/compute/v2/aggregate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
"""Compute v2 Aggregate action implementations"""
1818

19+
from osc_lib.cli import parseractions
1920
from osc_lib import utils
2021
import six
2122

2223
from openstackclient.common import command
23-
from openstackclient.common import parseractions
2424
from openstackclient.i18n import _
2525

2626

openstackclient/compute/v2/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
import sys
1919

20+
from osc_lib.cli import parseractions
2021
from osc_lib import utils
2122
import six
2223

2324
from openstackclient.common import command
24-
from openstackclient.common import parseractions
2525
from openstackclient.i18n import _
2626

2727

openstackclient/compute/v2/flavor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
"""Flavor action implementations"""
1717

18+
from osc_lib.cli import parseractions
1819
from osc_lib import exceptions
1920
from osc_lib import utils
2021
import six
2122

2223
from openstackclient.common import command
23-
from openstackclient.common import parseractions
2424
from openstackclient.i18n import _
2525
from openstackclient.identity import common as identity_common
2626

openstackclient/compute/v2/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import os
2222
import sys
2323

24+
from osc_lib.cli import parseractions
2425
from osc_lib import exceptions
2526
from osc_lib import utils
2627
import six
@@ -31,7 +32,6 @@
3132
from novaclient.v1_1 import servers
3233

3334
from openstackclient.common import command
34-
from openstackclient.common import parseractions
3535
from openstackclient.i18n import _
3636
from openstackclient.identity import common as identity_common
3737

openstackclient/identity/v2_0/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"""Identity v2 Project action implementations"""
1717

1818
from keystoneauth1 import exceptions as ks_exc
19+
from osc_lib.cli import parseractions
1920
from osc_lib import utils
2021
import six
2122

2223
from openstackclient.common import command
23-
from openstackclient.common import parseractions
2424
from openstackclient.i18n import _
2525

2626

openstackclient/identity/v3/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"""Project action implementations"""
1717

1818
from keystoneauth1 import exceptions as ks_exc
19+
from osc_lib.cli import parseractions
1920
from osc_lib import utils
2021
import six
2122

2223
from openstackclient.common import command
23-
from openstackclient.common import parseractions
2424
from openstackclient.i18n import _
2525
from openstackclient.identity import common
2626

openstackclient/image/v1/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import argparse
1919
import io
2020
import os
21-
import six
2221
import sys
2322

2423
if os.name == "nt":
@@ -27,11 +26,12 @@
2726
msvcrt = None
2827

2928
from glanceclient.common import utils as gc_utils
29+
from osc_lib.cli import parseractions
3030
from osc_lib import utils
31+
import six
3132

3233
from openstackclient.api import utils as api_utils
3334
from openstackclient.common import command
34-
from openstackclient.common import parseractions
3535
from openstackclient.i18n import _
3636

3737

openstackclient/image/v2/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import argparse
1919

2020
from glanceclient.common import utils as gc_utils
21+
from osc_lib.cli import parseractions
2122
from osc_lib import exceptions
2223
from osc_lib import utils
2324
import six
2425

2526
from openstackclient.api import utils as api_utils
2627
from openstackclient.common import command
27-
from openstackclient.common import parseractions
2828
from openstackclient.i18n import _
2929
from openstackclient.identity import common
3030

0 commit comments

Comments
 (0)