1
##############################################################################
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5
# $Id: config.py 996 2005-07-22 10:40:10Z nicoe $
7
# WARNING: This program as such is intended to be used by professional
8
# programmers who take the whole responsability of assessing all potential
9
# consequences resulting from its eventual inadequacies and bugs
10
# End users who are looking for a ready-to-use solution with commercial
11
# garantees and support are strongly adviced to contract a Free Software
14
# This program is Free Software; you can redistribute it and/or
15
# modify it under the terms of the GNU General Public License
16
# as published by the Free Software Foundation; either version 2
17
# of the License, or (at your option) any later version.
19
# This program is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
# GNU General Public License for more details.
24
# You should have received a copy of the GNU General Public License
25
# along with this program; if not, write to the Free Software
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
##############################################################################
30
import ConfigParser,optparse,os,sys
33
class configmanager(object):
34
def __init__(self, fname=None):
37
'interface': '', # this will bind the server to all interfaces
49
'translate_out': None,
52
'admin_passwd': 'admin',
59
'smtp_server': 'localhost',
60
'stop_after_init': False, # this will stop the server after initialization
63
parser = optparse.OptionParser(version=tinyerp_version_string)
65
parser.add_option("-c", "--config", dest="config", help="specify alternate config file")
66
parser.add_option("-s", "--save", action="store_true", dest="save", default=False, help="save configuration to ~/.terp_serverrc")
67
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="enable debugging")
68
parser.add_option("--pidfile", dest="pidfile", help="file where the server pid will be stored")
69
parser.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
71
parser.add_option("-n", "--interface", dest="interface", help="specify the TCP IP address")
72
parser.add_option("-p", "--port", dest="port", help="specify the TCP port")
74
parser.add_option("-i", "--init", dest="init", help="init a module (use \"all\" for all modules)")
75
parser.add_option("--without-demo", dest="without_demo", help="load demo data for a module (use \"all\" for all modules)", default=False)
76
parser.add_option("-u", "--update", dest="update", help="update a module (use \"all\" for all modules)")
77
# stops the server from launching after initialization
78
parser.add_option("--stop-after-init", action="store_true", dest="stop_after_init", default=False, help="stop the server after it initializes")
79
parser.add_option('--debug', dest='debug_mode', action='store_true', default=False, help='enable debug mode')
80
parser.add_option("-S", "--secure", dest="secure", action="store_true", help="launch server over https instead of http", default=False)
81
parser.add_option('--smtp', dest='smtp_server', default='', help='specify the SMTP server for sending mail')
83
group = optparse.OptionGroup(parser, "Modules related options")
84
group.add_option("-g", "--upgrade", action="store_true", dest="upgrade", default=False, help="Upgrade/install/uninstall modules")
86
group = optparse.OptionGroup(parser, "Database related options")
87
group.add_option("-d", "--database", dest="db_name", help="specify the database name")
88
group.add_option("-r", "--db_user", dest="db_user", help="specify the database user name")
89
group.add_option("-w", "--db_password", dest="db_password", help="specify the database password")
90
group.add_option("--pg_path", dest="pg_path", help="specify the pg executable path")
91
group.add_option("--db_host", dest="db_host", help="specify the database host")
92
group.add_option("--db_port", dest="db_port", help="specify the database port")
93
parser.add_option_group(group)
95
group = optparse.OptionGroup(parser, "Internationalisation options",
96
"Use these options to translate Tiny ERP to another language."
97
"See i18n section of the user manual. Option '-l' is mandatory.")
99
group.add_option('-l', "--language", dest="language", help="specify the language of the translation file. Use it with --i18n-export and --i18n-import")
100
group.add_option("--i18n-export", dest="translate_out", help="export all sentences to be translated to a CSV file and exit")
101
group.add_option("--i18n-import", dest="translate_in", help="import a CSV file with translations and exit")
102
group.add_option("--modules", dest="translate_modules", help="specify modules to export. Use in combination with --i18n-export")
103
parser.add_option_group(group)
105
(opt, args) = parser.parse_args()
107
if (opt.translate_in or opt.translate_out) and (not opt.language or not opt.db_name):
108
print "Error: the i18n-import and i18n-export options cannot be used without the language (-l) and database (-d) options"
111
# place/search the config file on Win32 near the server installation
112
# (../etc from the server)
113
# if the server is run by an unprivileged user, he has to specify location of a config file where he has the rights to write,
114
# else he won't be able to save the configurations, or even to start the server...
116
rcfilepath = os.path.join(os.path.split(os.path.split(os.path.abspath(sys.argv[0]))[0])[0], 'etc', 'tinyerp-server.conf')
118
rcfilepath = os.path.expanduser('~/.terp_serverrc')
120
self.rcfile = fname or opt.config or os.environ.get('TERP_SERVER') or rcfilepath
124
# Verify that we want to log or not, if not the output will go to stdout
125
if self.options['logfile'] in ('None', 'False'):
126
self.options['logfile'] = False
127
# the same for the pidfile
128
if self.options['pidfile'] in ('None', 'False'):
129
self.options['pidfile'] = False
131
for arg in ('interface', 'port', 'db_name', 'db_user', 'db_password', 'db_host',
132
'db_port', 'logfile', 'pidfile', 'secure', 'smtp_server'):
133
if getattr(opt, arg):
134
self.options[arg] = getattr(opt, arg)
136
for arg in ('language', 'translate_out', 'translate_in',
137
'upgrade', 'verbose', 'debug_mode',
138
'stop_after_init', 'without_demo'):
139
self.options[arg] = getattr(opt, arg)
141
if not self.options['root_path'] or self.options['root_path']=='None':
142
self.options['root_path'] = os.path.abspath(os.path.dirname(sys.argv[0]))
143
if not self.options['addons_path'] or self.options['addons_path']=='None':
144
self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
148
for i in opt.init.split(','):
150
self.options['init'] = init
151
self.options["demo"] = not opt.without_demo and self.options['init'] or {}
155
for i in opt.update.split(','):
157
self.options['update'] = update
159
self.options['translate_modules'] = opt.translate_modules and opt.translate_modules.split(',') or ['all']
161
self.options['pg_path'] = opt.pg_path
163
if self.options.get('language', False):
164
assert len(self.options['language'])<=5, 'ERROR: The Lang name must take max 5 chars, Eg: -lfr_BE'
169
p = ConfigParser.ConfigParser()
171
p.read([self.rcfile])
172
for (name,value) in p.items('options'):
173
if value=='True' or value=='true':
175
if value=='False' or value=='false':
177
self.options[name] = value
180
except ConfigParser.NoSectionError:
184
p = ConfigParser.ConfigParser()
185
p.add_section('options')
186
for o in [opt for opt in self.options.keys() if opt not in ('version','language','translate_out','translate_in','init','update')]:
187
print o, self.options[o]
188
p.set('options', o, self.options[o])
190
# try to create the directories and write the file
192
if not os.path.exists(os.path.dirname(self.rcfile)):
193
os.makedirs(os.path.dirname(self.rcfile))
195
p.write(file(self.rcfile, 'w'))
197
sys.stderr.write("ERROR: couldn't write the config file\n")
200
# what to do if impossible?
201
sys.stderr.write("ERROR: couldn't create the config directory\n")
205
def get(self, key, default=None):
206
return self.options.get(key, default)
208
def __setitem__(self, key, value):
209
self.options[key] = value
211
def __getitem__(self, key):
212
return self.options[key]
214
config = configmanager()