<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 random

from statistics import Stat
from robot import utils
from robot.errors import DataError


class _TestAndSuiteHelper:

    def __init__(self, name, parent=None):
        self.name = name
        self.doc = ''
        self.parent = parent
        self.setup = None
        self.teardown = None
        self.status = 'NOT_RUN'
        self.message = ''

    # TODO: Is this property and other html/serialize stuff here used anymore?
    @property
    def htmldoc(self):
        return utils.html_format(self.doc)

    # TODO: Replace with simple @property in 2.7.
    # Cannot do that now because Mabot assigns longname.
    _longname = None
    longname = property(lambda self: self._longname or self.get_long_name(),
                        lambda self, name: setattr(self, '_longname', name))

    # TODO: Is separator still used?
    def get_long_name(self, separator='.'):
        """Returns long name. If separator is None, list of names is returned."""
        names = self.parent and self.parent.get_long_name(separator=None) or []
        names.append(self.name)
        if separator:
            return separator.join(names)
        return names

    def _set_teardown_fail_msg(self, message):
        if self.message == '':
            self.message = message
        else:
            self.message += '\n\nAlso ' + message[0].lower() + message[1:]

    def __str__(self):
        return self.name

    def __repr__(self):
        return repr(self.name)


class BaseTestSuite(_TestAndSuiteHelper):
    """Base class for TestSuite used in runtime and by rebot."""

    def __init__(self, name, source=None, parent=None):
        _TestAndSuiteHelper.__init__(self, name, parent)
        self.source = utils.abspath(source) if source else None
        self._id = None
        self.metadata = utils.NormalizedDict()
        self.suites = []
        self.tests = []
        self.critical = _Critical()
        self.critical_stats = Stat()
        self.all_stats = Stat()
        if parent:
            parent.suites.append(self)

    def set_name(self, name):
        if name:
            self.name = name
        elif not self.parent and self.name == '':  # MultiSourceSuite
            self.name = ' & '.join(suite.name for suite in self.suites)

    @property
    def id(self):
        if not self._id:
            self._find_root()._set_id()
        return self._id

    def _find_root(self):
        if self.parent:
            return self.parent._find_root()
        return self

    def _set_id(self):
        if not self._id:
            self._id = 's1'
        for index, suite in enumerate(self.suites):
            suite._id = '%s-s%s' % (self._id, index+1)
            suite._set_id()

    def set_critical_tags(self, critical, non_critical):
        if critical is not None or non_critical is not None:
            self.critical.set(critical, non_critical)
            self._set_critical_tags(self.critical)

    def _set_critical_tags(self, critical):
        self.critical = critical
        for suite in self.suites:
            suite._set_critical_tags(critical)<commit_msg>cleanup - use helper method instead of comment<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 random

from statistics import Stat
from robot import utils
from robot.errors import DataError


class _TestAndSuiteHelper:

    def __init__(self, name, parent=None):
        self.name = name
        self.doc = ''
        self.parent = parent
        self.setup = None
        self.teardown = None
        self.status = 'NOT_RUN'
        self.message = ''

    # TODO: Is this property and other html/serialize stuff here used anymore?
    @property
    def htmldoc(self):
        return utils.html_format(self.doc)

    # TODO: Replace with simple @property in 2.7.
    # Cannot do that now because Mabot assigns longname.
    _longname = None
    longname = property(lambda self: self._longname or self.get_long_name(),
                        lambda self, name: setattr(self, '_longname', name))

    # TODO: Is separator still used?
    def get_long_name(self, separator='.'):
        """Returns long name. If separator is None, list of names is returned."""
        names = self.parent and self.parent.get_long_name(separator=None) or []
        names.append(self.name)
        if separator:
            return separator.join(names)
        return names

    def _set_teardown_fail_msg(self, message):
        if self.message == '':
            self.message = message
        else:
            self.message += '\n\nAlso ' + message[0].lower() + message[1:]

    def __str__(self):
        return self.name

    def __repr__(self):
        return repr(self.name)


class BaseTestSuite(_TestAndSuiteHelper):
    """Base class for TestSuite used in runtime and by rebot."""

    def __init__(self, name, source=None, parent=None):
        _TestAndSuiteHelper.__init__(self, name, parent)
        self.source = utils.abspath(source) if source else None
        self._id = None
        self.metadata = utils.NormalizedDict()
        self.suites = []
        self.tests = []
        self.critical = _Critical()
        self.critical_stats = Stat()
        self.all_stats = Stat()
        if parent:
            parent.suites.append(self)

    def set_name(self, name):
        if name:
            self.name = name
        elif self._is_multi_source_suite():
            self.name = ' & '.join(suite.name for suite in self.suites)

    def _is_multi_source_suite(self):
        return self.parent is None and self.name == ''

    @property
    def id(self):
        if not self._id:
            self._find_root()._set_id()
        return self._id

    def _find_root(self):
        if self.parent:
            return self.parent._find_root()
        return self

    def _set_id(self):
        if not self._id:
            self._id = 's1'
        for index, suite in enumerate(self.suites):
            suite._id = '%s-s%s' % (self._id, index+1)
            suite._set_id()

    def set_critical_tags(self, critical, non_critical):
        if critical is not None or non_critical is not None:
            self.critical.set(critical, non_critical)
            self._set_critical_tags(self.critical)

    def _set_critical_tags(self, critical):
        self.critical = critical
        for suite in self.suites:
            suite._set_critical_tags(critical)
{'fraction_non_alphanumeric': 0.09236469498478232, 'fraction_numerical': 0.004763795156808257, 'mean_word_length': 2.4029716343989196, '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': '1277373', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2126, 'n_words': 681}