Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

proxy.py 2.2KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from .utils import format_environment
  2. class ProxyConfig(dict):
  3. '''
  4. Hold the client's proxy configuration
  5. '''
  6. @property
  7. def http(self):
  8. return self.get('http')
  9. @property
  10. def https(self):
  11. return self.get('https')
  12. @property
  13. def ftp(self):
  14. return self.get('ftp')
  15. @property
  16. def no_proxy(self):
  17. return self.get('no_proxy')
  18. @staticmethod
  19. def from_dict(config):
  20. '''
  21. Instantiate a new ProxyConfig from a dictionary that represents a
  22. client configuration, as described in `the documentation`_.
  23. .. _the documentation:
  24. https://docs.docker.com/network/proxy/#configure-the-docker-client
  25. '''
  26. return ProxyConfig(
  27. http=config.get('httpProxy'),
  28. https=config.get('httpsProxy'),
  29. ftp=config.get('ftpProxy'),
  30. no_proxy=config.get('noProxy'),
  31. )
  32. def get_environment(self):
  33. '''
  34. Return a dictionary representing the environment variables used to
  35. set the proxy settings.
  36. '''
  37. env = {}
  38. if self.http:
  39. env['http_proxy'] = env['HTTP_PROXY'] = self.http
  40. if self.https:
  41. env['https_proxy'] = env['HTTPS_PROXY'] = self.https
  42. if self.ftp:
  43. env['ftp_proxy'] = env['FTP_PROXY'] = self.ftp
  44. if self.no_proxy:
  45. env['no_proxy'] = env['NO_PROXY'] = self.no_proxy
  46. return env
  47. def inject_proxy_environment(self, environment):
  48. '''
  49. Given a list of strings representing environment variables, prepend the
  50. environment variables corresponding to the proxy settings.
  51. '''
  52. if not self:
  53. return environment
  54. proxy_env = format_environment(self.get_environment())
  55. if not environment:
  56. return proxy_env
  57. # It is important to prepend our variables, because we want the
  58. # variables defined in "environment" to take precedence.
  59. return proxy_env + environment
  60. def __str__(self):
  61. return 'ProxyConfig(http={}, https={}, ftp={}, no_proxy={})'.format(
  62. self.http, self.https, self.ftp, self.no_proxy)