def create(cls, repo=None, legacy_mode=False, prefix='pbox-'): """ Create an empty session manager. This method creates an empty session manager. This is the most generic API that allows applications to freely work with any set of devices. Typically applications will use the :meth:`add_device_context()` method to add additional context objects at a later time. This method creates and populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`). :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :param legacy_mode: Propagated to :meth:`~plainbox.impl.session.storage.SessionStorage.create()` to ensure that legacy (single session) mode is used. :ptype legacy_mode: bool :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create()") if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location, legacy_mode, prefix) WellKnownDirsHelper(storage).populate() return cls([], storage)
def create_with_state(cls, state, repo=None, legacy_mode=False): """ Create a session manager by wrapping existing session state. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param stage: A pre-existing SessioState object. :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :param legacy_mode: Propagated to :meth:`~plainbox.impl.session.storage.SessionStorage.create()` to ensure that legacy (single session) mode is used. :ptype legacy_mode: bool :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_with_state()") if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location, legacy_mode) WellKnownDirsHelper(storage).populate() return cls(state, storage)
def create_with_state(cls, state, repo=None, legacy_mode=False): """ Create a session manager by wrapping existing session state. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param stage: A pre-existing SessionState object. :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :param legacy_mode: Propagated to :meth:`~plainbox.impl.session.storage.SessionStorage.create()` to ensure that legacy (single session) mode is used. :ptype legacy_mode: bool :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_with_state()") if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location, legacy_mode) WellKnownDirsHelper(storage).populate() context = SessionDeviceContext(state) return cls([context], storage)
def create_with_unit_list(cls, unit_list=None, repo=None): """ Create a session manager with a fresh session. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param unit_list: If specified then this will be the initial list of units known by the session state object. :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_with_unit_list()") if unit_list is None: unit_list = [] state = SessionState(unit_list) if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location) context = SessionDeviceContext(state) WellKnownDirsHelper(storage).populate() return cls([context], storage)
def test_load_save_checkpoint(self): with TemporaryDirectory() as tmp: # Create a new storage in the specified directory storage = SessionStorage.create(tmp) # Save some checkpoint data data_out = b'some data' storage.save_checkpoint(data_out) # Load it back data_in = storage.load_checkpoint() # Check if it's right self.assertEqual(data_out, data_in)
def test_load_save_checkpoint__modern(self): with TemporaryDirectory() as tmp: # Create a new storage in the specified directory storage = SessionStorage.create(tmp, legacy_mode=False) # Save some checkpoint data data_out = b'some data' storage.save_checkpoint(data_out) # Load it back data_in = storage.load_checkpoint() # Check if it's right self.assertEqual(data_out, data_in)
def test_create_remove(self): with TemporaryDirectory() as tmp: # Create a new storage in the specified directory storage = SessionStorage.create(tmp) # The location should have been created self.assertTrue(os.path.exists(storage.location)) # And it should be in the directory we indicated self.assertEqual(os.path.dirname(storage.location), tmp) # Remove the storage now storage.remove() # And make sure the storage is gone self.assertFalse(os.path.exists(storage.location))
def test_load_save_checkpoint(self): session_prefix = "test_storage-" # Create a new storage in the specified directory storage = SessionStorage.create(session_prefix) # Save some checkpoint data data_out = b'some data' storage.save_checkpoint(data_out) # Load it back data_in = storage.load_checkpoint() # Check if it's right self.assertEqual(data_out, data_in) # Remove the storage now storage.remove()
def test_create_remove(self): session_prefix = "test_storage-" # Create a new storage in the specified directory storage = SessionStorage.create(session_prefix) session_id = storage.id # The location should have been created self.assertTrue(os.path.exists(storage.location)) # And it should be in the directory we indicated self.assertEqual( os.path.dirname(storage.location), os.path.dirname(WellKnownDirsHelper.session_dir(session_id))) # Remove the storage now storage.remove() # And make sure the storage is gone self.assertFalse(os.path.exists(storage.location))
def test_create_remove__modern(self): with TemporaryDirectory() as tmp: # Create a new storage in the specified directory storage = SessionStorage.create(tmp, legacy_mode=False) # The location should have been created self.assertTrue(os.path.exists(storage.location)) # And it should be in the directory we indicated self.assertEqual(os.path.dirname(storage.location), tmp) # There should not be any symlink now, pointing to this storage self.assertFalse( os.path.exists(os.path.join( tmp, SessionStorageRepository._LAST_SESSION_SYMLINK))) # Remove the storage now storage.remove() # And make sure the storage is gone self.assertFalse(os.path.exists(storage.location))
def create_with_state(cls, state): """ Create a session manager by wrapping existing session state. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param stage: A pre-existing SessionState object. :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_with_state()") storage = SessionStorage.create() context = SessionDeviceContext(state) return cls([context], storage)
def create(cls, prefix='pbox-'): """ Create an empty session manager. This method creates an empty session manager. This is the most generic API that allows applications to freely work with any set of devices. Typically applications will use the :meth:`add_device_context()` method to add additional context objects at a later time. This method creates and populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`). :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create()") storage = SessionStorage.create(prefix) return cls([], storage)
def create_with_unit_list(cls, unit_list=None): """ Create a session manager with a fresh session. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param unit_list: If specified then this will be the initial list of units known by the session state object. :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_with_unit_list()") if unit_list is None: unit_list = [] state = SessionState(unit_list) storage = SessionStorage.create() context = SessionDeviceContext(state) return cls([context], storage)
def create_session(cls, job_list=None, repo=None, legacy_mode=False): """ Create a session manager with a fresh session. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param job_list: If specified then this will be the initial list of jobs known by the session state object. This can be specified for convenience but is really optional since the application can always add more jobs to an existing session. :ptype job_list: list of :class:`~plainbox.abc.IJobDefinition`. :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :param legacy_mode: Propagated to :meth:`~plainbox.impl.session.storage.SessionStorage.create()` to ensure that legacy (single session) mode is used. :ptype legacy_mode: bool :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_session()") if job_list is None: job_list = [] state = SessionState(job_list) if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location, legacy_mode) WellKnownDirsHelper(storage).populate() return cls(state, storage)
def create_with_job_list(cls, job_list=None, repo=None, legacy_mode=False): """ Create a session manager with a fresh session. This method populates the session storage with all of the well known directories (using :meth:`WellKnownDirsHelper.populate()`) :param job_list: If specified then this will be the initial list of jobs known by the session state object. This can be specified for convenience but is really optional since the application can always add more jobs to an existing session. :ptype job_list: list of :class:`~plainbox.abc.IJobDefinition`. :param repo: If specified then this particular repository will be used to create the storage for this session. If left out, a new repository is constructed with the default location. :ptype repo: :class:`~plainbox.impl.session.storage.SessionStorageRepository`. :param legacy_mode: Propagated to :meth:`~plainbox.impl.session.storage.SessionStorage.create()` to ensure that legacy (single session) mode is used. :ptype legacy_mode: bool :return: fresh :class:`SessionManager` instance """ logger.debug("SessionManager.create_session()") if job_list is None: job_list = [] state = SessionState(job_list) if repo is None: repo = SessionStorageRepository() storage = SessionStorage.create(repo.location, legacy_mode) WellKnownDirsHelper(storage).populate() return cls(state, storage)
def test_smoke(self): session_prefix = "test_storage-" storage = SessionStorage(session_prefix) session_id = storage.id self.assertEqual(storage.location, WellKnownDirsHelper.session_dir(session_id))
def test_smoke(self): storage = SessionStorage('foo') self.assertEqual(storage.location, 'foo')