-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from neutrons/config_managment
Configuration management mechanism and initial settings variables
- Loading branch information
Showing
12 changed files
with
843 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"""Module to load the the settings from SHOME/.shiver/configuration.ini file | ||
Will fall back to a default""" | ||
import os | ||
import shutil | ||
|
||
from configparser import ConfigParser | ||
from pathlib import Path | ||
from mantid.kernel import Logger | ||
|
||
logger = Logger("SHIVER") | ||
|
||
# configuration settings file path | ||
CONFIG_PATH_FILE = os.path.join(Path.home(), ".shiver", "configuration.ini") | ||
|
||
|
||
class Configuration: | ||
"""Load and validate Configuration Data""" | ||
|
||
def __init__(self): | ||
"""initialization of configuration mechanism""" | ||
# capture the current state | ||
self.valid = False | ||
|
||
# locate the template configuration file | ||
project_directory = Path(__file__).resolve().parent | ||
self.template_file_path = os.path.join(project_directory, "configuration_template.ini") | ||
|
||
# retrieve the file path of the file | ||
self.config_file_path = CONFIG_PATH_FILE | ||
logger.information(f"{self.config_file_path} with be used") | ||
|
||
# if template conf file path exists | ||
if os.path.exists(self.template_file_path): | ||
# file does not exist create it from template | ||
if not os.path.exists(self.config_file_path): | ||
# if directory structure does not exist create it | ||
if not os.path.exists(os.path.dirname(self.config_file_path)): | ||
os.makedirs(os.path.dirname(self.config_file_path)) | ||
shutil.copy2(self.template_file_path, self.config_file_path) | ||
|
||
self.config = ConfigParser() | ||
# parse the file | ||
try: | ||
self.config.read(self.config_file_path) | ||
# validate the file has the all the latest variables | ||
self.validate() | ||
except ValueError as err: | ||
logger.error(str(err)) | ||
logger.error(f"Problem with the file: {self.config_file_path}") | ||
else: | ||
logger.error(f"Template configuration file: {self.template_file_path} is missing!") | ||
|
||
def validate(self): | ||
"""validates that the fields exist at the config_file_path and writes any missing fields/data | ||
using the template configuration file: configuration_template.ini as a guide""" | ||
template_config = ConfigParser() | ||
template_config.read(self.template_file_path) | ||
for section in template_config.sections(): | ||
# if section is missing | ||
if section not in self.config.sections(): | ||
# copy the whole section | ||
self.config.add_section(section) | ||
|
||
for field in template_config[section]: | ||
if field not in self.config[section]: | ||
# copy the field | ||
self.config[section][field] = template_config[section][field] | ||
with open(self.config_file_path, "w", encoding="utf8") as config_file: | ||
self.config.write(config_file) | ||
self.valid = True | ||
|
||
def is_valid(self): | ||
"""returns the configuration state""" | ||
return self.valid | ||
|
||
|
||
def get_data(section, name=None): | ||
"""retrieves the configuration data for a variable with name""" | ||
# default file path location | ||
config_file_path = CONFIG_PATH_FILE | ||
if os.path.exists(config_file_path): | ||
config = ConfigParser() | ||
# parse the file | ||
config.read(config_file_path) | ||
try: | ||
if name: | ||
value = config[section][name] | ||
# in case of boolean string value cast it to bool | ||
if value in ("True", "False"): | ||
return value == "True" | ||
return value | ||
return config[section] | ||
except KeyError as err: | ||
# requested section/field do not exist | ||
logger.error(str(err)) | ||
return None | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[generate_tab.oncat] | ||
#url to oncat portal | ||
oncat_url = https://oncat.ornl.gov | ||
#client id for on cat; it is unique for Shiver | ||
client_id = 99025bb3-ce06-4f4b-bcf2-36ebf925cd1d | ||
#the flag (bool: True/False) indicates the location of the names of the datasets (notes/comments vs. sequence name) | ||
use_notes = False | ||
|
||
[main_tab.plot] | ||
#options: full prints dimension data, name_only, workspace title, None: no title is printed in plots | ||
title = True | ||
#the flag (bool: True/False) indicates the plot scale (logarithmic or not) | ||
logarithmic_intensity = False | ||
|
||
[global.other] | ||
help_url = https://neutrons.github.io/Shiver/GUI/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
""" single help module """ | ||
import webbrowser | ||
from shiver.configuration import get_data | ||
|
||
|
||
def help_function(context): | ||
""" | ||
open a browser with the appropriate help page | ||
""" | ||
help_url = get_data("global.other", "help_url") | ||
if context: | ||
webbrowser.open("https://neutrons.github.io/Shiver/GUI/") | ||
webbrowser.open(help_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.