[MERGE] OPW 577045: translate: properly export and translate terms in XSL-based reports
[odoo/odoo.git] / openerp / tools / config.py
index 3304a4f..f0783bc 100644 (file)
@@ -3,6 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
+#    Copyright (C) 2010-2012 OpenERP s.a. (<http://openerp.com>).
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as
@@ -57,6 +58,8 @@ def check_ssl():
     except:
         return False
 
+DEFAULT_LOG_HANDLER = [':INFO']
+
 class configmanager(object):
     def __init__(self, fname=None):
         # Options not exposed on the command line. Command line options will be added
@@ -73,7 +76,7 @@ class configmanager(object):
         # Not exposed in the configuration file.
         self.blacklist_for_save = set(
             ['publisher_warranty_url', 'load_language', 'root_path',
-            'init', 'save', 'config', 'update'])
+            'init', 'save', 'config', 'update', 'stop_after_init'])
 
         # dictionary mapping option destination (keys in self.options) to MyOptions.
         self.casts = {}
@@ -82,8 +85,7 @@ class configmanager(object):
         self.config_file = fname
         self.has_ssl = check_ssl()
 
-        self._LOGLEVELS = dict([(getattr(loglevels, 'LOG_%s' % x), getattr(logging, x))
-                          for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'DEBUG_RPC', 'DEBUG_SQL', 'DEBUG_RPC_ANSWER','NOTSET')])
+        self._LOGLEVELS = dict([(getattr(loglevels, 'LOG_%s' % x), getattr(logging, x)) for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'NOTSET')])
 
         version = "%s %s" % (release.description, release.version)
         self.parser = parser = optparse.OptionParser(version=version, option_class=MyOption)
@@ -102,7 +104,10 @@ class configmanager(object):
         group.add_option("-P", "--import-partial", dest="import_partial", my_default='',
                         help="Use this for big data importation, if it crashes you will be able to continue at the current state. Provide a filename to store intermediate importation states.")
         group.add_option("--pidfile", dest="pidfile", help="file where the server pid will be stored")
-        group.add_option("--load", dest="server_wide_modules", help="Comma-separated list of server-wide modules")
+        group.add_option("--addons-path", dest="addons_path",
+                         help="specify additional addons paths (separated by commas).",
+                         action="callback", callback=self._check_addons_path, nargs=1, type="string")
+        group.add_option("--load", dest="server_wide_modules", help="Comma-separated list of server-wide modules default=web")
         parser.add_option_group(group)
 
         # XML-RPC / HTTP
@@ -113,6 +118,8 @@ class configmanager(object):
                          help="specify the TCP port for the XML-RPC protocol", type="int")
         group.add_option("--no-xmlrpc", dest="xmlrpc", action="store_false", my_default=True,
                          help="disable the XML-RPC protocol")
+        group.add_option("--proxy-mode", dest="proxy_mode", action="store_true", my_default=False,
+                         help="Enable correct behavior when behind a reverse proxy")
         parser.add_option_group(group)
 
         # XML-RPC / HTTPS
@@ -175,13 +182,20 @@ class configmanager(object):
         # Logging Group
         group = optparse.OptionGroup(parser, "Logging Configuration")
         group.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
-        group.add_option("--no-logrotate", dest="logrotate", action="store_false", my_default=True,
-                         help="do not rotate the logfile")
-        group.add_option("--syslog", action="store_true", dest="syslog",
-                         my_default=False, help="Send the log to the syslog server")
-        group.add_option('--log-level', dest='log_level', type='choice', choices=self._LOGLEVELS.keys(),
-                         my_default='info',
-                         help='specify the level of the logging. Accepted values: ' + str(self._LOGLEVELS.keys()))
+        group.add_option("--no-logrotate", dest="logrotate", action="store_false", my_default=True, help="do not rotate the logfile")
+        group.add_option("--syslog", action="store_true", dest="syslog", my_default=False, help="Send the log to the syslog server")
+        group.add_option('--log-handler', action="append", default=DEFAULT_LOG_HANDLER, my_default=DEFAULT_LOG_HANDLER, metavar="PREFIX:LEVEL", help='setup a handler at LEVEL for a given PREFIX. An empty PREFIX indicates the root logger. This option can be repeated. Example: "openerp.orm:DEBUG" or "werkzeug:CRITICAL" (default: ":INFO")')
+        group.add_option('--log-request', action="append_const", dest="log_handler", const="openerp.netsvc.rpc.request:DEBUG", help='shortcut for --log-handler=openerp.netsvc.rpc.request:DEBUG')
+        group.add_option('--log-response', action="append_const", dest="log_handler", const="openerp.netsvc.rpc.response:DEBUG", help='shortcut for --log-handler=openerp.netsvc.rpc.response:DEBUG')
+        group.add_option('--log-web', action="append_const", dest="log_handler", const="openerp.addons.web.common.http:DEBUG", help='shortcut for --log-handler=openerp.addons.web.common.http:DEBUG')
+        group.add_option('--log-sql', action="append_const", dest="log_handler", const="openerp.sql_db:DEBUG", help='shortcut for --log-handler=openerp.sql_db:DEBUG')
+        # For backward-compatibility, map the old log levels to something
+        # quite close.
+        levels = ['info', 'debug_rpc', 'warn', 'test', 'critical',
+            'debug_sql', 'error', 'debug', 'debug_rpc_answer', 'notset']
+        group.add_option('--log-level', dest='log_level', type='choice', choices=levels,
+            my_default='info', help='specify the level of the logging. Accepted values: ' + str(levels) + ' (deprecated option).')
+
         parser.add_option_group(group)
 
         # SMTP Group
@@ -214,6 +228,8 @@ class configmanager(object):
                          help="specify the database port", type="int")
         group.add_option("--db_maxconn", dest="db_maxconn", type='int', my_default=64,
                          help="specify the the maximum number of physical connections to posgresql")
+        group.add_option("--db-template", dest="db_template", my_default="template0",
+                         help="specify a custom database template to create a new database")
         parser.add_option_group(group)
 
         group = optparse.OptionGroup(parser, "Internationalisation options",
@@ -233,9 +249,6 @@ class configmanager(object):
                          help="overwrites existing translation terms on updating a module or importing a CSV or a PO file.")
         group.add_option("--modules", dest="translate_modules",
                          help="specify modules to export. Use in combination with --i18n-export")
-        group.add_option("--addons-path", dest="addons_path",
-                         help="specify additional addons paths (separated by commas).",
-                         action="callback", callback=self._check_addons_path, nargs=1, type="string")
         parser.add_option_group(group)
 
         security = optparse.OptionGroup(parser, 'Security-related options')
@@ -261,15 +274,34 @@ class configmanager(object):
                               "osv_memory tables. This is a decimal value expressed in hours, "
                               "and the default is 1 hour.",
                          type="float")
+        group.add_option("--max-cron-threads", dest="max_cron_threads", my_default=4,
+                         help="Maximum number of threads processing concurrently cron jobs.",
+                         type="int")
+        # TODO sensible default for the three following limits.
+        group.add_option("--virtual-memory-limit", dest="virtual_memory_limit", my_default=768 * 1024 * 1024,
+                         help="Maximum allowed virtual memory per Gunicorn process. "
+                         "When the limit is reached, any memory allocation will fail.",
+                         type="int")
+        group.add_option("--virtual-memory-reset", dest="virtual_memory_reset", my_default=640 * 1024 * 1024,
+                         help="Maximum allowed virtual memory per Gunicorn process. "
+                         "When the limit is reached, the worker will be reset after "
+                         "the current request.",
+                         type="int")
+        group.add_option("--cpu-time-limit", dest="cpu_time_limit", my_default=60,
+                         help="Maximum allowed CPU time per Gunicorn process. "
+                         "When the limit is reached, an exception is raised.",
+                         type="int")
         group.add_option("--unaccent", dest="unaccent", my_default=False, action="store_true",
                          help="Use the unaccent function provided by the database when available.")
+
         parser.add_option_group(group)
 
         # Copy all optparse options (i.e. MyOption) into self.options.
         for group in parser.option_groups:
             for option in group.option_list:
-                self.options[option.dest] = option.my_default
-                self.casts[option.dest] = option
+                if option.dest not in self.options:
+                    self.options[option.dest] = option.my_default
+                    self.casts[option.dest] = option
 
         self.parse_config(None, False)
 
@@ -341,31 +373,39 @@ class configmanager(object):
         if self.options['pidfile'] in ('None', 'False'):
             self.options['pidfile'] = False
 
+        # if defined dont take the configfile value even if the defined value is None
         keys = ['xmlrpc_interface', 'xmlrpc_port', 'db_name', 'db_user', 'db_password', 'db_host',
-                'db_port', 'logfile', 'pidfile', 'smtp_port', 'cache_timeout',
+                'db_port', 'db_template', 'logfile', 'pidfile', 'smtp_port', 'cache_timeout',
                 'email_from', 'smtp_server', 'smtp_user', 'smtp_password',
                 'netrpc_interface', 'netrpc_port', 'db_maxconn', 'import_partial', 'addons_path',
                 'netrpc', 'xmlrpc', 'syslog', 'without_demo', 'timezone',
                 'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs',
                 'static_http_enable', 'static_http_document_root', 'static_http_url_prefix',
-                'secure_cert_file', 'secure_pkey_file', 'dbfilter'
+                'secure_cert_file', 'secure_pkey_file', 'dbfilter', 'log_handler', 'log_level'
                 ]
 
         for arg in keys:
-            # Copy the command-line argument...
-            if getattr(opt, arg):
+            # Copy the command-line argument (except the special case for log_handler, due to
+            # action=append requiring a real default, so we cannot use the my_default workaround)
+            if getattr(opt, arg) and getattr(opt, arg) != DEFAULT_LOG_HANDLER:
                 self.options[arg] = getattr(opt, arg)
             # ... or keep, but cast, the config file value.
             elif isinstance(self.options[arg], basestring) and self.casts[arg].type in optparse.Option.TYPE_CHECKER:
                 self.options[arg] = optparse.Option.TYPE_CHECKER[self.casts[arg].type](self.casts[arg], arg, self.options[arg])
 
+
+        if isinstance(self.options['log_handler'], basestring):
+            self.options['log_handler'] = self.options['log_handler'].split(',')
+
+        # if defined but None take the configfile value
         keys = [
             'language', 'translate_out', 'translate_in', 'overwrite_existing_translations',
             'debug_mode', 'smtp_ssl', 'load_language',
             'stop_after_init', 'logrotate', 'without_demo', 'netrpc', 'xmlrpc', 'syslog',
-            'list_db', 'xmlrpcs',
+            'list_db', 'xmlrpcs', 'proxy_mode',
             'test_file', 'test_disable', 'test_commit', 'test_report_directory',
-            'osv_memory_count_limit', 'osv_memory_age_limit', 'unaccent',
+            'osv_memory_count_limit', 'osv_memory_age_limit', 'max_cron_threads',
+            'virtual_memory_limit', 'virtual_memory_reset', 'cpu_time_limit', 'unaccent',
         ]
 
         for arg in keys:
@@ -381,11 +421,6 @@ class configmanager(object):
         else:
             self.options['assert_exit_level'] = self._LOGLEVELS.get(self.options['assert_exit_level']) or int(self.options['assert_exit_level'])
 
-        if opt.log_level:
-            self.options['log_level'] = self._LOGLEVELS[opt.log_level]
-        else:
-            self.options['log_level'] = self._LOGLEVELS.get(self.options['log_level']) or int(self.options['log_level'])
-
         self.options['root_path'] = os.path.abspath(os.path.expanduser(os.path.expandvars(os.path.dirname(openerp.__file__))))
         if not self.options['addons_path'] or self.options['addons_path']=='None':
             self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
@@ -447,16 +482,16 @@ class configmanager(object):
         if opt.save:
             self.save()
 
+        openerp.conf.max_cron_threads = self.options['max_cron_threads']
+
         openerp.conf.addons_paths = self.options['addons_path'].split(',')
-        openerp.conf.server_wide_modules = \
-            map(lambda m: m.strip(), opt.server_wide_modules.split(',')) if \
-            opt.server_wide_modules else []
+        if opt.server_wide_modules:
+            openerp.conf.server_wide_modules = map(lambda m: m.strip(), opt.server_wide_modules.split(','))
+        else:
+            openerp.conf.server_wide_modules = ['web']
         if complete:
             openerp.modules.module.initialize_sys_path()
             openerp.modules.loading.open_openerp_namespace()
-            # openerp.addons.__path__.extend(openerp.conf.addons_paths) # This
-            # is not compatible with initialize_sys_path(): import crm and
-            # import openerp.addons.crm load twice the module.
 
     def _generate_pgpassfile(self):
         """
@@ -561,6 +596,7 @@ class configmanager(object):
                 p.set('options', opt, self.options[opt])
 
         for sec in sorted(self.misc.keys()):
+            p.add_section(sec)
             for opt in sorted(self.misc[sec].keys()):
                 p.set(sec,opt,self.misc[sec][opt])
 
@@ -588,9 +624,14 @@ class configmanager(object):
 
     def __setitem__(self, key, value):
         self.options[key] = value
+        if key in self.options and isinstance(self.options[key], basestring) and \
+                key in self.casts and self.casts[key].type in optparse.Option.TYPE_CHECKER:
+            self.options[key] = optparse.Option.TYPE_CHECKER[self.casts[key].type](self.casts[key], key, self.options[key])
 
     def __getitem__(self, key):
         return self.options[key]
 
 config = configmanager()
 
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: