File size: 6,327 Bytes
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7757db2
 
 
 
 
 
 
 
0fd441a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path
import sys
import ctypes
from typing import Union
from configparser import ConfigParser as config
from venv import logger
from utils.get_arg_name import get_arg_name_as_string
from utils.get_config import get_config_value
import traceback

from utils.logger import get_logger

logger = get_logger(__name__)

def set_weasyprint_library(libpath: Union[str, Path] = None, config_file: Union[str, Path] = "utils\\config.ini"):
    """ Loads Weasyprint backend dependency libraries to environment """
    # Check if the current platform is Windows
    if sys.platform == 'win32':
        
        #libgobject_path =  #"/path/to/your/custom/glib/install/lib/libgobject-2.0.so.0"
        if not libpath:
            #from file_handler.file_utils import find_file
            #config_file = find_file("config.ini")  ##from file_handler.file_utils
            
            ## Alternate to speed up while sacrificing 
            from globals import config_load_models
            config_file = config_load_models.config_ini

            lib_path = get_config_value(Path(config_file), "LIBRARIES_CAP", "WEASYPRINT_DLL_DIRECTORIES") if not libpath else "C:\\msys64\\mingw64\\bin"

            # Check if the file exists before attempting to load it
            #if not os.path.exists(libobject):
            if not Path(lib_path).exists():
                raise FileNotFoundError(f"The specified Weasyprint DLL Directory does not exist: {lib_path}. Follow Weasyprint installation guide or provide a valid GTK3-runtime path.")
            #logger.exception(f"gobject library path: {libgobject_path}")  ##debug
            
        try:
            # Set a new environment variable
            lib_path = lib_path  ##SMY: on dev machine, using extracted 'portable' GTK3 rather than installing 'MSYS2'
            os.environ["WEASYPRINT_DLL_DIRECTORIES"] = lib_path
            #logger.info(f"sets Weasyprint DLL library path: {lib_path}")   #debug
            
        except Exception as exc:
            tb = traceback.format_exc()
            logger.exception(f"Error setting environ: weasyprint backend dependency β†’ {exc}\n{tb}", exc_info=True)  # Log the full traceback
            
            raise RuntimeWarning(f"βœ— error during setting environ: weasyprint backend dependency β†’ {exc}\n{tb}")
        

def load_library(libobject_name: Union[str, Path]):
    """ 
    Loads Weasyprint backend dependency libraries 
    usage:  list(map(load_library, library_list))  ##SMY: map the load_library function to each item in library_list
    The library list was starting to grow excessively, opt to setting environ   
    """
    # Check if the current platform is Windows
    if sys.platform == 'win32':
        
        #libgobject_path =  #"/path/to/your/custom/glib/install/lib/libgobject-2.0.so.0"
        cfg = config()
        cfg.read("utils\\config.ini")
        lib_path = cfg["libraries"].get(f"libobject_path", "C:\\Dat\\dev\\gtk3-runtime\\bin")
        lib_object_dll = get_arg_name_as_string(libobject_name)  ## future use

        # Construct the path to libgobject-2.0.dll
        #libgobject_path = os.path.join(os.environ.get('GLIB_PREFIX', 'C:\\glib'), 'bin', 'libgobject-2.0-0.dll')
        libobject = f"{lib_path}\\{libobject_name}.dll"   ##libgobject-2.0-0.dll"
        #print(f"Loading gobject library: {libgobject}")   #debug
        
        # Check if the file exists before attempting to load it
        #if not os.path.exists(libobject):
        if not Path(libobject).exists():
            raise FileNotFoundError(f"The specified library file does not exist: {libobject}")
        #print(f"gobject library path: {libgobject_path}")  ##debug

        # Load the library using ctypes
        try:
            ctypes_libgobject = ctypes.CDLL(libobject)
            #msg = f"libgobject-2.0-0.dll loaded successfully via ctypes. {str(ctypes_libgobject)}"
            #print(msg)  ##debug
        except OSError as exc:
            tb = traceback.format_exc()
            raise RuntimeWarning(f"Failed to load library: {exc}\n{tb}")  ##raise RuntimeError

## Test
#load_library("libpango-1.0-0")
#load_library("libgobject-2.0-0")


##SMY: Original implementation: TODO: for refactoring
def load_libgobject():
    # Check if the current platform is Windows
    if sys.platform == 'win32':
        
        #libgobject_path =  #"/path/to/your/custom/glib/install/lib/libgobject-2.0.so.0"
        cfg = config()
        cfg.read("utils\\config.ini")
        libgobject_path = cfg["libraries"].get("libgobject_path", "C:\\Dat\\dev\\gtk3-runtime\\bin")

        # Construct the path to libgobject-2.0.dll
        #libgobject_path = os.path.join(os.environ.get('GLIB_PREFIX', 'C:\\glib'), 'bin', 'libgobject-2.0-0.dll')
        libgobject = f"{libgobject_path}\\libgobject-2.0-0.dll"
        #print(f"Loading gobject library: {libgobject}")   #debug
        
        # Check if the file exists before attempting to load it
        if not os.path.exists(libgobject):
            raise FileNotFoundError(f"The specified library file does not exist: {libgobject}")
        #print(f"gobject library path: {libgobject_path}")  ##debug

        # Load the library using ctypes
        try:
            ctypes_libgobject = ctypes.CDLL(libgobject)
            #msg = f"libgobject-2.0-0.dll loaded successfully via ctypes. {str(ctypes_libgobject)}"
            #print(msg)  ##debug

            return ctypes_libgobject
        except OSError as exc:
            tb = traceback.format_exc()
            raise RuntimeWarning(f"Failed to load library: {exc}\n{tb}")  ##raise RuntimeError


    # Load the library using ctypes (Linux/macOS)
    # Construct the path to libgobject-2.0.so.0 in the custom GLib installation
    #libgobject_path = os.path.join(os.environ.get('GLIB_PREFIX', '/opt/glib'), 'lib', 'libgobject-2.0.so.0') 
    #print("This script is intended to run on Unix-like systems, not Windows.")
    else:
        # Load the library using ctypes (Linux/macOS)
        # Construct the path to libgobject-2.0.so.0 in the custom GLib installation
        libgobject_path = os.path.join(os.environ.get('GLIB_PREFIX', '/opt/glib'), 'lib', 'libgobject-2.0.so.0') 
        #print("This script is intended to run on Unix-like systems, not Windows.")

        return libgobject_path