Changeset - bfccdcd998fc
[Not reviewed]
default
0 4 0
Dennis Fink - 3 years ago 2022-06-10 09:36:16
dennis.fink@c3l.lu
Remove dead code
4 files changed with 6 insertions and 21 deletions:
0 comments (0 inline, 0 general)
spaceapi/__init__.py
Show inline comments
 
@@ -90,18 +90,17 @@ def create_app():
 
    from .views import root_views
 

	
 
    app.register_blueprint(root_views)
 

	
 
    from .state import state_views
 

	
 
    app.register_blueprint(state_views, url_prefix="/state")
 

	
 
    from .sensors import sensors_views
 

	
 
    app.register_blueprint(sensors_views, url_prefix="/sensors")
 

	
 
    from .utils import ActiveStatus, ActiveStatusv14
 
    from .utils import ActiveStatusv14
 

	
 
    ActiveStatus().reload()
 
    ActiveStatusv14().reload()
 

	
 
    return app
spaceapi/sensors.py
Show inline comments
 
import json
 
from functools import partial
 

	
 
import jsonschema
 
from flask import Blueprint, abort, current_app, jsonify, request
 
from pkg_resources import resource_filename
 

	
 
from .auth import httpauth
 
from .utils import ActiveStatus, ActiveStatusv14, first, fuzzy_list_find
 
from .utils import ActiveStatusv14, first, fuzzy_list_find
 

	
 
sensors_views = Blueprint("sensors", __name__)
 

	
 
ALLOWED_SENSORS_KEYS = json.load(
 
    open(resource_filename("spaceapi", "schema/sensors.json"), encoding="utf-8")
 
)
 

	
 
RADIATON_SUBKEYS = frozenset(("alpha", "beta", "gamma", "beta_gamma"))
 

	
 
get_identification_key = partial(first, keys=frozenset(("name", "location")))
 

	
 

	
spaceapi/utils.py
Show inline comments
 
import json
 
import os.path
 
import random
 
from functools import wraps
 
from time import time
 

	
 
import mastodon
 
import tweepy
 
from flask import current_app, request
 

	
 
default_json_file = os.path.abspath("default.json")
 
last_state_file = os.path.abspath("laststate.json")
 

	
 
default_json_file_v14 = os.path.abspath("default_v14.json")
 
last_state_file_v14 = os.path.abspath("laststate_v14.json")
 

	
 
if not os.path.exists(default_json_file):
 
    raise RuntimeError("default.json does not exists!")
 
elif not os.path.isfile(default_json_file):
 
    raise RuntimeError("default.json is not a file!")
 

	
 
if not os.path.exists(default_json_file_v14):
 
    raise RuntimeError("default_v14.json does not exists!")
 
elif not os.path.isfile(default_json_file):
 
    raise RuntimeError("default_v14.json is not a file!")
 

	
 

	
 
possible_open_tweets = (
 
    "The space is now open!",
 
    "The space is open! Come in and hack something!",
 
    "Yes, we're open! Come in and create something!",
 
    "Come by and hack something! We've just opened!",
 
    "The ChaosStuff is now open for everyone!",
 
@@ -80,28 +72,28 @@ class Singleton:
 
    def __new__(cls, *args, **kwargs):
 
        key = str(hash(cls))
 

	
 
        if not hasattr(cls, "_instance_dict"):
 
            cls._instance_dict = {}
 

	
 
        if key not in cls._instance_dict:
 
            cls._instance_dict[key] = super().__new__(cls, *args, **kwargs)
 

	
 
        return cls._instance_dict[key]
 

	
 

	
 
class ActiveStatus(Singleton, dict):
 
class ActiveStatusv14(Singleton, dict):
 
    def __init__(self):
 
        self.default_json_file = default_json_file
 
        self.last_state_file = last_state_file
 
        self.default_json_file = default_json_file_v14
 
        self.last_state_file = last_state_file_v14
 

	
 
    def reload(self):
 

	
 
        with open(self.default_json_file, encoding="utf-8") as f:
 
            self.update(json.load(f))
 

	
 
        if os.path.exists(self.last_state_file) and os.path.isfile(
 
            self.last_state_file
 
        ):
 
            with open(self.last_state_file, encoding="utf-8") as f:
 
                last_state_json = json.load(f)
 

	
 
@@ -199,30 +191,24 @@ class ActiveStatus(Singleton, dict):
 
            if trigger_person is None:
 
                if "trigger_person" in self["state"]:
 
                    del self["state"]["trigger_person"]
 
            else:
 
                self["state"]["trigger_person"] = trigger_person
 

	
 
            self["state"]["lastchange"] = int(time())
 

	
 
        if message is not None and message:
 
            self["state"]["message"] = message
 

	
 

	
 
class ActiveStatusv14(ActiveStatus):
 
    def __init__(self):
 
        self.default_json_file = default_json_file_v14
 
        self.last_state_file = last_state_file_v14
 

	
 

	
 
def request_wants_json():
 
    best = request.accept_mimetypes.best_match(["application/json", "text/html"])
 
    return (
 
        best == "application/json"
 
        and request.accept_mimetypes[best] > request.accept_mimetypes["text/html"]
 
    )
 

	
 

	
 
def fuzzy_list_find(lst, key, value):
 

	
 
    for i, dic in enumerate(lst):
 
        if dic[key] == value:
spaceapi/views.py
Show inline comments
 
from flask import (
 
    Blueprint,
 
    abort,
 
    current_app,
 
    jsonify,
 
    redirect,
 
    render_template,
 
    request,
 
    url_for,
 
)
 

	
 
from .auth import basicauth, httpauth
 
from .utils import ActiveStatus, ActiveStatusv14, request_wants_json
 
from .utils import ActiveStatusv14, request_wants_json
 

	
 
root_views = Blueprint("root", __name__)
 

	
 

	
 
@root_views.route("/")
 
def index():
 
    if request_wants_json():
 
        return jsonify(ActiveStatusv14())
 
    return render_template("index.html", status=ActiveStatusv14())
 

	
 

	
 
@root_views.route("/status.json")
0 comments (0 inline, 0 general)