Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,25 @@
#
# Helper Functions
#

# For python2 compatibility
Copy link
Copy Markdown
Collaborator

@qiluo-msft qiluo-msft Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python2

If this is needed, add a unit test to cover this feature. #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made changes addressing your comments

  1. on non-ascii chars - I kept the behavior the same as previous - instead of throwing UnicodeEcodeError, encode with hex escapes.
  2. moved the compatibility code under sys.version_info.major == 2 check
  3. added bool values to sample_platform.json. The changes are already covered by test_cfggen_platformJson

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft please review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft please review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def py2JsonStrHook(j):
if isinstance(j, unicode):
return j.encode('utf-8', 'backslashreplace')
if isinstance(j, list):
return [py2JsonStrHook(item) for item in j]
if isinstance(j, dict):
return {py2JsonStrHook(key): py2JsonStrHook(value)
for key, value in j.iteritems()}
return j

def readJson(filename):
# Read 'platform.json' or 'hwsku.json' file
try:
with open(filename) as fp:
try:
data = json.load(fp)
except json.JSONDecodeError:
print("Json file does not exist")
data_dict = ast.literal_eval(json.dumps(data))
return data_dict
if sys.version_info.major == 2:
return json.load(fp, object_hook=py2JsonStrHook)
return json.load(fp)
except Exception as e:
print("error occurred while parsing json: {}".format(sys.exc_info()[1]))
return None
Expand Down
12 changes: 12 additions & 0 deletions src/sonic-config-engine/tests/sample_platform.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"chassis": {
"psus": [
{
"name": "PSU 1",
"temperature": false
},
{
"name": "PSU 2",
"temperature": false
}
]
},
"interfaces": {
"Ethernet0": {
"index": "1,1,1,1",
Expand Down