from harpoon.tasks import available_tasks
from harpoon.errors import BadOption
from harpoon.imager import Imager

from input_algorithms.dictobj import dictobj
from option_merge import MergedOptions

class Task(dictobj):
    fields = [("action", "run"), ("label", "Project"), ("options", None), ("overrides", None), ("description", "")]

    def run(self, harpoon, cli_args):
        """Run this task"""
        task_func = available_tasks[self.action]
        configuration = MergedOptions.using(harpoon.configuration)

        image = getattr(self, "image", cli_args["harpoon"].get("chosen_image"))
        if image:
            configuration.update({"harpoon": {"chosen_image": image}})
        del cli_args["harpoon"]["chosen_image"]

        imager = None
        images = None
        image_name = None
        if task_func.needs_imager:
            imager, images, image_name = self.determine_image(harpoon, configuration, needs_image=task_func.needs_image)

        if image_name:
            conf = images[image_name].configuration
        else:
            conf = configuration

        if self.options:
            conf.update(self.options)

        configuration.update(cli_args)

        if self.overrides:
            configuration.update(self.overrides)

        if imager:
            imager.setup_images(images)

        # Complain about missing env early
        if image_name:
            images[image_name].env

        return available_tasks[self.action](harpoon, conf, imager=imager, images=images, image=image_name)

    def determine_image(self, harpoon, configuration, needs_image=True):
        """Complain if we don't have an image"""
        image = configuration.get("harpoon.chosen_image")
        imager = Imager(configuration, harpoon.docker_context)

        available = None
        try:
            available = imager.images.keys()
        except:
            pass

        images = imager.images
        if needs_image:
            if image is None:
                info = {}
                if available:
                    info["available"] = available
                raise BadOption("Please use --image to specify an image to run /bin/bash in", **info)

            if image not in images:
                raise BadOption("No such image", wanted=image, available=images.keys())

        return imager, images, image

    def specify_image(self, image):
        """Specify the image this task belongs to"""
        self.image = image


@@ -449,76 +449,8 @@
 on%5D%0A
-        configuration = MergedOptions.using(harpoon.configuration)%0A%0A
     
@@ -525,16 +525,108 @@
 mage%22))%0A
+%0A        configuration = MergedOptions.using(harpoon.configuration, dont_prefix=%5Bdictobj%5D)%0A%0A
         
@@ -768,102 +768,44 @@
    i
-mager = None%0A        images = None%0A        image_name = None%0A        if task_func.needs_
+f self.options:%0A            if 
 image
-r
 :%0A  
@@ -818,193 +818,72 @@
     
-imager, images, image_name = self.determine_image(harpoon, configuration, needs_image=task_func.needs_image)%0A%0A        if image_name:%0A            conf = images%5Bimage_name%5D.configuration%0A
+    configuration.update(%7B%22images%22: %7Bimage: self.options%7D%7D)%0A    
     
@@ -908,14 +908,11 @@
     
-conf =
+   
  con
@@ -925,51 +925,8 @@
 tion
-%0A%0A        if self.options:%0A            conf
 .upd
@@ -1073,23 +1073,22 @@
     
-if 
 imager
-:%0A    
+ = None%0A
     
@@ -1100,74 +1100,161 @@
 mage
-r.setup_images(images)%0A%0A        # Complain about missing env early
+s = None%0A        if task_func.needs_imager:%0A            imager, images = self.determine_image(harpoon, configuration, needs_image=task_func.needs_image)%0A
 %0A   
@@ -1266,21 +1266,17 @@
 if image
-_name
+r
 :%0A      
@@ -1290,25 +1290,30 @@
 mage
-s%5Bimage_name%5D.env
+r.setup_images(images)
 %0A%0A  
@@ -1367,16 +1367,25 @@
 on, conf
+iguration
 , imager
@@ -1423,13 +1423,8 @@
 mage
-_name
 )%0A%0A 
@@ -1695,25 +1695,8 @@
 one%0A
-        try:%0A    
     
@@ -1735,41 +1735,8 @@
 ys()
-%0A        except:%0A            pass
 %0A%0A  
@@ -2181,23 +2181,16 @@
 , images
-, image
 %0A%0A    de

Simplifying task_spec