File: files.py

package info (click to toggle)
plotly 5.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 82,992 kB
  • sloc: python: 368,735; javascript: 195,632; sh: 50; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 922 bytes parent folder | download | duplicates (3)
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
import os

PLOTLY_DIR = os.environ.get(
    "PLOTLY_DIR", os.path.join(os.path.expanduser("~"), ".plotly")
)
TEST_FILE = os.path.join(PLOTLY_DIR, ".permission_test")


def _permissions():
    try:
        if not os.path.exists(PLOTLY_DIR):
            try:
                os.mkdir(PLOTLY_DIR)
            except Exception:
                # in case of race
                if not os.path.isdir(PLOTLY_DIR):
                    raise
        with open(TEST_FILE, "w") as f:
            f.write("testing\n")
        try:
            os.remove(TEST_FILE)
        except Exception:
            pass
        return True
    except Exception:  # Do not trap KeyboardInterrupt.
        return False


_file_permissions = None


def ensure_writable_plotly_dir():
    # Cache permissions status
    global _file_permissions
    if _file_permissions is None:
        _file_permissions = _permissions()
    return _file_permissions