Skip to content

Commit

Permalink
Defer external dependency check to DilcaDistance init
Browse files Browse the repository at this point in the history
make function out of external depency checking code
  • Loading branch information
m-martin-j committed Nov 24, 2022
1 parent f7e3dd4 commit 605aecb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
26 changes: 0 additions & 26 deletions cdcstream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,5 @@
__version__ = '0.2.0'


import sys
import logging

from .cdcstream import DriftDetector, CDCStream
from .dilca_wrapper import DilcaDistance, dilca_workflow

from . import tools


logger = logging.getLogger(__name__)

WEKA_DEPENDENCIES = [DilcaDistance]


# Check if external dependencies fulfilled
tools.manage_jvm_start()
any_installations = False
for dep in WEKA_DEPENDENCIES:
installed = tools.check_package_installed(
package_name=dep._weka_package_name, package_version=dep._weka_package_version_min,
raise_error=False)
if not installed:
any_installations = True
tools.attempt_install_package(package_name=dep._weka_package_name, defer_application_exit=True)
if any_installations:
logger.warning('An application restart is necessary - exiting.')
tools.manage_jvm_stop()
sys.exit(0)
5 changes: 4 additions & 1 deletion cdcstream/dilca_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def __init__(self, supervised=False, attribute_indices=None):
tools.manage_jvm_start()

_jobject = DistanceFunction.new_instance('weka.core.' + self._weka_package_name)
self.enforce_type(_jobject, 'weka.core.DistanceFunction')
try:
self.enforce_type(_jobject, 'weka.core.DistanceFunction')
except AssertionError: # does not feel elegant...
tools.manage_external_dependencies(weka_depends=[self.__class__])
super().__init__(jobject=_jobject, options=None)
self.is_optionhandler = True # forcefully...

Expand Down
21 changes: 21 additions & 0 deletions cdcstream/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import sys
import logging
from typing import List

import weka.core.jvm as jvm
from weka.core import packages
Expand Down Expand Up @@ -32,6 +33,26 @@ def manage_jvm_detach():
def manage_jvm_stop():
jvm.stop()

def manage_external_dependencies(weka_depends: List[object]) -> None:
"""Checks whether external WEKA packages associated to passed interfaces are installed.
Args:
weka_depends (List[object]): The interfaces/classes.
"""
manage_jvm_start()
any_installations = False
for dep in weka_depends:
installed = check_package_installed(
package_name=dep._weka_package_name, package_version=dep._weka_package_version_min,
raise_error=False)
if not installed:
any_installations = True
attempt_install_package(package_name=dep._weka_package_name, defer_application_exit=True)
if any_installations:
logger.warning('An application restart is necessary - exiting.')
manage_jvm_stop()
sys.exit(0)

def check_package_installed(package_name, package_version=None, raise_error=False):
res = packages.is_installed(package_name, version=None)
if res and package_version:
Expand Down

0 comments on commit 605aecb

Please sign in to comment.