<commit_before>#  Copyright 2008-2009 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 os

from robot.errors import DataError
from robot.parsing.model import ResourceFile
from robot.parsing.settings import Library, Resource, Variables
from robot.utils.match import eq
from robot.utils.normalizing import normalize
from robot.variables import Variables as RobotVariables

from robotide.namespace.cache import LibraryCache
from robotide.spec.iteminfo import TestCaseUserKeywordInfo, ResourceseUserKeywordInfo, VariableInfo, LibraryKeywordInfo
from robotide import utils


class Namespace(object):

    def __init__(self):
        self.lib_cache = LibraryCache()
        self.res_cache = ResourceCache()
        self.retriever = DatafileRetriever(self.lib_cache, self.res_cache)
        self._content_assist_hooks = []

    def register_content_assist_hook(self, hook):
        self._content_assist_hooks.append(hook)

    def get_all_keywords(self, datafiles):
        kws = set()
        kws.update(self._get_default_keywords())
        kws.update(self.retriever.get_keywords_from_several(datafiles))
        return list(kws)

    def _get_default_keywords(self):
        return self.lib_cache.get_default_keywords()

    def get_suggestions_for(self, datafile, start):
        sugs = set()
        sugs.update(self._get_suggestions_from_hooks(datafile, start))
        if self._blank(start):
            sugs.update(self._all_suggestions(datafile))
        elif self._looks_like_variable(start):
            sugs.update(self._variable_suggestions(datafile, start))
        else:
            sugs.update(self._keyword_suggestions(datafile, start))
        sugs_list = list(sugs)
        sugs_list.sort()
        return sugs_list

    def _get_suggestions_from_hooks(self, datafile, start):
        sugs = []
        for hook in self._content_assist_hooks:
            sugs.extend(hook(datafile, start))
        return sugs

    def _blank(self, start):
        return start == ''

    def _all_suggestions(self, datafile):
        vars = self._variable_suggestions(datafile, '')
        kws = self._keyword_suggestions(datafile, '')
        all = vars + kws
        all.sort()
        return all

    def _looks_like_variable(self, start):
        return (len(start) == 1 and start.startswith('$') or start.startswith('@')) \
            or (len(start) >= 2 and start.startswith('${') or start.startswith('@{'))

    def _variable_suggestions(self, datafile, start):
        start_normalized = normalize(start)
        source = os.path.basename(datafile.source) if datafile.source else ''
        vars = self.retriever.get_variables_from(datafile)
        return [VariableInfo(k, v, source) for k, v in vars.items()
                if normalize(k).startswith(start_normalized)]

    def _keyword_suggestions(self, datafile, start):
        start_normalized = normalize(start)
        suggestions = self._get_default_keywords()
        suggestions.extend(self.retriever.get_keywords_from(datafile))
        return sorted([sug for sug in suggestions
                       if normalize(sug.name).startswith(start_normalized)])

    def get_resources(self, datafile):
        return self.retriever.get_resources_from(datafile)

    def get_resource(self, path):
        return self.res_cache.get_resource('', path)

    def find_user_keyword(self, datafile, kw_name):
        uks = self.retriever.get_user_keywords_from(datafile)
        return self._find_from(uks, lambda kw: eq(kw.name, kw_name))

    def _find_from(self, kws, predicate):
        for k in kws:
            if predicate(k):
                return k
        return None

    def is_user_keyword(self, datafile, kw_name):
        return bool(self.find_user_keyword(datafile, kw_name))

    def find_library_keyword(self, datafile, kw_name):
        return self._find_from_lib_keywords(datafile,
            lambda k: eq(k.name, kw_name) and isinstance(k, LibraryKeywordInfo))

    def _find_from_lib_keywords(self, datafile, predicate):
        kws = self._get_default_keywords()
        kws.extend(self.retriever.get_keywords_from(datafile))
        return self._find_from(kws, predicate)

    def is_library_keyword(self, datafile, kw_name):
        return bool(self.find_library_keyword(datafile, kw_name))

    def keyword_details(self, datafile, name):
        return self._find_from_lib_keywords(datafile, lambda k: eq(k.name, name))


class ResourceCache(object):

    def __init__(self):
        self.cache = {}
        self.non_existing = []

    def get_resource(self, directory, name):<commit_msg>return details correctly<commit_after>#  Copyright 2008-2009 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 os

from robot.errors import DataError
from robot.parsing.model import ResourceFile
from robot.parsing.settings import Library, Resource, Variables
from robot.utils.match import eq
from robot.utils.normalizing import normalize
from robot.variables import Variables as RobotVariables

from robotide.namespace.cache import LibraryCache
from robotide.spec.iteminfo import TestCaseUserKeywordInfo, ResourceseUserKeywordInfo, VariableInfo, LibraryKeywordInfo
from robotide import utils


class Namespace(object):

    def __init__(self):
        self.lib_cache = LibraryCache()
        self.res_cache = ResourceCache()
        self.retriever = DatafileRetriever(self.lib_cache, self.res_cache)
        self._content_assist_hooks = []

    def register_content_assist_hook(self, hook):
        self._content_assist_hooks.append(hook)

    def get_all_keywords(self, datafiles):
        kws = set()
        kws.update(self._get_default_keywords())
        kws.update(self.retriever.get_keywords_from_several(datafiles))
        return list(kws)

    def _get_default_keywords(self):
        return self.lib_cache.get_default_keywords()

    def get_suggestions_for(self, datafile, start):
        sugs = set()
        sugs.update(self._get_suggestions_from_hooks(datafile, start))
        if self._blank(start):
            sugs.update(self._all_suggestions(datafile))
        elif self._looks_like_variable(start):
            sugs.update(self._variable_suggestions(datafile, start))
        else:
            sugs.update(self._keyword_suggestions(datafile, start))
        sugs_list = list(sugs)
        sugs_list.sort()
        return sugs_list

    def _get_suggestions_from_hooks(self, datafile, start):
        sugs = []
        for hook in self._content_assist_hooks:
            sugs.extend(hook(datafile, start))
        return sugs

    def _blank(self, start):
        return start == ''

    def _all_suggestions(self, datafile):
        vars = self._variable_suggestions(datafile, '')
        kws = self._keyword_suggestions(datafile, '')
        all = vars + kws
        all.sort()
        return all

    def _looks_like_variable(self, start):
        return (len(start) == 1 and start.startswith('$') or start.startswith('@')) \
            or (len(start) >= 2 and start.startswith('${') or start.startswith('@{'))

    def _variable_suggestions(self, datafile, start):
        start_normalized = normalize(start)
        source = os.path.basename(datafile.source) if datafile.source else ''
        vars = self.retriever.get_variables_from(datafile)
        return [VariableInfo(k, v, source) for k, v in vars.items()
                if normalize(k).startswith(start_normalized)]

    def _keyword_suggestions(self, datafile, start):
        start_normalized = normalize(start)
        suggestions = self._get_default_keywords()
        suggestions.extend(self.retriever.get_keywords_from(datafile))
        return sorted([sug for sug in suggestions
                       if normalize(sug.name).startswith(start_normalized)])

    def get_resources(self, datafile):
        return self.retriever.get_resources_from(datafile)

    def get_resource(self, path):
        return self.res_cache.get_resource('', path)

    def find_user_keyword(self, datafile, kw_name):
        uks = self.retriever.get_user_keywords_from(datafile)
        return self._find_from(uks, lambda kw: eq(kw.name, kw_name))

    def _find_from(self, kws, predicate):
        for k in kws:
            if predicate(k):
                return k
        return None

    def is_user_keyword(self, datafile, kw_name):
        return bool(self.find_user_keyword(datafile, kw_name))

    def find_library_keyword(self, datafile, kw_name):
        return self._find_from_lib_keywords(datafile,
            lambda k: eq(k.name, kw_name) and isinstance(k, LibraryKeywordInfo))

    def _find_from_lib_keywords(self, datafile, predicate):
        kws = self._get_default_keywords()
        kws.extend(self.retriever.get_keywords_from(datafile))
        return self._find_from(kws, predicate)

    def is_library_keyword(self, datafile, kw_name):
        return bool(self.find_library_keyword(datafile, kw_name))

    def keyword_details(self, datafile, name):
        return self._find_from_lib_keywords(datafile, lambda k: eq(k.name, name)).details


class ResourceCache(object):

    def __init__(self):
        self.cache = {}
        self.non_existing = []

    def get_resource(self, directory, name):
{'fraction_non_alphanumeric': 0.09128185102020893, 'fraction_numerical': 0.00273357414819877, 'mean_word_length': 3.315080033698399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '956271', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3005, 'n_words': 731}