Skip to content

Commit 4488ee0

Browse files
author
Joel Collins
committed
Tidied up static path generation
1 parent a7b179b commit 4488ee0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

examples/simple_extensions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from labthings.server.find import find_component
1818
from labthings.server import fields
1919
from labthings.core.tasks import taskify
20+
from labthings.core.utilities import path_relative_to
2021
import os
2122

2223
from labthings.server.extensions import BaseExtension
@@ -38,7 +39,8 @@ def ext_on_my_component(component):
3839
logging.info(f"{component} registered and noticed by extension")
3940

4041

41-
static_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")
42+
static_folder = path_relative_to(__file__, "static")
43+
print(static_folder)
4244

4345
example_extension = BaseExtension(
4446
"org.labthings.examples.extension", static_folder=static_folder

labthings/core/utilities.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import operator
44
import sys
5+
import os
56
from functools import reduce
67

78
PY3 = sys.version_info > (3,)
@@ -189,3 +190,12 @@ def snake_to_camel(snake_str):
189190
components = snake_str.split("_")
190191
return components[0] + "".join(x.title() for x in components[1:])
191192

193+
194+
def path_relative_to(source_file, *paths):
195+
"""Given a python module __file__, return an absolute path relative to its location
196+
197+
Args:
198+
source_file: Module __file__ attribute
199+
paths {str} -- Paths to add to source file location
200+
"""
201+
return os.path.join(os.path.abspath(os.path.dirname(source_file)), *paths)

labthings/server/extensions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(
5353
self.methods = {}
5454

5555
if static_folder:
56+
print(f"Adding route for {static_folder}")
5657
self.static_view_class = static_from(static_folder)
5758
self.add_view(self.static_view_class, f"{static_url_path}/<path:path>")
5859
else:
@@ -71,7 +72,12 @@ def add_view(self, view_class, rule, **kwargs):
7172
# Expand the rule to include extension name
7273
full_rule = "/{}/{}".format(self._name_uri_safe, cleaned_rule)
7374

74-
view_id = cleaned_rule.replace("/", "_").replace("<", "").replace(">", "")
75+
view_id = (
76+
cleaned_rule.replace("/", "_")
77+
.replace("<", "")
78+
.replace(">", "")
79+
.replace(":", "_")
80+
)
7581

7682
# Store route information in a dictionary
7783
d = {"rule": full_rule, "view": view_class, "kwargs": kwargs}

0 commit comments

Comments
 (0)