Skip to content

Commit

Permalink
Merge pull request #258 from dhapati2/Fix/encoding_issue
Browse files Browse the repository at this point in the history
Updated fix for CDET issue of default encoding
  • Loading branch information
t-raghu authored Sep 19, 2024
2 parents 77a9cf3 + c7eb0ef commit 62f017f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ucsmsdk/ucsgenutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()):
driver = UcsDriver()\n
upload_firmware(driver=UcsDriver(), uri="http://fileurl", file_dir='/home/user/backup', file_name='my_config_backup.xml')
"""
# Change encoding to ISO-8859-1 for python <=2.x
def_encoding = sys.getdefaultencoding() # Old encoding type : e.g ascii
set_default_encoding('ISO-8859-1')

content_path = os.path.join(file_dir, file_name)
content_size = os.path.getsize(content_path)
Expand Down Expand Up @@ -298,6 +301,9 @@ def upload_firmware(driver, uri, file_dir, file_name, progress=Progress()):
except Exception as e:
raise Exception(e)

# Reset to default encoding
set_default_encoding(def_encoding)


def check_registry_key(java_key):
""" Method checks for the java in the registry entries. """
Expand Down Expand Up @@ -600,3 +606,10 @@ def remove_invalid_chars(xml_str):
for each in to_repl:
xml_str = xml_str.replace(each, to_repl[each])
return xml_str


def set_default_encoding(encoding_str):
if sys.version_info[:2] <= (2, 7):
# If Python 2.7 or older, then set 'ISO-8859-1' as default encoding
reload(sys)
sys.setdefaultencoding(encoding_str)

0 comments on commit 62f017f

Please sign in to comment.