Skip to content

Commit

Permalink
configuration path envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
richibrics committed Dec 23, 2022
1 parent d05a07d commit 2a4e8a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ commands_topic.txt
.vscode
.DS_Store
IoTuring/Configurator/configurations.json*
IoTuring/Configurator/dontmoveconf.itg*
.venv
build
*.egg-info
6 changes: 3 additions & 3 deletions IoTuring/Configurator/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

from IoTuring.Configurator.MenuPreset import MenuPreset

from IoTuring.Configurator.ConfiguratorIO import ConfiguratorIO
import IoTuring.Configurator.ConfiguratorIO as ConfiguratorIO

# TODO Find new location for this message
HELP_MESSAGE = "\nYou can find the configuration file in the following path: \n\tmacOS\t~/Application Support/IoTuring/configurations.json \n\tLinux\t~/.config/IoTuring/configurations.json \n\tWindows\t%APPDATA%/IoTuring/configurations.json \n\nYou can also set you preferred path that will contain the configuration file by setting the environment variable IOTURING_CONFIG_PATH (file will be stored in $IOTURING_CONFIG_PATH/configurations.json).\n"
HELP_MESSAGE = "\nYou can find the configuration file in the following path: \n\tmacOS\t\t~/Library/Application Support/IoTuring/configurations.json \n\tLinux\t\t~/.config/IoTuring/configurations.json \n\tWindows\t\t%APPDATA%/IoTuring/configurations.json\n\tFallback\t[ioturing_install_path]/Configurator/configurations.json \n\nYou can also set you preferred path that will contain the configuration file by setting the environment variable " + ConfiguratorIO.CONFIG_PATH_ENV_VAR + " (file will be stored in $" + ConfiguratorIO.CONFIG_PATH_ENV_VAR + "/configurations.json).\n"


BLANK_CONFIGURATION = {'active_entities': [
Expand All @@ -31,7 +31,7 @@ class Configurator(LogObject):

def __init__(self) -> None:
self.config = None
self.configuratorIO = ConfiguratorIO()
self.configuratorIO = ConfiguratorIO.ConfiguratorIO()
self.LoadConfigurations()

def GetConfigurations(self):
Expand Down
30 changes: 20 additions & 10 deletions IoTuring/Configurator/ConfiguratorIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
except:
macos_support = False

CONFIG_PATH_ENV_VAR = "IOTURING_CONFIG_DIR"

CONFIGURATION_FILE_NAME = "configurations.json"

Expand Down Expand Up @@ -61,22 +62,28 @@ def getFolderPath(self):

folderPath = self.defaultFolderPath()
try:
_os = platform.system()
if _os == 'Darwin' and macos_support:
folderPath = self.macOSFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
elif _os == "Windows":
folderPath = self.windowsFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
elif _os == "Linux":
folderPath = self.linuxFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
# Use path from environment variable if present, otherwise os specific folders, otherwise use default path
envvarPath = self.envvarFolderPath()
if envvarPath is not None:
folderPath = envvarPath
else:
_os = platform.system()
if _os == 'Darwin' and macos_support:
folderPath = self.macOSFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
elif _os == "Windows":
folderPath = self.windowsFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
elif _os == "Linux":
folderPath = self.linuxFolderPath()
folderPath = os.path.join(folderPath, self.directoryName)
except:
pass # default folder path will be used

# add slash if missing (for log reasons)
if not folderPath.endswith(os.sep):
folderPath += os.sep

return folderPath

def defaultFolderPath(self):
Expand All @@ -96,6 +103,9 @@ def windowsFolderPath(self):
def linuxFolderPath(self):
return os.environ["XDG_CONFIG_HOME"] if "XDG_CONFIG_HOME" in os.environ else os.path.join(os.environ["HOME"], ".config")

def envvarFolderPath(self):
return os.environ[CONFIG_PATH_ENV_VAR] if CONFIG_PATH_ENV_VAR in os.environ else None

# In versions prior to 2022.12.2, the configurations file was stored in the same folder as this file
def oldFolderPath(self):
return os.path.dirname(inspect.getfile(ConfiguratorIO))
Expand Down

0 comments on commit 2a4e8a5

Please sign in to comment.