Added spine-to-snake function for new plugin routes

This commit is contained in:
Joel Collins 2019-12-13 15:17:35 +00:00
parent 4eef909d13
commit 469e8218e0
2 changed files with 13 additions and 3 deletions

View file

@ -6,7 +6,11 @@ from importlib import util
import sys import sys
from openflexure_microscope.config import USER_CONFIG_DIR from openflexure_microscope.config import USER_CONFIG_DIR
from openflexure_microscope.utilities import camel_to_snake, camel_to_spine from openflexure_microscope.utilities import (
camel_to_snake,
camel_to_spine,
snake_to_spine,
)
class BasePlugin: class BasePlugin:
@ -96,7 +100,7 @@ class BasePlugin:
@property @property
def _name_uri_safe(self): def _name_uri_safe(self):
return camel_to_spine(self._name_python_safe) return snake_to_spine(self._name_python_safe)
def find_plugins(plugin_path, module_name="plugins"): def find_plugins(plugin_path, module_name="plugins"):

View file

@ -12,12 +12,14 @@ def deserialise_array_b64(b64_string, dtype, shape):
flat_arr = np.fromstring(base64.b64decode(b64_string), dtype) flat_arr = np.fromstring(base64.b64decode(b64_string), dtype)
return flat_arr.reshape(shape) return flat_arr.reshape(shape)
def serialise_array_b64(npy_arr): def serialise_array_b64(npy_arr):
b64_string = base64.b64encode(npy_arr).decode('ascii') b64_string = base64.b64encode(npy_arr).decode("ascii")
dtype = str(npy_arr.dtype) dtype = str(npy_arr.dtype)
shape = npy_arr.shape shape = npy_arr.shape
return b64_string, dtype, shape return b64_string, dtype, shape
def get_by_path(root, items): def get_by_path(root, items):
"""Access a nested object in root by item sequence.""" """Access a nested object in root by item sequence."""
return reduce(operator.getitem, items, root) return reduce(operator.getitem, items, root)
@ -69,6 +71,10 @@ def camel_to_spine(name):
return re.sub("([a-z0-9])([A-Z])", r"\1-\2", s1).lower() return re.sub("([a-z0-9])([A-Z])", r"\1-\2", s1).lower()
def snake_to_spine(name):
return name.replace("_", "-")
@contextmanager @contextmanager
def set_properties(obj, **kwargs): def set_properties(obj, **kwargs):
"""A context manager to set, then reset, certain properties of an object. """A context manager to set, then reset, certain properties of an object.