<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


CALC_CHECKSUMS_METHOD_NAME = 'markKeyForCountChecksum'
ADD_CHECKSUM_METHOD_NAME = 'addCheckSums'


class CLApplication(Application):
    """
    Base class to interact with Genestack command-line applications.
    The ``APPLICATION_ID`` is mandatory. You can either pass it as an argument to the class constructor or override
    it in a child class.
    Source files and parameters are application-specific.

    """
    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):
        """
        Create a native file with the application and return its accession.
        If a source file is not found or is not of the expected type, an exception will be thrown.

        :param source_files: list of source files accessions
        :type source_files: list
        :param name: if a name is provided, the created file will be renamed
        :type name: str
        :param params: custom command-line arguments strings; if None, the application defaults will be used.
        :param params: list
        :param calculate_checksums: a flag used in the initialization script to compute checksums for the created files
        :type calculate_checksums: bool
        :param expected_checksums: List of expected checksums, in any order
        :type expected_checksums: list
        :param initialize: should initialization be started immediately after the file is created?
        :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.
        Test file will calculate md5 checksums for processed files stored in the storage during initialization.

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

    def add_checksums(self, app_file, expected_checksums):
        """
        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.
            For example Reference Genome have 2 entries (annotation and fasta files).
          - Order of the checksums does not matter (TODO: that might entail problems!).
          - 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 then after initialization metainfo
            will have both expected and actual checksums.

        :param app_file: accession of application file
        :param expected_checksums: collection of md5 checksums
        :return: None
        """

        self.connection.application('genestack/bio-test-cla').invoke(
            ADD_CHECKSUM_METHOD_NAME, app_file, expected_checksums)

    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):
        """
        Change the command-line arguments strings in the file's metainfo.
        ``params`` is a list of command-line strings.

        If the file is not found, does not have the right file type
        or is already initialized, an exception will be thrown.

        :param accession: file accession
        :type accession: str<commit_msg>Fix docs indentation.<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


CALC_CHECKSUMS_METHOD_NAME = 'markKeyForCountChecksum'
ADD_CHECKSUM_METHOD_NAME = 'addCheckSums'


class CLApplication(Application):
    """
    Base class to interact with Genestack command-line applications.
    The ``APPLICATION_ID`` is mandatory. You can either pass it as an argument to the class constructor or override
    it in a child class.
    Source files and parameters are application-specific.

    """
    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):
        """
        Create a native file with the application and return its accession.
        If a source file is not found or is not of the expected type, an exception will be thrown.

        :param source_files: list of source files accessions
        :type source_files: list
        :param name: if a name is provided, the created file will be renamed
        :type name: str
        :param params: custom command-line arguments strings; if None, the application defaults will be used.
        :param params: list
        :param calculate_checksums: a flag used in the initialization script to compute checksums for the created files
        :type calculate_checksums: bool
        :param expected_checksums: List of expected checksums, in any order
        :type expected_checksums: list
        :param initialize: should initialization be started immediately after the file is created?
        :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.
        Test file will calculate md5 checksums for processed files stored in the storage during initialization.

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

    def add_checksums(self, app_file, expected_checksums):
        """
        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.
             For example Reference Genome have 2 entries (annotation and fasta files).
           - Order of the checksums does not matter (TODO: that might entail problems!).
           - 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 then after initialization metainfo
             will have both expected and actual checksums.

        :param app_file: accession of application file
        :param expected_checksums: collection of md5 checksums
        :return: None
        """

        self.connection.application('genestack/bio-test-cla').invoke(
            ADD_CHECKSUM_METHOD_NAME, app_file, expected_checksums)

    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):
        """
        Change the command-line arguments strings in the file's metainfo.
        ``params`` is a list of command-line strings.

        If the file is not found, does not have the right file type
        or is already initialized, an exception will be thrown.

        :param accession: file accession
        :type accession: str
{'fraction_non_alphanumeric': 0.05680564747407898, 'fraction_numerical': 0.002867857930730201, 'mean_word_length': 2.7684954280964256, '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': '4919018', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2249, 'n_words': 923}