import os
import logging
from nose.plugins import Plugin

log = logging.getLogger('nose.plugins.nose_exclude')

class NoseExclude(Plugin):

    def options(self, parser, env=os.environ):
        """Define the command line options for the plugin."""
        super(NoseExclude, self).options(parser, env)
        parser.add_option(
            "--exclude-dir", action="append",
            dest="exclude_dirs",
            help="Directory to exclude from test discovery. \
                Path can be relative to current working directory \
                or an absolute path. May be specified multiple \
                times. [NOSE_EXCLUDE_DIRS]")
        
        parser.add_option(
            "--exclude-dir-file", type="string",
            dest="exclude_dir_file",
            help="A file containing a list of directories to exclude \
                from test discovery. Paths can be relative to current \
                working directory or an absolute path. \
                [NOSE_EXCLUDE_DIRS_FILE]")

    def _force_to_abspath(self, pathname):
        if os.path.isabs(pathname):
            abspath = pathname
        else:
            abspath = os.path.abspath(pathname)

        if os.path.exists(abspath):
            return abspath
        else:
            raise ValueError("invalid path: %s" % pathname)
    
    def _load_from_file(self, filename):
        infile = open(filename)
        new_list = [l.strip() for l in infile.readlines() if l.strip()]
            
        return new_list

    def configure(self, options, conf):
        """Configure plugin based on command line options"""
        super(NoseExclude, self).configure(options, conf)

        self.exclude_dirs = {}
        
        # preload directories from file
        if options.exclude_dir_file:
            if not options.exclude_dirs:
                options.exclude_dirs = []
    
            new_dirs = self._load_from_file(options.exclude_dir_file)
            options.exclude_dirs.extend(new_dirs)

        if not options.exclude_dirs:
            self.enabled = False
            return
        
        self.enabled = True
        root = os.getcwd()
        log.debug('cwd: %s' % root)

        # Normalize excluded directory names for lookup
        for d in options.exclude_dirs:
            abs_d = self._force_to_abspath(d)
            self.exclude_dirs[abs_d] = True

        exclude_str = "excluding dirs: %s" % ",".join(self.exclude_dirs.keys())
        log.debug(exclude_str)

    def wantDirectory(self, dirname):
        """Check if directory is eligible for test discovery"""
        if dirname in self.exclude_dirs:
            log.debug("excluded: %s" % dirname)
            return False
        else:
            return True



@@ -642,25 +642,17 @@
 DIRS%5D%22)%0A
-        
 %0A
+
         
@@ -1310,21 +1310,17 @@
 thname)%0A
-    
 %0A
+
     def 
@@ -1460,21 +1460,9 @@
 ()%5D%0A
-            
 %0A
+
     
@@ -1673,25 +1673,17 @@
 rs = %7B%7D%0A
-        
 %0A
+
         
@@ -1834,21 +1834,17 @@
 rs = %5B%5D%0A
-    
 %0A
+
         
@@ -2049,17 +2049,9 @@
 urn%0A
-        
 %0A
+
     
@@ -2206,17 +2206,29 @@
     for 
-d
+exclude_param
  in opti
@@ -2237,32 +2237,257 @@
 s.exclude_dirs:%0A
+            # when using setup.cfg, you can specify only one 'exclude-dir'%0A            # separated by some character (new line is good enough)%0A            for d in exclude_param.split('%5Cn'):%0A                d = d.strip()%0A    
             abs_
@@ -2516,16 +2516,20 @@
 path(d)%0A
+    
         

Add support for multiple exclude-dir values for single parameter.