<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
import sys

from robot import utils
from robot.errors import DataError


class _Metadata:

    def __init__(self, metadata=None):
        self._data = {}
        for name in self._names.values():
            self._data[name.replace(' ','')] = None
        if metadata is not None:
            for item in metadata:
                self.set(item)

    def set(self, item):
        try:
            name = self._names[utils.normalize(item.name)].replace(' ', '')
        except KeyError:
            self._report_invalid_meta(item)
        else:
            if self._data[name] is None:
                self._data[name] = item.value
            else:
                self._data[name].extend(item.value)

    def _report_invalid_meta(self, item):
        name = self.__class__.__name__.replace('Metadata','')
        name = utils.printable_name(name, code_style=True)
        available = self._get_available_names()
        msg = "%s does not have metadata '%s'. Available: %s"
        item.report_invalid_syntax(msg % (name, item.name, available))

    def _get_available_names(self):
        names = dict([ (name, 1) for name in self._names.values() ]).keys()
        names.sort()
        return utils.seq2str(names)

    def get(self, key, default=None):
        value = self._data[key]
        if value is None:
            return default
        if key == 'Documentation':
            return ' '.join(value)
        return value

    def __getitem__(self, key):
        return self.get(key)


class TestCaseMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'setup': 'Setup',
               'precondition': 'Setup',
               'teardown': 'Teardown',
               'postcondition': 'Teardown',
               'tags': 'Tags',
               'timeout': 'Timeout' }


class UserKeywordMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'arguments': 'Arguments',
               'return': 'Return',
               'timeout': 'Timeout' }


class TestCaseFileMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'suitesetup': 'Suite Setup',
               'suiteprecondition': 'Suite Setup',
               'suiteteardown': 'Suite Teardown',
               'suitepostcondition': 'Suite Teardown',
               'testsetup': 'Test Setup',
               'testprecondition': 'Test Setup',
               'testteardown': 'Test Teardown',
               'testpostcondition': 'Test Teardown',
               'defaulttags': 'Default Tags',
               'forcetags': 'Force Tags',
               'testtimeout': 'Test Timeout' }

    def __init__(self, rawdata=None):
        _Metadata.__init__(self)
        self.user_metadata = {}
        self.imports = []
        if rawdata is not None:
            self._set_rawdata(rawdata)

    def _set_rawdata(self, rawdata):
        for item in rawdata.settings:
            name = item.name.lower()
            if name in ['library', 'resource', 'variables']:
                self.imports.append(ImportSetting(item))
            elif name.startswith('meta:'):
                self._set_user_metadata(item.name[5:].strip(), item.value)
            else:
                self.set(item)

    def _set_user_metadata(self, name, value):
        name = utils.printable_name(name)
        value = ' '.join(value)
        if self.user_metadata.has_key(name):
            value = '%s %s' % (self.user_metadata[name], value)
        self.user_metadata[name] = value


class TestSuiteInitFileMetadata(TestCaseFileMetadata):
    _deprecated_metadata = ['testsetup', 'testteardown', 'defaulttags',
                            'testtimeout']

    def set(self, item):
        TestCaseFileMetadata.set(self, item)
        if utils.normalize(item.name) in self._deprecated_metadata:
            msg = "Using setting '%s' in init file is deprecated and will be " \
                    "removed in the next major release." % (item.name)
            item.report_invalid_syntax(msg, 'WARN')


class ImportSetting:

    def __init__(self, item):
        self._item = item
        self.name = item.name = utils.normalize(item.name).capitalize()
        self.value = None

    def replace_variables(self, variables):
        item = self._item.copy()
        try:
            self.value = variables.replace_list(item.value)
        except DataError, err:
            raise DataError("Replacing variables from setting '%s' failed: %s"
                            % (self.name, utils.unic(err)))
        if len(self.value) == 0:
            raise DataError("Setting '%s' requires a value" % self.name)
        if self.name == 'Resource' and len(self.value) > 1:
            raise DataError('Invalid resource import parameters: %s' % utils.seq2str2(self.value))<commit_msg>workaround for Python2.4 bug<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
import sys

from robot import utils
from robot.errors import DataError


class _Metadata:

    def __init__(self, metadata=None):
        self._data = {}
        for name in self._names.values():
            self._data[name.replace(' ','')] = None
        if metadata is not None:
            for item in metadata:
                self.set(item)

    def set(self, item):
        try:
            name = self._names[utils.normalize(item.name)].replace(' ', '')
        except KeyError:
            self._report_invalid_meta(item)
        else:
            if self._data[name] is None:
                self._data[name] = item.value
            else:
                self._data[name].extend(item.value)

    def _report_invalid_meta(self, item):
        name = self.__class__.__name__.replace('Metadata','')
        name = utils.printable_name(name, code_style=True)
        available = self._get_available_names()
        msg = "%s does not have metadata '%s'. Available: %s"
        item.report_invalid_syntax(msg % (name, item.name, available))

    def _get_available_names(self):
        names = dict([ (name, 1) for name in self._names.values() ]).keys()
        names.sort()
        return utils.seq2str(names)

    def get(self, key, default=None):
        value = self._data[key]
        if value is None:
            return default
        if key == 'Documentation':
            return ' '.join(value)
        return value

    def __getitem__(self, key):
        return self.get(key)


class TestCaseMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'setup': 'Setup',
               'precondition': 'Setup',
               'teardown': 'Teardown',
               'postcondition': 'Teardown',
               'tags': 'Tags',
               'timeout': 'Timeout' }


class UserKeywordMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'arguments': 'Arguments',
               'return': 'Return',
               'timeout': 'Timeout' }


class TestCaseFileMetadata(_Metadata):
    _names = { 'documentation': 'Documentation',
               'document': 'Documentation',
               'suitesetup': 'Suite Setup',
               'suiteprecondition': 'Suite Setup',
               'suiteteardown': 'Suite Teardown',
               'suitepostcondition': 'Suite Teardown',
               'testsetup': 'Test Setup',
               'testprecondition': 'Test Setup',
               'testteardown': 'Test Teardown',
               'testpostcondition': 'Test Teardown',
               'defaulttags': 'Default Tags',
               'forcetags': 'Force Tags',
               'testtimeout': 'Test Timeout' }

    def __init__(self, rawdata=None):
        _Metadata.__init__(self)
        self.user_metadata = {}
        self.imports = []
        if rawdata is not None:
            self._set_rawdata(rawdata)

    def _set_rawdata(self, rawdata):
        for item in rawdata.settings:
            name = item.name.lower()
            if name in ['library', 'resource', 'variables']:
                self.imports.append(ImportSetting(item))
            elif name.startswith('meta:'):
                self._set_user_metadata(item.name[5:].strip(), item.value)
            else:
                self.set(item)

    def _set_user_metadata(self, name, value):
        name = utils.printable_name(name)
        value = ' '.join(value)
        if self.user_metadata.has_key(name):
            value = '%s %s' % (self.user_metadata[name], value)
        self.user_metadata[name] = value


class TestSuiteInitFileMetadata(TestCaseFileMetadata):
    _deprecated_metadata = ['testsetup', 'testteardown', 'defaulttags',
                            'testtimeout']

    def set(self, item):
        TestCaseFileMetadata.set(self, item)
        if utils.normalize(item.name) in self._deprecated_metadata:
            msg = "Using setting '%s' in init file is deprecated and will be " \
                    "removed in the next major release." % (item.name)
            item.report_invalid_syntax(msg, 'WARN')


class ImportSetting:

    def __init__(self, item):
        self._item = item
        self.name = item.name = utils.normalize(item.name).capitalize()
        self.value = None

    def replace_variables(self, variables):
        item = self._item.copy()
        try:
            self.value = variables.replace_list(item.value)
        except DataError:
            raise DataError("Replacing variables from setting '%s' failed: %s"
                            % (self.name, utils.get_error_message()))
        if len(self.value) == 0:
            raise DataError("Setting '%s' requires a value" % self.name)
        if self.name == 'Resource' and len(self.value) > 1:
            raise DataError('Invalid resource import parameters: %s' % utils.seq2str2(self.value))
{'fraction_non_alphanumeric': 0.11386940097139774, 'fraction_numerical': 0.003597769382982551, 'mean_word_length': 2.189615605278256, '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': '7532017', 'n_tokens_mistral': 3098, 'n_tokens_neox': 3038, 'n_words': 829}