def testUseService(self): """ Tests utilies.use_service() """ framework = pelix.framework.create_framework([]) framework.start() context = framework.get_bundle_context() # Try without the service reference: TypeError self.assertRaises(TypeError, utilities.use_service(context, None).__enter__) # Start the service bundle bundle = context.install_bundle("tests.service_bundle") bundle.start() # Get the service reference svc_ref = context.get_service_reference(IEchoService) # Use it with utilities.use_service(context, svc_ref) as service: # Test the usage information self.assertIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using the service not updated") # Get the service the Pelix way got_service = context.get_service(svc_ref) # Test the service object self.assertIs(service, got_service, "Found a different service.") # Clean up the test usage context.unget_service(svc_ref) got_service = None # Re-test the usage information self.assertIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using service not kept") # Test the usage information self.assertNotIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using service kept after block") # Stop the iPOPO bundle bundle.stop() # Ensure the service is not accessible anymore self.assertRaises(pelix.constants.BundleException, utilities.use_service(context, svc_ref).__enter__) # Uninstall the bundle bundle.uninstall() # Ensure the service is not accessible anymore self.assertRaises(pelix.constants.BundleException, utilities.use_service(context, svc_ref).__enter__)
def testUseService(self): """ Tests utilities.use_service() """ framework = pelix.framework.create_framework([]) framework.start() context = framework.get_bundle_context() # Try without the service reference: TypeError self.assertRaises(TypeError, utilities.use_service(context, None).__enter__) # Start the service bundle bundle = context.install_bundle("tests.framework.service_bundle") bundle.start() # Get the service reference svc_ref = context.get_service_reference(IEchoService) # Use it with utilities.use_service(context, svc_ref) as service: # Test the usage information self.assertIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using the service not updated") # Get the service the Pelix way got_service = context.get_service(svc_ref) # Test the service object self.assertIs(service, got_service, "Found a different service.") # Clean up the test usage context.unget_service(svc_ref) got_service = None # Re-test the usage information self.assertIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using service not kept") # Test the usage information self.assertNotIn(context.get_bundle(), svc_ref.get_using_bundles(), "Bundles using service kept after block") # Stop the iPOPO bundle bundle.stop() # Ensure the service is not accessible anymore self.assertRaises(pelix.constants.BundleException, utilities.use_service(context, svc_ref).__enter__) # Uninstall the bundle bundle.uninstall() # Ensure the service is not accessible anymore self.assertRaises(pelix.constants.BundleException, utilities.use_service(context, svc_ref).__enter__)
def testEarlyConfig(self): """ Tests the behaviour if a configuration is already set when the managed service is registered """ # Create the configuration config = self.config.get_configuration(self.pid) config.update({'config.value': 42}) # Start the test bundle self.bundle.start() # Get the service with use_service(self.framework.get_bundle_context(), self.get_ref()) as svc: # Give some time for the notification self.pause() # The service should already have been configured self.assertEqual(svc.value, 42, "Value hasn't been set") self.assertFalse( svc.deleted, "Configuration considered as deleted") # Delete the configuration config.delete() # Give some time for the notification self.pause() # The flag must have been set self.assertTrue(svc.deleted, "Configuration considered as deleted")
def testNoConfigDelete(self): """ Tests the behaviour of the service with an empty configuration """ # Start the test bundle self.bundle.start() # Get the service with use_service(self.framework.get_bundle_context(), self.get_ref()) as svc: # Create the configuration config = self.config.get_configuration(self.pid) # Give some time for the possible erroneous notification self.pause() # Nothing should have happened yet self.assertIsNone(svc.value, "Value has been set") self.assertFalse( svc.deleted, "Configuration considered as deleted") # Delete the configuration config.delete() # Give some time for the possible erroneous notification self.pause() # Nothing should have happened either self.assertIsNone(svc.value, "Value has been set") self.assertFalse( svc.deleted, "Configuration considered as deleted")
def list_isolates(self, io_handler, node=None): """ Lists the isolates of the given node, or of all nodes """ # Get all node composers svc_refs = self._context.get_all_service_references( cohorte.composer.SERVICE_COMPOSER_NODE) if not svc_refs: io_handler.write_line("No node composer found.") return # Node name/UID -> isolates isolates = {} # For each node, get the isolate bean for svc_ref in svc_refs: with use_service(self._context, svc_ref) as composer: try: node = svc_ref.get_property( cohorte.composer.PROP_NODE_NAME) isolates[node] = composer.get_running_isolates() except Exception as ex: self.logger.error("Error calling composer: %s", ex) # Sort by node name names = sorted(isolates.keys()) # Make tree lines = [] for name in names: lines.append('+ {0}'.format(name)) for isolate in isolates[name]: lines.append('|- {0}'.format(isolate)) io_handler.write_line('\n'.join(lines))
def completion_hints(config, prompt, session, context, current, arguments): # type: (CompletionInfo, str, ShellSession, BundleContext, str, List[str]) -> List[str] """ Returns the possible completions of the current argument :param config: Configuration of the current completion :param prompt: The shell prompt string :param session: Current shell session :param context: Context of the shell UI bundle :param current: Current argument (to be completed) :param arguments: List of all arguments in their current state :return: A list of possible completions """ if not current: # No word yet, so the current position is after the existing ones arg_idx = len(arguments) else: # Find the current word position arg_idx = arguments.index(current) # Find the ID of the next completer completers = config.completers if arg_idx > len(completers) - 1: # Argument is too far to be positional, try if config.multiple: # Multiple calls allowed for the last completer completer_id = completers[-1] else: # Nothing to return return [] else: completer_id = completers[arg_idx] if completer_id == DUMMY: # Dummy completer: do nothing return [] # Find the matching service svc_ref = context.get_service_reference( SVC_COMPLETER, "({}={})".format(PROP_COMPLETER_ID, completer_id) ) if svc_ref is None: # Handler not found _logger.debug("Unknown shell completer ID: %s", completer_id) return [] # Call the completer try: with use_service(context, svc_ref) as completer: matches = completer.complete( config, prompt, session, context, arguments, current ) if not matches: return [] return matches except Exception as ex: _logger.exception("Error calling completer %s: %s", completer_id, ex) return []
def assertNotProvides(self, specification, provider): """ Asserts that the given service is not provided by the given provider """ context = self.framework.get_bundle_context() svc_refs = context.get_all_service_references(specification) if svc_refs: for svc_ref in svc_refs: with use_service(context, svc_ref) as svc: if svc is provider: # Found it self.fail("Service {0} is provided by {1}" .format(specification, provider))
def assertNotProvides(self, specification, provider): """ Asserts that the given service is not provided by the given provider """ context = self.framework.get_bundle_context() svc_refs = context.get_all_service_references(specification) if svc_refs: for svc_ref in svc_refs: with use_service(context, svc_ref) as svc: if svc is provider: # Found it self.fail("Service {0} is provided by {1}".format( specification, provider))
def start_fileinstall(self): """ Starts the file install bundle and tweaks its service """ # Start the bundle self.bnd_fileinstall.start() # Speed up the poll time context = self.framework.get_bundle_context() fileinstall_ref = context.get_service_reference( services.SERVICE_FILEINSTALL) with use_service(context, fileinstall_ref) as svc: svc._poll_time = .1 time.sleep(1)
def main(): """ Starts a Pelix framework and waits for it to stop """ # Prepare the framework, with iPOPO and the shell console # Warning: we only use the first argument of this method, a list of bundles framework = pelix.framework.create_framework(( # iPOPO "pelix.ipopo.core", # Shell core (engine) "pelix.shell.core", # Text console "pelix.shell.console")) # Start the framework, and the pre-installed bundles framework.start() # Get the bundle context of the framework, i.e. the link between the # framework starter and its content. context = framework.get_bundle_context() # Start the spell dictionary bundles, which provide the dictionary services context.install_bundle("spell_dictionary_EN").start() context.install_bundle("spell_dictionary_FR").start() # Start the spell checker bundle, which provides the spell checker service. context.install_bundle("spell_checker").start() # Sample usage of the spell checker service # 1. get its service reference, that describes the service itself ref_config = context.get_service_reference("spell_checker_service") # 2. the use_service method allows to grab a service and to use it inside a # with block. It automatically releases the service when exiting the block, # even if an exception was raised with use_service(context, ref_config) as svc_config: # Here, svc_config points to the spell checker service passage = "Welcome to our framwork iPOPO" print("1. Testing Spell Checker:", passage) misspelled_words = svc_config.check(passage) print("> Misspelled_words are:", misspelled_words) # Start the spell client bundle, which provides a shell command context.install_bundle("spell_client").start() # Wait for the framework to stop framework.wait_for_stop()
def assertProvides(self, specification, provider): """ Asserts that the given service is provided and is the given object """ context = self.framework.get_bundle_context() svc_refs = context.get_all_service_references(specification) if not svc_refs: self.fail("Service {0} not registered".format(specification)) for svc_ref in svc_refs: with use_service(context, svc_ref) as svc: if svc is provider: # Found it break else: self.fail("Service {0} is not provided by {1}" .format(specification, provider))
def assertProvides(self, specification, provider): """ Asserts that the given service is provided and is the given object """ context = self.framework.get_bundle_context() svc_refs = context.get_all_service_references(specification) if not svc_refs: self.fail("Service {0} not registered".format(specification)) for svc_ref in svc_refs: with use_service(context, svc_ref) as svc: if svc is provider: # Found it break else: self.fail("Service {0} is not provided by {1}".format( specification, provider))
def testUpdateConfig(self): """ Tests the behaviour if a configuration is updated """ # Create the configuration config = self.config.get_configuration(self.pid) # Start the test bundle self.bundle.start() # Get the service with use_service(self.framework.get_bundle_context(), self.get_ref()) as svc: # Give some time for the notification self.pause() # Nothing should have happened yet self.check_call_count(svc, 0) self.assertIsNone(svc.value, "Value has been set") self.assertFalse( svc.deleted, "Configuration considered as deleted") # Update the configuration config.update({'config.value': 42}) # Update is done a another thread self.pause() # The service should have been configured self.check_call_count(svc, 1) self.assertEqual(svc.value, 42, "Value hasn't been set") self.assertFalse( svc.deleted, "Configuration considered as deleted") # Delete the configuration config.delete() # Give some time for the notification self.pause() # The flag must have been set self.check_call_count(svc, 1) self.assertTrue(svc.deleted, "Configuration considered as deleted")
def testAddUpdateDelete(self): """ Tests a whole file life cycle """ # Start file install self.start_fileinstall() context = self.framework.get_bundle_context() # Start the test bundle self.bundle.start() ref = self.get_ref() # Wait a little time.sleep(.4) with use_service(context, ref) as svc: self.check_call_count(svc, 0) self.assertIsNone(svc.value, "Value has been set") # Get the watched folder persistence_ref = context.get_service_reference( services.SERVICE_CONFIGADMIN_PERSISTENCE) folder = persistence_ref.get_property(services.PROP_FILEINSTALL_FOLDER) # JSON persistence file name filepath = os.path.join(folder, self.pid + '.config.js') # Create the empty configuration value = 'Ni !' self.write(filepath, value) # Wait a little time.sleep(.4) # Check if the service has been updated with use_service(context, ref) as svc: self.assertEqual(svc.value, value, "Incorrect value") self.check_call_count(svc, 1) # Update the properties value = 'Ecky-ecky-ecky-ecky-pikang-zoom-boing' self.write(filepath, value) # Wait a little time.sleep(.4) # Check if the service has been updated with use_service(context, ref) as svc: self.assertEqual(svc.value, value, "Incorrect value") self.check_call_count(svc, 1) # Reset the flags svc.reset() # Touch the file self.touch(filepath) # Wait a little time.sleep(.4) # Check if the service has been updated with use_service(context, ref) as svc: self.check_call_count(svc, 0) self.assertIsNone(svc.value, "File updated after simple touch") self.assertFalse(svc.deleted, "Configuration considered deleted") # Delete the file os.remove(filepath) # Wait a little time.sleep(.4) with use_service(context, ref) as svc: self.check_call_count(svc, 1) self.assertTrue(svc.deleted, "Configuration not deleted")
def service(self, service_name): service_ref = self.i1820_framework_context.get_service_reference( service_name) return use_service(self.i1820_framework_context, service_ref)