Use snake_case for method names
This commit is contained in:
@@ -33,27 +33,27 @@ def is_username_valid(username):
|
||||
re.match(r"^[A-Za-z0-9._-]*$", username) and not re.match(r"^\.*$", username)
|
||||
|
||||
|
||||
def isYes(val):
|
||||
def is_yes(val):
|
||||
return val and val.lower() in YESES
|
||||
|
||||
|
||||
def isNo(val):
|
||||
return val and not isYes(val)
|
||||
def is_no(val):
|
||||
return val and not is_yes(val)
|
||||
|
||||
|
||||
def nonEmptyOrNone(str):
|
||||
def nonempty_or_none(str):
|
||||
if str is None or str == "":
|
||||
return None
|
||||
|
||||
return str
|
||||
|
||||
|
||||
def shouldReturnJson():
|
||||
def should_return_json():
|
||||
return "application/json" in request.accept_mimetypes and \
|
||||
not "text/html" in request.accept_mimetypes
|
||||
|
||||
|
||||
def randomString(n):
|
||||
def random_string(n):
|
||||
return secrets.token_hex(int(n / 2))
|
||||
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ from urllib.parse import urlsplit
|
||||
from git import GitCommandError
|
||||
|
||||
from app.tasks import TaskError
|
||||
from app.utils import randomString
|
||||
from app.utils import random_string
|
||||
|
||||
|
||||
def generate_git_url(urlstr):
|
||||
@@ -40,7 +40,7 @@ def generate_git_url(urlstr):
|
||||
|
||||
@contextlib.contextmanager
|
||||
def get_temp_dir():
|
||||
temp = os.path.join(tempfile.gettempdir(), randomString(10))
|
||||
temp = os.path.join(tempfile.gettempdir(), random_string(10))
|
||||
yield temp
|
||||
shutil.rmtree(temp)
|
||||
|
||||
@@ -50,21 +50,21 @@ def get_temp_dir():
|
||||
# Throws `TaskError` on failure.
|
||||
# Caller is responsible for deleting returned directory.
|
||||
@contextlib.contextmanager
|
||||
def clone_repo(urlstr, ref=None, recursive=False):
|
||||
gitDir = os.path.join(tempfile.gettempdir(), randomString(10))
|
||||
def clone_repo(url_str, ref=None, recursive=False):
|
||||
git_dir = os.path.join(tempfile.gettempdir(), random_string(10))
|
||||
|
||||
try:
|
||||
gitUrl = generate_git_url(urlstr)
|
||||
print("Cloning from " + gitUrl)
|
||||
git_url = generate_git_url(url_str)
|
||||
print("Cloning from " + git_url)
|
||||
|
||||
if ref is None:
|
||||
repo = git.Repo.clone_from(gitUrl, gitDir,
|
||||
repo = git.Repo.clone_from(git_url, git_dir,
|
||||
progress=None, env=None, depth=1, recursive=recursive, kill_after_timeout=15)
|
||||
else:
|
||||
assert ref != ""
|
||||
|
||||
repo = git.Repo.init(gitDir)
|
||||
origin = repo.create_remote("origin", url=gitUrl)
|
||||
repo = git.Repo.init(git_dir)
|
||||
origin = repo.create_remote("origin", url=git_url)
|
||||
assert origin.exists()
|
||||
origin.fetch()
|
||||
repo.git.checkout(ref)
|
||||
@@ -72,7 +72,7 @@ def clone_repo(urlstr, ref=None, recursive=False):
|
||||
repo.git.submodule('update', '--init')
|
||||
|
||||
yield repo
|
||||
shutil.rmtree(gitDir)
|
||||
shutil.rmtree(git_dir)
|
||||
return
|
||||
|
||||
except GitCommandError as e:
|
||||
@@ -83,7 +83,7 @@ def clone_repo(urlstr, ref=None, recursive=False):
|
||||
err = "Unable to find the reference " + (ref or "?") + "\n" + e.stderr
|
||||
|
||||
raise TaskError(err.replace("stderr: ", "") \
|
||||
.replace("Cloning into '" + gitDir + "'...", "") \
|
||||
.replace("Cloning into '" + git_dir + "'...", "") \
|
||||
.strip())
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ from sqlalchemy.orm import sessionmaker
|
||||
from app.models import User, NotificationType, Package, UserRank, Notification, db, AuditSeverity, AuditLogEntry, ThreadReply, Thread, PackageState, PackageType, PackageAlias
|
||||
|
||||
|
||||
def getPackageByInfo(author, name):
|
||||
def get_package_by_info(author, name):
|
||||
user = User.query.filter_by(username=author).first()
|
||||
if user is None:
|
||||
return None
|
||||
@@ -39,6 +39,7 @@ def getPackageByInfo(author, name):
|
||||
|
||||
return package
|
||||
|
||||
|
||||
def is_package_page(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
@@ -48,9 +49,9 @@ def is_package_page(f):
|
||||
author = kwargs["author"]
|
||||
name = kwargs["name"]
|
||||
|
||||
package = getPackageByInfo(author, name)
|
||||
package = get_package_by_info(author, name)
|
||||
if package is None:
|
||||
package = getPackageByInfo(author, name + "_game")
|
||||
package = get_package_by_info(author, name + "_game")
|
||||
if package and package.type == PackageType.GAME:
|
||||
args = dict(kwargs)
|
||||
args["name"] = name + "_game"
|
||||
@@ -72,28 +73,28 @@ def is_package_page(f):
|
||||
return decorated_function
|
||||
|
||||
|
||||
def addNotification(target, causer: User, type: NotificationType, title: str, url: str, package: Package = None):
|
||||
def add_notification(target, causer: User, type: NotificationType, title: str, url: str, package: Package = None):
|
||||
try:
|
||||
iter(target)
|
||||
for x in target:
|
||||
addNotification(x, causer, type, title, url, package)
|
||||
add_notification(x, causer, type, title, url, package)
|
||||
return
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
if target.rank.atLeast(UserRank.NEW_MEMBER) and target != causer:
|
||||
if target.rank.at_least(UserRank.NEW_MEMBER) and target != causer:
|
||||
Notification.query.filter_by(user=target, causer=causer, type=type, title=title, url=url, package=package).delete()
|
||||
notif = Notification(target, causer, type, title, url, package)
|
||||
db.session.add(notif)
|
||||
|
||||
|
||||
def addAuditLog(severity: AuditSeverity, causer: User, title: str, url: typing.Optional[str],
|
||||
package: Package = None, description: str = None):
|
||||
def add_audit_log(severity: AuditSeverity, causer: User, title: str, url: typing.Optional[str],
|
||||
package: Package = None, description: str = None):
|
||||
entry = AuditLogEntry(causer, severity, title, url, package, description)
|
||||
db.session.add(entry)
|
||||
|
||||
|
||||
def clearNotifications(url):
|
||||
def clear_notifications(url):
|
||||
if current_user.is_authenticated:
|
||||
Notification.query.filter_by(user=current_user, url=url).delete()
|
||||
db.session.commit()
|
||||
@@ -105,12 +106,12 @@ def get_system_user():
|
||||
return system_user
|
||||
|
||||
|
||||
def addSystemNotification(target, type: NotificationType, title: str, url: str, package: Package = None):
|
||||
return addNotification(target, get_system_user(), type, title, url, package)
|
||||
def add_system_notification(target, type: NotificationType, title: str, url: str, package: Package = None):
|
||||
return add_notification(target, get_system_user(), type, title, url, package)
|
||||
|
||||
|
||||
def addSystemAuditLog(severity: AuditSeverity, title: str, url: str, package=None, description=None):
|
||||
return addAuditLog(severity, get_system_user(), title, url, package, description)
|
||||
def add_system_audit_log(severity: AuditSeverity, title: str, url: str, package=None, description=None):
|
||||
return add_audit_log(severity, get_system_user(), title, url, package, description)
|
||||
|
||||
|
||||
def post_bot_message(package: Package, title: str, message: str):
|
||||
@@ -133,8 +134,7 @@ def post_bot_message(package: Package, title: str, message: str):
|
||||
reply.comment = "**{}**\n\n{}\n\nThis is an automated message, but you can reply if you need help".format(title, message)
|
||||
db.session.add(reply)
|
||||
|
||||
addNotification(thread.watchers, system_user, NotificationType.BOT,
|
||||
title, thread.get_view_url(), thread.package)
|
||||
add_notification(thread.watchers, system_user, NotificationType.BOT, title, thread.get_view_url(), thread.package)
|
||||
|
||||
thread.replies.append(reply)
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@ from urllib.parse import urlencode
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
def urlEncodeNonAscii(b):
|
||||
def url_encode_non_ascii(b):
|
||||
return re.sub('[\x80-\xFF]', lambda c: '%%%02x' % ord(c.group(0)), b)
|
||||
|
||||
|
||||
class Profile:
|
||||
def __init__(self, username):
|
||||
self.username = username
|
||||
@@ -31,6 +32,7 @@ class Profile:
|
||||
def __str__(self):
|
||||
return self.username + "\n" + str(self.signature) + "\n" + str(self.properties)
|
||||
|
||||
|
||||
def __extract_properties(profile, soup):
|
||||
el = soup.find(id="viewprofile")
|
||||
if el is None:
|
||||
@@ -66,6 +68,7 @@ def __extract_properties(profile, soup):
|
||||
elif element and element.name is not None:
|
||||
print("Unexpected other")
|
||||
|
||||
|
||||
def __extract_signature(soup):
|
||||
res = soup.find_all("div", class_="signature")
|
||||
if len(res) != 1:
|
||||
@@ -74,7 +77,7 @@ def __extract_signature(soup):
|
||||
return str(res[0])
|
||||
|
||||
|
||||
def getProfileURL(url, username):
|
||||
def get_profile_url(url, username):
|
||||
url = urlparse.urlparse(url)
|
||||
|
||||
# Update path
|
||||
@@ -89,8 +92,8 @@ def getProfileURL(url, username):
|
||||
return urlparse.urlunparse(url)
|
||||
|
||||
|
||||
def getProfile(url, username):
|
||||
url = getProfileURL(url, username)
|
||||
def get_profile(url, username):
|
||||
url = get_profile_url(url, username)
|
||||
|
||||
try:
|
||||
req = urllib.request.urlopen(url, timeout=15)
|
||||
@@ -114,7 +117,8 @@ def getProfile(url, username):
|
||||
|
||||
regex_id = re.compile(r"^.*t=([0-9]+).*$")
|
||||
|
||||
def parseForumListPage(id, page, out, extra=None):
|
||||
|
||||
def parse_forum_list_page(id, page, out, extra=None):
|
||||
num_per_page = 30
|
||||
start = page*num_per_page+1
|
||||
print(" - Fetching page {} (topics {}-{})".format(page, start, start+num_per_page))
|
||||
@@ -171,15 +175,11 @@ def parseForumListPage(id, page, out, extra=None):
|
||||
|
||||
return True
|
||||
|
||||
def getTopicsFromForum(id, out, extra=None):
|
||||
|
||||
def get_topics_from_forum(id, out, extra=None):
|
||||
print("Fetching all topics from forum {}".format(id))
|
||||
page = 0
|
||||
while parseForumListPage(id, page, out, extra):
|
||||
while parse_forum_list_page(id, page, out, extra):
|
||||
page = page + 1
|
||||
|
||||
return out
|
||||
|
||||
def dumpTitlesToFile(topics, path):
|
||||
with open(path, "w") as out_file:
|
||||
for topic in topics.values():
|
||||
out_file.write(topic["title"] + "\n")
|
||||
|
||||
@@ -76,7 +76,7 @@ def rank_required(rank):
|
||||
def decorated_function(*args, **kwargs):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(url_for("users.login"))
|
||||
if not current_user.rank.atLeast(rank):
|
||||
if not current_user.rank.at_least(rank):
|
||||
abort(403)
|
||||
|
||||
return f(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user