<commit_before># -*- coding: utf-8 -*-

#
# Copyright (c) 2011-2015 Genestack Limited
# All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF GENESTACK LIMITED
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#

from Connection import Application

CALCULATE_CHECKSUMS = 'markKeyForCountChecksum'


class CLApplication(Application):
    """
    Base class to interact with CommandLine Applications.
    Application_ID is required you can pass it as init argument or overwrite it in children.
    Source files and params depends on application.

    """
    APPLICATION_ID = None

    def __repr__(self):
        return self.APPLICATION_ID

    def create_file(self, source_files, name=None, params=None, calculate_checksums=False, expected_checksums=None,
                    initialize=False):
        """
        Creates application native file and returns its accession.
        If source file is not found or is not of source file type, the corresponding exceptions are thrown.

        :param source_files: list of source file accessions
        :type source_files: list
        :param name: if name is specified created file will be renamed
        :type name: str
        :param params: is command arguments to be set, if None then arguments stay default.
        :param params: list
        :param calculate_checksums: flag that used in initialization script to count checsums of created files
        :type calculate_checksums: bool
        :param expected_checksums: List of expected checksums in any order
        :type expected_checksums: list
        :param initialize: flag if initialization should be started immediately
        :return: accession of created file
        :rtype: str
        """
        app_file = self.__create_file(source_files, params)

        if name:
            self.rename_file(app_file, name)

        if calculate_checksums:
            # hack to support both master and stable
            try:

                self.connection.application('genestack/bio-test-cla').invoke(
                    '%s' % CALCULATE_CHECKSUMS, app_file
                )
            except:
                # old version
                self.connection.application('genestack/bio-test-preprocess').invoke(
                    '%s' % CALCULATE_CHECKSUMS, app_file
                )

        if expected_checksums:
            # hack to support both master and stable
            try:
                self.connection.application('genestack/bio-test-cla').invoke(
                    'addCheckSums', app_file, expected_checksums or []
                )
            except:
                # old version
                self.connection.application('bio-test-preprocess').invoke(
                    'addCheckSums', app_file, expected_checksums or []
                )

        if initialize:
            self.start(app_file)
        return app_file

    def __create_file(self, source_files, params=None):
        source_file_list = source_files if isinstance(source_files, list) else [source_files]
        result_file = self.invoke('createFile', source_file_list)
        if params is not None:
            self.change_command_line_arguments(result_file, params)
        return result_file

    def change_command_line_arguments(self, accession, params):<commit_msg>Extract test related code to public methods. refs 4775<commit_after># -*- coding: utf-8 -*-

#
# Copyright (c) 2011-2015 Genestack Limited
# All Rights Reserved
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF GENESTACK LIMITED
# The copyright notice above does not evidence any
# actual or intended publication of such source code.
#

from Connection import Application


CALCULATE_CHECKSUMS = 'markKeyForCountChecksum'


class CLApplication(Application):
    """
    Base class to interact with CommandLine Applications.
    Application_ID is required you can pass it as init argument or overwrite it in children.
    Source files and params depends on application.

    """
    APPLICATION_ID = None

    def __repr__(self):
        return self.APPLICATION_ID

    def create_file(self, source_files, name=None, params=None, calculate_checksums=False, expected_checksums=None,
                    initialize=False):
        """
        Creates application native file and returns its accession.
        If source file is not found or is not of source file type, the corresponding exceptions are thrown.

        :param source_files: list of source file accessions
        :type source_files: list
        :param name: if name is specified created file will be renamed
        :type name: str
        :param params: is command arguments to be set, if None then arguments stay default.
        :param params: list
        :param calculate_checksums: flag that used in initialization script to count checsums of created files
        :type calculate_checksums: bool
        :param expected_checksums: List of expected checksums in any order
        :type expected_checksums: list
        :param initialize: flag if initialization should be started immediately
        :return: accession of created file
        :rtype: str
        """
        app_file = self.__create_file(source_files, params)

        if name:
            self.rename_file(app_file, name)

        if calculate_checksums:
            self.mark_for_tests(app_file)

        if expected_checksums:
            self.add_checksums(app_file, expected_checksums)

        if initialize:
            self.start(app_file)
        return app_file

    def mark_for_tests(self, app_file):
        """
        Mark file for test via add corresponding key to the metainfo.
        During initialization test file will calculate md5 checksums for processed files stored in storage.

        :param app_file: accession of file
        :return: None
        """
        self.connection.application('genestack/bio-test-cla').invoke(
            '%s' % CALCULATE_CHECKSUMS, app_file
        )

    def add_checksums(self, app_file, expected_checksums_set):
        """
        Add expected md5 checksum to the metainfo.
        Expected checksums calculated in the next way: number of checksums is same as number of entries in the storage.
        Order of the checksums does not matter.
        For example Reference Genome have 2 entries (annotation and fasta files).
        If there are multiple files in one entry it will concatenate them in order as they were put to storage
        by the initialization script.
        If file marked for test and initialized metainfo have both expected and actual checksums.

        :param app_file: accession of application file
        :param expected_checksums_set: collection of md5 checksums
        :return: None
        """
        self.connection.application('genestack/bio-test-cla').invoke(
            'addCheckSums', app_file, expected_checksums_set
        )

    def __create_file(self, source_files, params=None):
        source_file_list = source_files if isinstance(source_files, list) else [source_files]
        result_file = self.invoke('createFile', source_file_list)
        if params is not None:
            self.change_command_line_arguments(result_file, params)
        return result_file

    def change_command_line_arguments(self, accession, params):
{'fraction_non_alphanumeric': 0.0597707423580786, 'fraction_numerical': 0.0035480349344978166, 'mean_word_length': 2.599705304518664, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2459310', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1824, 'n_words': 677}