client.py 937 B

12345678910111213141516171819202122232425262728
  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. @classmethod
  12. def settings_to_cmd_args_env(cls, settings_dict, parameters):
  13. raise NotImplementedError(
  14. "subclasses of BaseDatabaseClient must provide a "
  15. "settings_to_cmd_args_env() method or override a runshell()."
  16. )
  17. def runshell(self, parameters):
  18. args, env = self.settings_to_cmd_args_env(
  19. self.connection.settings_dict, parameters
  20. )
  21. env = {**os.environ, **env} if env else None
  22. subprocess.run(args, env=env, check=True)