switch file loading to pkg_resources
This commit is contained in:
parent
21cb0e8feb
commit
7a6d021e18
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
**/*.swp
|
**/*.swp
|
||||||
tags
|
tags
|
||||||
.idea
|
.idea
|
||||||
|
packages
|
||||||
|
0
htdocs/__init__.py
Normal file
0
htdocs/__init__.py
Normal file
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import json
|
import json
|
||||||
|
import pkg_resources
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from string import Template
|
from string import Template
|
||||||
from owrx.websocket import WebSocketConnection
|
from owrx.websocket import WebSocketConnection
|
||||||
@ -55,17 +56,17 @@ class StatusController(Controller):
|
|||||||
|
|
||||||
|
|
||||||
class AssetsController(Controller):
|
class AssetsController(Controller):
|
||||||
def __init__(self, handler, request, path):
|
def getModified(self, file):
|
||||||
if not path.endswith("/"):
|
return None
|
||||||
path += "/"
|
|
||||||
self.path = path
|
def openFile(self, file):
|
||||||
super().__init__(handler, request)
|
pass
|
||||||
|
|
||||||
def serve_file(self, file, content_type=None):
|
def serve_file(self, file, content_type=None):
|
||||||
try:
|
try:
|
||||||
modified = datetime.fromtimestamp(os.path.getmtime(self.path + file))
|
modified = self.getModified(file)
|
||||||
|
|
||||||
if "If-Modified-Since" in self.handler.headers:
|
if modified is not None and "If-Modified-Since" in self.handler.headers:
|
||||||
client_modified = datetime.strptime(
|
client_modified = datetime.strptime(
|
||||||
self.handler.headers["If-Modified-Since"], "%a, %d %b %Y %H:%M:%S %Z"
|
self.handler.headers["If-Modified-Since"], "%a, %d %b %Y %H:%M:%S %Z"
|
||||||
)
|
)
|
||||||
@ -73,7 +74,7 @@ class AssetsController(Controller):
|
|||||||
self.send_response("", code=304)
|
self.send_response("", code=304)
|
||||||
return
|
return
|
||||||
|
|
||||||
f = open(self.path + file, "rb")
|
f = self.openFile(file)
|
||||||
data = f.read()
|
data = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
@ -81,6 +82,7 @@ class AssetsController(Controller):
|
|||||||
(content_type, encoding) = mimetypes.MimeTypes().guess_type(file)
|
(content_type, encoding) = mimetypes.MimeTypes().guess_type(file)
|
||||||
self.send_response(data, content_type=content_type, last_modified=modified, max_age=3600)
|
self.send_response(data, content_type=content_type, last_modified=modified, max_age=3600)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
logger.debug("404")
|
||||||
self.send_response("file not found", code=404)
|
self.send_response("file not found", code=404)
|
||||||
|
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
@ -89,21 +91,33 @@ class AssetsController(Controller):
|
|||||||
|
|
||||||
|
|
||||||
class OwrxAssetsController(AssetsController):
|
class OwrxAssetsController(AssetsController):
|
||||||
def __init__(self, handler, request):
|
def openFile(self, file):
|
||||||
super().__init__(handler, request, "htdocs/")
|
return pkg_resources.resource_stream('htdocs', file)
|
||||||
|
|
||||||
|
|
||||||
class AprsSymbolsController(AssetsController):
|
class AprsSymbolsController(AssetsController):
|
||||||
def __init__(self, handler, request):
|
def __init__(self, handler, request):
|
||||||
pm = PropertyManager.getSharedInstance()
|
pm = PropertyManager.getSharedInstance()
|
||||||
super().__init__(handler, request, pm["aprs_symbols_path"])
|
path = pm["aprs_symbols_path"]
|
||||||
|
if not path.endswith("/"):
|
||||||
|
path += "/"
|
||||||
|
self.path = path
|
||||||
|
super().__init__(handler, request)
|
||||||
|
|
||||||
|
def getFilePath(self, file):
|
||||||
|
return self.path + file
|
||||||
|
|
||||||
|
def getModified(self, file):
|
||||||
|
return datetime.fromtimestamp(os.path.getmtime(self.getFilePath(file)))
|
||||||
|
|
||||||
|
def openFile(self, file):
|
||||||
|
return open(self.getFilePath(file), "rb")
|
||||||
|
|
||||||
|
|
||||||
class TemplateController(Controller):
|
class TemplateController(Controller):
|
||||||
def render_template(self, file, **vars):
|
def render_template(self, file, **vars):
|
||||||
f = open("htdocs/" + file, "r", encoding="utf-8")
|
file_content = pkg_resources.resource_string("htdocs", file).decode("utf-8")
|
||||||
template = Template(f.read())
|
template = Template(file_content)
|
||||||
f.close()
|
|
||||||
|
|
||||||
return template.safe_substitute(**vars)
|
return template.safe_substitute(**vars)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user