Skip to content

Commit 6e0d16e

Browse files
authored
Merge pull request #8 from cisco-ie/python2
Python 2 Support
2 parents 851961f + cdbaef2 commit 6e0d16e

File tree

10 files changed

+38
-440
lines changed

10 files changed

+38
-440
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Custom
22
.vscode/
3+
Pipfile.lock
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/

Pipfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ name = "pypi"
77
grpcio-tools = "*"
88
googleapis-common-protos = "*"
99
pylint = "*"
10-
black = "==19.3b0"
1110
twine = "*"
1211
setuptools = "*"
1312
wheel = "*"
1413

1514
[packages]
1615
grpcio = "*"
1716
protobuf = "*"
17+
enum34 = "*"
18+
six = "*"
1819

1920
[requires]
2021
python_version = "3.6"

Pipfile.lock

Lines changed: 0 additions & 424 deletions
This file was deleted.

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ gRPC Network Management Interface (gNMI) is a service defining an interface for
3030
gNMI is a specification developed by [OpenConfig](https://openconfig.net), an operator-driven working-group. It is important to note that gNMI only defines a protocol of behavior - not data models. This is akin to SNMP/MIBs and NETCONF/YANG. SNMP and NETCONF are respectively decoupled from the data itself in MIBs and YANG modules. gNMI is a control protocol, not a standardization of data. OpenConfig does develop standard data models as well, and does have some specialized behavior with OpenConfig originating models, but the data models themselves are out of the scope of gNMI.
3131

3232
## Development
33-
Requires Python and utilizes `pipenv` for environment management. Manual usage of `pip`/`virtualenv` is not covered. Uses `black` for code formatting and `pylint` for code linting.
33+
Requires Python and utilizes `pipenv` for environment management. Manual usage of `pip`/`virtualenv` is not covered. Uses `black` for code formatting and `pylint` for code linting. `black` is not explicitly installed as it requires Python 3.6+.
3434

3535
### Get Source
3636
```bash
@@ -39,17 +39,20 @@ cd cisco-gnmi-python
3939
# If pipenv not installed, install!
4040
pip install --user pipenv
4141
# Now use pipenv
42-
pipenv --three install
42+
pipenv --three install --dev
4343
# Enter virtual environment
4444
pipenv shell
4545
# Do your thing.
4646
exit
4747
```
4848

4949
### Code Hygiene
50-
We use [`black`](https://github.com/ambv/black) for code formatting and [`pylint`](https://www.pylint.org/) for code linting. `hygiene.sh` will run `black` against all of the code under `gnmi/` except for `protoc` compiled protobufs, and run `pylint` against Python files directly under `gnmi/`. They don't totally agree, so we're not looking for perfection here.
50+
We use [`black`](https://github.com/ambv/black) for code formatting and [`pylint`](https://www.pylint.org/) for code linting. `hygiene.sh` will run `black` against all of the code under `gnmi/` except for `protoc` compiled protobufs, and run `pylint` against Python files directly under `gnmi/`. They don't totally agree, so we're not looking for perfection here. `black` is not automatically installed due to requiring Python 3.6+. `hygiene.sh` will check for regular path availability and via `pipenv`, and otherwise falls directly to `pylint`. If `black` usage is desired, please install it into `pipenv` if using Python 3.6+ or separate methods e.g. `brew install black`.
5151

5252
```bash
53+
# If using Python 3.6+
54+
pipenv install --dev black
55+
# Otherwise...
5356
./hygiene.sh
5457
```
5558

hygiene.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/usr/bin/env bash
22
# Script which autoformats (black) and lints (pylint) code
3-
pipenv run black --safe --verbose --exclude proto src/cisco_gnmi
4-
# Many disabled to accomodate black disagreements...
3+
echo "Running black ..."
4+
FORMAT_COMMAND="black --safe --verbose --exclude proto src/cisco_gnmi"
5+
if black &> /dev/null; then
6+
eval $FORMAT_COMMAND
7+
elif pipenv run black &> /dev/null; then
8+
eval "pipenv run $FORMAT_COMMAND"
9+
else
10+
echo "black formatter not found on system, proceeding to pylint..."
11+
fi
12+
echo "Running pylint ..."
513
# Many failures due to protos being runtime-functional and difficult to lint.
6-
pipenv run pylint --disable line-too-long --disable pointless-string-statement --disable no-member --disable wrong-import-position --disable bad-continuation src/cisco_gnmi/*.py
14+
pipenv run pylint --disable no-member --disable wrong-import-position --disable bad-continuation src/cisco_gnmi/*.py

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-i https://pypi.org/simple
2+
enum34
3+
futures ; python_version < '3.2'
4+
grpcio
5+
protobuf
6+
six

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
install_requires=[
5353
"grpcio",
5454
"protobuf",
55+
"enum34",
56+
"six",
5557
],
5658
extras_require={
5759
"dev": [
5860
"grpcio-tools",
5961
"googleapis-common-protos",
6062
"pylint",
61-
"black==19.3b0",
6263
"twine",
6364
"setuptools",
6465
"wheel",

src/cisco_gnmi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
from .client import Client
2828
from .xr import XRClient
2929

30-
__version__ = "0.0.1"
30+
__version__ = "0.0.2"

src/cisco_gnmi/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# Python 2
3131
from urlparse import urlparse
3232

33+
from six import string_types
3334
import grpc
3435
from . import proto
3536

@@ -98,11 +99,11 @@ def gen_options(tls_server_override):
9899

99100
def parse_xpath_to_gnmi_path(xpath, origin=None):
100101
"""Parses an XPath to proto.gnmi_pb2.Path."""
101-
if not isinstance(xpath, str):
102+
if not isinstance(xpath, string_types):
102103
raise Exception("xpath must be a string!")
103104
path = proto.gnmi_pb2.Path()
104105
if origin:
105-
if not isinstance(origin, str):
106+
if not isinstance(origin, string_types):
106107
raise Exception("origin must be a string!")
107108
path.origin = origin
108109
for element in xpath.split("/"):

src/cisco_gnmi/xr.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import json
2727
import logging
2828

29+
from six import string_types
2930
from .client import Client, proto, util
3031

3132

@@ -88,7 +89,7 @@ def delete_xpaths(self, xpaths, prefix=None):
8889
-------
8990
set()
9091
"""
91-
if isinstance(xpaths, str):
92+
if isinstance(xpaths, string_types):
9293
xpaths = [xpaths]
9394
paths = []
9495
for xpath in xpaths:
@@ -128,7 +129,7 @@ def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=Tru
128129
raise Exception("Must supply at least one set of configurations to method!")
129130

130131
def check_configs(name, configs):
131-
if isinstance(name, str):
132+
if isinstance(name, string_types):
132133
logging.debug("Handling %s as JSON string.", name)
133134
try:
134135
configs = json.loads(configs)
@@ -204,7 +205,7 @@ def get_xpaths(self, xpaths, data_type="ALL", encoding="JSON_IETF"):
204205
gnmi_path = None
205206
if isinstance(xpaths, (list, set)):
206207
gnmi_path = map(util.parse_xpath_to_gnmi_path, set(xpaths))
207-
elif isinstance(xpaths, str):
208+
elif isinstance(xpaths, string_types):
208209
gnmi_path = [util.parse_xpath_to_gnmi_path(xpaths)]
209210
else:
210211
raise Exception(
@@ -279,11 +280,11 @@ def subscribe_xpaths(
279280
subscription_list.encoding = util.validate_proto_enum(
280281
"encoding", encoding, "Encoding", proto.gnmi_pb2.Encoding
281282
)
282-
if isinstance(xpath_subscriptions, str):
283+
if isinstance(xpath_subscriptions, string_types):
283284
xpath_subscriptions = [xpath_subscriptions]
284285
for xpath_subscription in xpath_subscriptions:
285286
subscription = None
286-
if isinstance(xpath_subscription, str):
287+
if isinstance(xpath_subscription, string_types):
287288
subscription = proto.gnmi_pb2.Subscription()
288289
subscription.path.CopyFrom(
289290
util.parse_xpath_to_gnmi_path(xpath_subscription)

0 commit comments

Comments
 (0)