__init__.py 2.8 KB

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