add target new functionality with view_id
[openerp-client.git] / bin / options.py
blobf806c1350246e3a90712d38f9f53e2d79ab795be
1 ##############################################################################
3 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # $Id$
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
12 # Service Company
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
31 import os, sys
32 import gtk
33 import gettext
35 def get_home_dir():
36 """Return the closest possible equivalent to a 'home' directory.
37 For Posix systems, this is $HOME, and on NT it's $HOMEDRIVE\$HOMEPATH.
38 Currently only Posix and NT are implemented, a HomeDirError exception is
39 raised for all other OSes. """
41 if os.name == 'posix':
42 return os.path.expanduser('~')
43 elif os.name == 'nt':
44 try:
45 return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
46 except:
47 try:
48 import _winreg as wreg
49 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
50 homedir = wreg.QueryValueEx(key,'Personal')[0]
51 key.Close()
52 return homedir
53 except:
54 return 'C:\\'
55 elif os.name == 'dos':
56 return 'C:\\'
57 else:
58 return '.'
60 class configmanager(object):
61 def __init__(self,fname=None):
62 self.options = {
63 'login.login': 'demo',
64 'login.server': 'localhost',
65 'login.port': '8069',
66 'login.protocol': 'http://',
67 'login.db': 'terp',
68 'client.modepda': False,
69 'client.toolbar': 'both',
70 'client.theme': 'none',
71 'path.share': os.path.join(sys.prefix, 'share/tinyerp-client/'),
72 'path.pixmaps': os.path.join(sys.prefix, 'share/pixmaps/tinyerp-client/'),
73 'tip.autostart': False,
74 'tip.position': 0,
75 'survey.position': 0,
76 'form.autosave': False,
77 'printer.preview': True,
78 'printer.softpath': 'none',
79 'printer.softpath_html': 'none',
80 'printer.path': 'none',
81 'logging.logger': '',
82 'logging.level': 'DEBUG',
83 'logging.output': 'stdout',
84 'logging.verbose': False,
85 'client.default_path': os.path.expanduser('~'),
86 'support.recipient': 'support@tiny.be',
87 'support.support_id' : '',
88 'form.toolbar': True,
89 'client.form_tab': 'left',
90 'client.form_tab_orientation': 0,
91 'client.lang': False,
92 'client.filetype': {},
94 parser = optparse.OptionParser(version=_("Tiny ERP Client %s" % tinyerp_version))
95 parser.add_option("-c", "--config", dest="config",help=_("specify alternate config file"))
96 parser.add_option("-v", "--verbose", action="store_true", default=False, dest="verbose", help=_("enable basic debugging"))
97 parser.add_option("-d", "--log", dest="log_logger", default='', help=_("specify channels to log"))
98 parser.add_option("-l", "--log-level", dest="log_level",default='ERROR', help=_("specify the log level: INFO, DEBUG, WARNING, ERROR, CRITICAL"))
99 parser.add_option("-u", "--user", dest="login", help=_("specify the user login"))
100 parser.add_option("-p", "--port", dest="port", help=_("specify the server port"))
101 parser.add_option("-s", "--server", dest="server", help=_("specify the server ip/name"))
102 (opt, args) = parser.parse_args()
104 self.rcfile = self._get_rcfile(fname, opt.config)
105 self.load()
107 if opt.verbose:
108 self.options['logging.verbose']=True
109 self.options['logging.logger'] = opt.log_logger
110 self.options['logging.level'] = opt.log_level
112 for arg in ('login', 'port', 'server'):
113 if getattr(opt, arg):
114 self.options['login.'+arg] = getattr(opt, arg)
116 def _get_rcfile(self, fname, optconfigfile):
117 rcfile = fname or optconfigfile or os.environ.get('TERPRC') or os.path.join(get_home_dir(), '.terprc')
118 if not os.path.exists(rcfile):
119 import logging
120 log = logging.getLogger('common.options')
121 additional_info = ""
122 if optconfigfile:
123 additional_info = " Be sure to specify an absolute path name if you are using the '-c' command line switch"
124 log.warn('Config file %s does not exist !%s'% (rcfile, additional_info ))
125 return os.path.abspath(rcfile)
127 def save(self, fname = None):
128 try:
129 p = ConfigParser.ConfigParser()
130 sections = {}
131 for o in self.options.keys():
132 if not len(o.split('.'))==2:
133 continue
134 osection,oname = o.split('.')
135 if not p.has_section(osection):
136 p.add_section(osection)
137 p.set(osection,oname,self.options[o])
138 p.write(file(self.rcfile,'wb'))
139 except:
140 import logging
141 log = logging.getLogger('common.options')
142 log.warn('Unable to write config file %s !'% (self.rcfile,))
143 return True
146 def load(self, fname=None):
147 try:
148 self.rcexist = False
149 if not os.path.isfile(self.rcfile):
150 self.save()
151 return False
152 self.rcexist = True
154 p = ConfigParser.ConfigParser()
155 p.read([self.rcfile])
156 for section in p.sections():
157 for (name,value) in p.items(section):
158 if value=='True' or value=='true':
159 value = True
160 if value=='False' or value=='false':
161 value = False
162 self.options[section+'.'+name] = value
163 except Exception, e:
164 import logging
165 log = logging.getLogger('common.options')
166 log.warn('Unable to read config file %s !'% (self.rcfile,))
167 return True
169 def __setitem__(self, key, value):
170 self.options[key]=value
172 def __getitem__(self, key):
173 return self.options[key]
175 options = configmanager()