1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import configparser
import gettext
import locale
import logging
import optparse
import os
import shutil
import sys
import tempfile
from gi.repository import GdkPixbuf
from tryton import __version__
logger = logging.getLogger(__name__)
_ = gettext.gettext
def _reverse_series_iterator(starting_version):
major, minor = map(int, starting_version.split('.'))
while major >= 0 and minor >= 0:
yield f"{major}.{minor}"
if minor == 0:
major -= 1
minor = 8
else:
minor -= 2 if not minor % 2 else 1
def copy_previous_configuration(config_element):
current_version = __version__.rsplit('.', 1)[0]
config_dir = get_config_root()
for version in _reverse_series_iterator(current_version):
config_path = os.path.join(config_dir, version, config_element)
if version == current_version and os.path.exists(config_path):
break
elif os.path.exists(config_path):
if os.path.isfile(config_path):
shutil.copy(config_path, get_config_dir())
elif os.path.isdir(config_path):
shutil.copytree(
config_path,
os.path.join(get_config_dir(), config_element))
break
def get_config_root():
if os.name == 'nt':
appdata = os.environ['APPDATA']
if not isinstance(appdata, str):
appdata = str(appdata, sys.getfilesystemencoding())
config_path = os.path.join(appdata, '.config')
else:
config_path = os.path.expanduser(os.getenv(
'XDG_CONFIG_HOME', os.path.join('~', '.config')))
return os.path.join(config_path, 'tryton')
def get_config_dir():
return os.path.join(get_config_root(), __version__.rsplit('.', 1)[0])
os.makedirs(get_config_dir(), mode=0o700, exist_ok=True)
class ConfigManager(object):
"Config manager"
def __init__(self):
short_version = '.'.join(__version__.split('.', 2)[:2])
demo_server = 'demo%s.tryton.org' % short_version
demo_database = 'demo%s' % short_version
self.defaults = {
'login.profile': demo_server,
'login.login': 'demo',
'login.service': '',
'login.service.port': 8001,
'login.host': demo_server,
'login.db': demo_database,
'login.expanded': False,
'client.title': 'Tryton',
'client.modepda': False,
'client.toolbar': 'default',
'client.save_tree_width': True,
'client.save_tree_state': True,
'client.spellcheck': False,
'client.code_scanner_sound': True,
'client.lang': locale.getdefaultlocale()[0],
'client.language_direction': 'ltr',
'client.email': '',
'client.limit': 1000,
# Debian: check_version not for us
'client.check_version': False,
'client.bus_timeout': 10 * 60,
'icon.colors': '#3465a4,#555753,#cc0000',
'tree.colors': '#777,#198754,#ffc107,#dc3545',
'calendar.colors': '#fff,#3465a4',
'graph.color': '#3465a4',
'image.max_size': 10 ** 6,
'image.cache_size': 1024,
'doc.url': 'https://docs.tryton.org/%(version)s',
'bug.url': 'https://bugs.tryton.org/',
'download.url': 'https://downloads.tryton.org/',
'download.frequency': 60 * 60 * 8,
'menu.pane': 320,
}
self.config = {}
self.options = {}
self.arguments = []
def parse(self):
parser = optparse.OptionParser(version=("Tryton %s" % __version__),
usage="Usage: %prog [options] [url]")
parser.add_option("-c", "--config", dest="config",
help=_("specify alternate config file"))
parser.add_option("-d", "--dev", action="store_true",
default=False, dest="dev",
help=_("development mode"))
parser.add_option("-v", "--verbose", action="store_true",
default=False, dest="verbose",
help=_("logging everything at INFO level"))
parser.add_option("-l", "--log-level", dest="log_level",
help=_("specify the log level: "
"DEBUG, INFO, WARNING, ERROR, CRITICAL"))
parser.add_option("-o", "--log-ouput", dest="log_output", default=None,
help=_("specify the file used to output logging information"))
parser.add_option("-u", "--user", dest="login",
help=_("specify the login user"))
parser.add_option("-s", "--server", dest="host",
help=_("specify the server hostname:port"))
parser.add_option(
'--no-thread', default=True, action='store_false', dest='thread',
help=_("disable thread usage"))
opt, self.arguments = parser.parse_args()
self.rcfile = opt.config or os.path.join(
get_config_dir(), 'tryton.conf')
self.load()
logging_config = {}
if opt.log_output:
logging_config['filename'] = opt.log_output
loglevels = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL,
}
if not opt.log_level:
if opt.verbose:
opt.log_level = 'INFO'
else:
opt.log_level = 'ERROR'
logging_config['level'] = loglevels[opt.log_level.upper()]
logging.basicConfig(**logging_config)
self.options['dev'] = opt.dev
for arg in ['login', 'host']:
if getattr(opt, arg):
self.options['login.' + arg] = getattr(opt, arg)
self.options['thread'] = opt.thread
def save(self):
try:
parser = configparser.ConfigParser()
for entry in list(self.config.keys()):
if not len(entry.split('.')) == 2:
continue
section, name = entry.split('.')
if not parser.has_section(section):
parser.add_section(section)
parser.set(section, name, str(self.config[entry]))
with open(self.rcfile, 'w') as fp:
parser.write(fp)
except IOError:
logger.warn("Unable to write config file %s", self.rcfile)
return False
return True
def load(self):
parser = configparser.ConfigParser()
try:
parser.read([self.rcfile])
except configparser.Error:
config_dir = os.path.dirname(self.rcfile)
with tempfile.NamedTemporaryFile(
delete=False, prefix='tryton_', suffix='.conf',
dir=config_dir) as temp_file:
temp_name = temp_file.name
shutil.copy(self.rcfile, temp_name)
logger.error(
f"Failed to parse {self.rcfile}. "
f"A backup can be found at {temp_name}", exc_info=True)
return
for section in parser.sections():
for (name, value) in parser.items(section):
if value.lower() == 'true':
value = True
elif value.lower() == 'false':
value = False
if section == 'client' and name == 'limit':
# First convert to float to be backward compatible with old
# configuration
value = int(float(value))
self.config[section + '.' + name] = value
return True
def __setitem__(self, key, value, config=True):
self.options[key] = value
if config:
self.config[key] = value
def __getitem__(self, key):
return self.options.get(key, self.config.get(key,
self.defaults.get(key)))
CONFIG = ConfigManager()
CURRENT_DIR = os.path.dirname(__file__)
if not isinstance(CURRENT_DIR, str):
CURRENT_DIR = str(CURRENT_DIR, sys.getfilesystemencoding())
PIXMAPS_DIR = os.path.join(sys.prefix, 'share', 'icons', 'tryton')
if not os.path.isdir(PIXMAPS_DIR):
try:
import importlib.resources
ref = importlib.resources.files('tryton') / 'data/pixmaps/tryton'
with importlib.resources.as_file(ref) as path:
PIXMAPS_DIR = path
except ImportError:
import pkg_resources
PIXMAPS_DIR = pkg_resources.resource_filename(
'tryton', 'data/pixmaps/tryton')
SOUNDS_DIR = os.path.join(CURRENT_DIR, 'data', 'sounds')
if not os.path.isdir(SOUNDS_DIR):
try:
import importlib.resources
ref = importlib.resources.files('tryton') / 'data/sounds'
with importlib.resources.as_file(ref) as path:
SOUNDS_DIR = path
except ImportError:
import pkg_resources
SOUNDS_DIR = pkg_resources.resource_filename('tryton', 'data/sounds')
TRYTON_ICON = GdkPixbuf.Pixbuf.new_from_file(
os.path.join(PIXMAPS_DIR, 'tryton-icon.png'))
|