Skip to content
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

[ENH] Orange.misc.environ config #4576

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Orange/canvas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import requests

from AnyQt.QtGui import QPainter, QFont, QFontMetrics, QColor, QPixmap, QIcon
from AnyQt.QtCore import Qt, QPoint, QRect
from AnyQt.QtCore import Qt, QPoint, QRect, QSettings

from orangecanvas import config as occonfig
from orangecanvas.utils.settings import config_slot
from orangewidget.workflow import config
from orangewidget.settings import set_widget_settings_dir_components

import Orange
from Orange.misc import environ

# generated from biolab/orange3-addons repository
OFFICIAL_ADDON_LIST = "https://orange.biolab.si/addons/list"
Expand Down Expand Up @@ -68,6 +70,21 @@ class Config(config.Config):

def init(self):
super().init()
widget_settings_dir_cfg = environ.get_path("widget_settings_dir", "")
if widget_settings_dir_cfg:
# widget_settings_dir is configured via config file
set_widget_settings_dir_components(
widget_settings_dir_cfg, self.ApplicationVersion
)

canvas_settings_dir_cfg = environ.get_path("canvas_settings_dir", "")
if canvas_settings_dir_cfg:
# canvas_settings_dir is configured via config file
QSettings.setPath(
QSettings.IniFormat, QSettings.UserScope,
canvas_settings_dir_cfg
)

for t in spec:
occonfig.register_setting(*t)

Expand Down Expand Up @@ -197,7 +214,6 @@ def data_dir():
Return the Orange application data directory. If the directory path
does not yet exists then create it.
"""
from Orange.misc import environ
path = os.path.join(environ.data_dir(), "canvas")
try:
os.makedirs(path, exist_ok=True)
Expand All @@ -211,7 +227,6 @@ def cache_dir():
Return the Orange application cache directory. If the directory path
does not yet exists then create it.
"""
from Orange.misc import environ
path = os.path.join(environ.cache_dir(), "canvas")
try:
os.makedirs(path, exist_ok=True)
Expand All @@ -230,6 +245,8 @@ def log_dir():
else:
logdir = data_dir()

logdir = environ.get_path("log_dir", logdir)

try:
os.makedirs(logdir, exist_ok=True)
except OSError:
Expand Down
119 changes: 100 additions & 19 deletions Orange/misc/environ.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,97 @@
"""
Retrive basic library/application data/cache locations.
environ
=======

The basic FS layout for Orange data files is
This module contains some basic configuration options for Orange
(for now mostly changing directories where settings and data are saved).

$DATA_HOME/Orange/$VERSION/
widgets/
canvas/
How it works
------------

The configuration is read from '{sys.prefix}/etc/orangerc.cfg'
which is a standard `configparser` file.

orangerc.cfg
------------

.. code-block:: cfg

# An exemple orangerc.cfg file
# ----------------------------
#
# A number of variables are predefined:
# - prefix: `sys.prefix`
# - name: The application/library name ('Orange')
# - version: The application/library name ('Orange.__version__')
# - version.major, version.minor, version.micro: The version components

[paths]
# The base path where persistent application data can be stored
# (here we define a prefix relative path)
data_dir_base = %(prefix)s/share
# the base path where application data can be stored
cache_dir = %(prefix)s/cache/%(name)s/%(version)s

# The following is only applicable for a running orange canvas application.

# The base dir where widgets store their settings
widget_settings_dir = %(prefix)s/config/%(name)s/widgets
# The base dir where canvas stores its settings
canvas_settings_dir = %(prefix)s/config/%(name)s/canvas

where DATA_HOME is a platform dependent application directory
(:ref:`data_dir_base`) and VERSION is Orange.__version__ string.
"""
import os
import sys
import warnings
import sysconfig
import configparser

from typing import Optional

import Orange


def data_dir_base():
def _get_parsed_config():
version = Orange.__version__.split(".")
data = sysconfig.get_path("data")
vars = {
"home": os.path.expanduser("~/"),
"prefix": sys.prefix,
"data": sysconfig.get_path("data"),
"name": "Orange",
"version": Orange.__version__,
"version.major": version[0],
"version.minor": version[1],
"version.micro": version[2],
}
conf = configparser.ConfigParser(vars)
conf.read([
os.path.join(data, "etc/orangerc.conf"),
], encoding="utf-8")
if not conf.has_section("paths"):
conf.add_section("paths")
return conf


def get_path(name: str, default: Optional[str] = None) -> Optional[str]:
"""
Return the platform dependent application directory.

This is usually

- on windows: "%USERPROFILE%\\AppData\\Local\\"
- on OSX: "~/Library/Application Support/"
- other: "~/.local/share/
Get configured path

Parameters
----------
name: str
The named config path value
default: Optional[str]
The default to return if `name` is not defined
"""
cfg = _get_parsed_config()
try:
return cfg.get('paths', name)
except (configparser.NoOptionError, configparser.NoSectionError):
return default


def _default_data_dir_base():
if sys.platform == "darwin":
base = os.path.expanduser("~/Library/Application Support")
elif sys.platform == "win32":
Expand All @@ -39,6 +103,19 @@ def data_dir_base():
return base


def data_dir_base():
"""
Return the platform dependent application directory.

This is usually

- on windows: "%USERPROFILE%\\AppData\\Local\\"
- on OSX: "~/Library/Application Support/"
- other: "~/.local/share/
"""
return get_path('data_dir_base', _default_data_dir_base())


def data_dir(versioned=True):
"""
Return the platform dependent Orange data directory.
Expand Down Expand Up @@ -67,10 +144,7 @@ def widget_settings_dir(versioned=True):
return orangewidget.settings.widget_settings_dir(versioned)


def cache_dir(*args):
"""
Return the platform dependent Orange cache directory.
"""
def _default_cache_dir():
if sys.platform == "darwin":
base = os.path.expanduser("~/Library/Caches")
elif sys.platform == "win32":
Expand All @@ -87,3 +161,10 @@ def cache_dir(*args):
return os.path.join(base, "Cache")
else:
return base


def cache_dir(*args):
"""
Return the platform dependent Orange cache directory.
"""
return get_path("cache_dir", _default_cache_dir())