-
Notifications
You must be signed in to change notification settings - Fork 10
Implement Configuration and Log dirs classes #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
richibrics
wants to merge
2
commits into
main
Choose a base branch
from
new-logs-folder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 +1,4 @@ | ||
| import inspect | ||
| from IoTuring.Logger.Logger import Logger | ||
| from importlib_metadata import metadata | ||
| import os | ||
|
|
||
|
|
||
This file contains hidden or 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,29 @@ | ||
| import os | ||
| from .GetDirectory import GetDirectory | ||
|
|
||
| # macOS dep (in PyObjC) | ||
| try: | ||
| from AppKit import * | ||
| from Foundation import * | ||
| macos_support = True | ||
| except: | ||
| macos_support = False | ||
|
|
||
| class ConfigurationsDirectory(GetDirectory): | ||
| PATH_ENVVAR_NAME = "IOTURING_CONFIG_DIR" # Environment variable name | ||
| SUBFOLDER_NAME = "Configurations" # Subfolder name for configurations in default folder | ||
|
|
||
| # https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html | ||
| def _macOSFolderPath(self): | ||
| paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,True) | ||
| basePath = (len(paths) > 0 and paths[0]) or NSTemporaryDirectory() | ||
| return basePath | ||
|
|
||
| # https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid | ||
| def _windowsFolderPath(self): | ||
| return os.environ["APPDATA"] | ||
|
|
||
| # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html | ||
| def _linuxFolderPath(self): | ||
| return os.environ["XDG_CONFIG_HOME"] if "XDG_CONFIG_HOME" in os.environ else os.path.join(os.environ["HOME"], ".config") | ||
|
|
This file contains hidden or 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,94 @@ | ||
| import platform | ||
| import os | ||
| import sys | ||
| from importlib_metadata import metadata | ||
|
|
||
|
|
||
| class GetDirectory(): | ||
| """ Class to get the path to the folder where things are stored in the system. | ||
| It will try to get the path from the environment variable IOTURING_PATH, if it is not set | ||
| it will use the default path for the OS. | ||
|
|
||
| There are 3 os-specific methods to get the default path. | ||
|
|
||
| Path selection is done in the following order: | ||
| 1. Environment variable | ||
| 2. OS-specific method | ||
| 3. Default path | ||
|
|
||
| Once the path is obtained, it will be joined with the directory name of the application. | ||
|
|
||
| e.g.: | ||
| - by ENVVAR: CHOSEN_PATH | ||
| - Windows: C:\\Users\\username\\AppData\\Roaming\\ + IoTuring | ||
| - macOS: /Users/username/Library/Application Support/ + IoTuring/ | ||
| - Linux: /home/username/.config/ + IoTuring/ | ||
| - if error: default path | ||
|
|
||
| In addition, if default path is used and SUBFOLDER_NAME is set, it will be joined with the path. | ||
|
|
||
| e.g.: | ||
| - by ENVVAR: CHOSEN_PATH - like above | ||
| - Windows: C:\\Users\\username\\AppData\\Roaming\\ + IoTuring - like above | ||
| - if error: default path + SUBFOLDER_NAME | ||
| """ | ||
|
|
||
| PATH_ENVVAR_NAME = "IOTURING_PATH" # Environment variable name, default one | ||
| SUBFOLDER_NAME = None | ||
|
|
||
| APP_NAME = metadata('IoTuring')['Name'] | ||
|
|
||
| def getFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data. """ | ||
| folderPath = self._defaultFolderPath() | ||
| try: | ||
| # 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': | ||
| folderPath = self._macOSFolderPath() | ||
| folderPath = os.path.join(folderPath, self.APP_NAME) | ||
| elif _os == "Windows": | ||
| folderPath = self._windowsFolderPath() | ||
| folderPath = os.path.join(folderPath, self.APP_NAME) | ||
| elif _os == "Linux": | ||
| folderPath = self._linuxFolderPath() | ||
| folderPath = os.path.join(folderPath, self.APP_NAME) | ||
| except: | ||
| folderPath = self._defaultFolderPath() # 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 _macOSFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data in macOS """ | ||
| raise NotImplementedError("_macOSFolderPath() is not implemented yet") | ||
|
|
||
| def _windowsFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data in Windows """ | ||
| raise NotImplementedError("_windowsFolderPath() is not implemented yet") | ||
|
|
||
| def _linuxFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data in Linux """ | ||
| raise NotImplementedError("_linuxFolderPath() is not implemented yet") | ||
|
|
||
| def _envvarFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data from the environment variable; None if not set. | ||
| The environment variable name must be stored in self.PATH_ENVVAR_NAME """ | ||
| # Get path from environment variable | ||
| return os.environ.get(self.PATH_ENVVAR_NAME) | ||
|
|
||
| def _defaultFolderPath(self): | ||
| """ Returns the path to the folder where the application will store its data in the default path. | ||
| If not overriden, it will return IoTuring install directory + "UserFiles/" + [SUBFOLDER_NAME if not None]""" | ||
| if self.SUBFOLDER_NAME is None: | ||
| return os.path.join(os.path.dirname(os.path.realpath(sys.modules['__main__'].__file__)), "UserFiles") | ||
| else: | ||
| return os.path.join(os.path.dirname(os.path.realpath(sys.modules['__main__'].__file__)), "UserFiles", self.SUBFOLDER_NAME) |
This file contains hidden or 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,32 @@ | ||||||||||||||||||
| import os | ||||||||||||||||||
| from .GetDirectory import GetDirectory | ||||||||||||||||||
|
|
||||||||||||||||||
| # macOS dep (in PyObjC) | ||||||||||||||||||
| try: | ||||||||||||||||||
| from AppKit import * | ||||||||||||||||||
| from Foundation import * | ||||||||||||||||||
| macos_support = True | ||||||||||||||||||
| except: | ||||||||||||||||||
| macos_support = False | ||||||||||||||||||
|
|
||||||||||||||||||
| class LogsDirectory(GetDirectory): | ||||||||||||||||||
| PATH_ENVVAR_NAME = "IOTURING_LOG_DIR" # Environment variable name | ||||||||||||||||||
| SUBFOLDER_NAME = "Logs" # Subfolder name for configurations in default folder | ||||||||||||||||||
|
|
||||||||||||||||||
| # get the folder where macos stores all the application log files | ||||||||||||||||||
| def _macOSFolderPath(self): | ||||||||||||||||||
| return os.path.join(NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, True)[0], "Logs") | ||||||||||||||||||
|
|
||||||||||||||||||
| # get the folder on windows where to store application log file | ||||||||||||||||||
| def _windowsFolderPath(self): | ||||||||||||||||||
| # return joined local app data folder and "Logs" subfolder | ||||||||||||||||||
| return os.path.join(os.environ["LOCALAPPDATA"], "Logs") | ||||||||||||||||||
|
|
||||||||||||||||||
| # get the folder where linux stores all the application log files | ||||||||||||||||||
| def _linuxFolderPath(self): | ||||||||||||||||||
| # There is no standard in the XDG spec for logs, so place it in $XDG_CACHE_HOME, and fallback to $HOME/.cache | ||||||||||||||||||
| if "XDG_CACHE_HOME" in os.environ: | ||||||||||||||||||
| return os.path.join(os.environ["XDG_CACHE_HOME"], "Logs") | ||||||||||||||||||
| else: | ||||||||||||||||||
| return os.path.join(os.environ["HOME"], ".cache", "Logs") | ||||||||||||||||||
|
Comment on lines
+28
to
+31
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
These folders don't exist by default |
||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or 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,2 @@ | ||
| from .ConfigurationsDirectory import ConfigurationsDirectory | ||
| from .LogsDirectory import LogsDirectory |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well, create a separate folder for IoTuring