configuration.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. Configuration settings.
  3. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  4. """
  5. # Env vars used at package import time (see __init__.py):
  6. # PYRO_LOGLEVEL (enable Pyro log config and set level)
  7. # PYRO_LOGFILE (the name of the logfile if you don't like the default)
  8. from __future__ import print_function
  9. import os
  10. import platform
  11. import pickle
  12. import socket
  13. from Pyro4 import constants
  14. class Configuration(object):
  15. __slots__ = ("HOST", "NS_HOST", "NS_PORT", "NS_BCPORT", "NS_BCHOST", "NS_AUTOCLEAN",
  16. "COMPRESSION", "SERVERTYPE", "COMMTIMEOUT", "POLLTIMEOUT", "ONEWAY_THREADED",
  17. "DETAILED_TRACEBACK", "SOCK_REUSE", "SOCK_NODELAY", "PREFER_IP_VERSION",
  18. "THREADPOOL_SIZE", "THREADPOOL_SIZE_MIN", "AUTOPROXY", "PICKLE_PROTOCOL_VERSION",
  19. "BROADCAST_ADDRS", "NATHOST", "NATPORT", "MAX_MESSAGE_SIZE",
  20. "FLAME_ENABLED", "SERIALIZER", "SERIALIZERS_ACCEPTED", "LOGWIRE",
  21. "METADATA", "REQUIRE_EXPOSE", "USE_MSG_WAITALL", "JSON_MODULE",
  22. "MAX_RETRIES", "DILL_PROTOCOL_VERSION", "ITER_STREAMING", "ITER_STREAM_LIFETIME",
  23. "ITER_STREAM_LINGER", "SSL", "SSL_REQUIRECLIENTCERT", "SSL_CACERTS",
  24. "SSL_SERVERCERT", "SSL_SERVERKEY", "SSL_SERVERKEYPASSWD",
  25. "SSL_CLIENTCERT", "SSL_CLIENTKEY", "SSL_CLIENTKEYPASSWD")
  26. def __init__(self):
  27. self.reset()
  28. def reset(self, useenvironment=True):
  29. """
  30. Set default config items.
  31. If useenvironment is False, won't read environment variables settings (useful if you can't trust your env).
  32. """
  33. self.HOST = "localhost" # don't expose us to the outside world by default
  34. self.NS_HOST = self.HOST
  35. self.NS_PORT = 9090 # tcp
  36. self.NS_BCPORT = 9091 # udp
  37. self.NS_BCHOST = None
  38. self.NS_AUTOCLEAN = 0.0
  39. self.NATHOST = None
  40. self.NATPORT = 0
  41. self.COMPRESSION = False
  42. self.SERVERTYPE = "thread"
  43. self.COMMTIMEOUT = 0.0
  44. self.POLLTIMEOUT = 2.0 # seconds
  45. self.SOCK_REUSE = True # so_reuseaddr on server sockets?
  46. self.SOCK_NODELAY = False # tcp_nodelay on socket?
  47. self.ONEWAY_THREADED = True # oneway calls run in their own thread
  48. self.DETAILED_TRACEBACK = False
  49. self.THREADPOOL_SIZE = 40
  50. self.THREADPOOL_SIZE_MIN = 4
  51. self.AUTOPROXY = True
  52. self.MAX_MESSAGE_SIZE = 0 # 0 = unlimited
  53. self.BROADCAST_ADDRS = "<broadcast>, 0.0.0.0" # comma separated list of broadcast addresses
  54. self.FLAME_ENABLED = False
  55. self.PREFER_IP_VERSION = 4 # 4, 6 or 0 (let OS choose according to RFC 3484)
  56. self.SERIALIZER = "serpent"
  57. self.SERIALIZERS_ACCEPTED = "serpent,marshal,json" # these are the 'safe' serializers that are always available
  58. self.LOGWIRE = False # log wire-level messages
  59. self.PICKLE_PROTOCOL_VERSION = pickle.HIGHEST_PROTOCOL
  60. try:
  61. import dill
  62. self.DILL_PROTOCOL_VERSION = dill.HIGHEST_PROTOCOL # Highest protocol
  63. except ImportError:
  64. self.DILL_PROTOCOL_VERSION = -1
  65. self.METADATA = True # get metadata from server on proxy connect
  66. self.REQUIRE_EXPOSE = True # require @expose to make members remotely accessible (if False, everything is accessible)
  67. self.USE_MSG_WAITALL = hasattr(socket, "MSG_WAITALL") and platform.system() != "Windows" # waitall is not reliable on windows
  68. self.JSON_MODULE = "json"
  69. self.MAX_RETRIES = 0
  70. self.ITER_STREAMING = True
  71. self.ITER_STREAM_LIFETIME = 0.0
  72. self.ITER_STREAM_LINGER = 30.0
  73. self.SSL = False
  74. self.SSL_SERVERCERT = ""
  75. self.SSL_SERVERKEY = ""
  76. self.SSL_SERVERKEYPASSWD = ""
  77. self.SSL_REQUIRECLIENTCERT = False
  78. self.SSL_CLIENTCERT = ""
  79. self.SSL_CLIENTKEY = ""
  80. self.SSL_CLIENTKEYPASSWD = ""
  81. self.SSL_CACERTS = ""
  82. if useenvironment:
  83. # process environment variables
  84. PREFIX = "PYRO_"
  85. for symbol in self.__slots__:
  86. if PREFIX + symbol in os.environ:
  87. value = getattr(self, symbol)
  88. envvalue = os.environ[PREFIX + symbol]
  89. if value is not None:
  90. valuetype = type(value)
  91. if valuetype is bool:
  92. # booleans are special
  93. envvalue = envvalue.lower()
  94. if envvalue in ("0", "off", "no", "false"):
  95. envvalue = False
  96. elif envvalue in ("1", "yes", "on", "true"):
  97. envvalue = True
  98. else:
  99. raise ValueError("invalid boolean value: %s%s=%s" % (PREFIX, symbol, envvalue))
  100. else:
  101. envvalue = valuetype(envvalue) # just cast the value to the appropriate type
  102. setattr(self, symbol, envvalue)
  103. self.SERIALIZERS_ACCEPTED = set(self.SERIALIZERS_ACCEPTED.split(','))
  104. def asDict(self):
  105. """returns the current config as a regular dictionary"""
  106. result = {}
  107. for item in self.__slots__:
  108. result[item] = getattr(self, item)
  109. return result
  110. def parseAddressesString(self, addresses):
  111. """
  112. Parses the addresses string which contains one or more ip addresses separated by a comma.
  113. Returns a sequence of these addresses. '' is replaced by the empty string.
  114. """
  115. result = []
  116. for addr in addresses.split(','):
  117. addr = addr.strip()
  118. if addr == "''":
  119. addr = ""
  120. result.append(addr)
  121. return result
  122. def dump(self):
  123. # easy config diagnostics
  124. if hasattr(platform, "python_implementation"):
  125. implementation = platform.python_implementation()
  126. else:
  127. implementation = "???"
  128. config = self.asDict()
  129. config["LOGFILE"] = os.environ.get("PYRO_LOGFILE")
  130. config["LOGLEVEL"] = os.environ.get("PYRO_LOGLEVEL")
  131. result = ["Pyro version: %s" % constants.VERSION,
  132. "Loaded from: %s" % os.path.dirname(__file__),
  133. "Python version: %s %s (%s, %s)" % (implementation, platform.python_version(), platform.system(), os.name),
  134. "Protocol version: %d" % constants.PROTOCOL_VERSION,
  135. "Currently active configuration settings:"]
  136. for n, v in sorted(config.items()):
  137. result.append("%s = %s" % (n, v))
  138. return "\n".join(result)
  139. config = Configuration()
  140. # entrypoint from script
  141. def main():
  142. print(config.dump())
  143. if __name__ == "__main__":
  144. main()