Remove redundant else blocks

This commit is contained in:
flan 2017-11-08 13:23:48 +01:00
parent c0ea23c307
commit aae65cc5d8
3 changed files with 49 additions and 54 deletions

View File

@ -51,9 +51,7 @@ class CollectionWrapper:
try: try:
os.makedirs(dirname) os.makedirs(dirname)
except OSError as exc: except OSError as exc:
if exc.errno == errno.EEXIST: if exc.errno != errno.EEXIST:
pass
else:
raise raise
col = anki.storage.Collection(self.path, server=True) col = anki.storage.Collection(self.path, server=True)

View File

@ -161,18 +161,18 @@ class SyncMediaHandler(anki.sync.MediaSyncer):
for i in zip_file.infolist(): for i in zip_file.infolist():
if i.filename == "_meta": # Ignore previously retrieved metadata. if i.filename == "_meta": # Ignore previously retrieved metadata.
continue continue
else:
file_data = zip_file.read(i)
csum = anki.utils.checksum(file_data)
filename = self._normalize_filename(meta[int(i.filename)][0])
file_path = os.path.join(self.col.media.dir(), filename)
# Save file to media directory. file_data = zip_file.read(i)
with open(file_path, 'wb') as f: csum = anki.utils.checksum(file_data)
f.write(file_data) filename = self._normalize_filename(meta[int(i.filename)][0])
mtime = self.col.media._mtime(file_path) file_path = os.path.join(self.col.media.dir(), filename)
media_to_add.append((filename, csum, mtime, 0)) # Save file to media directory.
with open(file_path, 'wb') as f:
f.write(file_data)
mtime = self.col.media._mtime(file_path)
media_to_add.append((filename, csum, mtime, 0))
# We count all files we are to remove, even if we don't have them in # We count all files we are to remove, even if we don't have them in
# our media directory and our db doesn't know about them. # our media directory and our db doesn't know about them.

View File

@ -51,15 +51,15 @@ class SqliteUserManager(SimpleUserManager):
if not self.auth_db_exists(): if not self.auth_db_exists():
raise ValueError("Cannot list users for nonexistent auth db {}." raise ValueError("Cannot list users for nonexistent auth db {}."
.format(self.auth_db_path)) .format(self.auth_db_path))
else:
conn = sqlite.connect(self.auth_db_path)
cursor = conn.cursor()
cursor.execute("SELECT user FROM auth")
rows = cursor.fetchall()
conn.commit()
conn.close()
return [row[0] for row in rows] conn = sqlite.connect(self.auth_db_path)
cursor = conn.cursor()
cursor.execute("SELECT user FROM auth")
rows = cursor.fetchall()
conn.commit()
conn.close()
return [row[0] for row in rows]
def user_exists(self, username): def user_exists(self, username):
users = self.user_list() users = self.user_list()
@ -69,14 +69,13 @@ class SqliteUserManager(SimpleUserManager):
if not self.auth_db_exists(): if not self.auth_db_exists():
raise ValueError("Cannot remove user from nonexistent auth db {}." raise ValueError("Cannot remove user from nonexistent auth db {}."
.format(self.auth_db_path)) .format(self.auth_db_path))
else:
conn = sqlite.connect(self.auth_db_path) conn = sqlite.connect(self.auth_db_path)
cursor = conn.cursor() cursor = conn.cursor()
logging.info("Removing user '{}' from auth db." logging.info("Removing user '{}' from auth db".format(username))
.format(username)) cursor.execute("DELETE FROM auth WHERE user=?", (username,))
cursor.execute("DELETE FROM auth WHERE user=?", (username,)) conn.commit()
conn.commit() conn.close()
conn.close()
def add_user(self, username, password): def add_user(self, username, password):
self._add_user_to_auth_db(username, password) self._add_user_to_auth_db(username, password)
@ -102,21 +101,21 @@ class SqliteUserManager(SimpleUserManager):
def set_password_for_user(self, username, new_password): def set_password_for_user(self, username, new_password):
if not self.auth_db_exists(): if not self.auth_db_exists():
raise ValueError("Cannot remove user from nonexistent auth db {}." raise ValueError("Cannot remove user from nonexistent auth db {}"
.format(self.auth_db_path)) .format(self.auth_db_path))
elif not self.user_exists(username): elif not self.user_exists(username):
raise ValueError("Cannot remove nonexistent user {}." raise ValueError("Cannot remove nonexistent user {}"
.format(username)) .format(username))
else:
hash = self._create_pass_hash(username, new_password)
conn = sqlite.connect(self.auth_db_path) hash = self._create_pass_hash(username, new_password)
cursor = conn.cursor()
cursor.execute("UPDATE auth SET hash=? WHERE user=?", (hash, username))
conn.commit()
conn.close()
logging.info("Changed password for user {}.".format(username)) conn = sqlite.connect(self.auth_db_path)
cursor = conn.cursor()
cursor.execute("UPDATE auth SET hash=? WHERE user=?", (hash, username))
conn.commit()
conn.close()
logging.info("Changed password for user {}".format(username))
def authenticate(self, username, password): def authenticate(self, username, password):
"""Returns True if this username is allowed to connect with this password. False otherwise.""" """Returns True if this username is allowed to connect with this password. False otherwise."""
@ -132,22 +131,20 @@ class SqliteUserManager(SimpleUserManager):
logging.info("Authentication failed for nonexistent user {}." logging.info("Authentication failed for nonexistent user {}."
.format(username)) .format(username))
return False return False
expected_value = str(db_hash[0])
salt = self._extract_salt(expected_value)
hashobj = hashlib.sha256()
hashobj.update((username + password + salt).encode())
actual_value = hashobj.hexdigest() + salt
if actual_value == expected_value:
logging.info("Authentication succeeded for user {}".format(username))
return True
else: else:
expected_value = str(db_hash[0]) logging.info("Authentication failed for user {}".format(username))
salt = self._extract_salt(expected_value) return False
hashobj = hashlib.sha256()
hashobj.update((username + password + salt).encode())
actual_value = hashobj.hexdigest() + salt
if actual_value == expected_value:
logging.info("Authentication succeeded for user {}."
.format(username))
return True
else:
logging.info("Authentication failed for user {}."
.format(username))
return False
@staticmethod @staticmethod
def _extract_salt(hash): def _extract_salt(hash):