You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
7.2 KiB

10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Adapted by Nicolas Bessi. Copyright Camptocamp SA
  5. # Based on Florent Xicluna original code. Copyright Wingo SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import os
  22. import ConfigParser
  23. from lxml import etree
  24. from openerp.osv import fields, orm
  25. from openerp.tools.config import config as system_base_config
  26. from .system_info import get_server_environment
  27. from openerp.addons import server_environment_files
  28. _dir = os.path.dirname(server_environment_files.__file__)
  29. # Same dict as RawConfigParser._boolean_states
  30. _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
  31. '0': False, 'no': False, 'false': False, 'off': False}
  32. if not system_base_config.get('running_env', False):
  33. raise Exception(
  34. "The parameter 'running_env' has not be set neither in base config "
  35. "file option -c or in openerprc.\n"
  36. "We strongly recommend against using the rc file but instead use an "
  37. "explicit config file with this content:\n"
  38. "[options]\nrunning_env = dev"
  39. )
  40. ck_path = os.path.join(_dir, system_base_config['running_env'])
  41. if not os.path.exists(ck_path):
  42. raise Exception(
  43. "Provided server environment does not exist, "
  44. "please add a folder %s" % ck_path
  45. )
  46. def setboolean(obj, attr, _bool=None):
  47. """Replace the attribute with a boolean."""
  48. if _bool is None:
  49. _bool = dict(_boolean_states) # we copy original dict
  50. res = _bool[getattr(obj, attr).lower()]
  51. setattr(obj, attr, res)
  52. return res
  53. # Borrowed from MarkupSafe
  54. def _escape(s):
  55. """Convert the characters &<>'" in string s to HTML-safe sequences."""
  56. return (str(s).replace('&', '&amp;')
  57. .replace('>', '&gt;')
  58. .replace('<', '&lt;')
  59. .replace("'", '&#39;')
  60. .replace('"', '&#34;'))
  61. def _listconf(env_path):
  62. """List configuration files in a folder."""
  63. files = [os.path.join(env_path, name)
  64. for name in sorted(os.listdir(env_path))
  65. if name.endswith('.conf')]
  66. return files
  67. def _load_config():
  68. """Load the configuration and return a ConfigParser instance."""
  69. default = os.path.join(_dir, 'default')
  70. running_env = os.path.join(_dir,
  71. system_base_config['running_env'])
  72. if os.path.isdir(default):
  73. conf_files = _listconf(default) + _listconf(running_env)
  74. else:
  75. conf_files = _listconf(running_env)
  76. config_p = ConfigParser.SafeConfigParser()
  77. # options are case-sensitive
  78. config_p.optionxform = str
  79. try:
  80. config_p.read(conf_files)
  81. except Exception as e:
  82. raise Exception('Cannot read config files "%s": %s' % (conf_files, e))
  83. return config_p
  84. serv_config = _load_config()
  85. class _Defaults(dict):
  86. __slots__ = ()
  87. def __setitem__(self, key, value):
  88. return dict.__setitem__(self,
  89. key,
  90. lambda *a: str(value))
  91. class ServerConfiguration(orm.TransientModel):
  92. """Display server configuration."""
  93. _name = 'server.config'
  94. _columns = {}
  95. _conf_defaults = _Defaults()
  96. def __init__(self, pool, cr):
  97. super(ServerConfiguration, self).__init__(pool, cr)
  98. self.running_env = system_base_config['running_env']
  99. # Only show passwords in development
  100. self.show_passwords = self.running_env in ('dev',)
  101. self._arch = None
  102. self._build_osv()
  103. def _group(self, items, prefix):
  104. """Return an XML chunk which represents a group of fields."""
  105. names = []
  106. for k, v in items:
  107. key = '%s\\%s' % (prefix, k)
  108. # Mask passwords
  109. if 'passw' in k and not self.show_passwords:
  110. v = '**********'
  111. self._columns[key] = fields.char(k, size=1024)
  112. self._conf_defaults[key] = v
  113. names.append(key)
  114. return ('<group col="2" colspan="4">' +
  115. ''.join(['<field name="%s" readonly="1"/>' %
  116. _escape(name) for name in names]) +
  117. '</group>')
  118. def _build_osv(self):
  119. """Build the view for the current configuration."""
  120. arch = ('<?xml version="1.0" encoding="utf-8"?>'
  121. '<form string="Configuration Form">'
  122. '<notebook colspan="4">')
  123. # OpenERP server configuration
  124. rcfile = system_base_config.rcfile
  125. items = sorted(system_base_config.options.items())
  126. arch += '<page string="OpenERP">'
  127. arch += '<separator string="%s" colspan="4"/>' % _escape(rcfile)
  128. arch += self._group(items, prefix='openerp')
  129. arch += '<separator colspan="4"/></page>'
  130. arch += '<page string="Environment based configurations">'
  131. for section in sorted(serv_config.sections()):
  132. items = sorted(serv_config.items(section))
  133. arch += '<separator string="[%s]" colspan="4"/>' % _escape(section)
  134. arch += self._group(items, prefix=section)
  135. arch += '<separator colspan="4"/></page>'
  136. # System information
  137. arch += '<page string="System">'
  138. arch += '<separator string="Server Environment" colspan="4"/>'
  139. arch += self._group(get_server_environment(), prefix='system')
  140. arch += '<separator colspan="4"/></page>'
  141. arch += '</notebook></form>'
  142. self._arch = etree.fromstring(arch)
  143. def fields_view_get(
  144. self, cr, uid, view_id=None, view_type='form', context=None,
  145. toolbar=False, submenu=False):
  146. """Overwrite the default method to render the custom view."""
  147. res = super(ServerConfiguration, self).fields_view_get(cr, uid,
  148. view_id,
  149. view_type,
  150. context,
  151. toolbar)
  152. if view_type == 'form':
  153. arch_node = self._arch
  154. xarch, xfields = self._view_look_dom_arch(cr, uid,
  155. arch_node,
  156. view_id,
  157. context=context)
  158. res['arch'] = xarch
  159. res['fields'] = xfields
  160. return res
  161. def default_get(self, cr, uid, fields_list, context=None):
  162. res = {}
  163. for key in self._conf_defaults:
  164. res[key] = self._conf_defaults[key]()
  165. return res