forked from Amourspirit/python_libre_pythonista_ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
230 lines (185 loc) · 7.73 KB
/
Copy pathconfig.py
File metadata and controls
230 lines (185 loc) · 7.73 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from __future__ import annotations
from typing import Set, Dict, List, Any, cast
import os
from . import file_util
import toml
from pathlib import Path
from .meta.singleton import Singleton
from .processing.token import Token
from . import file_util
class Config(metaclass=Singleton):
"""Singleton class for the project configuration."""
def __init__(self) -> None:
toml_path = file_util.find_file_in_parent_dirs("pyproject.toml")
if not toml_path:
raise FileNotFoundError("pyproject.toml not found")
self._toml_path = Path(toml_path)
self._root_path = self._toml_path.parent
cfg = toml.load(self._toml_path)
token = Token()
cfg_meta: Dict[str, Any] = cfg["tool"]["oxt"]["config"]
self._build_dir_name = token.process(cast(str, cfg_meta["build_dir"]))
self._dist_dir_name = token.process(cast(str, cfg_meta["dist_dir"]))
self._otx_name = token.process(cast(str, cfg_meta["oxt_name"]))
self._update_file = token.process(cast(str, cfg_meta["update_file"]))
self._ver_str = cast(str, cfg["tool"]["poetry"]["version"])
self._license = cast(str, cfg["tool"]["poetry"]["license"])
self._token_file_ext: Set[str] = set(cast(List, cfg_meta["token_file_ext"]))
self._token_files: Set[str] = set(cast(List, cfg_meta.get("tokes_files", [])))
self._py_pkg_dir = cast(str, cfg_meta["py_pkg_dir"])
self._zip_preinstall_pure = cast(bool, cfg_meta["zip_preinstall_pure"])
self._default_locale = cast(List[str], cfg_meta["default_locale"])
self._resource_dir_name = cast(str, cfg_meta["resource_dir_name"])
self._resource_properties_prefix = cast(str, cfg_meta["resource_properties_prefix"])
self._oo_types_uno = ""
if "oo_types_uno" in cfg_meta:
self._oo_types_uno = cast(str, cfg_meta["oo_types_uno"])
if not self._oo_types_uno:
self._oo_types_uno = os.environ.get("OO_TYPES_UNO", "/usr/lib/libreoffice/program/types.rdb")
self._oo_types_office = ""
if "oo_types_office" in cfg_meta:
self._oo_types_office = cast(str, cfg_meta["oo_types_office"])
if not self._oo_types_office:
self._oo_types_office = os.environ.get("OO_TYPES_OFFICE", "/usr/lib/libreoffice/program/types/offapi.rdb")
self._validate()
self._has_locals = self._get_has_locals()
def _validate(self) -> None:
"""Validate the configuration."""
if not self._build_dir_name:
raise ValueError("build_dir_name is empty")
if not self._dist_dir_name:
raise ValueError("dist_dir_name is empty")
if not self._otx_name:
raise ValueError("otx_name is empty")
if not self._update_file:
raise ValueError("update_file is empty")
if not self._ver_str:
raise ValueError("ver_str is empty")
if not self._license:
raise ValueError("license is empty")
if not self._py_pkg_dir:
raise ValueError("py_pkg_dir is empty")
def _get_has_locals(self) -> bool:
"""Gets if there are any wheel or tar.gz files in the local directory."""
if not self.local_path.exists():
return False
for pkg in self.local_path.iterdir():
if pkg.is_file():
lower_name = pkg.name.lower()
if lower_name.endswith(".whl") or lower_name.endswith(".tar.gz"):
return True
return False
# region Properties
@property
def toml_path(self) -> Path:
"""The path to the pyproject.toml file."""
return self._toml_path
@property
def root_path(self) -> Path:
"""The root path of the project."""
return self._root_path
@property
def build_dir_name(self) -> str:
"""The name of the build directory."""
return self._build_dir_name
@property
def dist_dir_name(self) -> str:
"""The name of the dist directory."""
return self._dist_dir_name
@property
def otx_name(self) -> str:
"""The name of the oxt file."""
return self._otx_name
@property
def ver_str(self) -> str:
"""The version string."""
return self._ver_str
@property
def license(self) -> str:
"""The license."""
return self._license
@property
def token_file_ext(self) -> Set[str]:
"""The file extensions of token files."""
return self._token_file_ext
@property
def token_files(self) -> Set[str]:
"""The token files."""
return self._token_files
@property
def update_file(self) -> str:
"""The name of the update file."""
return self._update_file
@property
def py_pkg_dir(self) -> str:
"""
The name of the oxt zip package directory.
This property is related ot the py_pkg_names and py_pkg_files properties.
This is the directory that will be created in the oxt zip file and an
embedded zip that contains 'py_pkg_names' directories and 'py_pkg_files' names.
"""
return self._py_pkg_dir
@property
def zip_preinstall_pure(self) -> bool:
"""
Whether to zip the preinstall pure python files.
If True, the preinstall pure python files will be zipped and embedded in the
oxt zip file. If False, the preinstall pure python files will be copied to
the oxt pythonpath file.
"""
return self._zip_preinstall_pure
@property
def build_path(self) -> Path:
"""The path to the build directory."""
return self.root_path / self.build_dir_name
@property
def local_path(self) -> Path:
"""The path to the local directory."""
return self.root_path / "oxt" / "local"
@property
def has_locals(self) -> bool:
"""Whether there are any local packages."""
return self._has_locals
@property
def default_locale(self) -> List[str]:
"""
Gets the default locale.
The value for this property can be set in pyproject.toml (tool.oxt.config.default_locale)
This is the default locale to use if the locale is not set in the LibreOffice configuration.
"""
return self._default_locale
@property
def resource_dir_name(self) -> str:
"""
Gets the resource directory name.
The value for this property can be set in pyproject.toml (tool.oxt.config.resource_dir_name)
This is the name of the directory containing the resource files.
"""
return self._resource_dir_name
@property
def resource_properties_prefix(self) -> str:
"""
Gets the resource properties prefix.
The value for this property can be set in pyproject.toml (tool.oxt.config.resource_properties_prefix)
This is the prefix for the resource properties.
"""
return self._resource_properties_prefix
@property
def oo_types_uno(self) -> str:
"""
Gets the path to the uno types file.
The value for this property can be set in pyproject.toml (tool.oxt.config.oo_types_uno)
This is the path to the uno types file.
If not set in pyproject.toml, the value will be read from the environment variable ``OO_TYPES_UNO``.
"""
return self._oo_types_uno
@property
def oo_types_office(self) -> str:
"""
Gets the path to the office types file.
The value for this property can be set in pyproject.toml (tool.oxt.config.oo_types_office)
This is the path to the office types file.
If not set in pyproject.toml, the value will be read from the environment variable ``OO_TYPES_OFFICE``.
"""
return self._oo_types_office
# endregion Properties