Skip to content

Commit f2cda6b

Browse files
committed
Black formatting
1 parent dadfd60 commit f2cda6b

File tree

1 file changed

+93
-66
lines changed

1 file changed

+93
-66
lines changed

src/cisco_gnmi/cli.py

Lines changed: 93 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,31 @@ def main():
2222
"capabilities": gnmi_capabilities,
2323
"subscribe": gnmi_subscribe,
2424
"get": gnmi_get,
25-
"set": gnmi_set
25+
"set": gnmi_set,
2626
}
27-
parser = argparse.ArgumentParser(description="gNMI CLI demonstrating library usage.", usage="""
27+
parser = argparse.ArgumentParser(
28+
description="gNMI CLI demonstrating library usage.",
29+
usage="""
2830
gnmcli <rpc> [<args>]
2931
3032
Supported RPCs:
3133
%s
3234
3335
See --help for RPC options.
34-
""".format('\n'.join(rpc_map.keys())))
36+
""".format(
37+
"\n".join(rpc_map.keys())
38+
),
39+
)
3540
parser.add_argument("rpc", help="gNMI RPC to perform against network element.")
3641
if len(sys.argv) < 2:
3742
logging.error("Must at minimum provide RPC and required arguments!")
3843
parser.print_help()
3944
exit(1)
4045
args = parser.parse_args(sys.argv[1:2])
4146
if args.rpc not in rpc_map.keys():
42-
logging.error("%s not in supported RPCs: %s!", args.rpc, ', '.join(rpc_map.keys()))
47+
logging.error(
48+
"%s not in supported RPCs: %s!", args.rpc, ", ".join(rpc_map.keys())
49+
)
4350
parser.print_help()
4451
exit(1)
4552
try:
@@ -48,13 +55,16 @@ def main():
4855
logging.exception("Error during usage!")
4956
exit(1)
5057

58+
5159
def gnmi_capabilities():
5260
parser = argparse.ArgumentParser(
5361
description="Performs Capabilities RPC against network element."
5462
)
5563
args = __common_args_handler(parser)
5664
client = __gen_client(args)
57-
logging.info(client.capabilities())
65+
capability_response = client.capabilities()
66+
logging.info(__format_message(capability_response))
67+
5868

5969
def gnmi_subscribe():
6070
"""Performs a sampled Subscribe against network element.
@@ -64,33 +74,34 @@ def gnmi_subscribe():
6474
description="Performs Subscribe RPC against network element."
6575
)
6676
parser.add_argument(
67-
"-xpath",
68-
help="XPath to subscribe to.",
69-
type=str,
70-
action="append"
77+
"-xpath", help="XPath to subscribe to.", type=str, action="append"
7178
)
7279
parser.add_argument(
7380
"-interval",
7481
help="Sample interval in seconds for Subscription.",
7582
type=int,
76-
default=10*int(1e9)
83+
default=10 * int(1e9),
7784
)
7885
parser.add_argument(
7986
"-dump_file",
8087
help="Filename to dump to. Defaults to stdout.",
8188
type=str,
82-
default="stdout"
89+
default="stdout",
8390
)
8491
parser.add_argument(
8592
"-dump_json",
8693
help="Dump as JSON instead of textual protos.",
87-
action="store_true"
94+
action="store_true",
8895
)
8996
parser.add_argument(
9097
"-sync_stop", help="Stop on sync_response.", action="store_true"
9198
)
9299
parser.add_argument(
93-
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?", choices=list(proto.gnmi_pb2.Encoding.keys())
100+
"-encoding",
101+
help="gNMI subscription encoding.",
102+
type=str,
103+
nargs="?",
104+
choices=list(proto.gnmi_pb2.Encoding.keys()),
94105
)
95106
args = __common_args_handler(parser)
96107
# Set default XPath outside of argparse due to default being persistent in argparse.
@@ -104,19 +115,19 @@ def gnmi_subscribe():
104115
if args.sample_interval:
105116
kwargs["sample_interval"] = args.sample_interval
106117
try:
107-
logging.info("Dumping responses to %s as %s ...", args.dump_file, "JSON" if args.dump_json else "textual proto")
108-
logging.info("Subscribing to:\n%s", '\n'.join(args.xpath))
118+
logging.info(
119+
"Dumping responses to %s as %s ...",
120+
args.dump_file,
121+
"JSON" if args.dump_json else "textual proto",
122+
)
123+
logging.info("Subscribing to:\n%s", "\n".join(args.xpath))
109124
for subscribe_response in client.subscribe_xpaths(args.xpath, **kwargs):
110125
if subscribe_response.sync_response:
111126
logging.debug("sync_response received.")
112127
if args.sync_stop:
113128
logging.warning("Stopping on sync_response.")
114129
break
115-
formatted_message = None
116-
if args.dump_json:
117-
formatted_message = json_format.MessageToJson(subscribe_response, sort_keys=True)
118-
else:
119-
formatted_message = text_format.MessageToString(subscribe_response)
130+
formatted_message = __format_message(subscribe_response)
120131
if args.dump_file == "stdout":
121132
logging.info(formatted_message)
122133
else:
@@ -127,29 +138,37 @@ def gnmi_subscribe():
127138
except Exception:
128139
logging.exception("Stopping due to exception!")
129140

141+
130142
def gnmi_get():
131143
"""Provides Get RPC usage. Assumes JSON or JSON_IETF style configurations.
132144
TODO: This is the least well understood/implemented. Need to validate if there is an OOO for update/replace/delete.
133145
"""
134146
parser = argparse.ArgumentParser(
135147
description="Performs Get RPC against network element."
136148
)
149+
parser.add_argument("-xpath", help="XPaths to Get.", type=str, action="append")
137150
parser.add_argument(
138-
"-xpath",
139-
help="XPaths to Get.",
151+
"-encoding",
152+
help="gNMI subscription encoding.",
140153
type=str,
141-
action="append"
142-
)
143-
parser.add_argument(
144-
"-encoding", help="gNMI subscription encoding.", type=str, nargs="?", choices=list(proto.gnmi_pb2.Encoding.keys())
154+
nargs="?",
155+
choices=list(proto.gnmi_pb2.Encoding.keys()),
145156
)
146157
parser.add_argument(
147-
"-data_type", help="gNMI GetRequest DataType", type=str, nargs="?", choices=list(enum_type_wrapper.EnumTypeWrapper(proto.gnmi_pb2._GETREQUEST_DATATYPE).keys())
158+
"-data_type",
159+
help="gNMI GetRequest DataType",
160+
type=str,
161+
nargs="?",
162+
choices=list(
163+
enum_type_wrapper.EnumTypeWrapper(
164+
proto.gnmi_pb2._GETREQUEST_DATATYPE
165+
).keys()
166+
),
148167
)
149168
parser.add_argument(
150169
"-dump_json",
151170
help="Dump as JSON instead of textual protos.",
152-
action="store_true"
171+
action="store_true",
153172
)
154173
args = __common_args_handler(parser)
155174
# Set default XPath outside of argparse due to default being persistent in argparse.
@@ -162,12 +181,8 @@ def gnmi_get():
162181
if args.data_type:
163182
kwargs["data_type"] = args.data_type
164183
get_response = client.get_xpaths(args.xpath, **kwargs)
165-
formatted_message = None
166-
if args.dump_json:
167-
formatted_message = json_format.MessageToJson(get_response, sort_keys=True)
168-
else:
169-
formatted_message = text_format.MessageToString(get_response)
170-
logging.info(formatted_message)
184+
logging.info(__format_message(get_response))
185+
171186

172187
def gnmi_set():
173188
"""Provides Set RPC usage. Assumes JSON or JSON_IETF style configurations.
@@ -178,37 +193,32 @@ def gnmi_set():
178193
description="Performs Set RPC against network element."
179194
)
180195
parser.add_argument(
181-
"-update_json_config",
182-
description="JSON-modeled config to apply as an update."
196+
"-update_json_config", description="JSON-modeled config to apply as an update."
183197
)
184198
parser.add_argument(
185-
"-replace_json_config",
186-
description="JSON-modeled config to apply as an update."
199+
"-replace_json_config", description="JSON-modeled config to apply as an update."
187200
)
188201
parser.add_argument(
189-
"-delete_xpath",
190-
help="XPaths to delete.",
191-
type=str,
192-
action="append"
202+
"-delete_xpath", help="XPaths to delete.", type=str, action="append"
193203
)
194204
parser.add_argument(
195-
"-no_ietf",
196-
help="JSON is not IETF conformant.",
197-
action="store_true"
205+
"-no_ietf", help="JSON is not IETF conformant.", action="store_true"
198206
)
199207
parser.add_argument(
200208
"-dump_json",
201209
help="Dump as JSON instead of textual protos.",
202-
action="store_true"
210+
action="store_true",
203211
)
204212
args = __common_args_handler(parser)
205213
if not any([args.update_json_config, args.replace_json_config, args.delete_xpath]):
206214
raise Exception("Must specify update, replace, or delete parameters!")
215+
207216
def load_json_file(filename):
208217
config = None
209218
with open(filename, "r") as config_fd:
210219
config = json.load(config_fd)
211220
return config
221+
212222
kwargs = {}
213223
if args.update_json_config:
214224
kwargs["update_json_configs"] = load_json_file(args.update_json_config)
@@ -218,23 +228,16 @@ def load_json_file(filename):
218228
kwargs["ietf"] = False
219229
client = __gen_client(args)
220230
set_response = client.set_json(**kwargs)
221-
formatted_message = None
222-
if args.dump_json:
223-
formatted_message = json_format.MessageToJson(set_response, sort_keys=True)
224-
else:
225-
formatted_message = text_format.MessageToString(set_response)
226-
logging.info(formatted_message)
231+
logging.info(__format_message(set_response))
227232
if args.delete_xpath:
228233
if getattr(client, "delete_xpaths", None) is not None:
229234
delete_response = client.delete_xpaths(args.xpath)
230-
formatted_message = None
231-
if args.dump_json:
232-
formatted_message = json_format.MessageToJson(delete_response, sort_keys=True)
233-
else:
234-
formatted_message = text_format.MessageToString(delete_response)
235-
logging.info(formatted_message)
235+
logging.info(__format_message(delete_response))
236236
else:
237-
raise Exception("Convenience delete_xpaths is not supported in the client library!")
237+
raise Exception(
238+
"Convenience delete_xpaths is not supported in the client library!"
239+
)
240+
238241

239242
def __gen_client(args):
240243
builder = ClientBuilder(args.netloc)
@@ -243,13 +246,25 @@ def __gen_client(args):
243246
if not any([args.root_certificates, args.private_key, args.certificate_chain]):
244247
builder.set_secure_from_target()
245248
else:
246-
builder.set_secure_from_file(args.root_certificates, args.private_key, args.certificate_chain)
249+
builder.set_secure_from_file(
250+
args.root_certificates, args.private_key, args.certificate_chain
251+
)
247252
if args.ssl_target_override:
248253
builder.set_ssl_target_override(args.ssl_target_override)
249254
elif args.auto_ssl_target_override:
250255
builder.set_ssl_target_override()
251256
return builder.construct()
252257

258+
259+
def __format_message(message, as_json=False):
260+
formatted_message = None
261+
if as_json:
262+
formatted_message = json_format.MessageToJson(message, sort_keys=True)
263+
else:
264+
formatted_message = text_format.MessageToString(message)
265+
return formatted_message
266+
267+
253268
def __common_args_handler(parser):
254269
"""Ideally would be a decorator."""
255270
parser.add_argument("netloc", help="<host>:<port>", type=str)
@@ -260,11 +275,23 @@ def __common_args_handler(parser):
260275
default="IOS XR",
261276
choices=list(ClientBuilder.os_class_map.keys()),
262277
)
263-
parser.add_argument("-root_certificates", description="Root certificates for secure connection.")
264-
parser.add_argument("-private_key", description="Private key for secure connection.")
265-
parser.add_argument("-certificate_chain", description="Certificate chain for secure connection.")
266-
parser.add_argument("-ssl_target_override", description="gRPC SSL target override option.")
267-
parser.add_argument("-auto_ssl_target_override", description="Root certificates for secure connection.", action="store_true")
278+
parser.add_argument(
279+
"-root_certificates", description="Root certificates for secure connection."
280+
)
281+
parser.add_argument(
282+
"-private_key", description="Private key for secure connection."
283+
)
284+
parser.add_argument(
285+
"-certificate_chain", description="Certificate chain for secure connection."
286+
)
287+
parser.add_argument(
288+
"-ssl_target_override", description="gRPC SSL target override option."
289+
)
290+
parser.add_argument(
291+
"-auto_ssl_target_override",
292+
description="Root certificates for secure connection.",
293+
action="store_true",
294+
)
268295
parser.add_argument("-debug", help="Print debug messages", action="store_true")
269296
args = parser.parse_args(sys.argv[2:])
270297
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
@@ -274,4 +301,4 @@ def __common_args_handler(parser):
274301

275302

276303
if __name__ == "__main__":
277-
main()
304+
main()

0 commit comments

Comments
 (0)