def test_serialize_kvp(): """Test serializing and deserializing key-value pairs.""" doc = util.to_dict( args=[util.to_kvp(key='a', value=1), util.to_kvp(key='b', value='2')]) assert len(doc) == 2 assert doc['a'] == 1 assert doc['b'] == '2'
def create_worker(doc: Dict) -> Worker: """Factory pattern for workers. Create an instance of a worker implementation from a given worker serialization. Parameters ---------- doc: dict Dictionary serialization for a worker. Returns ------- flowserv.controller.worker.base.Worker """ identifier = doc['name'] worker_type = doc['type'] env = util.to_dict(doc.get('env', [])) vars = util.to_dict(doc.get('variables', [])) volume = doc.get('volume') if worker_type == SUBPROCESS_WORKER: return SubprocessWorker(variables=vars, env=env, identifier=identifier, volume=volume) elif worker_type == DOCKER_WORKER: return DockerWorker(variables=vars, env=env, identifier=identifier, volume=volume) elif worker_type == CODE_WORKER: return CodeWorker(identifier=identifier, volume=volume) elif worker_type == NOTEBOOK_WORKER: return NotebookEngine(identifier=identifier, volume=volume) elif worker_type == NOTEBOOK_DOCKER_WORKER: return NotebookDockerWorker(identifier=identifier, env=env, volume=volume) raise ValueError(f"unknown worker type '{worker_type}'")
def from_dict(doc) -> FileSystemStorage: """Get file system storage volume instance from dictionary serialization. Parameters ---------- doc: dict Dictionary serialization as returned by the ``to_dict()`` method. Returns ------- flowserv.volume.fs.FileSystemStorage """ return FileSystemStorage(identifier=doc.get('id'), basedir=util.to_dict(doc.get( 'args', [])).get('basedir'))
def from_dict(doc) -> S3Volume: """Get S3 bucket storage volume instance from dictionary serialization. Parameters ---------- doc: dict Dictionary serialization as returned by the ``to_dict()`` method. Returns ------- flowserv.volume.s3.S3Volume """ args = util.to_dict(doc.get('args', [])) return S3Volume( identifier=doc.get('id'), bucket_id=args.get('bucket'), prefix=args.get('prefix') )
def from_dict(doc) -> RemoteStorage: """Get remote storage volume instance from dictionary serialization. Parameters ---------- doc: dict Dictionary serialization as returned by the ``to_dict()`` method. Returns ------- flowserv.volume.ssh.RemoteStorage """ args = util.to_dict(doc.get('args', [])) return RemoteStorage(identifier=doc.get('id'), client=SSHClient( hostname=args.get('hostname'), port=args.get('port'), timeout=args.get('timeout'), look_for_keys=args.get('look_for_keys'), sep=args.get('sep')), remotedir=args.get('basedir'))