<commit_before>#  Copyright 2008-2011 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
import sys
import copy

from robot import utils
from robot.errors import FrameworkError, DataError
from robot.variables import GLOBAL_VARIABLES, is_scalar_var
from robot.common import UserErrorHandler
from robot.output import LOGGER
from robot.parsing.settings import Library, Variables, Resource
import robot

from userkeyword import UserLibrary
from importer import Importer
from runkwregister import RUN_KW_REGISTER
from handlers import _XTimesHandler


STDLIB_NAMES = ['BuiltIn', 'Collections', 'Dialogs', 'Easter', 'OperatingSystem',
                'Remote', 'Reserved', 'Screenshot', 'String', 'Telnet']
IMPORTER = Importer()


class Namespace:
    """A database for keywords and variables.

    A new instance of this class is created for each test suite.
    """

    def __init__(self, suite, parent_vars, skip_imports=False):
        if suite is not None:
            LOGGER.info("Initializing namespace for test suite '%s'" % suite.longname)
        self.variables = _VariableScopes(suite, parent_vars)
        self.suite = suite
        self.test = None
        self.uk_handlers = []
        self.library_search_order = []
        self._testlibs = {}
        self._userlibs = []
        self._imported_resource_files = []
        self._imported_variable_files = []
        self.import_library('BuiltIn')
        self.import_library('Reserved')
        self.import_library('Easter')
        if suite.source and not skip_imports:
            self._handle_imports(suite.imports)
        robot.running.NAMESPACES.start_suite(self)
        self.variables['${SUITE_NAME}'] = suite.longname
        self.variables['${SUITE_SOURCE}'] = suite.source

    def _handle_imports(self, import_settings):
        for item in import_settings:
            try:
                if not item.name:
                    raise DataError('%s setting requires a name' % item.type)
                self._import(item)
            except:
                item.report_invalid_syntax(utils.get_error_message())

    def _import(self, import_setting):
        try:
            action = {'Library': self._import_library,
                      'Resource': self._import_resource,
                      'Variables': self._import_variables}[import_setting.type]
            action(import_setting, self.variables.current)
        except KeyError:
            raise FrameworkError("Invalid import setting: %s" % import_setting)

    def import_resource(self, name):
            self._import_resource(Resource(None, name))

    def _import_resource(self, import_setting, variables=None):
        path = self._resolve_name(import_setting, variables)
        if path not in self._imported_resource_files:
            self._imported_resource_files.append(path)
            resource = IMPORTER.import_resource(path)
            self.variables.set_from_variable_table(resource.variable_table)
            self._userlibs.append(UserLibrary(resource.keyword_table.keywords,
                                              resource.source))
            self._handle_imports(resource.setting_table.imports)
        else:
            LOGGER.info("Resource file '%s' already imported by suite '%s'"
                        % (path, self.suite.longname))

    def import_variables(self, name, args, overwrite=False, variables=None):
        self._import_variables(Variables(None, name, args), variables, overwrite)
<commit_msg>correct indent<commit_after>#  Copyright 2008-2011 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
import sys
import copy

from robot import utils
from robot.errors import FrameworkError, DataError
from robot.variables import GLOBAL_VARIABLES, is_scalar_var
from robot.common import UserErrorHandler
from robot.output import LOGGER
from robot.parsing.settings import Library, Variables, Resource
import robot

from userkeyword import UserLibrary
from importer import Importer
from runkwregister import RUN_KW_REGISTER
from handlers import _XTimesHandler


STDLIB_NAMES = ['BuiltIn', 'Collections', 'Dialogs', 'Easter', 'OperatingSystem',
                'Remote', 'Reserved', 'Screenshot', 'String', 'Telnet']
IMPORTER = Importer()


class Namespace:
    """A database for keywords and variables.

    A new instance of this class is created for each test suite.
    """

    def __init__(self, suite, parent_vars, skip_imports=False):
        if suite is not None:
            LOGGER.info("Initializing namespace for test suite '%s'" % suite.longname)
        self.variables = _VariableScopes(suite, parent_vars)
        self.suite = suite
        self.test = None
        self.uk_handlers = []
        self.library_search_order = []
        self._testlibs = {}
        self._userlibs = []
        self._imported_resource_files = []
        self._imported_variable_files = []
        self.import_library('BuiltIn')
        self.import_library('Reserved')
        self.import_library('Easter')
        if suite.source and not skip_imports:
            self._handle_imports(suite.imports)
        robot.running.NAMESPACES.start_suite(self)
        self.variables['${SUITE_NAME}'] = suite.longname
        self.variables['${SUITE_SOURCE}'] = suite.source

    def _handle_imports(self, import_settings):
        for item in import_settings:
            try:
                if not item.name:
                    raise DataError('%s setting requires a name' % item.type)
                self._import(item)
            except:
                item.report_invalid_syntax(utils.get_error_message())

    def _import(self, import_setting):
        try:
            action = {'Library': self._import_library,
                      'Resource': self._import_resource,
                      'Variables': self._import_variables}[import_setting.type]
            action(import_setting, self.variables.current)
        except KeyError:
            raise FrameworkError("Invalid import setting: %s" % import_setting)

    def import_resource(self, name):
        self._import_resource(Resource(None, name))

    def _import_resource(self, import_setting, variables=None):
        path = self._resolve_name(import_setting, variables)
        if path not in self._imported_resource_files:
            self._imported_resource_files.append(path)
            resource = IMPORTER.import_resource(path)
            self.variables.set_from_variable_table(resource.variable_table)
            self._userlibs.append(UserLibrary(resource.keyword_table.keywords,
                                              resource.source))
            self._handle_imports(resource.setting_table.imports)
        else:
            LOGGER.info("Resource file '%s' already imported by suite '%s'"
                        % (path, self.suite.longname))

    def import_variables(self, name, args, overwrite=False, variables=None):
        self._import_variables(Variables(None, name, args), variables, overwrite)

{'fraction_non_alphanumeric': 0.0851487613593925, 'fraction_numerical': 0.002987675837171667, 'mean_word_length': 2.8662175168431183, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '793807', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2111, 'n_words': 605}