<commit_before>#  Copyright 2008-2013 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.

from robot import model
from robot.conf import RobotSettings
from robot.output import LOGGER, Output, pyloggingconf
from robot.utils import setter
from robot.variables import init_global_variables

from .namespace import IMPORTER
from .randomizer import Randomizer
from .runner import Runner
from .signalhandler import STOP_SIGNAL_MONITOR


class Keyword(model.Keyword):
    __slots__ = ['assign']
    message_class = None  # TODO: Remove from base model?

    def __init__(self, name='', args=(), assign=(), type='kw'):
        # TODO: What exactly are arguments `assign` and `type`?
        """Running model for single keyword.

        :param name: Name of the keyword.
        :param args: Arguments for the keyword.
        :param assign:
        :param type:
        """
        model.Keyword.__init__(self, name=name, args=args, type=type)
        self.assign = assign

    def is_for_loop(self):
        return False

    def is_comment(self):
        return False

    @property
    def keyword(self):
        return self.name


class ForLoop(Keyword):
    __slots__ = ['range']
    keyword_class = Keyword

    def __init__(self, vars, items, range):
        Keyword.__init__(self, assign=vars, args=items, type='for')
        self.range = range

    @property
    def vars(self):
        return self.assign

    @property
    def items(self):
        return self.args

    def is_for_loop(self):
        return True

    @property
    def steps(self):
        return self.keywords


class TestCase(model.TestCase):
    __slots__ = ['template']
    keyword_class = Keyword

    def __init__(self, name='', doc='', tags=None, timeout=None, template=None):
        # TODO: what exactly is `template`?
        """Running model for single test case.

        :param name: Name of the test case.
        :param doc: Documentation of the test case.
        :param tags: Tags of the test case.
        :param timeout: Timeout limit of the test case
        :param template:
        """
        model.TestCase.__init__(self, name, doc, tags, timeout)
        self.template = template

    @setter
    def timeout(self, timeout):
        return Timeout(*timeout) if timeout else None


class TestSuite(model.TestSuite):
    __slots__ = []
    test_class = TestCase
    keyword_class = Keyword

    def __init__(self,  name='', doc='', metadata=None, source=None):
        """Running model for single test suite.

        :ivar parent: Parent :class:`TestSuite` or `None`.
        :ivar name: Test suite name.
        :ivar doc: Test suite documentation.
        :ivar metadata: Test suite metadata as a dictionary.
        :ivar source: Path to the source file or directory.
        :ivar suites: Child suites.
        :ivar tests: A list of :class:`~.testcase.TestCase` instances.
        :ivar keywords: A list containing setup and teardown
            :class:`Keyword <robot.running.model.Keyword>` objects.

        This class might change in the future.
        """
        model.TestSuite.__init__(self, name, doc, metadata, source)
        self.imports = []
        self.user_keywords = []
        self.variables = []

    @setter
    def imports(self, imports):
        return model.Imports(self.source, imports)

    @setter
    def user_keywords(self, keywords):
        return model.ItemList(UserKeyword, items=keywords)

    @setter
    def variables(self, variables):
        return model.ItemList(Variable, {'source': self.source}, items=variables)

    def configure(self, randomize_suites=False, randomize_tests=False,
                  **options):
        model.TestSuite.configure(self, **options)
        self.randomize(randomize_suites, randomize_tests)

    def randomize(self, suites=True, tests=True):
        self.visit(Randomizer(suites, tests))

    def run(self, settings=None, **options):
        STOP_SIGNAL_MONITOR.start()
        IMPORTER.reset()
        settings = settings or RobotSettings(options)
        pyloggingconf.initialize(settings['LogLevel'])
        init_global_variables(settings)
        output = Output(settings)
        runner = Runner(output, settings)
        self.visit(runner)
        output.close(runner.result)
        return runner.result


class Variable(object):

    def __init__(self, name, value, source=None):
        # TODO: check name and value
        self.name = name
        self.value = value<commit_msg>running.model: Resolved apidoc related TODOs and added some new ones.<commit_after>#  Copyright 2008-2013 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.

from robot import model
from robot.conf import RobotSettings
from robot.output import LOGGER, Output, pyloggingconf
from robot.utils import setter
from robot.variables import init_global_variables

from .namespace import IMPORTER
from .randomizer import Randomizer
from .runner import Runner
from .signalhandler import STOP_SIGNAL_MONITOR


# TODO: This module should be turned into a package with submodules.
# No time for that prior to 2.8, but ii ought to be safe also in 2.8.x.
# Important to check that references in API docs don't break.


class Keyword(model.Keyword):
    __slots__ = ['assign']
    message_class = None  # TODO: Remove from base model?

    def __init__(self, name='', args=(), assign=(), type='kw'):
        """Running model for single keyword.

        :ivar name: Name of the keyword.
        :ivar args: Arguments for the keyword.
        :ivar assign: Variables to be assigned.
        :ivar type: Keyword type. Either 'kw', 'setup', or 'teardown'
        """
        model.Keyword.__init__(self, name=name, args=args, type=type)
        self.assign = assign

    def is_for_loop(self):
        return False

    def is_comment(self):
        return False

    @property
    def keyword(self):
        return self.name


class ForLoop(Keyword):
    __slots__ = ['range']
    keyword_class = Keyword

    def __init__(self, vars, items, range):
        Keyword.__init__(self, assign=vars, args=items, type='for')
        self.range = range

    @property
    def vars(self):
        return self.assign

    @property
    def items(self):
        return self.args

    def is_for_loop(self):
        return True

    @property
    def steps(self):
        return self.keywords


class TestCase(model.TestCase):
    __slots__ = ['template']
    keyword_class = Keyword

    def __init__(self, name='', doc='', tags=None, timeout=None, template=None):
        """Running model for single test case.

        :ivar name: Name of the test case.
        :ivar doc: Documentation of the test case.
        :ivar tags: Tags of the test case.
        :ivar timeout: Timeout limit of the test case
        :ivar template: Name of the keyword that has been used as template
            when building the test. `None` if no template used.
        """
        model.TestCase.__init__(self, name, doc, tags, timeout)
        self.template = template

    @setter
    def timeout(self, timeout):
        return Timeout(*timeout) if timeout else None


class TestSuite(model.TestSuite):
    __slots__ = []
    test_class = TestCase
    keyword_class = Keyword

    def __init__(self,  name='', doc='', metadata=None, source=None):
        """Running model for single test suite.

        :ivar parent: Parent :class:`TestSuite` or `None`.
        :ivar name: Test suite name.
        :ivar doc: Test suite documentation.
        :ivar metadata: Test suite metadata as a dictionary.
        :ivar source: Path to the source file or directory.
        :ivar suites: Child suites.
        :ivar tests: A list of :class:`~.testcase.TestCase` instances.
        :ivar keywords: A list containing setup and teardown as
            :class:`~robot.running.model.Keyword` objects.
        :ivar imports: Imports the suite contains.
        :ivar user_keywords: User keywords defined in the same file as the
            suite. **Likely to change or to be removed.**
        :ivar variables: Variables defined in the same file as the suite.
            **Likely to change or to be removed.**
        """
        model.TestSuite.__init__(self, name, doc, metadata, source)
        self.imports = []
        self.user_keywords = []
        self.variables = []

    @setter
    def imports(self, imports):
        return model.Imports(self.source, imports)

    @setter
    def user_keywords(self, keywords):
        return model.ItemList(UserKeyword, items=keywords)

    @setter
    def variables(self, variables):
        return model.ItemList(Variable, {'source': self.source}, items=variables)

    def configure(self, randomize_suites=False, randomize_tests=False,
                  **options):
        model.TestSuite.configure(self, **options)
        self.randomize(randomize_suites, randomize_tests)

    def randomize(self, suites=True, tests=True):
        """Randomizes the order of suites and/or tests, recursively."""
        self.visit(Randomizer(suites, tests))

    def run(self, settings=None, **options):
        # TODO: This method should be documented already in 2.8.
        # - Would be great to document accepted options, but just a note that
        #   all execution related options can be used is better than nothing.
        # - Reference to examples in the package level is a must.
        STOP_SIGNAL_MONITOR.start()
        IMPORTER.reset()
        settings = settings or RobotSettings(options)
        pyloggingconf.initialize(settings['LogLevel'])
        init_global_variables(settings)
        output = Output(settings)
        runner = Runner(output, settings)
        self.visit(runner)
        output.close(runner.result)
        return runner.result


class Variable(object):

    def __init__(self, name, value, source=None):
        # TODO: check name and value
        self.name = name
        self.value = value
{'fraction_non_alphanumeric': 0.09134131846027246, 'fraction_numerical': 0.0027429825363445184, 'mean_word_length': 2.792649098474341, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1652972', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2996, 'n_words': 1068}