Skip to content

Commit aa3ae07

Browse files
authored
Merge pull request #30 from cisco-ie/fix-proto-msg-append
Replace proto append with extend for protobuf < 3.8.0
2 parents 6867727 + 0262b78 commit aa3ae07

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

src/cisco_gnmi/client.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def get(
154154
request = proto.gnmi_pb2.GetRequest()
155155
if not isinstance(paths, (list, set)):
156156
raise Exception("paths must be an iterable containing Path(s)!")
157-
for path in paths:
158-
request.path.append(path)
157+
request.path.extend(paths)
159158
request.type = data_type
160159
request.encoding = encoding
161160
if prefix:
@@ -201,17 +200,13 @@ def set(
201200
if not isinstance(item, (list, set)):
202201
raise Exception("updates, replaces, and deletes must be iterables!")
203202
if updates:
204-
for update in updates:
205-
request.update.append(update)
203+
request.update.extend(updates)
206204
if replaces:
207-
for update in replaces:
208-
request.replace.append(update)
205+
request.replaces.extend(replaces)
209206
if deletes:
210-
for path in deletes:
211-
request.delete.append(path)
207+
request.delete.extend(deletes)
212208
if extensions:
213-
for extension in extensions:
214-
request.extension.append(extension)
209+
request.extension.extend(extensions)
215210
response = self.service.Set(request)
216211
return response
217212

@@ -248,8 +243,7 @@ def validate_request(request):
248243
"request must be a SubscriptionList, Poll, or AliasList!"
249244
)
250245
if extensions:
251-
for extension in extensions:
252-
subscribe_request.extensions.append(extension)
246+
subscribe_request.extensions.extend(extensions)
253247
return subscribe_request
254248

255249
response_stream = self.service.Subscribe(
@@ -280,14 +274,15 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
280274
# TODO: Lazy
281275
xpath = xpath.strip("/")
282276
xpath_elements = xpath_tokenizer_re.findall(xpath)
277+
path_elems = []
283278
for index, element in enumerate(xpath_elements):
284279
# stripped initial /, so this indicates a completed element
285280
if element[0] == "/":
286281
if not curr_elem.name:
287282
raise Exception(
288283
"Current PathElem has no name yet is trying to be pushed to path! Invalid XPath?"
289284
)
290-
path.elem.append(curr_elem)
285+
path_elems.append(curr_elem)
291286
curr_elem = proto.gnmi_pb2.PathElem()
292287
continue
293288
# We are entering a filter
@@ -333,8 +328,9 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
333328
# If we have a dangling element that hasn't been completed due to no
334329
# / element then let's just append the final element.
335330
if curr_elem:
336-
path.elem.append(curr_elem)
331+
path_elems.append(curr_elem)
337332
curr_elem = None
338333
if any([curr_elem, curr_key, in_filter]):
339334
raise Exception("Unfinished elements in XPath parsing!")
335+
path.elem.extend(path_elems)
340336
return path

src/cisco_gnmi/nx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def subscribe_xpaths(
123123
)
124124
if isinstance(xpath_subscriptions, string_types):
125125
xpath_subscriptions = [xpath_subscriptions]
126+
subscriptions = []
126127
for xpath_subscription in xpath_subscriptions:
127128
subscription = None
128129
if isinstance(xpath_subscription, string_types):
@@ -159,7 +160,8 @@ def subscribe_xpaths(
159160
subscription = xpath_subscription
160161
else:
161162
raise Exception("xpath in list must be xpath or dict/Path!")
162-
subscription_list.subscription.append(subscription)
163+
subscriptions.append(subscription)
164+
subscription_list.subscription.extend(subscriptions)
163165
return self.subscribe([subscription_list])
164166

165167
def parse_xpath_to_gnmi_path(self, xpath, origin=None):

src/cisco_gnmi/xe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def subscribe_xpaths(
270270
)
271271
if isinstance(xpath_subscriptions, string_types):
272272
xpath_subscriptions = [xpath_subscriptions]
273+
subscriptions = []
273274
for xpath_subscription in xpath_subscriptions:
274275
subscription = None
275276
if isinstance(xpath_subscription, string_types):
@@ -306,7 +307,8 @@ def subscribe_xpaths(
306307
subscription = xpath_subscription
307308
else:
308309
raise Exception("xpath in list must be xpath or dict/Path!")
309-
subscription_list.subscription.append(subscription)
310+
subscriptions.append(subscription)
311+
subscription_list.subscription.extend(subscriptions)
310312
return self.subscribe([subscription_list])
311313

312314
def parse_xpath_to_gnmi_path(self, xpath, origin=None):

src/cisco_gnmi/xr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def subscribe_xpaths(
284284
)
285285
if isinstance(xpath_subscriptions, string_types):
286286
xpath_subscriptions = [xpath_subscriptions]
287+
subscriptions = []
287288
for xpath_subscription in xpath_subscriptions:
288289
subscription = None
289290
if isinstance(xpath_subscription, string_types):
@@ -324,7 +325,8 @@ def subscribe_xpaths(
324325
subscription = xpath_subscription
325326
else:
326327
raise Exception("xpath in list must be xpath or dict/Path!")
327-
subscription_list.subscription.append(subscription)
328+
subscriptions.append(subscription)
329+
subscription_list.subscription.extend(subscriptions)
328330
return self.subscribe([subscription_list])
329331

330332
def parse_xpath_to_gnmi_path(self, xpath, origin=None):

0 commit comments

Comments
 (0)