File: environ.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (170 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
environ
=======

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

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

"""
import os
import sys
import warnings
import sysconfig
import configparser

from typing import Optional

import Orange


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]:
    """
    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":
        base = os.getenv("APPDATA", os.path.expanduser("~/AppData/Local"))
    elif os.name == "posix":
        base = os.getenv('XDG_DATA_HOME', os.path.expanduser("~/.local/share"))
    else:
        base = os.path.expanduser("~/.local/share")
    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.

    This is ``data_dir_base()``/Orange/__VERSION__/ directory if versioned is
    `True` and ``data_dir_base()``/Orange/ otherwise.
    """
    base = data_dir_base()
    if versioned:
        return os.path.join(base, "Orange", Orange.__version__)
    else:
        return os.path.join(base, "Orange")


def widget_settings_dir(versioned=True):
    """
    Return the platform dependent directory where widgets save their settings.

    .. deprecated:: 3.23
    """
    warnings.warn(
        f"'{__name__}.widget_settings_dir' is deprecated.",
        DeprecationWarning, stacklevel=2
    )
    import orangewidget.settings
    return orangewidget.settings.widget_settings_dir(versioned)


def _default_cache_dir():
    if sys.platform == "darwin":
        base = os.path.expanduser("~/Library/Caches")
    elif sys.platform == "win32":
        base = os.getenv("APPDATA", os.path.expanduser("~/AppData/Local"))
    elif os.name == "posix":
        base = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache"))
    else:
        base = os.path.expanduser("~/.cache")

    base = os.path.join(base, "Orange", Orange.__version__)
    if sys.platform == "win32":
        # On Windows cache and data dir are the same.
        # Microsoft suggest using a Cache subdirectory
        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())