client.py 989 B

12345678910111213141516171819202122232425262728293031
  1. import os
  2. import subprocess
  3. class BaseDatabaseClient:
  4. """Encapsulate backend-specific methods for opening a client shell."""
  5. # This should be a string representing the name of the executable
  6. # (e.g., "psql"). Subclasses must override this.
  7. executable_name = None
  8. def __init__(self, connection):
  9. # connection is an instance of BaseDatabaseWrapper.
  10. self.connection = connection
  11. def __del__(self):
  12. del self.connection
  13. @classmethod
  14. def settings_to_cmd_args_env(cls, settings_dict, parameters):
  15. raise NotImplementedError(
  16. "subclasses of BaseDatabaseClient must provide a "
  17. "settings_to_cmd_args_env() method or override a runshell()."
  18. )
  19. def runshell(self, parameters):
  20. args, env = self.settings_to_cmd_args_env(
  21. self.connection.settings_dict, parameters
  22. )
  23. env = {**os.environ, **env} if env else None
  24. subprocess.run(args, env=env, check=True)