mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
gir: only initialize search paths once
This commit is contained in:
parent
f2d4643bf5
commit
c5949de543
1 changed files with 36 additions and 11 deletions
|
@ -35,6 +35,10 @@ _namespace_cache: T.Dict[str, "Namespace"] = {}
|
||||||
_xml_cache = {}
|
_xml_cache = {}
|
||||||
|
|
||||||
_user_search_paths = []
|
_user_search_paths = []
|
||||||
|
_typelib_search_paths_initalized: bool = False
|
||||||
|
_typelib_search_paths: list[str] = []
|
||||||
|
_gir_search_paths_initalized: bool = False
|
||||||
|
_gir_search_paths: list[str] = []
|
||||||
|
|
||||||
|
|
||||||
def add_user_typelib_paths(paths: list):
|
def add_user_typelib_paths(paths: list):
|
||||||
|
@ -42,13 +46,39 @@ def add_user_typelib_paths(paths: list):
|
||||||
_user_search_paths += paths
|
_user_search_paths += paths
|
||||||
|
|
||||||
|
|
||||||
|
def collect_typelib_search_paths():
|
||||||
|
global _typelib_search_paths_initalized
|
||||||
|
if _typelib_search_paths_initalized:
|
||||||
|
return
|
||||||
|
|
||||||
|
global _typelib_search_paths
|
||||||
|
_typelib_search_paths += [
|
||||||
|
*_user_search_paths,
|
||||||
|
*GIRepository.Repository.get_search_path(),
|
||||||
|
]
|
||||||
|
_typelib_search_paths_initalized = True
|
||||||
|
|
||||||
|
|
||||||
|
def collect_gir_search_paths():
|
||||||
|
global _gir_search_paths_initalized
|
||||||
|
if _gir_search_paths_initalized:
|
||||||
|
return
|
||||||
|
|
||||||
|
global _gir_search_paths
|
||||||
|
if data_paths := os.environ.get("XDG_DATA_DIRS"):
|
||||||
|
_gir_search_paths += [
|
||||||
|
os.path.join(path, "gir-1.0") for path in data_paths.split(os.pathsep)
|
||||||
|
]
|
||||||
|
_gir_search_paths_initalized = True
|
||||||
|
|
||||||
|
|
||||||
def get_namespace(namespace: str, version: str) -> "Namespace":
|
def get_namespace(namespace: str, version: str) -> "Namespace":
|
||||||
search_paths = [*GIRepository.Repository.get_search_path(), *_user_search_paths]
|
collect_typelib_search_paths()
|
||||||
|
|
||||||
filename = f"{namespace}-{version}.typelib"
|
filename = f"{namespace}-{version}.typelib"
|
||||||
|
|
||||||
if filename not in _namespace_cache:
|
if filename not in _namespace_cache:
|
||||||
for search_path in search_paths:
|
for search_path in _typelib_search_paths:
|
||||||
path = os.path.join(search_path, filename)
|
path = os.path.join(search_path, filename)
|
||||||
|
|
||||||
if os.path.exists(path) and os.path.isfile(path):
|
if os.path.exists(path) and os.path.isfile(path):
|
||||||
|
@ -61,7 +91,7 @@ def get_namespace(namespace: str, version: str) -> "Namespace":
|
||||||
if filename not in _namespace_cache:
|
if filename not in _namespace_cache:
|
||||||
raise CompileError(
|
raise CompileError(
|
||||||
f"Namespace {namespace}-{version} could not be found",
|
f"Namespace {namespace}-{version} could not be found",
|
||||||
hints=["search path: " + os.pathsep.join(search_paths)],
|
hints=["search path: " + os.pathsep.join(_typelib_search_paths)],
|
||||||
)
|
)
|
||||||
|
|
||||||
return _namespace_cache[filename]
|
return _namespace_cache[filename]
|
||||||
|
@ -94,17 +124,12 @@ def get_available_namespaces() -> T.List[T.Tuple[str, str]]:
|
||||||
|
|
||||||
|
|
||||||
def get_xml(namespace: str, version: str):
|
def get_xml(namespace: str, version: str):
|
||||||
search_paths = []
|
collect_gir_search_paths()
|
||||||
|
|
||||||
if data_paths := os.environ.get("XDG_DATA_DIRS"):
|
|
||||||
search_paths += [
|
|
||||||
os.path.join(path, "gir-1.0") for path in data_paths.split(os.pathsep)
|
|
||||||
]
|
|
||||||
|
|
||||||
filename = f"{namespace}-{version}.gir"
|
filename = f"{namespace}-{version}.gir"
|
||||||
|
|
||||||
if filename not in _xml_cache:
|
if filename not in _xml_cache:
|
||||||
for search_path in search_paths:
|
for search_path in _gir_search_paths:
|
||||||
path = os.path.join(search_path, filename)
|
path = os.path.join(search_path, filename)
|
||||||
|
|
||||||
if os.path.exists(path) and os.path.isfile(path):
|
if os.path.exists(path) and os.path.isfile(path):
|
||||||
|
@ -114,7 +139,7 @@ def get_xml(namespace: str, version: str):
|
||||||
if filename not in _xml_cache:
|
if filename not in _xml_cache:
|
||||||
raise CompileError(
|
raise CompileError(
|
||||||
f"GObject introspection file '{namespace}-{version}.gir' could not be found",
|
f"GObject introspection file '{namespace}-{version}.gir' could not be found",
|
||||||
hints=["search path: " + os.pathsep.join(search_paths)],
|
hints=["search path: " + os.pathsep.join(_gir_search_paths)],
|
||||||
)
|
)
|
||||||
|
|
||||||
return _xml_cache[filename]
|
return _xml_cache[filename]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue