import os
import logging

export_commands = [('status', 'report status of this repository.')]

def load(options, readonly=False):
	assert 'path' in options

	if 'extensions' in options:
		allowedExtensions = options['extensions'].split(',')
	else:
		allowedExtensions = None

	if 'resursive' in options:
		recursive = bool(options['resursive'])
	else:
		recursive = True

	return FileRepository(options['path'], allowedExtensions = allowedExtensions, recursive = recursive)

class FileRepository:

	def __init__(self, path, allowedExtensions=None, recursive=True):
		self.root = path
		self.allowedExtensions = allowedExtensions
		self.recursive = recursive

	def __enter__(self):
		pass

	def __exit__(self, exc_type, exc_value, traceback):
		pass

	def status(self):
		print("Folder at {}".format(self.root))

	def iterFiles(self):
		"""
		Iterate files in this file repository. Returns a generator of 3-tuples, 
		containing a handle, filename and file extension of the current opened file.
		"""
		logging.debug("Iterating files in "+ self.root)
		for root, dirs, files in os.walk(self.root):
			# iterate files, filtering only allowed extensions
			for f in files:
				fname, ext = os.path.splitext(f)
				if self.allowedExtensions == None or ext in self.allowedExtensions:
					with open(os.path.join(root, f), 'r') as fp:
						yield (fp, f, ext)

			# stop after first iteration if not recursive
			if not self.recursive:
				break

	def close(self):
		pass

@@ -17,16 +17,41 @@
  logging
+%0Aimport zipfile%0Aimport io
 %0A%0Aexport
@@ -301,17 +301,17 @@
 %0A%09if 're
-s
+c
 ursive' 
@@ -352,17 +352,17 @@
 ions%5B're
-s
+c
 ursive'%5D
@@ -390,16 +390,97 @@
 = True%0A%0A
+%09if 'unzip' in options:%0A%09%09unzip = bool(options%5B'unzip'%5D)%0A%09else:%0A%09%09unzip = False%0A%0A
 %09return 
@@ -571,16 +571,31 @@
 ecursive
+, unzip = unzip
 )%0A%0Aclass
@@ -676,16 +676,29 @@
 ive=True
+, unzip=False
 ):%0A%09%09sel
@@ -784,16 +784,37 @@
 ecursive
+%0A%09%09self.unzip = unzip
 %0A%0A%09def _
@@ -1501,16 +1501,488 @@
  f, ext)
+%0A%09%09%09%09# zip file auto-extract%0A%09%09%09%09elif self.unzip and ext == '.zip':%0A%09%09%09%09%09with zipfile.ZipFile(os.path.join(root, f), 'r') as z:%0A%09%09%09%09%09%09for zname in z.namelist():%0A%09%09%09%09%09%09%09zfname, ext = os.path.splitext(zname)%0A%09%09%09%09%09%09%09if self.allowedExtensions == None or ext in self.allowedExtensions:%0A%09%09%09%09%09%09%09%09with z.open(zname, 'r') as fp:%0A%09%09%09%09%09%09%09%09%09# zipfile returns a binary file, so we require a TextIOWrapper to decode it%0A%09%09%09%09%09%09%09%09%09yield (io.TextIOWrapper(fp, encoding='ascii'), zname, ext)
 %0A%0A%09%09%09# s

Add auto unzip option for file repository.