From 3d84f31876c3b4926b793cbd30ab3d6697fb75e5 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Sat, 24 May 2014 15:24:23 +0200 Subject: [PATCH] Use with statement Use with statement when working with files whenever possible. Use os.makedirs() to create Glances target directory and all parent ones as needed. --- glances/core/glances_password.py | 36 +++++++++----------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/glances/core/glances_password.py b/glances/core/glances_password.py index f4ba33c9..673eb30e 100644 --- a/glances/core/glances_password.py +++ b/glances/core/glances_password.py @@ -145,41 +145,25 @@ class glancesPassword: """ Save the hashed password to the Glances folder """ - - # Check if the Glances folder already exist + # Check if the Glances folder already exists if not os.path.exists(self.password_path): # Create the Glances folder try: - os.mkdir(self.password_path) - except Exception as e: - sys.stdout.write(_("[Warning] Glances application data folder can not be created (%s)\n") % e) + os.makedirs(self.password_path) + except OSError as e: + print(_("[Warning] Cannot create Glances directory: {0}").format(e)) return - # Create/overwrite the password file to the Glances folder - try: - file_pwd = open(self.password_filepath, 'w') - except Exception as e: - sys.stdout.write(_("[Warning] Glances wan not create the password file (%s)\n") % e) - return - - # Generate the password file - file_pwd.write(hashed_password) - file_pwd.close() + # Create/overwrite the password file + with open(self.password_filepath, 'w') as file_pwd: + file_pwd.write(hashed_password) def load_password(self): """ Load the hashed password from the Glances folder """ - - # Create/overwrite the password file to the Glances folder - try: - file_pwd = open(self.password_filepath, 'r') - except Exception as e: - sys.stdout.write(_("[Warning] Glances wan not read the password file (%s)\n") % e) - return None - - # Read the password file - hashed_password = file_pwd.read() - file_pwd.close() + # Read the password file, if it exists + with open(self.password_filepath, 'r') as file_pwd: + hashed_password = file_pwd.read() return hashed_password