
"""
Nose plugin to add warnings filters (turn them into error) using nose.cfg file.
"""


__version__ = '0.0.5'


from nose.plugins import Plugin
import warnings
import sys

if sys.version_info < (3,):
    def from_builtins(k):
        return __builtins__[k]
else:
    import builtins
    def from_builtins(k):
        return getattr(builtins,k)


class WarningFilter(Plugin):
    def options(self, parser, env):
        """
        Add options to command line.
        """
        super(WarningFilter, self).options(parser, env)
        parser.add_option("--warningfilters",
                          default=None,
                          help="Treat warnings that occur WITHIN tests as errors.")

    def configure(self, options, conf):
        """
        Configure plugin.
        """
        for opt in options.warningfilters.split( '\n'):
            vs = [s.strip() for s in opt.split('|')]
            vs[2] = from_builtins(vs[2])
            warnings.filterwarnings(*vs)

        super(WarningFilter, self).configure(options, conf)


    def prepareTestRunner(self, runner):
        """
        Treat warnings as errors.
        """
        return WarningFilterRunner(runner)



class WarningFilterRunner(object):

    def __init__(self, runner):
        self.runner=runner
        
    def run(self, test):
        return self.runner.run(test)



        


@@ -168,16 +168,968 @@
 rt sys%0A%0A
+%0Adef import_item(name):%0A    %22%22%22Import and return %60%60bar%60%60 given the string %60%60foo.bar%60%60.%0A%0A    Calling %60%60bar = import_item(%22foo.bar%22)%60%60 is the functional equivalent of%0A    executing the code %60%60from foo import bar%60%60.%0A%0A    Parameters%0A    ----------%0A    name : string%0A      The fully qualified name of the module/package being imported.%0A%0A    Returns%0A    -------%0A    mod : module object%0A       The module that was imported.%0A    %22%22%22%0A    if sys.version_info %3C (3,):%0A        if not isinstance(name, bytes):%0A            name =  name.encode()%0A    parts = name.rsplit('.', 1)%0A    if len(parts) == 2:%0A        # called with 'foo.bar....'%0A        package, obj = parts%0A        module = __import__(package, fromlist=%5Bobj%5D)%0A        try:%0A            pak = getattr(module, obj)%0A        except AttributeError:%0A            raise ImportError('No module named %25s' %25 obj)%0A        return pak%0A    else:%0A        # called with un-dotted string%0A        return __import__(parts%5B0%5D)%0A%0A%0A
 if sys.v
@@ -1806,16 +1806,20 @@
        v
+alue
 s = %5Bs.s
@@ -1862,17 +1862,127 @@
         
-v
+if '.' in values%5B2%5D:%0A                values%5B2%5D = import_item(values%5B2%5D)%0A            else:%0A                value
 s%5B2%5D = f
@@ -1995,16 +1995,20 @@
 iltins(v
+alue
 s%5B2%5D)%0A  
@@ -2043,16 +2043,20 @@
 nings(*v
+alue
 s)%0A%0A    

add ability to import fully qualified names