master: PEP checks

This commit is contained in:
Eragon 2023-10-04 12:53:31 +02:00
parent 6e24bcfdab
commit 3c03cc2016
Signed by: Eragon
GPG Key ID: 087126EBFC725006
1 changed files with 24 additions and 16 deletions

View File

@ -11,7 +11,6 @@ import os
import sys
import yaml
import slugify
import readline
from PIL import Image
from PIL.Image import Resampling
@ -43,13 +42,16 @@ Install and update commands:
# Listing commands
#
def members(*args):
for m in Member.query.all():
print(m)
def groups(*args):
for g in Group.query.all():
print(f"#{g.id} {g.name}")
print(f"#{g.id} {g.name}")
def forums(*args):
for f in Forum.query.all():
@ -57,10 +59,12 @@ def forums(*args):
print(f"{f.url} ({parent}) [{f.prefix}]: {f.name}")
print(f" {f.descr}")
def trophies(*args):
for t in Trophy.query.all():
print(t)
def tags(*args):
tags = TagInformation.query.all()
for t in sorted(tags, key=lambda t: t.id):
@ -70,6 +74,7 @@ def tags(*args):
# Install and update commands
#
def update_all():
update_groups()
update_forums()
@ -78,18 +83,17 @@ def update_all():
update_tags()
generate_trophy_icons()
def update_groups():
existing = Group.query.all()
def update_groups():
gr = []
with open(os.path.join(app.root_path, "data", "groups.yaml")) as fp:
gr = yaml.safe_load(fp.read())
for group_info in gr:
name = group_info["name"]
css = group_info.get("css", "")
descr = group_info.get("descr", "")
privs = group_info.get("privs", "").split()
name = group_info["name"]
css = group_info.get("css", "")
descr = group_info.get("descr", "")
privs = group_info.get("privs", "").split()
g = Group.query.filter_by(name=name).first()
@ -180,7 +184,7 @@ def update_trophies():
# Get the list of what we want to obtain
with open(os.path.join(app.root_path, "data", "trophies.yaml")) as fp:
tr = yaml.safe_load(fp.read())
tr = { t["name"]: t for t in tr }
tr = {t["name"]: t for t in tr}
# Remove trophies that we don't want or that we want as a different type
for t in existing:
@ -202,8 +206,8 @@ def update_trophies():
# Updating existing trophies
if trophy is not None:
changes = (trophy.description != description) or \
(trophy.hidden != t["hidden"] or (isinstance(trophy,Title) and \
trophy.css != css))
(trophy.hidden != t["hidden"] or (isinstance(trophy, Title) and
trophy.css != css))
trophy.description = description
trophy.hidden = t["hidden"]
if isinstance(trophy, Title):
@ -229,8 +233,8 @@ def update_tags():
with open(os.path.join(app.root_path, "data", "tags.yaml")) as fp:
data = yaml.safe_load(fp.read())
tags = { ctgy + "." + name: data[ctgy][name]
for ctgy in data for name in data[ctgy] }
tags = {ctgy + "." + name: data[ctgy][name]
for ctgy in data for name in data[ctgy]}
# Remove bad tags
for t in existing:
@ -257,6 +261,7 @@ def update_tags():
db.session.commit()
def generate_trophy_icons():
tr = []
with open(os.path.join(app.root_path, "data", "trophies.yaml")) as fp:
@ -279,7 +284,7 @@ def generate_trophy_icons():
icon = img.crop((26*x+1, 26*y+1, 26*x+25, 26*y+25))
# Skip blank squares in the source image
if len(icon.getcolors()) > 1:
yield icon.resize((48,48), resample=Resampling.NEAREST)
yield icon.resize((48, 48), resample=Resampling.NEAREST)
for (name, icon) in zip(names, trophy_iterator(img)):
icon.save(os.path.join(dst, f"{name}.png"))
@ -340,8 +345,8 @@ def enable_user(member):
norm = unicode_names.normalize(member)
m = Member.query.filter_by(norm=norm).first()
if m is None:
print(f"error: no member has a normalized name of '{norm}'")
return
print(f"error: no member has a normalized name of '{norm}'")
return
m.email_confirmed = True
db.session.add(m)
@ -351,6 +356,7 @@ def enable_user(member):
# Main program
#
commands = {
"exit": lambda: sys.exit(0),
"help": lambda: print(help_msg),
@ -370,12 +376,14 @@ commands = {
"enable-user": enable_user,
}
def execute(cmd):
if cmd[0] not in commands:
print(f"error: unknown command '{cmd[0]}'")
else:
commands[cmd[0]](*cmd[1:])
# If a command is specified on the command-line, use it and do not prompt
if len(sys.argv) > 1:
execute(sys.argv[1:])