File: fs.py

package info (click to toggle)
fs-uae-arcade 3.1.63-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 24,456 kB
  • sloc: python: 56,011; makefile: 170
file content (144 lines) | stat: -rw-r--r-- 3,025 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
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
import os
import sys
import functools

# noinspection PyUnresolvedReferences
from fsbc.system import windows, macosx

# noinspection PyUnresolvedReferences
from fsbc.user import get_data_dir

# noinspection PyUnresolvedReferences
from .util import memoize


def cache(func):
    first_time = [True, None]

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if first_time[0]:
            first_time[1] = func(*args, **kwargs)
            first_time[0] = False
        return first_time[1]

    return wrapper


@cache
def get_lib_dir():
    lib_dir = os.environ.get("LIB_DIR", "").decode("UTF-8")
    if lib_dir:
        return unicode_path(lib_dir)
    lib_dir = os.path.join(os.getcwdu(), "lib")
    if os.path.exists(lib_dir):
        return lib_dir
    # raise RuntimeError("could not detect lib dir")
    return ""


EXCEPTION = "EXCEPTION"


@cache
def get_app_id():
    # FIXME
    return "fs-uae"
    # if windows or macosx:
    #     return "fs-uae"
    # else:
    #     return "fs-game-center"


@memoize
def get_app_data_dir(app=None):
    if not app:
        app = get_app_id()
    path = os.path.join(get_data_dir(), app)
    if not os.path.exists(path):
        os.makedirs(path)
    return path


@memoize
def get_app_config_dir(app=None):
    if not app:
        app = get_app_id()
    if windows:
        path = os.path.join(get_app_data_dir())
    elif macosx:
        path = os.path.join(get_home_dir(), "Library", "Preferences", app)
    else:
        path = os.path.join(get_home_dir(), ".config")
        path = os.environ.get("XDG_CONFIG_HOME", path)
        path = os.path.join(path, app)
        path = unicode_path(path)
    if not os.path.isdir(path):
        os.makedirs(path)
    return path


def cause(exc, _cause):
    exc.__cause__ = _cause
    _cause.__traceback__ = sys.exc_info()[2]
    return exc


def encode_path(path):
    return path


def unicode_path(path):
    return path


def from_utf8_str(obj):
    if isinstance(obj, bytes):
        return obj.decode("UTF-8")
    return str(obj)


def to_utf8_str(obj):
    return obj


def utf8(obj):
    return unicode_safe(obj, "utf-8").encode("utf-8")


def utf8_safe(obj):
    return unicode_safe(obj, "utf-8").encode("utf-8")


def unicode_safe(obj, encoding="ASCII"):
    try:
        return str(obj)
    except Exception:
        pass
    try:
        return str(obj, encoding, "replace")
    except Exception:
        pass
    try:
        return str(str(obj), encoding, "replace")
    except Exception:
        # logger.exception("Error in unicode_safe")
        return "String returned from unicode_safe (problem logged)"


def normalize_path(path):
    path = os.path.normcase(os.path.normpath(path))
    return unicode_path(path)


# noinspection PyUnresolvedReferences
from .user import get_home_dir

# noinspection PyUnresolvedReferences
from .util import Version

# noinspection PyUnresolvedReferences
from .util import split_version

# noinspection PyUnresolvedReferences
from .util import compare_versions