This repository has been archived on 2025-03-20. You can view files and clone it, but cannot push or open issues or pull requests.
sonic-buildimage/src/sonic-config-engine/tests/common_utils.py
Tamer Ahmed 110f7b7817 [cfggen] Build Python 2 And Python 3 Wheel Packages
This builds Python 2&3 wheel packages for sonic-cfggen script.

singed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
2020-09-30 07:07:43 -07:00

34 lines
1.0 KiB
Python

import json
import re
import sys
PY3x = sys.version_info >= (3, 0)
PYvX_DIR = "py3" if PY3x else "py2"
PYTHON_INTERPRETTER = "python3" if PY3x else "python2"
def tuple_to_str(tuplestr):
""" Convert Python tuple '('elem1', 'elem2')' representation into string on the for "elem1|elem2" """
def to_str(tupleobj):
tupleobj = re.sub(r"([\(\)])", '"', tupleobj.group(0))
return re.sub(r"( *, *)", '|', tupleobj).replace("'", '')
return re.sub(r"(\(.*?\))", to_str, tuplestr)
def to_dict(dictstr):
""" Convert string represention of Python dict into dict """
# handle tuple elements
dictstr = tuple_to_str(dictstr)
return json.loads(dictstr.replace("'", '"'))
def liststr_to_dict(liststr):
""" Convert string represention of Python list into dict with list key and sorted liststr as its value """
# handle tuple elements
liststr = tuple_to_str(liststr)
list_obj = json.loads("{\"list\":" + liststr.replace("'", '"') +'}')
list_obj["list"] = sorted(list_obj["list"])
return list_obj