<commit_before>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  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 sys
from types import MethodType, FunctionType

from robot import utils
from robot.errors import FrameworkError
from robot.common import BaseHandler
from runkwregister import RUN_KW_REGISTER


def Handler(library, name, method):
    if name == '__init__':
        if method is None:
            return _NoInitHandler(library)
        if _is_java_method(method):
            return _JavaInitHandler(library, method)
        else:
            return _PythonInitHandler(library, method)
    else:
        if _is_java_method(method):
            return _JavaHandler(library, name, method)
        else:
            return _PythonHandler(library, name, method)


def _is_java_method(method):
    if not utils.is_jython:
        return False
    try:
        return 'reflectedfunction' in str(type(method.im_func))
    except AttributeError:
        return 'reflectedconstructor' in str(type(method))


class _RunnableHandler(BaseHandler):
    """Base class for PythonHandler, JavaHandler and DynamicHandler"""

    type = 'library'

    def __init__(self, library, handler_name, handler_method):
        self.library = library
        self._handler_name = handler_name
        self.name = utils.printable_name(handler_name, code_style=True)
        self.longname = '%s.%s' % (library.name, self.name)
        self._method = library.scope == 'GLOBAL' and \
                self._get_global_handler(handler_method, handler_name) or None
        self.doc = ''
        self.timeout = ''  # Needed for set_attributes in runner.start_keyword

    def copy(self, library):
        handler = _RunnableHandler(library, self._handler_name, None)
        self._copy_attributes(handler)
        return handler
        
    def _copy_attributes(self, handler):
        handler._method = self._method
        handler.minargs = self.minargs
        handler.maxargs = self.maxargs
        handler.doc = self.doc
        <commit_msg>refactor<commit_after>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  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 sys
from types import MethodType, FunctionType

from robot import utils
from robot.errors import FrameworkError
from robot.common import BaseHandler
from runkwregister import RUN_KW_REGISTER

if utils.is_jython:
    from org.python.core import PyReflectedFunction, PyReflectedConstructor

    def _is_java_init(init):
        return isinstance(init, PyReflectedConstructor)
    def _is_java_method(method):
        return hasattr(method, 'im_func') \
               and isinstance(method.im_func, PyReflectedFunction)
else:
    _is_java_init = _is_java_method = lambda item: False
    

def Handler(library, name, method):
    if name == '__init__':
        if method is None:
            return _NoInitHandler(library)
        elif _is_java_init(method):
            return _JavaInitHandler(library, method)
        else:
            return _PythonInitHandler(library, method)
    else:
        if _is_java_method(method):
            return _JavaHandler(library, name, method)
        else:
            return _PythonHandler(library, name, method)


class _RunnableHandler(BaseHandler):
    """Base class for PythonHandler, JavaHandler and DynamicHandler"""

    type = 'library'

    def __init__(self, library, handler_name, handler_method):
        self.library = library
        self._handler_name = handler_name
        self.name = utils.printable_name(handler_name, code_style=True)
        self.longname = '%s.%s' % (library.name, self.name)
        self._method = library.scope == 'GLOBAL' and \
                self._get_global_handler(handler_method, handler_name) or None
        self.doc = ''
        self.timeout = ''  # Needed for set_attributes in runner.start_keyword

    def copy(self, library):
        handler = _RunnableHandler(library, self._handler_name, None)
        self._copy_attributes(handler)
        return handler
        
    def _copy_attributes(self, handler):
        handler._method = self._method
        handler.minargs = self.minargs
        handler.maxargs = self.maxargs
        handler.doc = self.doc
        
{'fraction_non_alphanumeric': 0.07547892720306514, 'fraction_numerical': 0.0030651340996168583, 'mean_word_length': 3.034775888717156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6175575', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1417, 'n_words': 454}