Skip to content

Commit

Permalink
formatted with black
Browse files Browse the repository at this point in the history
  • Loading branch information
arihant2math committed Nov 7, 2023
1 parent c6725a5 commit cb1f43f
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 61 deletions.
5 changes: 4 additions & 1 deletion prod_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

@click.command()
@click.option("--db", default="sqlite:///local.db")
@click.option("--secret-key", default="secret@!%(@!%!@)*(#$)*$@!)*@!%)*@!)*&%@!132509831207549035213028579674138")
@click.option(
"--secret-key",
default="secret@!%(@!%!@)*(#$)*$@!)*@!%)*@!)*&%@!132509831207549035213028579674138",
)
def cmd(db, secret_key):
BaseConfig.SECRET_KEY = secret_key
BaseConfig.SQLALCHEMY_DATABASE_URI = db
Expand Down
31 changes: 20 additions & 11 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,47 @@

if autotraders.__version__ in blacklisted_versions:
raise ValueError(
f"Please upgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}, your version has been blacklisted, which means it is likely no longer supported by the server.")
f"Please upgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}, your version has been blacklisted, which means it is likely no longer supported by the server."
)

if autotraders_major_version > accepted_autotraders_major_version:
raise ValueError(
f"Please downgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}")
f"Please downgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}"
)

if (
autotraders_minor_version not in accepted_autotraders_minor_versions
and autotraders_minor_version < min(accepted_autotraders_minor_versions)
autotraders_minor_version not in accepted_autotraders_minor_versions
and autotraders_minor_version < min(accepted_autotraders_minor_versions)
):
raise ValueError(
f"Please upgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}")
f"Please upgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}"
)
elif (
autotraders_minor_version not in accepted_autotraders_minor_versions
and autotraders_minor_version < max(accepted_autotraders_minor_versions)
autotraders_minor_version not in accepted_autotraders_minor_versions
and autotraders_minor_version < max(accepted_autotraders_minor_versions)
):
raise ValueError(
f"Please downgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}")
f"Please downgrade autotraders to v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}"
)
elif autotraders_minor_version not in accepted_autotraders_minor_versions:
raise ValueError(
f"Please install autotraders v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}")
f"Please install autotraders v{accepted_autotraders_major_version}.{max(accepted_autotraders_minor_versions)}"
)
elif autotraders_minor_version in warning_autotraders_minor_versions:
print(
f" * Warning: Autotraders v{autotraders.__version__} is not officially supported, but limited functionality might be available, proceed at your own risk.")
f" * Warning: Autotraders v{autotraders.__version__} is not officially supported, but limited functionality might be available, proceed at your own risk."
)


@click.command()
@click.option("--debug", is_flag=True)
@click.option("--port", default=5000)
@click.option("--threaded", is_flag=True)
@click.option("--db", default="sqlite:///local.db")
@click.option("--secret-key", default="secret@!%(@!%!@)*(#$)*$@!)*@!%)*@!)*&%@!132509831207549035213028579674138")
@click.option(
"--secret-key",
default="secret@!%(@!%!@)*(#$)*$@!)*@!%)*@!)*&%@!132509831207549035213028579674138",
)
def cmd(debug, port, threaded, db, secret_key):
BaseConfig.SECRET_KEY = secret_key
BaseConfig.SQLALCHEMY_DATABASE_URI = db
Expand Down
5 changes: 4 additions & 1 deletion website/bp/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def register_api():
password = request.form["password"]
requested_user = db.session.query(User).filter_by(username=username).first()
if requested_user is None:
user = User(username=username, password=werkzeug.security.generate_password_hash(password))
user = User(
username=username,
password=werkzeug.security.generate_password_hash(password),
)
db.session.add(user)
db.session.commit()
login_session(user.username)
Expand Down
4 changes: 3 additions & 1 deletion website/bp/faction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def factions():
page = int(request.args.get("page", default=1))
factions = Faction.all(session)
new_li = paginated_return(factions, page)
return render_template("faction/factions.html", factions=factions, page=page, li=new_li)
return render_template(
"faction/factions.html", factions=factions, page=page, li=new_li
)


@faction_bp.route("/faction/<symbol>/")
Expand Down
18 changes: 13 additions & 5 deletions website/bp/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def __init__(self, token, id, active):
@local_bp.route("/select-user-api/<token_id>")
@login_required
def select_user_api(token_id, user):
active_previous = db.session.query(Token).filter_by(active=True, user=user.id).first()
active_previous = (
db.session.query(Token).filter_by(active=True, user=user.id).first()
)
if active_previous is not None:
active_previous.active = False
current = db.session.query(Token).filter_by(id=token_id, user=user.id).first()
Expand Down Expand Up @@ -128,15 +130,19 @@ def update_local_data(session):
print("Getting Systems")
try:
all_systems = []
data: list[dict[str, Any]] = session.get(str(session.base_url) + "systems.json").json()
data: list[dict[str, Any]] = session.get(
str(session.base_url) + "systems.json"
).json()
for jsys in data:
all_systems.append(System(jsys["symbol"], session, jsys))
sanitized = all_systems
for system in sanitized:
system.session = None
for waypoint in system.waypoints:
waypoint.session = None
pickle.dump(all_systems, open("data.pickle", "wb"), protocol=pickle.HIGHEST_PROTOCOL)
pickle.dump(
all_systems, open("data.pickle", "wb"), protocol=pickle.HIGHEST_PROTOCOL
)
except Exception as e:
print("Error getting systems from systems.json, getting from api: " + str(e))
all_systems = System.all(session)
Expand All @@ -149,7 +155,9 @@ def update_local_data(session):
for waypoint in system.waypoints:
waypoint.session = None

pickle.dump(sanitized, open("data.pickle", "wb"), protocol=pickle.HIGHEST_PROTOCOL)
pickle.dump(
sanitized, open("data.pickle", "wb"), protocol=pickle.HIGHEST_PROTOCOL
)

data: list[System] = sanitized
data_dict = {}
Expand All @@ -175,4 +183,4 @@ def update_local_data(session):
"num_waypoints": len(waypoints),
}
json.dump(data_dict, open("./website/static/systems.json", "w"), indent=4)
return "Success<br><a href=\"/\">Back to the home page</a>"
return 'Success<br><a href="/">Back to the home page</a>'
11 changes: 8 additions & 3 deletions website/bp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def settings():


@main_bp.route("/settings-api/")
def settings_api(): # TODO: This is a security risk, need to filter tokens instead (make sure to filter by current user id)
def settings_api(): # TODO: This is a security risk, need to filter tokens instead (make sure to filter by current user id)
pass
# users = db.session.query(User).filter_by(active=True).first()
# if users is None:
Expand All @@ -84,7 +84,9 @@ def settings_api(): # TODO: This is a security risk, need to filter tokens inste

@main_bp.route("/automations/")
def automations():
return render_template("automation/automations.html", automations=db.session.query(Automation).all())
return render_template(
"automation/automations.html", automations=db.session.query(Automation).all()
)


@main_bp.route("/new-automation/")
Expand All @@ -94,7 +96,10 @@ def new_automation():

@main_bp.route("/automation/<i>/")
def automation(i):
return render_template("automation/automation.html", automation=db.session.query(Automation).filter_by(id=i).first())
return render_template(
"automation/automation.html",
automation=db.session.query(Automation).filter_by(id=i).first(),
)


@main_bp.route("/agents/")
Expand Down
15 changes: 13 additions & 2 deletions website/bp/map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from autotraders import SpaceTradersException
from autotraders.map.system import System
from flask import Blueprint, render_template, Response, redirect, flash, url_for, jsonify
from flask import (
Blueprint,
render_template,
Response,
redirect,
flash,
url_for,
jsonify,
)

from website.wrappers import minify_html, token_required

Expand Down Expand Up @@ -35,6 +43,9 @@ def system_map_api(system, session): # TODO: Allow no token
"x": system_data.x,
"y": system_data.y,
"type": system_data.star_type,
"waypoints": [{"symbol": str(w.symbol), "x": w.x, "y": w.y, "type": w.waypoint_type} for w in system_data.waypoints]
"waypoints": [
{"symbol": str(w.symbol), "x": w.x, "y": w.y, "type": w.waypoint_type}
for w in system_data.waypoints
],
}
return jsonify(j)
37 changes: 26 additions & 11 deletions website/bp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
from flask import Blueprint, render_template, request

from website.paginated_return import paginated_return
from website.search import read_query, quick_weight, check_filters_waypoint, weight, check_filters_system, \
check_filters_faction, check_filters_ship, check_filters_contract
from website.search import (
read_query,
quick_weight,
check_filters_waypoint,
weight,
check_filters_system,
check_filters_faction,
check_filters_ship,
check_filters_contract,
)
from website.session import get_session
from website.wrappers import token_required

Expand Down Expand Up @@ -68,26 +76,31 @@ def search():
if quick_weight(query, str(item.symbol)) > -0.1:
if check_filters_system(item, filters):
unweighted_map.append((item, weight(query, str(item.symbol))))
if "waypoint" in to_query and (not fast_search or quick_weight(query, str(item.symbol)) > -0.1):
if "waypoint" in to_query and (
not fast_search or quick_weight(query, str(item.symbol)) > -0.1
):
for waypoint in item.waypoints:
if quick_weight(query, str(waypoint.symbol)) > 0 and check_filters_waypoint(
waypoint, filters
):
if quick_weight(
query, str(waypoint.symbol)
) > 0 and check_filters_waypoint(waypoint, filters):
unweighted_map.append(
(waypoint, weight(query, str(waypoint.symbol)))
)
t1_5 = time.time()
if "faction" in to_query:
for item in faction_data:
if (
quick_weight(query, item.symbol) > -0.25 or quick_weight(query, item.name) > -0.25
quick_weight(query, item.symbol) > -0.25
or quick_weight(query, item.name) > -0.25
) and check_filters_faction(item, filters):
unweighted_map.append((item, weight(query, str(item.symbol))))
t1_6 = time.time()
if "ship" in to_query and session is not None:
ship_data = Ship.all(session)[1]
for item in ship_data:
if quick_weight(query, item.symbol) > -0.25 and check_filters_ship(item, filters):
if quick_weight(query, item.symbol) > -0.25 and check_filters_ship(
item, filters
):
unweighted_map.append((item, weight(query, item.symbol)))
if "contract" in to_query and session is not None:
contract_data = Contract.all(session)[1]
Expand All @@ -97,14 +110,16 @@ def search():
):
unweighted_map.append((item, weight(query, str(item.contract_id))))
amap = [
item for item, c in sorted(unweighted_map, key=lambda x: x[1], reverse=True) if c > -0.5
item
for item, c in sorted(unweighted_map, key=lambda x: x[1], reverse=True)
if c > -0.5
]
t2 = time.time()

# print(t1_2 - t1, t1_3 - t1_2, t1_4 - t1_3, t1_5 - t1_4, t1_6 - t1_5, t2 - t1_6)

def paginate(p, num_per_page): # TODO: test properly
return amap[(p - 1) * num_per_page: p * num_per_page], len(amap)
return amap[(p - 1) * num_per_page : p * num_per_page], len(amap)

paginated_list = PaginatedList(paginate, page, 100)
new_li = paginated_return(paginated_list, page)
Expand All @@ -116,5 +131,5 @@ def paginate(p, num_per_page): # TODO: test properly
results=paginated_list,
page=page,
time=str(t2 - t1),
li=new_li
li=new_li,
)
23 changes: 11 additions & 12 deletions website/bp/ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ def ships_json_api():
ships = Ship.all(s, 1)[1]
li_json = []
for ship in ships:
li_json.append({
"symbol": ship.symbol,
"role": ship.registration.role,
"nav": {
"location": str(ship.nav.location),
"status": ship.nav.status,
"flight_mode": ship.nav.flight_mode,
li_json.append(
{
"symbol": ship.symbol,
"role": ship.registration.role,
"nav": {
"location": str(ship.nav.location),
"status": ship.nav.status,
"flight_mode": ship.nav.flight_mode,
},
}
})
)
return jsonify(li_json)


Expand Down Expand Up @@ -178,10 +180,7 @@ def siphon(name):
@ship_bp.route("/ship/<name>/chart/")
def chart(name):
s = get_session()
ship = Ship(
name,
s
) # TODO: Fix extra api request
ship = Ship(name, s) # TODO: Fix extra api request
try:
ship.chart()
return jsonify({})
Expand Down
30 changes: 21 additions & 9 deletions website/bp/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def system(symbol, session):
if query.strip() == "": # TODO: Add support for traits
query = None
if query is not None:
waypoints_list = Waypoint.all(session, symbol, waypoint_type=query.upper(), page=page)
waypoints_list = Waypoint.all(
session, symbol, waypoint_type=query.upper(), page=page
)
else:
waypoints_list = Waypoint.all(session, symbol, page=page)
new_li = paginated_return(waypoints_list, page)
Expand All @@ -42,7 +44,7 @@ def system(symbol, session):
system=System(symbol, session),
waypoints=waypoints_list,
li=new_li,
query=query
query=query,
)


Expand All @@ -55,12 +57,15 @@ def system_api_json(symbol, session):
"type": system.star_type,
"x": system.x,
"y": system.y,
"waypoints": [{
"symbol": str(waypoint.symbol),
"type": waypoint.waypoint_type,
"x": waypoint.x,
"y": waypoint.y,
} for waypoint in system.waypoints]
"waypoints": [
{
"symbol": str(waypoint.symbol),
"type": waypoint.waypoint_type,
"x": waypoint.x,
"y": waypoint.y,
}
for waypoint in system.waypoints
],
}
return jsonify(json_dict)

Expand All @@ -86,7 +91,14 @@ def waypoint(symbol, session):
c = Construction(symbol, session)
else:
c = None
return render_template("map/waypoint.html", waypoint=w, marketplace=m, shipyard=s, jumpgate=j, construction=c)
return render_template(
"map/waypoint.html",
waypoint=w,
marketplace=m,
shipyard=s,
jumpgate=j,
construction=c,
)


@system_bp.route("/waypoint/<symbol>/buy-ship/")
Expand Down
6 changes: 2 additions & 4 deletions website/paginated_return.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
def paginated_return(result_list, page: int):
li = {
result_list.pages
}
li = {result_list.pages}
for i in range(1, 6):
if result_list.pages >= i:
li.add(i)
Expand All @@ -22,4 +20,4 @@ def paginated_return(result_list, page: int):
new_li.append("..")
new_li.append(i)
prev = i
return new_li
return new_li
Loading

0 comments on commit cb1f43f

Please sign in to comment.