Source code for mg_process_test.tool.testTool

.. See the NOTICE file distributed with this work for additional information
   regarding copyright ownership.

   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

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   See the License for the specific language governing permissions and
   limitations under the License.
from __future__ import print_function

import sys

from utils import logger

    if hasattr(sys, '_run_from_cmdl') is True:
        raise ImportError
    from pycompss.api.parameter import FILE_IN, FILE_OUT
    from pycompss.api.task import task
    from pycompss.api.api import compss_wait_on
except ImportError:
    logger.warn("[Warning] Cannot import \"pycompss\" API packages.")
    logger.warn("          Using mock decorators.")

    from utils.dummy_pycompss import FILE_IN, FILE_OUT  # pylint: disable=ungrouped-imports
    from utils.dummy_pycompss import task  # pylint: disable=ungrouped-imports
    from utils.dummy_pycompss import compss_wait_on  # pylint: disable=ungrouped-imports

from basic_modules.tool import Tool
from basic_modules.metadata import Metadata

# ------------------------------------------------------------------------------

[docs]class testTool(Tool): # pylint: disable=invalid-name """ Tool for writing to a file """ def __init__(self, configuration=None): """ Init function """"Test writer") Tool.__init__(self) if configuration is None: configuration = {} self.configuration.update(configuration)
[docs] @task(returns=bool, file_in_loc=FILE_IN, file_out_loc=FILE_OUT, isModifier=False) def test_writer(self, file_in_loc, file_out_loc): # pylint: disable=no-self-use """ Count the number of characters in a file and return a file with the count Parameters ---------- file_in_loc : str Location of the input file file_out_loc : str Location of an output file Returns ------- bool Writes to the file, which is returned by pyCOMPSs to the defined location """ try: with open(file_in_loc, "r") as f_in: with open(file_out_loc, "w") as file_handle: char_count = 0 for line in f_in: char_count += len(line) file_handle.write("There are " + str(char_count) + " chacaters the file") except IOError as error: logger.fatal("I/O error({0}): {1}".format(error.errno, error.strerror)) return False return True
[docs] def run(self, input_files, input_metadata, output_files): """ The main function to run the test_writer tool Parameters ---------- input_files : dict List of input files - In this case there are no input files required input_metadata: dict Matching metadata for each of the files, plus any additional data output_files : dict List of the output files that are to be generated Returns ------- output_files : dict List of files with a single entry. output_metadata : dict List of matching metadata for the returned files """ results = self.test_writer( input_files["input"], output_files["output"] ) results = compss_wait_on(results) if results is False: logger.fatal("Test Writer: run failed") return {}, {} output_metadata = { "output": Metadata( data_type="<data_type>", file_type="txt", file_path=output_files["output"], sources=[input_metadata["input"].file_path], taxon_id=input_metadata["input"].taxon_id, meta_data={ "tool": "testTool" } ) } return (output_files, output_metadata)