<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

from writer import FileWriter


class SerializationContext(object):

    def __init__(self, datafile, output=None, format=None, pipe_separated=False,
                 line_separator=os.linesep):
        self.datafile = datafile
        self.pipe_separated = pipe_separated
        self.line_separator = line_separator
        self._given_output = output
        self._output = output
        self._format = format

    def finish(self):
        if self._given_output is None:
            self._output.close()

    @property
    def output(self):
        if not self._output:
            self._output = open(self._get_source(), 'wb')
        return self._output

    @property
    def format(self):
        return self._format or self._format_from_file()

    def _get_source(self):
        return getattr(self.datafile, 'initfile', self.datafile.source)

    def _format_from_file(self):
        return os.path.splitext(self._get_source())[1][1:].lower()


class Serializer(object):

    def __init__(self, context):
        self._ctx = context

    def serialize(self):
        self._writer = FileWriter(self._ctx)
        self._serialize(self._ctx.datafile)
        self._ctx.finish()

    def _serialize(self, datafile):
        for table in datafile:
            if table:
                {'setting': self._setting_table_serializer,
                 'variable': self._variable_table_serializer,
                 'keyword': self._keyword_table_serializer,
                 'testcase': self._testcase_table_serializer}[table.type](table)

    def _setting_table_serializer(self, table):
        self._writer.start_settings()
        self._serialize_elements(table)
        self._writer.end_settings()

    def _serialize_elements(self, elements):
        for element in elements:
            if element.is_for_loop():
                self._serialize_for_loop(element)
            elif element.is_set():
                self._writer.element(element)

    def _serialize_for_loop(self, loop):
        self._writer.start_for_loop(loop)
        self._serialize_elements(loop)
        self._writer.end_for_loop()

    def _variable_table_serializer(self, table):
        self._writer.start_variables()
        self._serialize_elements(table)
        self._writer.end_variables()

    def _keyword_table_serializer(self, table):
        self._writer.start_keywords()
        for kw in table:
            self._serialize_keyword(kw)
        self._writer.end_keywords()

    def _serialize_keyword(self, kw):
        self._writer.start_keyword(kw)
        self._serialize_elements(kw)
        self._writer.end_keyword()

    def _testcase_table_serializer(self, table):
        self._writer.start_tests()
        for tc in table:
            self._serialize_testcase(tc)
        self._writer.end_tests()

    def _serialize_testcase(self, tc):
        self._writer.start_testcase(tc)
        self._serialize_elements(tc)
        self._writer.end_testcase()
<commit_msg>serializer: Added some API docs<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

from writer import FileWriter


class SerializationContext(object):

    def __init__(self, datafile, output=None, format=None, pipe_separated=False,
                 line_separator=os.linesep):
        """The SerializationContext class holds the needed information for
        serializing test data files. It contains a references to the data file to
        be serialized and to serialization options.

        :param datafile: The datafile to be serialized.
        :type datafile: TestCaseFile, ResourceFile, TestDataDirectory
        :param output: An open, file-like object used in serialization. If not
            given, value of `source` attribute of the given `datafile` is used
            to construct a new file object.
        :param str format: Serialization format. If not given, read from the
            extension of the `source` attribute of the given `datafile`.
        :param bool pipe_separated: Whether to use pipes as separator when
            serialization format is txt.
        :param str line_separator: Line separator used in serialization.
        """
        self.datafile = datafile
        self.pipe_separated = pipe_separated
        self.line_separator = line_separator
        self._given_output = output
        self._output = output
        self._format = format

    def finish(self):
        if self._given_output is None:
            self._output.close()

    @property
    def output(self):
        if not self._output:
            self._output = open(self._get_source(), 'wb')
        return self._output

    @property
    def format(self):
        return self._format or self._format_from_file()

    def _get_source(self):
        return getattr(self.datafile, 'initfile', self.datafile.source)

    def _format_from_file(self):
        return os.path.splitext(self._get_source())[1][1:].lower()


class Serializer(object):

    def __init__(self, context):
        """The Serializer class is used to serialize Robot Framework test data files.

        :param SerializationContext context: The context used with this serializer.
        """
        self._ctx = context

    def serialize(self):
        """Does the actual serialization, based on the context given in
        :py:meth:`__init__`.
        """
        self._writer = FileWriter(self._ctx)
        self._serialize(self._ctx.datafile)
        self._ctx.finish()

    def _serialize(self, datafile):
        for table in datafile:
            if table:
                {'setting': self._setting_table_serializer,
                 'variable': self._variable_table_serializer,
                 'keyword': self._keyword_table_serializer,
                 'testcase': self._testcase_table_serializer}[table.type](table)

    def _setting_table_serializer(self, table):
        self._writer.start_settings()
        self._serialize_elements(table)
        self._writer.end_settings()

    def _serialize_elements(self, elements):
        for element in elements:
            if element.is_for_loop():
                self._serialize_for_loop(element)
            elif element.is_set():
                self._writer.element(element)

    def _serialize_for_loop(self, loop):
        self._writer.start_for_loop(loop)
        self._serialize_elements(loop)
        self._writer.end_for_loop()

    def _variable_table_serializer(self, table):
        self._writer.start_variables()
        self._serialize_elements(table)
        self._writer.end_variables()

    def _keyword_table_serializer(self, table):
        self._writer.start_keywords()
        for kw in table:
            self._serialize_keyword(kw)
        self._writer.end_keywords()

    def _serialize_keyword(self, kw):
        self._writer.start_keyword(kw)
        self._serialize_elements(kw)
        self._writer.end_keyword()

    def _testcase_table_serializer(self, table):
        self._writer.start_tests()
        for tc in table:
            self._serialize_testcase(tc)
        self._writer.end_tests()

    def _serialize_testcase(self, tc):
        self._writer.start_testcase(tc)
        self._serialize_elements(tc)
        self._writer.end_testcase()

{'fraction_non_alphanumeric': 0.08352198260455142, 'fraction_numerical': 0.003336113427856547, 'mean_word_length': 2.6655021834061134, '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': '6588836', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2254, 'n_words': 625}