Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Load CLR with Python.NET #421

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 5 additions & 47 deletions stock_indicators/_cslib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
The target framework of dll is `.NET 6.0`.
"""

import json
import logging
import os
import platform
import sys
from pathlib import Path

from pythonnet import load
Expand All @@ -24,52 +21,16 @@

logger = logging.getLogger(__name__)


def verify_dotnet_environment():
"""Verify .NET environment setup"""
dotnet_root = os.environ.get("DOTNET_ROOT")
logger.debug("DOTNET_ROOT: %s", dotnet_root)

if platform.system() == "Darwin":
if not dotnet_root:
os.environ["DOTNET_ROOT"] = "/usr/local/share/dotnet"
logger.debug("Setting DOTNET_ROOT for macOS")


try:
verify_dotnet_environment()
# Load CLR
load(runtime="coreclr")
import clr
logger.debug("CLR loaded successfully on %s", platform.system())

# Get absolute paths
base_path = Path(__file__).parent.resolve()
runtime_config_path = base_path / "runtimeconfig.json"
dll_path = base_path / "lib" / "Skender.Stock.Indicators.dll"

logger.debug("Base path: %s", base_path)
logger.debug("Runtime config path: %s", runtime_config_path)
logger.debug("DLL path: %s", dll_path)

if not runtime_config_path.exists():
raise FileNotFoundError(
f"runtimeconfig.json not found at: {runtime_config_path}"
)

if not dll_path.exists():
raise FileNotFoundError(f"DLL not found at: {dll_path}")

# Initialize runtime based on platform using runtimeconfig.json
if platform.system() == "Darwin":
load(
"coreclr",
dotnet_root=os.environ["DOTNET_ROOT"],
runtime_config=str(runtime_config_path),
)
else:
load("coreclr", runtime_config=str(runtime_config_path))

import clr

logger.debug("CLR loaded successfully on %s", platform.system())


Check notice on line 33 in stock_indicators/_cslib/__init__.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

stock_indicators/_cslib/__init__.py#L33

Trailing whitespace
# Set assembly resolve path
from System import IO, AppDomain

Expand Down Expand Up @@ -132,6 +93,3 @@
from System.Collections.Generic import IEnumerable as CsIEnumerable
from System.Collections.Generic import List as CsList
from System.Globalization import CultureInfo as CsCultureInfo

# Export initialized modules
__all__ = ["clr"]
17 changes: 0 additions & 17 deletions stock_indicators/_cslib/runtimeconfig.json

This file was deleted.