Changed preferred paths to use PROGRAMDATA, /var, and /etc
This commit is contained in:
parent
2227d38be2
commit
feddcc546b
5 changed files with 112 additions and 42 deletions
|
|
@ -14,11 +14,8 @@ from flask_cors import CORS, cross_origin
|
|||
|
||||
from openflexure_microscope.api.utilities import list_routes, init_default_extensions
|
||||
|
||||
from openflexure_microscope.config import (
|
||||
settings_file_path,
|
||||
JSONEncoder,
|
||||
USER_EXTENSIONS_PATH,
|
||||
)
|
||||
from openflexure_microscope.config import JSONEncoder
|
||||
from openflexure_microscope.paths import EXTENSIONS_PATH, settings_file_path
|
||||
|
||||
from openflexure_microscope.common.flask_labthings.quick import create_app
|
||||
from openflexure_microscope.common.flask_labthings.extensions import find_extensions
|
||||
|
|
@ -75,9 +72,9 @@ app.json_encoder = JSONEncoder
|
|||
labthing.add_component(api_microscope, "org.openflexure.microscope")
|
||||
|
||||
# Attach extensions
|
||||
if not os.path.isfile(USER_EXTENSIONS_PATH):
|
||||
init_default_extensions(USER_EXTENSIONS_PATH)
|
||||
for extension in find_extensions(USER_EXTENSIONS_PATH):
|
||||
if not os.path.isfile(EXTENSIONS_PATH):
|
||||
init_default_extensions(EXTENSIONS_PATH)
|
||||
for extension in find_extensions(EXTENSIONS_PATH):
|
||||
labthing.register_extension(extension)
|
||||
|
||||
# Attach captures resources
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ from .capture import CaptureObject
|
|||
from openflexure_microscope.utilities import entry_by_uuid
|
||||
from openflexure_microscope.common.labthings_core.lock import StrictLock
|
||||
|
||||
from openflexure_microscope.paths import data_file_path
|
||||
|
||||
BASE_CAPTURE_PATH = os.path.join(os.path.expanduser("~"), "micrographs")
|
||||
BASE_CAPTURE_PATH = data_file_path("micrographs")
|
||||
TEMP_CAPTURE_PATH = os.path.join(BASE_CAPTURE_PATH, "tmp")
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ from .base import BaseCamera, CaptureObject
|
|||
# Richard's fix gain
|
||||
from .set_picamera_gain import set_analog_gain, set_digital_gain
|
||||
|
||||
from openflexure_microscope.config import settings_file_path
|
||||
from openflexure_microscope.paths import settings_file_path
|
||||
|
||||
|
||||
# MAIN CLASS
|
||||
|
|
|
|||
|
|
@ -7,15 +7,7 @@ from uuid import UUID
|
|||
import numpy as np
|
||||
from fractions import Fraction
|
||||
|
||||
"""
|
||||
Attributes:
|
||||
USER_CONFIG_DIR (str): Default path of the user-config directory, containing runtime-config and
|
||||
calibration files. Obtained from ``os.path.join(os.path.expanduser("~"), ".openflexure")``.
|
||||
USER_CONFIG_FILE_PATH (str): Default path of the user microscope_settings.json runtime-config file.
|
||||
Obtained from ``os.path.join(USER_CONFIG_DIR, "microscope_settings.json")``
|
||||
user_settings (OpenflexureSettingsFile): Default settings file object, which handles expansion and
|
||||
contraction of settings dictionaries.
|
||||
"""
|
||||
from .paths import OPENFLEXURE_ETC_PATH, CONFIG_FILE_PATH, DEFAULT_CONFIG_FILE_PATH
|
||||
|
||||
|
||||
class OpenflexureSettingsFile:
|
||||
|
|
@ -28,10 +20,10 @@ class OpenflexureSettingsFile:
|
|||
"""
|
||||
|
||||
def __init__(self, config_path: str = None):
|
||||
global DEFAULT_CONFIG, USER_CONFIG_FILE_PATH
|
||||
global DEFAULT_CONFIG, CONFIG_FILE_PATH
|
||||
|
||||
# Set arguments
|
||||
self.config_path = config_path or USER_CONFIG_FILE_PATH
|
||||
self.config_path = config_path or CONFIG_FILE_PATH
|
||||
|
||||
# Initialise basic config file with defaults if it doesn't exist
|
||||
initialise_file(self.config_path, populate=DEFAULT_CONFIG)
|
||||
|
|
@ -108,6 +100,7 @@ class JSONEncoder(json.JSONEncoder):
|
|||
|
||||
# HANDLE BASIC LOADING AND SAVING OF SETTINGS FILES
|
||||
|
||||
|
||||
def load_json_file(config_path) -> dict:
|
||||
"""
|
||||
Open a .json config file
|
||||
|
|
@ -185,29 +178,10 @@ def initialise_file(config_path, populate: str = "{}\n"):
|
|||
outfile.write(populate)
|
||||
|
||||
|
||||
def settings_file_path(filename: str):
|
||||
"""Generate a full file path for a filename to be stored in user settings"""
|
||||
global USER_CONFIG_DIR
|
||||
return os.path.join(USER_CONFIG_DIR, filename)
|
||||
|
||||
|
||||
# HANDLE THE DEFAULT CONFIGURATION FILE
|
||||
|
||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
#: Path of default (first-run) microscope settings
|
||||
DEFAULT_CONFIG_FILE_PATH = os.path.join(HERE, "microscope_settings.default.json")
|
||||
|
||||
#: Path of microscope settings directory
|
||||
USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure")
|
||||
#: Path of microscope settings directory
|
||||
USER_CONFIG_FILE_PATH = os.path.join(USER_CONFIG_DIR, "microscope_settings.json")
|
||||
#: Path of microscope extensions directory
|
||||
USER_EXTENSIONS_PATH = os.path.join(USER_CONFIG_DIR, "microscope_extensions")
|
||||
|
||||
# Load the default config
|
||||
with open(DEFAULT_CONFIG_FILE_PATH, "r") as default_rc:
|
||||
DEFAULT_CONFIG = default_rc.read()
|
||||
|
||||
|
||||
#: Default user settings object
|
||||
user_settings = OpenflexureSettingsFile(config_path=USER_CONFIG_FILE_PATH)
|
||||
user_settings = OpenflexureSettingsFile(config_path=CONFIG_FILE_PATH)
|
||||
|
|
|
|||
98
openflexure_microscope/paths.py
Normal file
98
openflexure_microscope/paths.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import os
|
||||
import logging
|
||||
|
||||
# HANDLE DEFAULTS FILES STORED IN THIS APPLICATION
|
||||
|
||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
#: Path of default (first-run) microscope settings
|
||||
DEFAULT_CONFIG_FILE_PATH = os.path.join(HERE, "microscope_settings.default.json")
|
||||
|
||||
|
||||
# HANDLE WINDOWS BASE PATHS
|
||||
"""
|
||||
if os.name == "nt":
|
||||
VAR_PATH = os.getenv("PROGRAMDATA") or "C:\\ProgramData"
|
||||
else:
|
||||
if os.access("/var", os.W_OK) and os.access("/var", os.R_OK):
|
||||
VAR_PATH = "/var"
|
||||
else:
|
||||
VAR_PATH = os.path.expanduser("~")
|
||||
OPENFLEXURE_VAR_PATH = os.path.join(VAR_PATH, "openflexure")
|
||||
print(f"Running with server settings directory {OPENFLEXURE_VAR_PATH}")
|
||||
"""
|
||||
|
||||
|
||||
def check_rw(path):
|
||||
return os.access(path, os.W_OK) and os.access(path, os.R_OK)
|
||||
|
||||
|
||||
# DATA BASE PATHS
|
||||
|
||||
if os.name == "nt":
|
||||
PREFERRED_VAR_PATH = os.getenv("PROGRAMDATA") or "C:\\ProgramData"
|
||||
FALLBACK_VAR_PATH = os.path.expanduser("~")
|
||||
else:
|
||||
PREFERRED_VAR_PATH = "/var"
|
||||
FALLBACK_VAR_PATH = os.path.expanduser("~")
|
||||
|
||||
PREFERRED_OPENFLEXURE_VAR_PATH = os.path.join(PREFERRED_VAR_PATH, "openflexure")
|
||||
FALLBACK_OPENFLEXURE_VAR_PATH = os.path.join(FALLBACK_VAR_PATH, "openflexure")
|
||||
|
||||
if not os.path.exists(PREFERRED_OPENFLEXURE_VAR_PATH) and check_rw(PREFERRED_VAR_PATH):
|
||||
os.makedirs(PREFERRED_OPENFLEXURE_VAR_PATH)
|
||||
|
||||
if check_rw(PREFERRED_OPENFLEXURE_VAR_PATH):
|
||||
OPENFLEXURE_VAR_PATH = PREFERRED_OPENFLEXURE_VAR_PATH
|
||||
else:
|
||||
if not os.path.exists(FALLBACK_OPENFLEXURE_VAR_PATH):
|
||||
os.makedirs(FALLBACK_OPENFLEXURE_VAR_PATH)
|
||||
OPENFLEXURE_VAR_PATH = FALLBACK_OPENFLEXURE_VAR_PATH
|
||||
print(f"Running with data path {OPENFLEXURE_VAR_PATH}")
|
||||
|
||||
# SERVER BASE PATHS
|
||||
|
||||
if os.name == "nt":
|
||||
PREFERRED_ETC_PATH = os.getenv("PROGRAMDATA") or "C:\\ProgramData"
|
||||
FALLBACK_ETC_PATH = os.path.expanduser("~")
|
||||
else:
|
||||
PREFERRED_ETC_PATH = "/etc"
|
||||
FALLBACK_ETC_PATH = os.path.join(os.path.expanduser("~"), ".config")
|
||||
|
||||
PREFERRED_OPENFLEXURE_ETC_PATH = os.path.join(PREFERRED_ETC_PATH, "openflexure")
|
||||
FALLBACK_OPENFLEXURE_ETC_PATH = os.path.join(FALLBACK_ETC_PATH, "openflexure")
|
||||
|
||||
if not os.path.exists(PREFERRED_OPENFLEXURE_ETC_PATH) and check_rw(PREFERRED_ETC_PATH):
|
||||
os.makedirs(PREFERRED_OPENFLEXURE_ETC_PATH)
|
||||
|
||||
if check_rw(PREFERRED_OPENFLEXURE_ETC_PATH):
|
||||
OPENFLEXURE_ETC_PATH = PREFERRED_OPENFLEXURE_ETC_PATH
|
||||
else:
|
||||
if not os.path.exists(FALLBACK_OPENFLEXURE_ETC_PATH):
|
||||
os.makedirs(FALLBACK_OPENFLEXURE_ETC_PATH)
|
||||
OPENFLEXURE_ETC_PATH = FALLBACK_OPENFLEXURE_ETC_PATH
|
||||
print(f"Running with server path {OPENFLEXURE_ETC_PATH}")
|
||||
|
||||
|
||||
# SERVER PATHS
|
||||
|
||||
#: Path of microscope settings directory
|
||||
CONFIG_FILE_PATH = os.path.join(OPENFLEXURE_ETC_PATH, "microscope_settings.json")
|
||||
#: Path of microscope extensions directory
|
||||
EXTENSIONS_PATH = os.path.join(OPENFLEXURE_ETC_PATH, "microscope_extensions")
|
||||
|
||||
|
||||
# DATA PATHS
|
||||
|
||||
|
||||
# UTILITIES
|
||||
|
||||
|
||||
def settings_file_path(filename: str):
|
||||
"""Generate a full file path for a filename to be stored in server settings folder"""
|
||||
return os.path.join(OPENFLEXURE_ETC_PATH, filename)
|
||||
|
||||
|
||||
def data_file_path(filename: str):
|
||||
"""Generate a full file path for a filename to be stored in server data folder"""
|
||||
return os.path.join(OPENFLEXURE_VAR_PATH, filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue