sonic-buildimage/platform/mellanox/mlnx-platform-api/sonic_platform/utils.py
Junchao-Mellanox 500395c56e
[Mellanox] Support max/min speed for PSU fan (#5682) (#5801)
As new hw-mgmt expose the sysfs for PSU fan max speed, we need support max/min speed for PSU fan in mellanox platform API.
Conflicts:
	platform/mellanox/mlnx-platform-api/sonic_platform/fan.py
2020-11-17 18:17:37 +02:00

58 lines
1.7 KiB
Python

def read_str_from_file(file_path, default='', raise_exception=False):
"""
Read string content from file
:param file_path: File path
:param default: Default return value if any exception occur
:param raise_exception: Raise exception to caller if True else just return default value
:return: String content of the file
"""
try:
with open(file_path, 'r') as f:
value = f.read().strip()
except (ValueError, IOError) as e:
if not raise_exception:
value = default
else:
raise e
return value
def read_int_from_file(file_path, default=0, raise_exception=False):
"""
Read content from file and cast it to integer
:param file_path: File path
:param default: Default return value if any exception occur
:param raise_exception: Raise exception to caller if True else just return default value
:return: Integer value of the file content
"""
try:
with open(file_path, 'r') as f:
value = int(f.read().strip())
except (ValueError, IOError) as e:
if not raise_exception:
value = default
else:
raise e
return value
def write_file(file_path, content, raise_exception=False):
"""
Write the given value to a file
:param file_path: File path
:param content: Value to write to the file
:param raise_exception: Raise exception to caller if True
:return: True if write success else False
"""
try:
with open(file_path, 'w') as f:
f.write(str(content))
except (ValueError, IOError) as e:
if not raise_exception:
return False
else:
raise e
return True