class PkgResourcesPlugInCollectionTests(TestCase): """ Tests for PlugInCollectionBase class """ _NAMESPACE = "namespace" def setUp(self): # Create a collection self.col = PkgResourcesPlugInCollection(self._NAMESPACE) def test_namespace_is_set(self): # Ensure that namespace was saved self.assertEqual(self.col._namespace, self._NAMESPACE) def test_plugins_are_empty(self): # Ensure that plugins start out empty self.assertEqual(len(self.col._plugins), 0) def test_initial_loaded_flag(self): # Ensure that 'loaded' flag is false self.assertFalse(self.col._loaded) def test_default_wrapper(self): # Ensure that the wrapper is :class:`PlugIn` self.assertEqual(self.col._wrapper, PlugIn) @mock.patch('pkg_resources.iter_entry_points') def test_load(self, mock_iter): # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mocked entry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.return_value = "one" # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins self.col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_called_with(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with() @mock.patch('plainbox.impl.secure.plugins.logger') @mock.patch('pkg_resources.iter_entry_points') def test_load_failing(self, mock_iter, mock_logger): # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mocked entry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.side_effect = ImportError("boom") # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins self.col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_called_with(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with() # Ensure that an exception was logged mock_logger.exception.assert_called_with("Unable to import %s", mock_ep2) # Ensure that the error was collected self.assertIsInstance(self.col.problem_list[0], ImportError)
def setUp(self): # Create a collection self.col = PkgResourcesPlugInCollection(self._NAMESPACE)
class AutotoolsBuildSystem(IBuildSystem): """ A build system for projects using autotools """ def probe(self, src_dir: str) -> int: if os.path.isfile(os.path.join(src_dir, "configure")): return 90 return 0 def get_build_command(self, src_dir: str, build_dir: str) -> str: return "{}/configure && make".format( shlex.quote(os.path.relpath(src_dir, build_dir))) class GoBuildSystem(IBuildSystem): """ A build system for projects written in go """ def probe(self, src_dir: str) -> int: if glob.glob("{}/*.go".format(src_dir)) != []: return 50 return 0 def get_build_command(self, src_dir: str, build_dir: str) -> str: return "go build {}/*.go".format(os.path.relpath(src_dir, build_dir)) # Collection of all buildsystems all_buildsystems = PkgResourcesPlugInCollection('plainbox.buildsystem')
========================================================== """ import string from plainbox.impl.secure.plugins import PkgResourcesPlugInCollection __all__ = ['get_accessed_parameters', 'all_unit'] def get_accessed_parameters(text): """ Parse a new-style python string template and return parameter names :param text: Text string to parse :returns: A frozenset() with a list of names (or indices) of accessed parameters """ # https://docs.python.org/3.4/library/string.html#string.Formatter.parse # # info[1] is the field_name (name of the referenced # formatting field) it _may_ be None if there are no format # parameters used return frozenset(info[1] for info in string.Formatter().parse(text) if info[1] is not None) # Collection of all unit classes all_units = PkgResourcesPlugInCollection('plainbox.unit')
class PkgResourcesPlugInCollectionTests(TestCase): """ Tests for PlugInCollectionBase class """ _NAMESPACE = "namespace" def setUp(self): # Create a collection self.col = PkgResourcesPlugInCollection(self._NAMESPACE) def test_namespace_is_set(self): # Ensure that namespace was saved self.assertEqual(self.col._namespace, self._NAMESPACE) def test_plugins_are_empty(self): # Ensure that plugins start out empty self.assertEqual(len(self.col._plugins), 0) def test_initial_loaded_flag(self): # Ensure that 'loaded' flag is false self.assertFalse(self.col._loaded) def test_default_wrapper(self): # Ensure that the wrapper is :class:`PlugIn` self.assertEqual(self.col._wrapper, PlugIn) @mock.patch('pkg_resources.iter_entry_points') def test_load(self, mock_iter): # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mocked entry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.return_value = "one" # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins self.col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_calledwith(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with() @mock.patch('plainbox.impl.secure.plugins.logger') @mock.patch('pkg_resources.iter_entry_points') def test_load_failing(self, mock_iter, mock_logger): # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mockeed antry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.side_effect = ImportError("boom") # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins self.col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_calledwith(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with() # Ensure that an exception was logged mock_logger.exception.assert_called_with( "Unable to import %s", mock_ep2) # Ensure that the error was collected self.assertIsInstance(self.col.problem_list[0], ImportError)
# TODO: portable parser error would be nice, to know where it # fails. This is difficult at this stage. logger.exception(_("Cannot parse input")) return None def _to_json(self, obj): """ Helper method to convert arbitrary objects to their JSON representation. Anything that has a 'as_json' attribute will be converted to the result of calling that method. For all other objects __dict__ is returned. """ if isinstance(obj, Pattern): return "<Pattern>" elif hasattr(obj, "as_json"): return obj.as_json() elif hasattr(obj, "__dict__"): return obj.__dict__ elif hasattr(obj, "__slots__"): return {slot: getattr(obj, slot) for slot in obj.__slots__} else: raise NotImplementedError("unable to json-ify {!r}".format( obj.__class__)) # Collection of all parsers all_parsers = PkgResourcesPlugInCollection('plainbox.parsers', wrapper=ParserPlugIn)