/neatx/lib/config.py

http://neatx.googlecode.com/ · Python · 176 lines · 104 code · 37 blank · 35 comment · 7 complexity · 1811c3d25acaf0a17476052154aaf03d MD5 · raw file

  1. #
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. # 02110-1301, USA.
  19. """Module for config functions"""
  20. import ConfigParser
  21. import os
  22. import socket
  23. from neatx import constants
  24. from neatx import utils
  25. VAR_AUTH_METHOD = "auth-method"
  26. VAR_AUTH_SSH_HOST = "auth-ssh-host"
  27. VAR_AUTH_SSH_PORT = "auth-ssh-port"
  28. VAR_LOGLEVEL = "loglevel"
  29. VAR_START_KDE_COMMAND = "start-kde-command"
  30. VAR_START_GNOME_COMMAND = "start-gnome-command"
  31. VAR_START_CONSOLE_COMMAND = "start-console-command"
  32. VAR_NX_PROTOCOL_VERSION = "nx-protocol-version"
  33. VAR_BASH = "bash-path"
  34. VAR_NETCAT = "netcat-path"
  35. VAR_XRDB = "xrdb-path"
  36. VAR_SU = "su-path"
  37. VAR_SSH = "ssh-path"
  38. VAR_XAUTH = "xauth-path"
  39. VAR_XSESSION = "xsession-path"
  40. VAR_NXAGENT = "nxagent-path"
  41. VAR_USE_XSESSION = "use-xsession"
  42. _LOGLEVEL_DEBUG = "debug"
  43. _GLOBAL_SECTION = "global"
  44. def _ReadConfig(filename):
  45. cfg = ConfigParser.RawConfigParser()
  46. cfg.read(filename)
  47. return cfg
  48. def __GetDefault(func):
  49. def wrapped(cfg, section, name, default):
  50. if cfg.has_option(section, name):
  51. return func(cfg, section, name)
  52. return default
  53. return wrapped
  54. _GetOption = __GetDefault(ConfigParser.RawConfigParser.get)
  55. _GetBoolOption = __GetDefault(ConfigParser.RawConfigParser.getboolean)
  56. _GetIntOption = __GetDefault(ConfigParser.RawConfigParser.getint)
  57. def _GetSshPort():
  58. """Get the SSH port.
  59. Note that this function queries the system-wide services database.
  60. @rtype: int
  61. """
  62. try:
  63. return socket.getservbyname("ssh", "tcp")
  64. except socket.error:
  65. return constants.DEFAULT_SSH_PORT
  66. class Config(object):
  67. def __init__(self, filename, section=_GLOBAL_SECTION, _hostname=None):
  68. """Load configuration file.
  69. @type filename: str
  70. @param filename: Path to configuration file
  71. """
  72. if _hostname is None:
  73. _hostname = socket.gethostname()
  74. cfg = _ReadConfig(filename)
  75. # If speed becomes an issue, the following attributes could be converted to
  76. # properties and be evaluated only when actually read.
  77. if cfg.has_option(section, VAR_LOGLEVEL):
  78. loglevel = cfg.get(section, VAR_LOGLEVEL)
  79. # Enable debug if loglevel is "debug"
  80. self.debug = (loglevel.lower() == _LOGLEVEL_DEBUG.lower())
  81. else:
  82. self.debug = False
  83. self.start_kde_command = \
  84. _GetOption(cfg, section, VAR_START_KDE_COMMAND,
  85. constants.START_KDE_COMMAND)
  86. self.start_gnome_command = \
  87. _GetOption(cfg, section, VAR_START_GNOME_COMMAND,
  88. constants.START_GNOME_COMMAND)
  89. self.start_console_command = \
  90. _GetOption(cfg, section, VAR_START_CONSOLE_COMMAND,
  91. constants.START_CONSOLE_COMMAND)
  92. self.auth_method = _GetOption(cfg, section, VAR_AUTH_METHOD,
  93. constants.AUTH_METHOD_DEFAULT)
  94. self.auth_ssh_host = _GetOption(cfg, section, VAR_AUTH_SSH_HOST,
  95. _hostname)
  96. self.auth_ssh_port = _GetIntOption(cfg, section, VAR_AUTH_SSH_PORT,
  97. _GetSshPort())
  98. ver_string = _GetOption(cfg, section, VAR_NX_PROTOCOL_VERSION,
  99. constants.DEFAULT_NX_PROTOCOL_VERSION)
  100. self.nx_protocol_version = \
  101. utils.ParseVersion(ver_string, constants.NXAGENT_VERSION_SEP,
  102. constants.PROTOCOL_VERSION_DIGITS)
  103. self.bash = \
  104. _GetOption(cfg, section, VAR_BASH,
  105. constants.BASH)
  106. self.netcat = \
  107. _GetOption(cfg, section, VAR_NETCAT,
  108. constants.NETCAT)
  109. self.xrdb = \
  110. _GetOption(cfg, section, VAR_XRDB,
  111. constants.XRDB)
  112. self.su = \
  113. _GetOption(cfg, section, VAR_SU,
  114. constants.SU)
  115. self.ssh = \
  116. _GetOption(cfg, section, VAR_SSH,
  117. constants.SSH)
  118. self.xauth = \
  119. _GetOption(cfg, section, VAR_XAUTH,
  120. constants.XAUTH)
  121. self.xsession = \
  122. _GetOption(cfg, section, VAR_XSESSION,
  123. constants.XSESSION)
  124. self.nxagent = \
  125. _GetOption(cfg, section, VAR_NXAGENT,
  126. constants.NXAGENT)
  127. self.use_xsession = \
  128. _GetBoolOption(cfg, section, VAR_USE_XSESSION,
  129. constants.USE_XSESSION)
  130. if self.use_xsession:
  131. self.start_kde_command = "%s %s" % \
  132. (self.xsession, self.start_kde_command)
  133. self.start_gnome_command = "%s %s" % \
  134. (self.xsession, self.start_gnome_command)