Commit 483cbbba authored by shadMod's avatar shadMod 💬

replaced pkg_resources with os.path with quick code clenaup

Fixes: https://code.ubuntu-it.org/ubuntu-it-web/www/-/issues/13
parent 4d70476e
...@@ -14,18 +14,16 @@ ...@@ -14,18 +14,16 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import functools import time
import uuid import uuid
import yaml
import flask import flask
import functools
import flask_openid import flask_openid
import pkg_resources
import time
import yaml
from .constants import UITWWW_DIR
from uitwww.third_party import openid_teams from uitwww.third_party import openid_teams
SESSION_EXPIRES_AFTER = 86400 SESSION_EXPIRES_AFTER = 86400
...@@ -148,8 +146,8 @@ class Sessions: ...@@ -148,8 +146,8 @@ class Sessions:
class Permissions: class Permissions:
def __init__(self): def __init__(self):
raw = pkg_resources.resource_string("uitwww", "data/permissions.yml") with open(UITWWW_DIR + "/data/permissions.yml") as fn:
self.config = yaml.safe_load(raw.decode("utf-8")) self.config = yaml.safe_load(fn.read())
def allowed_teams(self): def allowed_teams(self):
"""Return a list of teams allowed to log in""" """Return a list of teams allowed to log in"""
...@@ -177,6 +175,7 @@ class Permissions: ...@@ -177,6 +175,7 @@ class Permissions:
def permission(perms): def permission(perms):
"""Process the endpoint only if the user has permission""" """Process the endpoint only if the user has permission"""
def decorator(func): def decorator(func):
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
...@@ -200,6 +199,7 @@ def permission(perms): ...@@ -200,6 +199,7 @@ def permission(perms):
return flask.abort(403) return flask.abort(403)
return wrapper return wrapper
return decorator return decorator
...@@ -277,7 +277,6 @@ def prepare_blueprint(app): ...@@ -277,7 +277,6 @@ def prepare_blueprint(app):
flask.flash("La sessione è stata terminata correttamente.", "success") flask.flash("La sessione è stata terminata correttamente.", "success")
return flask.redirect(flask.url_for("pages.index")) return flask.redirect(flask.url_for("pages.index"))
@bp.route("/sessions") @bp.route("/sessions")
@permission("auth.sessions.manage") @permission("auth.sessions.manage")
def sessions_list(): def sessions_list():
......
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
import threading import threading
import sqlite3 import sqlite3
import pkg_resources
_LOCAL = threading.local() _LOCAL = threading.local()
......
...@@ -22,15 +22,14 @@ import random ...@@ -22,15 +22,14 @@ import random
import collections import collections
import flask import flask
import itsdangerous
import requests import requests
import pkg_resources import itsdangerous
from . import cache from . import cache
from . import launchpad from . import launchpad
from .constants import UITWWW_DIR
CONFIG_FILE = "/data/downloads.toml"
CONFIG_FILE = "data/downloads.toml"
CACHE_FILE = "download-cache.json" CACHE_FILE = "download-cache.json"
CACHE_FILE_VERSION = 1 CACHE_FILE_VERSION = 1
...@@ -39,16 +38,16 @@ class Downloads: ...@@ -39,16 +38,16 @@ class Downloads:
def __init__(self, data_path): def __init__(self, data_path):
# Load the configuration # Load the configuration
raw = pkg_resources.resource_string("uitwww", CONFIG_FILE) path = UITWWW_DIR + CONFIG_FILE
self.config = toml.loads( self.config = toml.load(
raw.decode("utf-8"), path, _dict=collections.OrderedDict,
_dict=collections.OrderedDict,
) )
self._strip_non_lts_releases() self._strip_non_lts_releases()
# Save the hash of the configuration # Save the hash of the configuration
self._config_hash = "sha1=%s" % hashlib.sha1(raw).hexdigest() with open(path, "rb") as raw:
self._config_hash = "sha1=%s" % hashlib.sha1(raw.read()).hexdigest()
self._cache_file = os.path.join(data_path, CACHE_FILE) self._cache_file = os.path.join(data_path, CACHE_FILE)
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import flask
import json
import pkg_resources
import yaml import yaml
import flask
from .constants import UITWWW_DIR
class Navbar: class Navbar:
...@@ -55,8 +55,8 @@ class Navbar: ...@@ -55,8 +55,8 @@ class Navbar:
def install(self, app): def install(self, app):
"""Install this navbar on the app""" """Install this navbar on the app"""
raw = pkg_resources.resource_string("uitwww", "data/navbar.yml") with open(UITWWW_DIR + "/data/navbar.yml") as fn:
config = yaml.load(raw.decode("utf-8")) config = yaml.safe_load(fn.read())
self._prepare_navbar_cache(config, []) self._prepare_navbar_cache(config, [])
# Add the _navbars variable to the templates # Add the _navbars variable to the templates
......
...@@ -15,10 +15,7 @@ ...@@ -15,10 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import json
import flask import flask
import pkg_resources
from uitwww import cache from uitwww import cache
......
...@@ -14,16 +14,17 @@ ...@@ -14,16 +14,17 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import flask
import pkg_resources
import yaml import yaml
import flask
import hashlib import hashlib
from .constants import UITWWW_DIR
def prepare_blueprint(app): def prepare_blueprint(app):
"""Prepare a blueprint containing all the redirects""" """Prepare a blueprint containing all the redirects"""
raw = pkg_resources.resource_string("uitwww", "data/redirects.yml") with open(UITWWW_DIR + "/data/redirects.yml") as fn:
config = yaml.safe_load(raw.decode("utf-8")) config = yaml.safe_load(fn.read())
bp = flask.Blueprint("redirects", __name__) bp = flask.Blueprint("redirects", __name__)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment