Handle more file closing using context manager (#11942)

This commit is contained in:
Ville Skyttä 2018-01-31 12:30:48 +02:00 committed by Pascal Vizeli
parent 4cb1f93019
commit 0376cc0917
4 changed files with 12 additions and 11 deletions

View File

@ -182,7 +182,8 @@ def check_pid(pid_file: str) -> None:
"""Check that Home Assistant is not already running.""" """Check that Home Assistant is not already running."""
# Check pid file # Check pid file
try: try:
pid = int(open(pid_file, 'r').readline()) with open(pid_file, 'r') as file:
pid = int(file.readline())
except IOError: except IOError:
# PID File does not exist # PID File does not exist
return return
@ -204,7 +205,8 @@ def write_pid(pid_file: str) -> None:
"""Create a PID File.""" """Create a PID File."""
pid = os.getpid() pid = os.getpid()
try: try:
open(pid_file, 'w').write(str(pid)) with open(pid_file, 'w') as file:
file.write(str(pid))
except IOError: except IOError:
print('Fatal Error: Unable to write pid file {}'.format(pid_file)) print('Fatal Error: Unable to write pid file {}'.format(pid_file))
sys.exit(1) sys.exit(1)

View File

@ -38,18 +38,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
tokenfile = hass.config.path('.greenwave') tokenfile = hass.config.path('.greenwave')
if config.get(CONF_VERSION) == 3: if config.get(CONF_VERSION) == 3:
if os.path.exists(tokenfile): if os.path.exists(tokenfile):
tokenfile = open(tokenfile) with open(tokenfile) as tokenfile:
token = tokenfile.read() token = tokenfile.read()
tokenfile.close()
else: else:
try: try:
token = greenwave.grab_token(host, 'hass', 'homeassistant') token = greenwave.grab_token(host, 'hass', 'homeassistant')
except PermissionError: except PermissionError:
_LOGGER.error('The Gateway Is Not In Sync Mode') _LOGGER.error('The Gateway Is Not In Sync Mode')
raise raise
tokenfile = open(tokenfile, "w+") with open(tokenfile, "w+") as tokenfile:
tokenfile.write(token) tokenfile.write(token)
tokenfile.close()
else: else:
token = None token = None
bulbs = greenwave.grab_bulbs(host, token) bulbs = greenwave.grab_bulbs(host, token)

View File

@ -44,7 +44,8 @@ def get_service(hass, config, discovery_info=None):
if config.get(CONF_APP_ICON) is None: if config.get(CONF_APP_ICON) is None:
icon_file = os.path.join(os.path.dirname(__file__), "..", "frontend", icon_file = os.path.join(os.path.dirname(__file__), "..", "frontend",
"www_static", "icons", "favicon-192x192.png") "www_static", "icons", "favicon-192x192.png")
app_icon = open(icon_file, 'rb').read() with open(icon_file, 'rb') as file:
app_icon = file.read()
else: else:
app_icon = config.get(CONF_APP_ICON) app_icon = config.get(CONF_APP_ICON)

View File

@ -66,8 +66,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
device_file, 'temperature')) device_file, 'temperature'))
else: else:
for family_file_path in glob(os.path.join(base_dir, '*', 'family')): for family_file_path in glob(os.path.join(base_dir, '*', 'family')):
family_file = open(family_file_path, "r") with open(family_file_path, "r") as family_file:
family = family_file.read() family = family_file.read()
if family in DEVICE_SENSORS: if family in DEVICE_SENSORS:
for sensor_key, sensor_value in DEVICE_SENSORS[family].items(): for sensor_key, sensor_value in DEVICE_SENSORS[family].items():
sensor_id = os.path.split( sensor_id = os.path.split(