<commit_before># PyXenon Xenon API wrapper
#
# Copyright 2015 Netherlands eScience Center
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from .util import module_path
import glob
import jpype

_JClass = None
_JPackage = None


def xenon_lib_dir():
    """ The lib directory that can be used in the code. """
    local_dir = os.path.dirname(os.path.realpath(module_path()))
    return os.path.join(local_dir, 'libs')


def xenon_classpath():
    """ list of classpath entries that xenon needs, using wildcards. """
    lib_dir = xenon_lib_dir()
    return [lib_dir, os.path.join(lib_dir, '*.jar')]


def cast(name_or_class, value):
    """ Cast a Java object to another Java object.

    Parameters
    ----------
    name_or_class : Java class or name of Java class.
    value : Java object
    """
    return jpype.JObject(value, tp=name_or_class)


def init_jvm(classpath=None, log_level=None, log_configuration_file=None,
             *args):
    """ Initialize the Java virtual machine.

    Parameters
    ----------
    classpath : list of strings with classpath entries. This must include Xenon
        dependencies. If None, xenon_classpath() is used as classpath.
    log_level : one of 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
    log_configuration_file : logback.xml configuration file path
    args :
    """
    global _JClass, _JPackage
    if classpath is None:
        classpath = xenon_classpath()

    jvm_args = ['-ea']

    cp = [filename for elem in classpath for filename in glob.glob(elem)]

    jvm_args.append('-Djava.class.path={0}'.format(os.path.pathsep.join(cp)))
    if log_configuration_file is None:
        log_configuration_file = os.path.join(xenon_lib_dir(), 'logback.xml')
    jvm_args.append('-Dlogback.configurationFile={0}'
                    .format(log_configuration_file))

    if log_level is not None:
        log_level = log_level.upper()
        levels = 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
        if log_level not in levels:
            raise ValueError('Log level is {0} but it must be one of {1}'
                             .format(log_level, levels))
        jvm_args.append('-Dloglevel={0}'.format(log_level))

    jvm_args += args

    jpype.startJVM(jpype.getDefaultJVMPath(), *jvm_args)

    _JPackage = jpype.JPackage
    _JClass = jpype.JClass

    # test whether the classpath contains Xenon
    # it will raise a TypeError otherwise
    nl.esciencecenter.xenon.Xenon.__javaclass__.getName()


class JavaPackage(object):
    """
    Wrapper around jpype.JPackage to avoid segmentation faults.

    Instead it will raise EnvironmentError if xenon.init has not yet been
    called.
    """
    def __init__(self, name):
        """ wraps jpype.JPackage.__init__ """
        self.__name = name
        self.__j_package = None

    @property
    def _j_package(self):
        """
        Actual package class
        @raise EnvironmentError: if xenon.init has not yet been called.
        """
        if self.__j_package is None:
            global _JPackage
            if _JPackage is None:
                raise EnvironmentError("Xenon is not yet initialized")
            self.__j_package = _JPackage(self.__name)

        return self.__j_package

    def __getattr__(self, n, *args, **kwargs):
        """
        wraps jpype.JPackage.__getattribute__, with the addition of
        constructing a new JPackage if it was not already done.
        """
        if n == '__test__':  # avoid pytest inspection
            raise AttributeError('Attribute {0} not found'.format(n))
        else:
            return self._j_package.__getattribute__(n, *args, **kwargs)

    def __setattr__(self, n, v, *args, **kwargs):
        """ wraps jpype.JPackage.__setattr__ """
        if n in ['_JavaPackage__j_package', '_JavaPackage__name']:
            object.__setattr__(self, n, v)
        else:
            self._j_package.__setattr__(n, v, *args, **kwargs)


class JavaClass(object):
    """ Wrapper around jpype.JClass to avoid segmentation faults. """
    def __init__(self, name):
        """ wraps jpype.JClass(name) """
        self.__name = name
        self.__j_class = None

    def __call__(self, *args, **kwargs):
        """
        wraps jpype._JavaClass.__new__
        """<commit_msg>Added EnvironmentError to JavaClass docstring<commit_after># PyXenon Xenon API wrapper
#
# Copyright 2015 Netherlands eScience Center
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from .util import module_path
import glob
import jpype

_JClass = None
_JPackage = None


def xenon_lib_dir():
    """ The lib directory that can be used in the code. """
    local_dir = os.path.dirname(os.path.realpath(module_path()))
    return os.path.join(local_dir, 'libs')


def xenon_classpath():
    """ list of classpath entries that xenon needs, using wildcards. """
    lib_dir = xenon_lib_dir()
    return [lib_dir, os.path.join(lib_dir, '*.jar')]


def cast(name_or_class, value):
    """ Cast a Java object to another Java object.

    Parameters
    ----------
    name_or_class : Java class or name of Java class.
    value : Java object
    """
    return jpype.JObject(value, tp=name_or_class)


def init_jvm(classpath=None, log_level=None, log_configuration_file=None,
             *args):
    """ Initialize the Java virtual machine.

    Parameters
    ----------
    classpath : list of strings with classpath entries. This must include Xenon
        dependencies. If None, xenon_classpath() is used as classpath.
    log_level : one of 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
    log_configuration_file : logback.xml configuration file path
    args :
    """
    global _JClass, _JPackage
    if classpath is None:
        classpath = xenon_classpath()

    jvm_args = ['-ea']

    cp = [filename for elem in classpath for filename in glob.glob(elem)]

    jvm_args.append('-Djava.class.path={0}'.format(os.path.pathsep.join(cp)))
    if log_configuration_file is None:
        log_configuration_file = os.path.join(xenon_lib_dir(), 'logback.xml')
    jvm_args.append('-Dlogback.configurationFile={0}'
                    .format(log_configuration_file))

    if log_level is not None:
        log_level = log_level.upper()
        levels = 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
        if log_level not in levels:
            raise ValueError('Log level is {0} but it must be one of {1}'
                             .format(log_level, levels))
        jvm_args.append('-Dloglevel={0}'.format(log_level))

    jvm_args += args

    jpype.startJVM(jpype.getDefaultJVMPath(), *jvm_args)

    _JPackage = jpype.JPackage
    _JClass = jpype.JClass

    # test whether the classpath contains Xenon
    # it will raise a TypeError otherwise
    nl.esciencecenter.xenon.Xenon.__javaclass__.getName()


class JavaPackage(object):
    """
    Wrapper around jpype.JPackage to avoid segmentation faults.

    Instead it will raise EnvironmentError if xenon.init has not yet been
    called.
    """
    def __init__(self, name):
        """ wraps jpype.JPackage.__init__ """
        self.__name = name
        self.__j_package = None

    @property
    def _j_package(self):
        """
        Actual package class
        @raise EnvironmentError: if xenon.init has not yet been called.
        """
        if self.__j_package is None:
            global _JPackage
            if _JPackage is None:
                raise EnvironmentError("Xenon is not yet initialized")
            self.__j_package = _JPackage(self.__name)

        return self.__j_package

    def __getattr__(self, n, *args, **kwargs):
        """
        wraps jpype.JPackage.__getattribute__, with the addition of
        constructing a new JPackage if it was not already done.
        """
        if n == '__test__':  # avoid pytest inspection
            raise AttributeError('Attribute {0} not found'.format(n))
        else:
            return self._j_package.__getattribute__(n, *args, **kwargs)

    def __setattr__(self, n, v, *args, **kwargs):
        """ wraps jpype.JPackage.__setattr__ """
        if n in ['_JavaPackage__j_package', '_JavaPackage__name']:
            object.__setattr__(self, n, v)
        else:
            self._j_package.__setattr__(n, v, *args, **kwargs)


class JavaClass(object):
    """
    Wrapper around jpype.JClass to avoid segmentation faults.

    Instead it will raise EnvironmentError if xenon.init has not yet been
    called.
    """
    def __init__(self, name):
        """ wraps jpype.JClass(name) """
        self.__name = name
        self.__j_class = None

    def __call__(self, *args, **kwargs):
        """
        wraps jpype._JavaClass.__new__
        """
{'fraction_non_alphanumeric': 0.10265962951464348, 'fraction_numerical': 0.0028976508330746144, 'mean_word_length': 2.7457364341085273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1622430', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2827, 'n_words': 892}