from contextlib import contextmanager
import tempfile
import shutil
import uuid
import six
import os

class FilesAssertionsMixin:

    def unique_val(self):
        """Return a uuid1 value"""
        return str(uuid.uuid1())

    def make_temp_dir(self):
        """
        Make a temp directory and remove it.
        Record it so it can be cleaned up later
        """
        if not hasattr(self, "_temp_dirs"):
            self._temp_dirs = []

        tmp = tempfile.mkdtemp()
        self._temp_dirs.append(tmp)
        return tmp

    def cleanup_temp_things(self):
        """
        Clean up any temporary things that were made during this test
        """
        if hasattr(self, "_temp_dirs"):
            for tmp in self._temp_dirs:
                if os.path.exists(tmp):
                    shutil.rmtree(tmp)
    cleanup_temp_things._harpoon_case_teardown = True

    @contextmanager
    def a_temp_file(self, body=None, removed=False):
        """
        Yield a temporary file and ensure it's deleted
        """
        filename = None
        try:
            filename = tempfile.NamedTemporaryFile(delete=False).name
            if body:
                with open(filename, 'w') as fle:
                    fle.write(body)
            if removed:
                os.remove(filename)
            yield filename
        finally:
            if filename and os.path.exists(filename):
                os.remove(filename)

    @contextmanager
    def a_temp_dir(self, removed=False):
        """
        Yield a temporary directory and ensure it is deleted
        """
        directory = None
        try:
            directory = tempfile.mkdtemp()
            if removed:
                shutil.rmtree(directory)
            yield directory
        finally:
            if directory and os.path.exists(directory):
                shutil.rmtree(directory)

    def touch_files(self, root, files_list):
        """
        Given a list of of [<file_spec>, ...] create the specified files under specified root.

        Where <file_spec> may be

            a string
                If it ends with a slash, it creates an empty directory at this location,
                otherwise it touches a file at this place

                In either case, it creates all directories leading up to it

            (a string, a string)
                It creates a file at the location of the first string and writes the
                second string to this location.
        """

        def create(location, contents=None):
            """Create this location with specified contents"""
            dirname, filename = os.path.split(location)
            if not os.path.exists(dirname):
                os.makedirs(dirname)

            if filename:
                with open(location, 'w') as fle:
                    if contents:
                        fle.write(contents)
                    else:
                        fle.write("")

        for file_spec in files_list:
            if isinstance(file_spec, six.string_types):
                create(os.path.join(root, file_spec), None)

            else:
                file_location, file_contents = file_spec
                create(os.path.join(root, file_location), file_contents)


@@ -219,16 +219,337 @@
 id1())%0A%0A
+    def make_temp_file(self):%0A        %22%22%22%0A        Make a temp file.%0A        Record it so it can be cleaned up later%0A        %22%22%22%0A        if not hasattr(self, %22_temp_files%22):%0A            self._temp_files = %5B%5D%0A%0A        tmp = tempfile.NamedTemporaryFile(delete=False)%0A        self._temp_files.append(tmp)%0A        return tmp%0A%0A
     def 
@@ -614,22 +614,8 @@
 tory
- and remove it
 .%0A  
@@ -980,23 +980,32 @@
     
-if hasattr(self
+for key in (%22_temp_dirs%22
 , %22_
@@ -1001,35 +1001,36 @@
 p_dirs%22, %22_temp_
-dir
+file
 s%22):%0A           
@@ -1041,31 +1041,38 @@
  tmp in 
-self._temp_dirs
+getattr(self, key, %5B%5D)
 :%0A      

Add make_temp_file helper