Browse Source

Merge pull request #48 from nbessi/port_of_server_env

Port of server_environement
pull/51/head
Leonardo Pistone 10 years ago
parent
commit
87868b026f
  1. 2
      .travis.yml
  2. 0
      server_environment/__init__.py
  3. 2
      server_environment/__openerp__.py
  4. 0
      server_environment/i18n/server_environment.pot
  5. 81
      server_environment/serv_config.py
  6. 0
      server_environment/serv_config.xml
  7. 9
      server_environment/system_info.py
  8. 22
      server_environment/tests/__init__.py
  9. 48
      server_environment/tests/test_server_environment.py
  10. 0
      server_environment_files_sample/__init__.py
  11. 0
      server_environment_files_sample/__openerp__.py
  12. 0
      server_environment_files_sample/default/base.conf
  13. 0
      server_environment_files_sample/default/mytopic.conf
  14. 0
      server_environment_files_sample/dev/base.conf
  15. 0
      server_environment_files_sample/dev/mytopic.conf
  16. 0
      server_environment_files_sample/i18n/server_environment_files.pot
  17. 0
      server_environment_files_sample/preprod/mytopic.conf
  18. 0
      server_environment_files_sample/prod/base.conf
  19. 0
      server_environment_files_sample/prod/mytopic.conf

2
.travis.yml

@ -20,7 +20,7 @@ install:
- travis_install_nightly ${VERSION}
- sudo pip install python-ldap
- printf '[options]\n\nrunning_env = dev' > ${HOME}/.openerp_serverrc
- ln -s server_environment_files_sample ./server_environment_files
script:
- travis_run_flake8
- travis_run_tests ${VERSION}

0
__unported__/server_environment/__init__.py → server_environment/__init__.py

2
__unported__/server_environment/__openerp__.py → server_environment/__openerp__.py

@ -21,7 +21,7 @@
{
"name": "server configuration environment files",
"version": "1.0",
"version": "1.1",
"depends": ["base", "server_environment_files"],
"author": "Camptocamp",
"description": """\

0
__unported__/server_environment/i18n/server_environment.pot → server_environment/i18n/server_environment.pot

81
__unported__/server_environment/serv_config.py → server_environment/serv_config.py

@ -22,8 +22,9 @@
import os
import ConfigParser
from lxml import etree
from itertools import chain
from openerp.osv import fields, orm
from openerp import models, fields
from openerp.tools.config import config as system_base_config
from .system_info import get_server_environment
@ -109,13 +110,17 @@ class _Defaults(dict):
return dict.__setitem__(self, key, func)
class ServerConfiguration(orm.TransientModel):
class ServerConfiguration(models.TransientModel):
"""Display server configuration."""
_name = 'server.config'
_columns = {}
_conf_defaults = _Defaults()
def __init__(self, pool, cr):
"""Add columns to model dynamically
and init some properties
"""
self._add_columns()
super(ServerConfiguration, self).__init__(pool, cr)
self.running_env = system_base_config['running_env']
# Only show passwords in development
@ -123,18 +128,55 @@ class ServerConfiguration(orm.TransientModel):
self._arch = None
self._build_osv()
def _group(self, items, prefix):
def _format_key(self, section, key):
return '%s | %s' % (section, key)
def _add_columns(self):
"""Add columns to model dynamically"""
cols = chain(
self._get_base_cols().items(),
self._get_env_cols().items(),
self._get_system_cols().items()
)
for col, value in cols:
col_name = col.replace('.', '_')
setattr(ServerConfiguration,
col_name,
fields.Char(string=col, readonly=True))
self._conf_defaults[col_name] = value
def _get_base_cols(self):
""" Compute base fields"""
res = {}
for col, item in system_base_config.options.items():
key = self._format_key('openerp', col)
res[key] = item
return res
def _get_env_cols(self, sections=None):
""" Compute base fields"""
res = {}
sections = sections if sections else serv_config.sections()
for section in sections:
for col, item in serv_config.items(section):
key = self._format_key(section, col)
res[key] = item
return res
def _get_system_cols(self):
""" Compute system fields"""
res = {}
for col, item in get_server_environment():
key = self._format_key('system', col)
res[key] = item
return res
def _group(self, items):
"""Return an XML chunk which represents a group of fields."""
names = []
for k, v in items:
key = '%s\\%s' % (prefix, k)
# Mask passwords
if 'passw' in k and not self.show_passwords:
v = '**********'
self._columns[key] = fields.char(k, size=1024)
self._conf_defaults[key] = v
names.append(key)
for key in sorted(items):
names.append(key.replace('.', '_'))
return ('<group col="2" colspan="4">' +
''.join(['<field name="%s" readonly="1"/>' %
_escape(name) for name in names]) +
@ -148,23 +190,23 @@ class ServerConfiguration(orm.TransientModel):
# OpenERP server configuration
rcfile = system_base_config.rcfile
items = sorted(system_base_config.options.items())
items = self._get_base_cols()
arch += '<page string="OpenERP">'
arch += '<separator string="%s" colspan="4"/>' % _escape(rcfile)
arch += self._group(items, prefix='openerp')
arch += self._group(items)
arch += '<separator colspan="4"/></page>'
arch += '<page string="Environment based configurations">'
for section in sorted(serv_config.sections()):
items = sorted(serv_config.items(section))
items = self._get_env_cols(sections=[section])
arch += '<separator string="[%s]" colspan="4"/>' % _escape(section)
arch += self._group(items, prefix=section)
arch += self._group(items)
arch += '<separator colspan="4"/></page>'
# System information
arch += '<page string="System">'
arch += '<separator string="Server Environment" colspan="4"/>'
arch += self._group(get_server_environment(), prefix='system')
arch += self._group(self._get_system_cols())
arch += '<separator colspan="4"/></page>'
arch += '</notebook></form>'
@ -191,5 +233,8 @@ class ServerConfiguration(orm.TransientModel):
def default_get(self, cr, uid, fields_list, context=None):
res = {}
for key in self._conf_defaults:
res[key] = self._conf_defaults[key]()
if 'passw' in key and not self.show_passwords:
res[key] = '**********'
else:
res[key] = self._conf_defaults[key]()
return res

0
__unported__/server_environment/serv_config.xml → server_environment/serv_config.xml

9
__unported__/server_environment/system_info.py → server_environment/system_info.py

@ -37,9 +37,12 @@ def _get_output(cmd):
def get_server_environment():
# inspired by server/bin/service/web_services.py
try:
rev_id = _get_output('bzr revision-info')
except Exception as e:
rev_id = 'Exception: %s' % (e,)
rev_id = 'git:%s' % _get_output('git rev-parse HEAD')
except Exception:
try:
rev_id = 'bzr: %s' % _get_output('bzr revision-info')
except Exception:
rev_id = 'Can not retrive revison from git or bzr'
os_lang = '.'.join([x for x in locale.getdefaultlocale() if x])
if not os_lang:

22
server_environment/tests/__init__.py

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Nicolas Bessi
# Copyright 2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import test_server_environment
checks = [test_server_environment]

48
server_environment/tests/test_server_environment.py

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Nicolas Bessi
# Copyright 2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.tests import common
from openerp.addons.server_environment import serv_config
class TestEnv(common.TransactionCase):
def test_view(self):
model = self.env['server.config']
view = model.fields_view_get()
self.assertTrue(view)
def test_default(self):
model = self.env['server.config']
rec = model.create({})
defaults = rec.default_get([])
self.assertTrue(defaults)
self.assertIsInstance(defaults, dict)
pass_checked = False
for default in defaults:
if 'passw' in default:
self.assertNotEqual(defaults[default],
'**********')
pass_checked = True
self.assertTrue(pass_checked)
def test_value_retrival(self):
val = serv_config.get('external_service.ftp', 'user')
self.assertEqual(val, 'toto')

0
__unported__/server_environment_files/__init__.py → server_environment_files_sample/__init__.py

0
__unported__/server_environment_files/__openerp__.py → server_environment_files_sample/__openerp__.py

0
__unported__/server_environment_files/default/base.conf → server_environment_files_sample/default/base.conf

0
__unported__/server_environment_files/default/mytopic.conf → server_environment_files_sample/default/mytopic.conf

0
__unported__/server_environment_files/dev/base.conf → server_environment_files_sample/dev/base.conf

0
__unported__/server_environment_files/dev/mytopic.conf → server_environment_files_sample/dev/mytopic.conf

0
__unported__/server_environment_files/i18n/server_environment_files.pot → server_environment_files_sample/i18n/server_environment_files.pot

0
__unported__/server_environment_files/preprod/mytopic.conf → server_environment_files_sample/preprod/mytopic.conf

0
__unported__/server_environment_files/prod/base.conf → server_environment_files_sample/prod/base.conf

0
__unported__/server_environment_files/prod/mytopic.conf → server_environment_files_sample/prod/mytopic.conf

Loading…
Cancel
Save