__init__.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. Pyro package. Some generic init stuff to set up logging etc.
  3. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  4. """
  5. import sys
  6. if sys.version_info < (2, 7):
  7. import warnings
  8. warnings.warn("This Pyro version is unsupported on Python versions older than 2.7", ImportWarning)
  9. def _configLogging():
  10. """Do some basic config of the logging module at package import time.
  11. The configuring is done only if the PYRO_LOGLEVEL env var is set.
  12. If you want to use your own logging config, make sure you do
  13. that before any Pyro imports. Then Pyro will skip the autoconfig.
  14. Set the env var PYRO_LOGFILE to change the name of the autoconfigured
  15. log file (default is pyro.log in the current dir). Use '{stderr}' to
  16. make the log go to the standard error output."""
  17. import os
  18. import logging
  19. level = os.environ.get("PYRO_LOGLEVEL")
  20. logfilename = os.environ.get("PYRO_LOGFILE", "pyro.log")
  21. if logfilename == "{stderr}":
  22. logfilename = None
  23. if level not in (None, ""):
  24. levelvalue = getattr(logging, level)
  25. if len(logging.root.handlers) == 0:
  26. # configure the logging with some sensible defaults.
  27. try:
  28. import tempfile
  29. tempfile = tempfile.TemporaryFile(dir=".")
  30. tempfile.close()
  31. except OSError:
  32. # cannot write in current directory, use the default console logger
  33. logging.basicConfig(level=levelvalue)
  34. else:
  35. # set up a basic logfile in current directory
  36. logging.basicConfig(
  37. level=levelvalue,
  38. filename=logfilename,
  39. datefmt="%Y-%m-%d %H:%M:%S",
  40. format="[%(asctime)s.%(msecs)03d,%(name)s,%(levelname)s] %(message)s"
  41. )
  42. log = logging.getLogger("Pyro4")
  43. log.info("Pyro log configured using built-in defaults, level=%s", level)
  44. else:
  45. # PYRO_LOGLEVEL is not set, disable Pyro logging. No message is printed about this fact.
  46. log = logging.getLogger("Pyro4")
  47. log.setLevel(9999)
  48. _configLogging()
  49. del _configLogging
  50. # initialize Pyro's configuration
  51. from Pyro4.configuration import Configuration
  52. config = Configuration()
  53. del Configuration
  54. # import the required Pyro symbols into this package
  55. from Pyro4.core import URI, Proxy, Daemon, callback, batch, async, oneway, expose, behavior, current_context
  56. from Pyro4.naming import locateNS, resolve
  57. from Pyro4.futures import Future
  58. from Pyro4.constants import VERSION as __version__