Skip to content

Commit

Permalink
switch file loading to pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jketterl committed Dec 8, 2019
1 parent 21cb0e8 commit 7a6d021
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
**/*.swp
tags
.idea
packages
Empty file added htdocs/__init__.py
Empty file.
42 changes: 28 additions & 14 deletions owrx/controllers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import mimetypes
import json
import pkg_resources
from datetime import datetime
from string import Template
from owrx.websocket import WebSocketConnection
Expand Down Expand Up @@ -55,32 +56,33 @@ def handle_request(self):


class AssetsController(Controller):
def __init__(self, handler, request, path):
if not path.endswith("/"):
path += "/"
self.path = path
super().__init__(handler, request)
def getModified(self, file):
return None

def openFile(self, file):
pass

def serve_file(self, file, content_type=None):
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(
self.handler.headers["If-Modified-Since"], "%a, %d %b %Y %H:%M:%S %Z"
)
if modified <= client_modified:
self.send_response("", code=304)
return

f = open(self.path + file, "rb")
f = self.openFile(file)
data = f.read()
f.close()

if content_type is None:
(content_type, encoding) = mimetypes.MimeTypes().guess_type(file)
self.send_response(data, content_type=content_type, last_modified=modified, max_age=3600)
except FileNotFoundError:
logger.debug("404")
self.send_response("file not found", code=404)

def handle_request(self):
Expand All @@ -89,21 +91,33 @@ def handle_request(self):


class OwrxAssetsController(AssetsController):
def __init__(self, handler, request):
super().__init__(handler, request, "htdocs/")
def openFile(self, file):
return pkg_resources.resource_stream('htdocs', file)


class AprsSymbolsController(AssetsController):
def __init__(self, handler, request):
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):
def render_template(self, file, **vars):
f = open("htdocs/" + file, "r", encoding="utf-8")
template = Template(f.read())
f.close()
file_content = pkg_resources.resource_string("htdocs", file).decode("utf-8")
template = Template(file_content)

return template.safe_substitute(**vars)

Expand Down

0 comments on commit 7a6d021

Please sign in to comment.