Skip to content

Commit

Permalink
[sonic-yang-mgmt]: Resolving LGTM.
Browse files Browse the repository at this point in the history
Changes:
To address 'import *' related LGTM alerts
1.) Moved all functions of _sonic_yang_ext.py file in sonic_yang_ext_mixin class.
2.) Use this class in sonic_yang to extend funtionality, i.e. python mixin.

Note: Python mixin are different from parent class since it can access all variables
of the class which is using it.

Other LGTM issues
3.) Address Other LGTM minor issues found in below link.
https://lgtm.com/projects/g/Azure/sonic-buildimage/rev/pr-0f82616403c02577e1155347eb5b693a51c76a9e

Signed-off-by: Praveen Chaudhary pchaudhary@linkedin.com
  • Loading branch information
Praveen Chaudhary committed Jun 30, 2020
1 parent 79bbc6e commit 3456669
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
13 changes: 5 additions & 8 deletions src/sonic-yang-mgmt/_sonic_yang_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

import yang as ly
import re
import pprint
import syslog

from json import dump, load, dumps, loads
from json import dump, dumps, loads
from xmltodict import parse
from os import listdir, walk, path
from os.path import isfile, join, splitext
from glob import glob

# class sonic_yang methods, use mixin to extend sonic_yang
Expand Down Expand Up @@ -168,7 +165,7 @@ def extractKey(self, tableKey, keys, regex):
"""
def fillLeafDict(self, leafs, leafDict, isleafList=False):

if leafs == None:
if leafs is None:
return

# fill default values
Expand Down Expand Up @@ -645,16 +642,16 @@ def delete_node(self, xpath):
nodeP = self.find_parent_node(xpath)
xpathP = nodeP.path()
if self._delete_node(xpath=xpathP, node=nodeP) == False:
raise('_delete_node failed')
raise Exception('_delete_node failed')
else:
return True

# delete non key element
if self._delete_node(xpath=xpath, node=node) == False:
raise('_delete_node failed')
raise Exception('_delete_node failed')
except Exception as e:
print(e)
raise('Failed to delete node {}'.format(xpath))
raise Exception('Failed to delete node {}'.format(xpath))

return True

Expand Down
17 changes: 8 additions & 9 deletions src/sonic-yang-mgmt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@

from setuptools import setup, find_packages
from setuptools.command.build_py import build_py
from os import system
from os import system, environ
from sys import exit
import pytest
import os

# find path of pkgs from os environment vars
prefix = '/sonic'; debs = os.environ["STRETCH_DEBS_PATH"]
wheels = os.environ["PYTHON_WHEELS_PATH"]
prefix = '/sonic'; debs = environ["STRETCH_DEBS_PATH"]
wheels = environ["PYTHON_WHEELS_PATH"]
wheels_path = '{}/{}'.format(prefix, wheels)
deps_path = '{}/{}'.format(prefix, debs)
# dependencies
libyang = '{}/{}'.format(deps_path, os.environ["LIBYANG"])
libyangCpp = '{}/{}'.format(deps_path, os.environ["LIBYANG_CPP"])
libyangPy2 = '{}/{}'.format(deps_path, os.environ["LIBYANG_PY2"])
libyangPy3 = '{}/{}'.format(deps_path, os.environ["LIBYANG_PY3"])
sonicYangModels = '{}/{}'.format(wheels_path, os.environ["SONIC_YANG_MODELS_PY3"])
libyang = '{}/{}'.format(deps_path, environ["LIBYANG"])
libyangCpp = '{}/{}'.format(deps_path, environ["LIBYANG_CPP"])
libyangPy2 = '{}/{}'.format(deps_path, environ["LIBYANG_PY2"])
libyangPy3 = '{}/{}'.format(deps_path, environ["LIBYANG_PY3"])
sonicYangModels = '{}/{}'.format(wheels_path, environ["SONIC_YANG_MODELS_PY3"])

# important reuirements parameters
build_requirements = [libyang, libyangCpp, libyangPy2, libyangPy3, sonicYangModels,]
Expand Down
13 changes: 6 additions & 7 deletions src/sonic-yang-mgmt/sonic_yang.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def validate_data (self, node=None, ctx=None):
ctx = self.ctx

try:
rc = node.validate(ly.LYD_OPT_CONFIG, ctx)
node.validate(ly.LYD_OPT_CONFIG, ctx)
except Exception as e:
self.fail(e)

Expand Down Expand Up @@ -374,7 +374,7 @@ def find_data_node_schema_xpath(self, data_xpath):
"""
def add_data_node(self, data_xpath, value):
try:
data_node = self.new_data_node(data_xpath, value)
self.new_node(xpath, value)
#check if the node added to the data tree
self.find_data_node(data_xpath)
except Exception as e:
Expand Down Expand Up @@ -452,7 +452,7 @@ def find_data_node_value(self, data_xpath):
"""
def set_data_node_value(self, data_xpath, value):
try:
data_node = self.root.new_path(self.ctx, data_xpath, str(value), ly.LYD_ANYDATA_STRING, ly.LYD_PATH_OPT_UPDATE)
self.root.new_path(self.ctx, data_xpath, str(value), ly.LYD_ANYDATA_STRING, ly.LYD_PATH_OPT_UPDATE)
except Exception as e:
print("set data node value failed for xpath: " + str(data_xpath))
self.fail(e)
Expand All @@ -474,7 +474,7 @@ def find_data_nodes(self, data_xpath):
raise Exception('data node not found')

for data_set in node_set.data():
schema = data_set.schema()
data_set.schema()
list.append(data_set.path())
return list

Expand All @@ -486,7 +486,6 @@ def find_data_nodes(self, data_xpath):
"""
def find_schema_dependencies (self, schema_xpath):
ref_list = []
node = self.root
try:
schema_node = self.find_schema_node(schema_xpath)
except Exception as e:
Expand Down Expand Up @@ -527,7 +526,7 @@ def find_data_dependencies (self, data_xpath):
for link in backlinks.schema():
node_set = node.find_path(link.path())
for data_set in node_set.data():
schema = data_set.schema()
data_set.schema()
casted = data_set.subtype()
if value == casted.value_str():
ref_list.append(data_set.path())
Expand Down Expand Up @@ -648,7 +647,7 @@ def get_leafref_type_schema (self, schema_xpath):
if subtype.type().base() != ly.LY_TYPE_LEAFREF:
return None
else:
leafref_path = subtype.type().info().lref().path()
subtype.type().info().lref().path()
target = subtype.type().info().lref().target()
target_path = target.path()
target_type = self.get_data_type(target_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import sys
import os
import pytest
import yang as ly
import sonic_yang as sy
import json
import getopt
import subprocess
import glob
import logging
from ijson import items as ijson_itmes
Expand All @@ -31,7 +28,6 @@ def data(self):
@pytest.fixture(autouse=True, scope='class')
def yang_s(self, data):
yang_dir = str(data['yang_dir'])
data_file = str(data['data_file'])
yang_s = sy.sonic_yang(yang_dir)
return yang_s

Expand Down Expand Up @@ -62,7 +58,7 @@ def readIjsonInput(self, yang_test_file, test):
raise(e)
return jInput

def setup_class(cls):
def setup_class(self):
pass

def load_yang_model_file(self, yang_s, yang_dir, yang_file, module_name):
Expand Down Expand Up @@ -140,14 +136,14 @@ def test_find_node(self, data, yang_s):
assert dnode is not None
assert dnode.path() == xpath
else:
assert dnode == None
assert dnode is None

#test add node
def test_add_node(self, data, yang_s):
for node in data['new_nodes']:
xpath = str(node['xpath'])
value = node['value']
status = yang_s.add_data_node(xpath, str(value))
yang_s.add_node(xpath, str(value))

data_node = yang_s.find_data_node(xpath)
assert data_node is not None
Expand All @@ -165,7 +161,6 @@ def test_find_data_node_value(self, data, yang_s):
#test delete data node
def test_delete_node(self, data, yang_s):
for node in data['delete_nodes']:
expected = node['valid']
xpath = str(node['xpath'])
yang_s._delete_node(xpath)

Expand Down Expand Up @@ -291,5 +286,5 @@ def test_xlate_rev_xlate(self):

return

def teardown_class(cls):
def teardown_class(self):
pass

0 comments on commit 3456669

Please sign in to comment.