1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2026-04-15 23:47:24 +02:00

Fixed UP031 (printf-string-formatting): Use format specifiers instead of percent format

This commit is contained in:
Teal Dulcet
2025-06-18 04:17:03 -07:00
parent 698e8ffc72
commit c64a24e870
13 changed files with 76 additions and 79 deletions

View File

@@ -108,9 +108,9 @@ def sizeof_fmt(num):
for unit in ['','K','M','G','T']:
if abs(num) < 1024.0:
if abs(num) > 99:
return "%3.0f%s" % (num, unit)
return "{:3.0f}{}".format(num, unit)
else:
return "%2.1f%s" % (num, unit)
return "{:2.1f}{}".format(num, unit)
num /= 1024.0
@@ -148,7 +148,7 @@ def get_mail_users_ex(env, with_archived=False):
box_quota = 0
percent = ''
try:
dirsize_file = os.path.join(env['STORAGE_ROOT'], 'mail/mailboxes/%s/%s/maildirsize' % (domain, user))
dirsize_file = os.path.join(env['STORAGE_ROOT'], 'mail/mailboxes/{}/{}/maildirsize'.format(domain, user))
with open(dirsize_file, 'r') as f:
box_quota = int(f.readline().split('S')[0])
for line in f.readlines():
@@ -174,7 +174,7 @@ def get_mail_users_ex(env, with_archived=False):
"quota": quota,
"box_quota": box_quota,
"box_size": sizeof_fmt(box_size) if box_size != '?' else box_size,
"percent": '%3.0f%%' % percent if type(percent) != str else percent,
"percent": '{:3.0f}%'.format(percent) if type(percent) != str else percent,
"status": "active",
}
users.append(user)
@@ -378,7 +378,7 @@ def set_mail_password(email, pw, env):
conn, c = open_database(env, with_connection=True)
c.execute("UPDATE users SET password=? WHERE email=?", (pw, email))
if c.rowcount != 1:
return ("That's not a user (%s)." % email, 400)
return ("That's not a user ({}).".format(email), 400)
conn.commit()
return "OK"
@@ -394,7 +394,7 @@ def get_mail_quota(email, env):
c.execute("SELECT quota FROM users WHERE email=?", (email,))
rows = c.fetchall()
if len(rows) != 1:
return ("That's not a user (%s)." % email, 400)
return ("That's not a user ({}).".format(email), 400)
return rows[0][0]
@@ -407,7 +407,7 @@ def set_mail_quota(email, quota, env):
conn, c = open_database(env, with_connection=True)
c.execute("UPDATE users SET quota=? WHERE email=?", (quota, email))
if c.rowcount != 1:
return ("That's not a user (%s)." % email, 400)
return ("That's not a user ({}).".format(email), 400)
conn.commit()
dovecot_quota_recalc(email)
@@ -446,7 +446,7 @@ def get_mail_password(email, env):
c.execute('SELECT password FROM users WHERE email=?', (email,))
rows = c.fetchall()
if len(rows) != 1:
raise ValueError("That's not a user (%s)." % email)
raise ValueError("That's not a user ({}).".format(email))
return rows[0][0]
def remove_mail_user(email, env):
@@ -454,7 +454,7 @@ def remove_mail_user(email, env):
conn, c = open_database(env, with_connection=True)
c.execute("DELETE FROM users WHERE email=?", (email,))
if c.rowcount != 1:
return ("That's not a user (%s)." % email, 400)
return ("That's not a user ({}).".format(email), 400)
conn.commit()
# Update things in case any domains are removed.
@@ -470,12 +470,12 @@ def get_mail_user_privileges(email, env, empty_on_error=False):
rows = c.fetchall()
if len(rows) != 1:
if empty_on_error: return []
return ("That's not a user (%s)." % email, 400)
return ("That's not a user ({}).".format(email), 400)
return parse_privs(rows[0][0])
def validate_privilege(priv):
if "\n" in priv or priv.strip() == "":
return ("That's not a valid privilege (%s)." % priv, 400)
return ("That's not a valid privilege ({}).".format(priv), 400)
return None
def add_remove_mail_user_privilege(email, priv, action, env):
@@ -518,7 +518,7 @@ def add_mail_alias(address, forwards_to, permitted_senders, env, update_if_exist
if address == "":
return ("No email address provided.", 400)
if not validate_email(address, mode='alias'):
return ("Invalid email address (%s)." % address, 400)
return ("Invalid email address ({}).".format(address), 400)
# validate forwards_to
validated_forwards_to = []
@@ -547,7 +547,7 @@ def add_mail_alias(address, forwards_to, permitted_senders, env, update_if_exist
# Strip any +tag from email alias and check privileges
privileged_email = re.sub(r"(?=\+)[^@]*(?=@)",'',email)
if not validate_email(email):
return ("Invalid receiver email address (%s)." % email, 400)
return ("Invalid receiver email address ({}).".format(email), 400)
if is_dcv_source and not is_dcv_address(email) and "admin" not in get_mail_user_privileges(privileged_email, env, empty_on_error=True):
# Make domain control validation hijacking a little harder to mess up by
# requiring aliases for email addresses typically used in DCV to forward
@@ -567,7 +567,7 @@ def add_mail_alias(address, forwards_to, permitted_senders, env, update_if_exist
login = login.strip()
if login == "": continue
if login not in valid_logins:
return ("Invalid permitted sender: %s is not a user on this system." % login, 400)
return ("Invalid permitted sender: {} is not a user on this system.".format(login), 400)
validated_permitted_senders.append(login)
# Make sure the alias has either a forwards_to or a permitted_sender.
@@ -586,7 +586,7 @@ def add_mail_alias(address, forwards_to, permitted_senders, env, update_if_exist
return_status = "alias added"
except sqlite3.IntegrityError:
if not update_if_exists:
return ("Alias already exists (%s)." % address, 400)
return ("Alias already exists ({}).".format(address), 400)
else:
c.execute("UPDATE aliases SET destination = ?, permitted_senders = ? WHERE source = ?", (forwards_to, permitted_senders, address))
return_status = "alias updated"
@@ -606,7 +606,7 @@ def remove_mail_alias(address, env, do_kick=True):
conn, c = open_database(env, with_connection=True)
c.execute("DELETE FROM aliases WHERE source=?", (address,))
if c.rowcount != 1:
return ("That's not an alias (%s)." % address, 400)
return ("That's not an alias ({}).".format(address), 400)
conn.commit()
if do_kick: