import os
import errno

from .batch_generator import BatchGenerator

class FilesystemPathGenerator(BatchGenerator):
    """
    /data/users/users_20180101.csv
    /data/users/users_20180102.csv
    """

    def __init__(self, name="default", datasource=None, base_directory="/data"):
        super(FilesystemPathGenerator, self).__init__(name, type_="filesystem", datasource=datasource)
        self._base_directory = base_directory

    def list_available_data_asset_names(self):
        known_assets = set()
        file_options = os.listdir(self._get_current_base_directory())
        for file_option in file_options:
            if file_option.endswith(".csv"):
                known_assets.add(file_option[:-4])
            else:
                known_assets.add(file_option)
        return known_assets

    def _get_iterator(self, data_asset_name):
        # If the data_asset_name is a file, then return the path.
        # Otherwise, use files in a subdir as batches
        if os.path.isdir(os.path.join(self._get_current_base_directory(), data_asset_name)):
            # return self._build_batch_kwargs_path_iter(os.scandir(os.path.join(self._get_current_base_directory(), data_asset_name)))
            return iter([{
                "path": os.path.join(self._get_current_base_directory(), data_asset_name, x)
            } for x in os.listdir(os.path.join(self._get_current_base_directory(), data_asset_name))])
        elif os.path.isfile(os.path.join(self._get_current_base_directory(), data_asset_name)):
            return iter([
                {
                    "path": os.path.join(self._get_current_base_directory(), data_asset_name)
                }
                ])
        elif os.path.isfile(os.path.join(self._get_current_base_directory(), data_asset_name + ".csv")):
            return iter([
                {
                    "path": os.path.join(self._get_current_base_directory(), data_asset_name + ".csv")
                }
                ])
        else:
            raise IOError(os.path.join(self._base_directory, data_asset_name))

    # Removed to support python2 (no scandir; listdir instead)
    # def _build_batch_kwargs_path_iter(self, path_iter):
    #     try:
    #         while True:
    #             yield {
    #                 "path": next(path_iter).path
    #             }
    #     except StopIteration:
    #         return

    # If base directory is a relative path, interpret it as relative to the data context's
    # context root directory (parent directory of great_expectation dir)
    def _get_current_base_directory(self):
        if os.path.isabs(self._base_directory) or self._datasource.get_data_context() is None:
            return self._base_directory
        else:
            return os.path.join(self._datasource.get_data_context().get_context_root_directory(), self._base_directory)

@@ -15,16 +15,31 @@
 rt errno
+%0Aimport hashlib
 %0A%0Afrom .
@@ -1069,32 +1069,165 @@
 a_asset_name)):%0A
+            return self._build_batch_kwargs_path_iter(os.listdir(os.path.join(self._get_current_base_directory(), data_asset_name)))%0A
             # re
@@ -1348,32 +1348,34 @@
 e)))%0A           
+ #
  return iter(%5B%7B%0A
@@ -1377,32 +1377,34 @@
 r(%5B%7B%0A           
+ #
      %22path%22: os.
@@ -1480,16 +1480,18 @@
         
+ #
  %7D for x
@@ -1686,67 +1686,14 @@
     
-return iter(%5B%0A                %7B%0A                    %22
 path
-%22:
+ =
  os.
@@ -1755,16 +1755,220 @@
 t_name)%0A
+            with open(path,'rb') as f:%0A                md5 = hashlib.md5(f.read()).hexdigest()%0A            return iter(%5B%0A                %7B%0A                    %22path%22: path,%0A                    %22md5%22: md5%0A
         
@@ -2117,67 +2117,14 @@
     
-return iter(%5B%0A                %7B%0A                    %22
 path
-%22:
+ =
  os.
@@ -2195,16 +2195,220 @@
 %22.csv%22)%0A
+            with open(path,'rb') as f:%0A                md5 = hashlib.md5(f.read()).hexdigest()%0A            return iter(%5B%0A                %7B%0A                    %22path%22: path,%0A                    %22md5%22: md5%0A
         
@@ -2540,70 +2540,63 @@
   # 
-Removed to support python2 (no scandir; listdir instead)
+def _build_batch_kwargs_path_iter(self, path_iter):
 %0A   
- #
  def
@@ -2641,24 +2641,241 @@
 ath_
-iter):%0A    #    
+list):%0A        for path in path_list:%0A            with open(path,'rb') as f:%0A                md5 = hashlib.md5(f.read()).hexdigest()%0A            yield %7B%0A                %22path%22: path,%0A                %22md5%22: md5%0A            %7D%0A        #
  try
@@ -2876,29 +2876,29 @@
  # try:%0A    
-#
     
+#
      while T
@@ -2902,29 +2902,29 @@
 e True:%0A    
-#
     
+#
          yie
@@ -2928,29 +2928,29 @@
 yield %7B%0A    
-#
     
+#
             
@@ -2979,29 +2979,29 @@
 r).path%0A    
-#
     
+#
          %7D%0A 
@@ -3003,21 +3003,21 @@
   %7D%0A    
-#
     
+#
  except 
@@ -3031,29 +3031,29 @@
 ration:%0A    
-#
     
+#
      return%0A

Add md5 to path generator