From 3456669fbf03e2932956f2fe2ca2fa52f3e4a1b2 Mon Sep 17 00:00:00 2001 From: Praveen Chaudhary <pchaudhary@linkedin.com> Date: Thu, 9 Apr 2020 11:15:13 -0700 Subject: [PATCH] [sonic-yang-mgmt]: Resolving LGTM. 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 --- src/sonic-yang-mgmt/_sonic_yang_ext.py | 13 +++++-------- src/sonic-yang-mgmt/setup.py | 17 ++++++++--------- src/sonic-yang-mgmt/sonic_yang.py | 13 ++++++------- .../libyang-python-tests/test_sonic_yang.py | 13 ++++--------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/sonic-yang-mgmt/_sonic_yang_ext.py b/src/sonic-yang-mgmt/_sonic_yang_ext.py index 29f1872c34ec..8c69f56faf6b 100644 --- a/src/sonic-yang-mgmt/_sonic_yang_ext.py +++ b/src/sonic-yang-mgmt/_sonic_yang_ext.py @@ -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 @@ -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 @@ -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 diff --git a/src/sonic-yang-mgmt/setup.py b/src/sonic-yang-mgmt/setup.py index 24a09cf958c7..15c8243aad7f 100644 --- a/src/sonic-yang-mgmt/setup.py +++ b/src/sonic-yang-mgmt/setup.py @@ -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,] diff --git a/src/sonic-yang-mgmt/sonic_yang.py b/src/sonic-yang-mgmt/sonic_yang.py index d2f91de452a2..6bc330c9ed6a 100644 --- a/src/sonic-yang-mgmt/sonic_yang.py +++ b/src/sonic-yang-mgmt/sonic_yang.py @@ -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) @@ -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: @@ -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) @@ -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 @@ -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: @@ -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()) @@ -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) diff --git a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py index aa98bdf149c8..6002b4386068 100644 --- a/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py +++ b/src/sonic-yang-mgmt/tests/libyang-python-tests/test_sonic_yang.py @@ -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 @@ -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 @@ -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): @@ -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 @@ -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) @@ -291,5 +286,5 @@ def test_xlate_rev_xlate(self): return - def teardown_class(cls): + def teardown_class(self): pass