PageRenderTime 385ms CodeModel.GetById 181ms app.highlight 11ms RepoModel.GetById 191ms app.codeStats 0ms

/neatx/lib/config.py

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