示例#1
0
def localize(fn, opts, task, *args, **kwargs):
    """ localize(input=True, output=True, input_kwargs=None, output_kwargs=None)
    Wraps a bound method of a task and temporarily changes the input and output methods to return
    localized targets. When *input* (*output*) is *True*, :py:meth:`Task.input`
    (:py:meth:`Task.output`) is adjusted. *input_kwargs* and *output_kwargs* can be dictionaries
    that are passed as keyword arguments to the respective localization method. Does **not** accept
    generator functions.
    """
    # get actual input and outputs
    input_struct = task.input() if opts["input"] else None
    output_struct = task.output() if opts["output"] else None

    # store original input and output methods
    input_orig = task.output
    output_orig = task.output

    # input and output kwargs
    input_kwargs = opts["input_kwargs"] or {}
    output_kwargs = opts["output_kwargs"] or {}

    # default modes
    input_kwargs.setdefault("mode", "r")
    output_kwargs.setdefault("mode", "w")

    try:
        # localize both target structs
        with localize_file_targets(input_struct, **input_kwargs) as localized_inputs, \
                localize_file_targets(output_struct, **output_kwargs) as localized_outputs:
            # patch the input method to always return the localized inputs
            if opts["input"]:

                def input_patched(self):
                    return localized_inputs

                task.input = input_patched.__get__(task)

            # patch the output method to always return the localized outputs
            if opts["output"]:

                def output_patched(self):
                    return localized_outputs

                task.output = output_patched.__get__(task)

            return fn(task, *args, **kwargs)

    finally:
        # restore the methods
        task.input = input_orig
        task.output = output_orig
示例#2
0
    def localize(self, *args, **kwargs):
        # when localizing collections using temporary files, it makes sense to put
        # them all in the same temporary directory
        tmp_dir = kwargs.get("tmp_dir")
        if not tmp_dir:
            tmp_dir = LocalDirectoryTarget(is_tmp=True)
        kwargs["tmp_dir"] = tmp_dir

        # enter localize contexts of all targets
        with localize_file_targets(self.targets, *args, **kwargs) as localized_targets:
            # create a copy of this collection that wraps the localized targets
            yield self.__class__(localized_targets, **self._copy_kwargs())
示例#3
0
文件: collection.py 项目: yrath/law
    def localize(self, *args, **kwargs):
        # when localizing collections using temporary files, it makes sense to put
        # them all in the same temporary directory
        tmp_dir = kwargs.get("tmp_dir")
        tmp_dir_created = False
        if not tmp_dir:
            tmp_dir = LocalDirectoryTarget(is_tmp=True)
            tmp_dir_created = True
            kwargs["tmp_dir"] = tmp_dir.path

        # enter localize contexts of all targets
        with localize_file_targets(self.targets, *args,
                                   **kwargs) as localized_targets:
            # create a copy of this collection that wraps the localized targets
            copy = self.__class__(localized_targets, **self._copy_kwargs())

            try:
                yield copy

            finally:
                # although tmp_dir would clean itself during garbage collection, an error might have
                # occurred, so for larger collections it is safer to delete the tmp_dir manually
                if tmp_dir_created:
                    tmp_dir.remove()
示例#4
0
 def localize_output(self, *args, **kwargs):
     return localize_file_targets(self.output(), *args, **kwargs)