def test_designates(self):
     """
     verify that WhiteList.designates() works
     """
     self.assertTrue(
         WhiteList.from_string("foo").designates(make_job('foo')))
     self.assertTrue(
         WhiteList.from_string("foo\nbar\n").designates(make_job('foo')))
     self.assertTrue(
         WhiteList.from_string("foo\nbar\n").designates(make_job('bar')))
     # Note, it's not matching either!
     self.assertFalse(
         WhiteList.from_string("foo").designates(make_job('foobar')))
     self.assertFalse(
         WhiteList.from_string("bar").designates(make_job('foobar')))
示例#2
0
 def test_designates(self):
     """
     verify that WhiteList.designates() works
     """
     self.assertTrue(
         WhiteList.from_string("foo").designates(make_job('foo')))
     self.assertTrue(
         WhiteList.from_string("foo\nbar\n").designates(make_job('foo')))
     self.assertTrue(
         WhiteList.from_string("foo\nbar\n").designates(make_job('bar')))
     # Note, it's not matching either!
     self.assertFalse(
         WhiteList.from_string("foo").designates(make_job('foobar')))
     self.assertFalse(
         WhiteList.from_string("bar").designates(make_job('foobar')))
示例#3
0
 def __init__(self, filename, text):
     """
     Initialize the plug-in with the specified name text
     """
     try:
         self._whitelist = WhiteList.from_string(text, filename=filename)
     except Exception as exc:
         raise PlugInError(
             "Cannot load whitelist {!r}: {}".format(filename, exc))
示例#4
0
 def __init__(self, filename, text, implicit_namespace=None):
     """
     Initialize the plug-in with the specified name text
     """
     try:
         self._whitelist = WhiteList.from_string(
             text, filename=filename, implicit_namespace=implicit_namespace)
     except Exception as exc:
         raise PlugInError(
             _("Cannot load whitelist {!r}: {}").format(filename, exc))
 def test_from_string(self):
     """
     verify that WhiteList.from_string() works
     """
     whitelist = WhiteList.from_string("\n".join(self._content))
     # verify that the patterns are okay
     self.assertEqual(repr(whitelist.qualifier_list[0]),
                      "RegExpJobQualifier('^foo$', inclusive=True)")
     # verify that whitelist name is the empty default
     self.assertEqual(whitelist.name, None)
     # verify that the origin got set to the default constructed value
     self.assertEqual(whitelist.origin, Origin(UnknownTextSource(), 1, 3))
示例#6
0
 def test_from_string(self):
     """
     verify that WhiteList.from_string() works
     """
     whitelist = WhiteList.from_string("\n".join(self._content))
     # verify that the patterns are okay
     self.assertEqual(
         repr(whitelist.qualifier_list[0]),
         "RegExpJobQualifier('^foo$', inclusive=True)")
     # verify that whitelist name is the empty default
     self.assertEqual(whitelist.name, None)
     # verify that the origin got set to the default constructed value
     self.assertEqual(whitelist.origin, Origin(UnknownTextSource(), 1, 3))
 def test_from_string__with_filename(self):
     """
     verify that WhiteList.from_string() works when passing filename
     """
     # construct a whitelist with some dummy data, the names, pathnames and
     # line ranges are arbitrary
     whitelist = WhiteList.from_string("\n".join(self._content),
                                       filename="somefile.txt")
     # verify that the patterns are okay
     self.assertEqual(repr(whitelist.qualifier_list[0]),
                      "RegExpJobQualifier('^foo$', inclusive=True)")
     # verify that whitelist name is derived from the filename
     self.assertEqual(whitelist.name, "somefile")
     # verify that the origin is properly derived from the filename
     self.assertEqual(whitelist.origin,
                      Origin(FileTextSource("somefile.txt"), 1, 3))
 def test_namespace_behavior(self):
     """
     verify that WhiteList() correctly respects namespace declarations
     and uses implict_namespace to fully qualifiy all patterns
     """
     whitelist = WhiteList.from_string(
         "foo\n"
         "2014\\.example\\.org::bar\n",
         implicit_namespace="2014.other.example.org")
     # verify that the implicit namespace was recorded
     self.assertEqual(whitelist.implicit_namespace,
                      "2014.other.example.org")
     # verify that the patterns are okay
     self.assertEqual(whitelist.qualifier_list[0].pattern_text,
                      "^2014\\.other\\.example\\.org::foo$")
     self.assertEqual(whitelist.qualifier_list[1].pattern_text,
                      "^2014\\.example\\.org::bar$")
示例#9
0
 def test_from_string__with_filename(self):
     """
     verify that WhiteList.from_string() works when passing filename
     """
     # construct a whitelist with some dummy data, the names, pathnames and
     # line ranges are arbitrary
     whitelist = WhiteList.from_string(
         "\n".join(self._content), filename="somefile.txt")
     # verify that the patterns are okay
     self.assertEqual(
         repr(whitelist.qualifier_list[0]),
         "RegExpJobQualifier('^foo$', inclusive=True)")
     # verify that whitelist name is derived from the filename
     self.assertEqual(whitelist.name, "somefile")
     # verify that the origin is properly derived from the filename
     self.assertEqual(
         whitelist.origin, Origin(FileTextSource("somefile.txt"), 1, 3))
示例#10
0
    def get_whitelist_from_file(self, filename, stream=None):
        """
        Load a whitelist from a file, with special behavior.

        :param filename:
            name of the file to load
        :param stream:
            (optional) pre-opened stream pointing at the whitelist
        :returns:
            The loaded whitelist or None if loading fails for any reason

        This function implements special loading behavior for whitelists that
        makes them inherit the implicit namespace of the provider they may be a
        part of. Before loading the whitelist directly from the file, all known
        providers are interrogated to see if any of them has a whitelist that
        was loaded from the same file (as indicated by os.path.realpath())

        The stream argument can be provided if the caller already has an open
        file object, which is typically the case when working with argparse.
        """
        # Look up a whitelist with the same name in any of the providers
        wanted_realpath = os.path.realpath(filename)
        for provider in self.provider_list:
            for whitelist in provider.get_builtin_whitelists():
                if (whitelist.origin is not None
                        and whitelist.origin.source is not None
                        and isinstance(whitelist.origin.source,
                                       FileTextSource)
                        and os.path.realpath(
                            whitelist.origin.source.filename) ==
                        wanted_realpath):
                    logger.debug(
                        _("Using whitelist %r obtained from provider %r"),
                        whitelist.name, provider)
                    return whitelist
        # Or load it directly
        try:
            if stream is not None:
                return WhiteList.from_string(stream.read(), filename=filename)
            else:
                return WhiteList.from_file(filename)
        except Exception as exc:
            logger.warning(
                _("Unable to load whitelist %r: %s"), filename, exc)
示例#11
0
 def test_namespace_behavior(self):
     """
     verify that WhiteList() correctly respects namespace declarations
     and uses implict_namespace to fully qualifiy all patterns
     """
     whitelist = WhiteList.from_string(
         "foo\n"
         "2014\\.example\\.org::bar\n",
         implicit_namespace="2014.other.example.org")
     # verify that the implicit namespace was recorded
     self.assertEqual(
         whitelist.implicit_namespace, "2014.other.example.org")
     # verify that the patterns are okay
     self.assertEqual(
         whitelist.qualifier_list[0].pattern_text,
         "^2014\\.other\\.example\\.org::foo$")
     self.assertEqual(
         whitelist.qualifier_list[1].pattern_text,
         "^2014\\.example\\.org::bar$")
示例#12
0
    def get_whitelist_from_file(self, filename, stream=None):
        """
        Load a whitelist from a file, with special behavior.

        :param filename:
            name of the file to load
        :param stream:
            (optional) pre-opened stream pointing at the whitelist
        :returns:
            The loaded whitelist or None if loading fails for any reason

        This function implements special loading behavior for whitelists that
        makes them inherit the implicit namespace of the provider they may be a
        part of. Before loading the whitelist directly from the file, all known
        providers are interrogated to see if any of them has a whitelist that
        was loaded from the same file (as indicated by os.path.realpath())

        The stream argument can be provided if the caller already has an open
        file object, which is typically the case when working with argparse.
        """
        # Look up a whitelist with the same name in any of the providers
        wanted_realpath = os.path.realpath(filename)
        for provider in self.provider_list:
            for whitelist in provider.get_builtin_whitelists():
                if (whitelist.origin is not None
                        and whitelist.origin.source is not None
                        and isinstance(whitelist.origin.source, FileTextSource)
                        and os.path.realpath(whitelist.origin.source.filename)
                        == wanted_realpath):
                    logger.debug(
                        _("Using whitelist %r obtained from provider %r"),
                        whitelist.name, provider)
                    return whitelist
        # Or load it directly
        try:
            if stream is not None:
                return WhiteList.from_string(stream.read(), filename=filename)
            else:
                return WhiteList.from_file(filename)
        except Exception as exc:
            logger.warning(_("Unable to load whitelist %r: %s"), filename, exc)
 def test_from_empty_string(self):
     """
     verify that WhiteList.from_string("") works
     """
     WhiteList.from_string("")
示例#14
0
 def test_from_empty_string(self):
     """
     verify that WhiteList.from_string("") works
     """
     WhiteList.from_string("")