dimanche 22 novembre 2020

Keep context manager open after an if ... else statement

I have the following scheme, with different conditional with statements:

if not remote:
    _open = open
    os.chdir(localpath)
else:
    sftp = pysftp.Connection(host, username=user, password=sftppwd)
    with sftp:
        sftp.chdir(remotepath)
        _open = sftp.open

with _open('myfile', 'rb') as f:        # and then lots of other files are opened too
    x = f.read(4)
    ...

The last line fails in the remote case because the sftp object / context manager has expired.

I have read Conditional with statement in Python, but here it's not exactly the same: I could create a dummy context manager for the non-remote case, but I'm not sure it would be enough.

I have thought about using ExitStack but then I'm afraid it would look complex when opening further files: each simple with _open(...) as f: would need to be rewritten into a less readable stack.enter_context(_open(...)) when some more code is arriving.

What's the simplest solution in this case? (avoiding creating new functions if possible and keep a simple if ... else flow)

Aucun commentaire:

Enregistrer un commentaire