author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 08 Jul 2009 17:22:24 +0200 | |
branch | stable |
changeset 2327 | 2aa28ac13d64 |
parent 2267 | e1d2df3f1091 |
child 2351 | dddee537e4d5 |
permissions | -rw-r--r-- |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
180
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
0 | 2 |
"""common configuration utilities for cubicweb |
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1948
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1493 | 7 |
|
8 |
.. envvar:: CW_CUBES_PATH |
|
9 |
||
10 |
Augments the default search path for cubes |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1681
diff
changeset
|
11 |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1948
diff
changeset
|
12 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 13 |
""" |
14 |
__docformat__ = "restructuredtext en" |
|
1948 | 15 |
_ = unicode |
0 | 16 |
|
17 |
import sys |
|
18 |
import os |
|
19 |
import logging |
|
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
20 |
from smtplib import SMTP |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
21 |
from threading import Lock |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
22 |
from os.path import exists, join, expanduser, abspath, normpath, basename, isdir |
0 | 23 |
|
24 |
from logilab.common.decorators import cached |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
25 |
from logilab.common.logging_ext import set_log_methods, init_log |
0 | 26 |
from logilab.common.configuration import (Configuration, Method, |
27 |
ConfigurationMixIn, merge_options) |
|
28 |
||
29 |
from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError |
|
1132 | 30 |
from cubicweb.toolsutils import env_path, create_dir |
0 | 31 |
|
32 |
CONFIGURATIONS = [] |
|
33 |
||
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
34 |
SMTP_LOCK = Lock() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
35 |
|
0 | 36 |
|
37 |
class metaconfiguration(type): |
|
38 |
"""metaclass to automaticaly register configuration""" |
|
39 |
def __new__(mcs, name, bases, classdict): |
|
40 |
cls = super(metaconfiguration, mcs).__new__(mcs, name, bases, classdict) |
|
41 |
if classdict.get('name'): |
|
42 |
CONFIGURATIONS.append(cls) |
|
43 |
return cls |
|
44 |
||
45 |
def configuration_cls(name): |
|
46 |
"""return the configuration class registered with the given name""" |
|
47 |
try: |
|
48 |
return [c for c in CONFIGURATIONS if c.name == name][0] |
|
49 |
except IndexError: |
|
50 |
raise ConfigurationError('no such config %r (check it exists with "cubicweb-ctl list")' % name) |
|
51 |
||
52 |
def possible_configurations(directory): |
|
53 |
"""return a list of installed configurations in a directory |
|
54 |
according to *-ctl files |
|
55 |
""" |
|
56 |
return [name for name in ('repository', 'twisted', 'all-in-one') |
|
57 |
if exists(join(directory, '%s.conf' % name))] |
|
58 |
||
59 |
def guess_configuration(directory): |
|
60 |
"""try to guess the configuration to use for a directory. If multiple |
|
61 |
configurations are found, ConfigurationError is raised |
|
62 |
""" |
|
63 |
modes = possible_configurations(directory) |
|
64 |
if len(modes) != 1: |
|
65 |
raise ConfigurationError('unable to guess configuration from %r %s' |
|
66 |
% (directory, modes)) |
|
67 |
return modes[0] |
|
68 |
||
69 |
||
70 |
# persistent options definition |
|
71 |
PERSISTENT_OPTIONS = ( |
|
72 |
('encoding', |
|
73 |
{'type' : 'string', |
|
74 |
'default': 'UTF-8', |
|
75 |
'help': _('user interface encoding'), |
|
76 |
'group': 'ui', 'sitewide': True, |
|
1446 | 77 |
}), |
0 | 78 |
('language', |
79 |
{'type' : 'string', |
|
80 |
'default': 'en', |
|
81 |
'vocabulary': Method('available_languages'), |
|
82 |
'help': _('language of the user interface'), |
|
1446 | 83 |
'group': 'ui', |
0 | 84 |
}), |
85 |
('date-format', |
|
86 |
{'type' : 'string', |
|
87 |
'default': '%Y/%m/%d', |
|
88 |
'help': _('how to format date in the ui ("man strftime" for format description)'), |
|
1446 | 89 |
'group': 'ui', |
0 | 90 |
}), |
91 |
('datetime-format', |
|
92 |
{'type' : 'string', |
|
93 |
'default': '%Y/%m/%d %H:%M', |
|
94 |
'help': _('how to format date and time in the ui ("man strftime" for format description)'), |
|
1446 | 95 |
'group': 'ui', |
0 | 96 |
}), |
97 |
('time-format', |
|
98 |
{'type' : 'string', |
|
99 |
'default': '%H:%M', |
|
100 |
'help': _('how to format time in the ui ("man strftime" for format description)'), |
|
1446 | 101 |
'group': 'ui', |
0 | 102 |
}), |
103 |
('float-format', |
|
104 |
{'type' : 'string', |
|
105 |
'default': '%.3f', |
|
106 |
'help': _('how to format float numbers in the ui'), |
|
1446 | 107 |
'group': 'ui', |
0 | 108 |
}), |
109 |
('default-text-format', |
|
110 |
{'type' : 'choice', |
|
111 |
'choices': ('text/plain', 'text/rest', 'text/html'), |
|
112 |
'default': 'text/html', # use fckeditor in the web ui |
|
113 |
'help': _('default text format for rich text fields.'), |
|
1446 | 114 |
'group': 'ui', |
0 | 115 |
}), |
116 |
('short-line-size', |
|
117 |
{'type' : 'int', |
|
118 |
'default': 40, |
|
119 |
'help': _('maximum number of characters in short description'), |
|
120 |
'group': 'navigation', |
|
121 |
}), |
|
122 |
) |
|
123 |
||
124 |
def register_persistent_options(options): |
|
125 |
global PERSISTENT_OPTIONS |
|
126 |
PERSISTENT_OPTIONS = merge_options(PERSISTENT_OPTIONS + options) |
|
1446 | 127 |
|
0 | 128 |
CFGTYPE2ETYPE_MAP = { |
129 |
'string': 'String', |
|
130 |
'choice': 'String', |
|
131 |
'yn': 'Boolean', |
|
132 |
'int': 'Int', |
|
133 |
'float' : 'Float', |
|
134 |
} |
|
1446 | 135 |
|
0 | 136 |
class CubicWebNoAppConfiguration(ConfigurationMixIn): |
137 |
"""base class for cubicweb configuration without a specific instance directory |
|
138 |
""" |
|
139 |
__metaclass__ = metaconfiguration |
|
140 |
# to set in concrete configuration |
|
141 |
name = None |
|
142 |
# log messages format (see logging module documentation for available keys) |
|
143 |
log_format = '%(asctime)s - (%(name)s) %(levelname)s: %(message)s' |
|
144 |
# nor remove vobjects based on unused interface |
|
145 |
cleanup_interface_sobjects = True |
|
146 |
||
147 |
if os.environ.get('APYCOT_ROOT'): |
|
148 |
mode = 'test' |
|
149 |
CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ |
|
436 | 150 |
# create __init__ file |
151 |
file(join(CUBES_DIR, '__init__.py'), 'w').close() |
|
0 | 152 |
elif exists(join(CW_SOFTWARE_ROOT, '.hg')): |
153 |
mode = 'dev' |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
154 |
CUBES_DIR = abspath(normpath(join(CW_SOFTWARE_ROOT, '../cubes'))) |
0 | 155 |
else: |
156 |
mode = 'installed' |
|
157 |
CUBES_DIR = '/usr/share/cubicweb/cubes/' |
|
158 |
||
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
159 |
options = ( |
0 | 160 |
('log-threshold', |
161 |
{'type' : 'string', # XXX use a dedicated type? |
|
162 |
'default': 'ERROR', |
|
163 |
'help': 'server\'s log level', |
|
164 |
'group': 'main', 'inputlevel': 1, |
|
165 |
}), |
|
166 |
# pyro name server |
|
167 |
('pyro-ns-host', |
|
168 |
{'type' : 'string', |
|
378
c0cd7398edff
revert local debug checkin
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
369
diff
changeset
|
169 |
'default': '', |
0 | 170 |
'help': 'Pyro name server\'s host. If not set, will be detected by a \ |
171 |
broadcast query', |
|
172 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
173 |
}), |
|
174 |
('pyro-ns-port', |
|
175 |
{'type' : 'int', |
|
176 |
'default': None, |
|
177 |
'help': 'Pyro name server\'s listening port. If not set, default \ |
|
178 |
port will be used.', |
|
179 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
180 |
}), |
|
181 |
('pyro-ns-group', |
|
182 |
{'type' : 'string', |
|
183 |
'default': 'cubicweb', |
|
184 |
'help': 'Pyro name server\'s group where the repository will be \ |
|
185 |
registered.', |
|
186 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
187 |
}), |
|
188 |
# common configuration options which are potentially required as soon as |
|
189 |
# you're using "base" application objects (ie to really server/web |
|
190 |
# specific) |
|
191 |
('base-url', |
|
192 |
{'type' : 'string', |
|
193 |
'default': None, |
|
194 |
'help': 'web server root url', |
|
195 |
'group': 'main', 'inputlevel': 1, |
|
196 |
}), |
|
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
197 |
('allow-email-login', |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
198 |
{'type' : 'yn', |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
199 |
'default': False, |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
200 |
'help': 'allow users to login with their primary email if set', |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
201 |
'group': 'main', 'inputlevel': 2, |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2221
diff
changeset
|
202 |
}), |
1520
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
203 |
('use-request-subdomain', |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
204 |
{'type' : 'yn', |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
205 |
'default': None, |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
206 |
'help': ('if set, base-url subdomain is replaced by the request\'s ' |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
207 |
'host, to help managing sites with several subdomains in a ' |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
208 |
'single cubicweb instance'), |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
209 |
'group': 'main', 'inputlevel': 1, |
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1446
diff
changeset
|
210 |
}), |
0 | 211 |
('mangle-emails', |
212 |
{'type' : 'yn', |
|
213 |
'default': False, |
|
214 |
'help': "don't display actual email addresses but mangle them if \ |
|
215 |
this option is set to yes", |
|
216 |
'group': 'email', 'inputlevel': 2, |
|
217 |
}), |
|
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
218 |
('disable-appobjects', |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
219 |
{'type' : 'csv', 'default': (), |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
220 |
'help': 'comma separated list of identifiers of application objects (<registry>.<oid>) to disable', |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
221 |
'group': 'appobjects', 'inputlevel': 2, |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
222 |
}), |
0 | 223 |
) |
224 |
# static and class methods used to get application independant resources ## |
|
1446 | 225 |
|
0 | 226 |
@staticmethod |
227 |
def cubicweb_version(): |
|
228 |
"""return installed cubicweb version""" |
|
229 |
from logilab.common.changelog import Version |
|
230 |
from cubicweb import __pkginfo__ |
|
231 |
version = __pkginfo__.numversion |
|
232 |
assert len(version) == 3, version |
|
233 |
return Version(version) |
|
1446 | 234 |
|
0 | 235 |
@staticmethod |
236 |
def persistent_options_configuration(): |
|
237 |
return Configuration(options=PERSISTENT_OPTIONS) |
|
238 |
||
239 |
@classmethod |
|
240 |
def shared_dir(cls): |
|
241 |
"""return the shared data directory (i.e. directory where standard |
|
242 |
library views and data may be found) |
|
243 |
""" |
|
244 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
|
245 |
return join(CW_SOFTWARE_ROOT, 'web') |
|
1039 | 246 |
return cls.cube_dir('shared') |
1446 | 247 |
|
0 | 248 |
@classmethod |
249 |
def i18n_lib_dir(cls): |
|
250 |
"""return application's i18n directory""" |
|
251 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
|
252 |
return join(CW_SOFTWARE_ROOT, 'i18n') |
|
253 |
return join(cls.shared_dir(), 'i18n') |
|
254 |
||
255 |
@classmethod |
|
256 |
def available_cubes(cls): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
257 |
cubes = set() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
258 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
259 |
for cube in os.listdir(directory): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
260 |
if isdir(join(directory, cube)) and not cube in ('CVS', '.svn', 'shared', '.hg'): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
261 |
cubes.add(cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
262 |
return sorted(cubes) |
1446 | 263 |
|
0 | 264 |
@classmethod |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
265 |
def cubes_search_path(cls): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
266 |
"""return the path of directories where cubes should be searched""" |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
267 |
path = [] |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
268 |
try: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
269 |
for directory in os.environ['CW_CUBES_PATH'].split(os.pathsep): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
270 |
directory = abspath(normpath(directory)) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
271 |
if exists(directory) and not directory in path: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
272 |
path.append(directory) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
273 |
except KeyError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
274 |
pass |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
275 |
if not cls.CUBES_DIR in path: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
276 |
path.append(cls.CUBES_DIR) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
277 |
return path |
1446 | 278 |
|
0 | 279 |
@classmethod |
280 |
def cube_dir(cls, cube): |
|
281 |
"""return the cube directory for the given cube id, |
|
282 |
raise ConfigurationError if it doesn't exists |
|
283 |
""" |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
284 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
285 |
cubedir = join(directory, cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
286 |
if exists(cubedir): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
287 |
return cubedir |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
288 |
raise ConfigurationError('no cube %s in %s' % (cube, cls.cubes_search_path())) |
0 | 289 |
|
290 |
@classmethod |
|
291 |
def cube_migration_scripts_dir(cls, cube): |
|
292 |
"""cube migration scripts directory""" |
|
293 |
return join(cls.cube_dir(cube), 'migration') |
|
1446 | 294 |
|
0 | 295 |
@classmethod |
296 |
def cube_pkginfo(cls, cube): |
|
297 |
"""return the information module for the given cube""" |
|
298 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
299 |
try: |
|
300 |
return getattr(__import__('cubes.%s.__pkginfo__' % cube), cube).__pkginfo__ |
|
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
301 |
except Exception, ex: |
0 | 302 |
raise ConfigurationError('unable to find packaging information for ' |
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
303 |
'cube %s (%s: %s)' % (cube, ex.__class__.__name__, ex)) |
0 | 304 |
|
305 |
@classmethod |
|
306 |
def cube_version(cls, cube): |
|
1446 | 307 |
"""return the version of the cube located in the given directory |
0 | 308 |
""" |
309 |
from logilab.common.changelog import Version |
|
310 |
version = cls.cube_pkginfo(cube).numversion |
|
311 |
assert len(version) == 3, version |
|
312 |
return Version(version) |
|
313 |
||
314 |
@classmethod |
|
315 |
def cube_dependencies(cls, cube): |
|
316 |
"""return cubicweb cubes used by the given cube""" |
|
317 |
return getattr(cls.cube_pkginfo(cube), '__use__', ()) |
|
318 |
||
319 |
@classmethod |
|
320 |
def cube_recommends(cls, cube): |
|
321 |
"""return cubicweb cubes recommended by the given cube""" |
|
322 |
return getattr(cls.cube_pkginfo(cube), '__recommend__', ()) |
|
323 |
||
324 |
@classmethod |
|
325 |
def expand_cubes(cls, cubes): |
|
326 |
"""expand the given list of top level cubes used by adding recursivly |
|
327 |
each cube dependencies |
|
328 |
""" |
|
329 |
cubes = list(cubes) |
|
330 |
todo = cubes[:] |
|
331 |
while todo: |
|
332 |
cube = todo.pop(0) |
|
333 |
for depcube in cls.cube_dependencies(cube): |
|
334 |
if depcube not in cubes: |
|
335 |
depcube = CW_MIGRATION_MAP.get(depcube, depcube) |
|
336 |
cubes.append(depcube) |
|
337 |
todo.append(depcube) |
|
338 |
return cubes |
|
339 |
||
340 |
@classmethod |
|
341 |
def reorder_cubes(cls, cubes): |
|
342 |
"""reorder cubes from the top level cubes to inner dependencies |
|
343 |
cubes |
|
344 |
""" |
|
345 |
from logilab.common.graph import get_cycles |
|
346 |
graph = {} |
|
347 |
for cube in cubes: |
|
348 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
349 |
deps = cls.cube_dependencies(cube) + \ |
|
350 |
cls.cube_recommends(cube) |
|
351 |
graph[cube] = set(dep for dep in deps if dep in cubes) |
|
352 |
cycles = get_cycles(graph) |
|
353 |
if cycles: |
|
354 |
cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles) |
|
355 |
raise ConfigurationError('cycles in cubes dependencies: %s' |
|
356 |
% cycles) |
|
357 |
cubes = [] |
|
358 |
while graph: |
|
359 |
# sorted to get predictable results |
|
360 |
for cube, deps in sorted(graph.items()): |
|
361 |
if not deps: |
|
362 |
cubes.append(cube) |
|
363 |
del graph[cube] |
|
364 |
for deps in graph.itervalues(): |
|
365 |
try: |
|
366 |
deps.remove(cube) |
|
367 |
except KeyError: |
|
368 |
continue |
|
369 |
return tuple(reversed(cubes)) |
|
1446 | 370 |
|
0 | 371 |
@classmethod |
372 |
def cls_adjust_sys_path(cls): |
|
373 |
"""update python path if necessary""" |
|
1023
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
374 |
cubes_parent_dir = normpath(join(cls.CUBES_DIR, '..')) |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
375 |
if not cubes_parent_dir in sys.path: |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
376 |
sys.path.insert(0, cubes_parent_dir) |
0 | 377 |
try: |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
378 |
import cubes |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
379 |
cubes.__path__ = cls.cubes_search_path() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
380 |
except ImportError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
381 |
return # cubes dir doesn't exists |
0 | 382 |
|
383 |
@classmethod |
|
384 |
def load_cwctl_plugins(cls): |
|
385 |
from logilab.common.modutils import load_module_from_file |
|
386 |
cls.cls_adjust_sys_path() |
|
387 |
for ctlfile in ('web/webctl.py', 'etwist/twctl.py', |
|
388 |
'server/serverctl.py', 'hercule.py', |
|
389 |
'devtools/devctl.py', 'goa/goactl.py'): |
|
390 |
if exists(join(CW_SOFTWARE_ROOT, ctlfile)): |
|
391 |
load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
|
392 |
cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
|
393 |
for cube in cls.available_cubes(): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
394 |
pluginfile = join(cls.cube_dir(cube), 'ecplugin.py') |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
395 |
initfile = join(cls.cube_dir(cube), '__init__.py') |
0 | 396 |
if exists(pluginfile): |
397 |
try: |
|
398 |
__import__('cubes.%s.ecplugin' % cube) |
|
399 |
cls.info('loaded cubicweb-ctl plugin from %s', cube) |
|
400 |
except: |
|
401 |
cls.exception('while loading plugin %s', pluginfile) |
|
402 |
elif exists(initfile): |
|
403 |
try: |
|
404 |
__import__('cubes.%s' % cube) |
|
405 |
except: |
|
406 |
cls.exception('while loading cube %s', cube) |
|
407 |
else: |
|
1446 | 408 |
cls.warning('no __init__ file in cube %s', cube) |
0 | 409 |
|
410 |
@classmethod |
|
411 |
def init_available_cubes(cls): |
|
412 |
"""cubes may register some sources (svnfile for instance) in their |
|
413 |
__init__ file, so they should be loaded early in the startup process |
|
414 |
""" |
|
415 |
for cube in cls.available_cubes(): |
|
416 |
try: |
|
417 |
__import__('cubes.%s' % cube) |
|
418 |
except Exception, ex: |
|
419 |
cls.warning("can't init cube %s: %s", cube, ex) |
|
1446 | 420 |
|
0 | 421 |
cubicweb_vobject_path = set(['entities']) |
422 |
cube_vobject_path = set(['entities']) |
|
423 |
||
424 |
@classmethod |
|
425 |
def build_vregistry_path(cls, templpath, evobjpath=None, tvobjpath=None): |
|
426 |
"""given a list of directories, return a list of sub files and |
|
427 |
directories that should be loaded by the application objects registry. |
|
428 |
||
429 |
:param evobjpath: |
|
430 |
optional list of sub-directories (or files without the .py ext) of |
|
431 |
the cubicweb library that should be tested and added to the output list |
|
432 |
if they exists. If not give, default to `cubicweb_vobject_path` class |
|
433 |
attribute. |
|
434 |
:param tvobjpath: |
|
435 |
optional list of sub-directories (or files without the .py ext) of |
|
436 |
directories given in `templpath` that should be tested and added to |
|
437 |
the output list if they exists. If not give, default to |
|
438 |
`cube_vobject_path` class attribute. |
|
439 |
""" |
|
440 |
vregpath = cls.build_vregistry_cubicweb_path(evobjpath) |
|
441 |
vregpath += cls.build_vregistry_cube_path(templpath, tvobjpath) |
|
442 |
return vregpath |
|
443 |
||
444 |
@classmethod |
|
445 |
def build_vregistry_cubicweb_path(cls, evobjpath=None): |
|
446 |
vregpath = [] |
|
447 |
if evobjpath is None: |
|
448 |
evobjpath = cls.cubicweb_vobject_path |
|
449 |
for subdir in evobjpath: |
|
450 |
path = join(CW_SOFTWARE_ROOT, subdir) |
|
451 |
if exists(path): |
|
452 |
vregpath.append(path) |
|
453 |
return vregpath |
|
454 |
||
455 |
@classmethod |
|
456 |
def build_vregistry_cube_path(cls, templpath, tvobjpath=None): |
|
457 |
vregpath = [] |
|
458 |
if tvobjpath is None: |
|
459 |
tvobjpath = cls.cube_vobject_path |
|
460 |
for directory in templpath: |
|
461 |
for subdir in tvobjpath: |
|
462 |
path = join(directory, subdir) |
|
463 |
if exists(path): |
|
464 |
vregpath.append(path) |
|
465 |
elif exists(path + '.py'): |
|
466 |
vregpath.append(path + '.py') |
|
467 |
return vregpath |
|
1446 | 468 |
|
0 | 469 |
def __init__(self): |
470 |
ConfigurationMixIn.__init__(self) |
|
471 |
self.adjust_sys_path() |
|
472 |
self.load_defaults() |
|
1446 | 473 |
self.translations = {} |
0 | 474 |
|
475 |
def adjust_sys_path(self): |
|
476 |
self.cls_adjust_sys_path() |
|
1446 | 477 |
|
478 |
def init_log(self, logthreshold=None, debug=False, |
|
0 | 479 |
logfile=None, syslog=False): |
480 |
"""init the log service""" |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
481 |
if logthreshold is None: |
0 | 482 |
if debug: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
483 |
logthreshold = 'DEBUG' |
0 | 484 |
else: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
485 |
logthreshold = self['log-threshold'] |
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
486 |
init_log(debug, syslog, logthreshold, logfile, self.log_format) |
0 | 487 |
# configure simpleTal logger |
488 |
logging.getLogger('simpleTAL').setLevel(logging.ERROR) |
|
489 |
||
490 |
def vregistry_path(self): |
|
491 |
"""return a list of files or directories where the registry will look |
|
492 |
for application objects. By default return nothing in NoApp config. |
|
493 |
""" |
|
494 |
return [] |
|
1446 | 495 |
|
0 | 496 |
def eproperty_definitions(self): |
497 |
cfg = self.persistent_options_configuration() |
|
498 |
for section, options in cfg.options_by_section(): |
|
499 |
section = section.lower() |
|
500 |
for optname, optdict, value in options: |
|
501 |
key = '%s.%s' % (section, optname) |
|
502 |
type, vocab = self.map_option(optdict) |
|
503 |
default = cfg.option_default(optname, optdict) |
|
504 |
pdef = {'type': type, 'vocabulary': vocab, 'default': default, |
|
505 |
'help': optdict['help'], |
|
506 |
'sitewide': optdict.get('sitewide', False)} |
|
507 |
yield key, pdef |
|
1446 | 508 |
|
0 | 509 |
def map_option(self, optdict): |
510 |
try: |
|
511 |
vocab = optdict['choices'] |
|
512 |
except KeyError: |
|
513 |
vocab = optdict.get('vocabulary') |
|
514 |
if isinstance(vocab, Method): |
|
515 |
vocab = getattr(self, vocab.method, ()) |
|
516 |
return CFGTYPE2ETYPE_MAP[optdict['type']], vocab |
|
517 |
||
1446 | 518 |
|
0 | 519 |
class CubicWebConfiguration(CubicWebNoAppConfiguration): |
520 |
"""base class for cubicweb server and web configurations""" |
|
1446 | 521 |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
522 |
INSTANCE_DATA_DIR = None |
0 | 523 |
if CubicWebNoAppConfiguration.mode == 'test': |
524 |
root = os.environ['APYCOT_ROOT'] |
|
525 |
REGISTRY_DIR = '%s/etc/cubicweb.d/' % root |
|
526 |
RUNTIME_DIR = '/tmp/' |
|
527 |
MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root |
|
528 |
if not exists(REGISTRY_DIR): |
|
529 |
os.makedirs(REGISTRY_DIR) |
|
530 |
elif CubicWebNoAppConfiguration.mode == 'dev': |
|
531 |
REGISTRY_DIR = expanduser('~/etc/cubicweb.d/') |
|
532 |
RUNTIME_DIR = '/tmp/' |
|
533 |
MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration') |
|
534 |
else: #mode = 'installed' |
|
535 |
REGISTRY_DIR = '/etc/cubicweb.d/' |
|
536 |
INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/' |
|
537 |
RUNTIME_DIR = '/var/run/cubicweb/' |
|
538 |
MIGRATION_DIR = '/usr/share/cubicweb/migration/' |
|
539 |
||
540 |
# for some commands (creation...) we don't want to initialize gettext |
|
541 |
set_language = True |
|
542 |
# set this to true to avoid false error message while creating an application |
|
543 |
creating = False |
|
1446 | 544 |
|
0 | 545 |
options = CubicWebNoAppConfiguration.options + ( |
546 |
('log-file', |
|
547 |
{'type' : 'string', |
|
548 |
'default': Method('default_log_file'), |
|
549 |
'help': 'file where output logs should be written', |
|
550 |
'group': 'main', 'inputlevel': 2, |
|
551 |
}), |
|
552 |
# email configuration |
|
553 |
('smtp-host', |
|
554 |
{'type' : 'string', |
|
555 |
'default': 'mail', |
|
556 |
'help': 'hostname of the SMTP mail server', |
|
557 |
'group': 'email', 'inputlevel': 1, |
|
558 |
}), |
|
559 |
('smtp-port', |
|
560 |
{'type' : 'int', |
|
561 |
'default': 25, |
|
562 |
'help': 'listening port of the SMTP mail server', |
|
563 |
'group': 'email', 'inputlevel': 1, |
|
564 |
}), |
|
565 |
('sender-name', |
|
566 |
{'type' : 'string', |
|
1446 | 567 |
'default': Method('default_application_id'), |
0 | 568 |
'help': 'name used as HELO name for outgoing emails from the \ |
569 |
repository.', |
|
570 |
'group': 'email', 'inputlevel': 2, |
|
571 |
}), |
|
572 |
('sender-addr', |
|
573 |
{'type' : 'string', |
|
574 |
'default': 'devel@logilab.fr', |
|
575 |
'help': 'email address used as HELO address for outgoing emails from \ |
|
576 |
the repository', |
|
577 |
'group': 'email', 'inputlevel': 1, |
|
578 |
}), |
|
579 |
) |
|
580 |
||
581 |
@classmethod |
|
582 |
def runtime_dir(cls): |
|
583 |
"""run time directory for pid file...""" |
|
584 |
return env_path('CW_RUNTIME', cls.RUNTIME_DIR, 'run time') |
|
1446 | 585 |
|
0 | 586 |
@classmethod |
587 |
def registry_dir(cls): |
|
588 |
"""return the control directory""" |
|
589 |
return env_path('CW_REGISTRY', cls.REGISTRY_DIR, 'registry') |
|
590 |
||
591 |
@classmethod |
|
592 |
def instance_data_dir(cls): |
|
593 |
"""return the instance data directory""" |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
594 |
return env_path('CW_INSTANCE_DATA', |
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
595 |
cls.INSTANCE_DATA_DIR or cls.REGISTRY_DIR, |
0 | 596 |
'additional data') |
1446 | 597 |
|
0 | 598 |
@classmethod |
599 |
def migration_scripts_dir(cls): |
|
600 |
"""cubicweb migration scripts directory""" |
|
601 |
return env_path('CW_MIGRATION', cls.MIGRATION_DIR, 'migration') |
|
602 |
||
603 |
@classmethod |
|
604 |
def config_for(cls, appid, config=None): |
|
605 |
"""return a configuration instance for the given application identifier |
|
606 |
""" |
|
607 |
config = config or guess_configuration(cls.application_home(appid)) |
|
608 |
configcls = configuration_cls(config) |
|
609 |
return configcls(appid) |
|
1446 | 610 |
|
0 | 611 |
@classmethod |
612 |
def possible_configurations(cls, appid): |
|
613 |
"""return the name of possible configurations for the given |
|
614 |
application id |
|
615 |
""" |
|
616 |
home = cls.application_home(appid) |
|
617 |
return possible_configurations(home) |
|
1446 | 618 |
|
0 | 619 |
@classmethod |
620 |
def application_home(cls, appid): |
|
621 |
"""return the home directory of the application with the given |
|
622 |
application id |
|
623 |
""" |
|
624 |
home = join(cls.registry_dir(), appid) |
|
625 |
if not exists(home): |
|
626 |
raise ConfigurationError('no such application %s (check it exists with "cubicweb-ctl list")' % appid) |
|
627 |
return home |
|
628 |
||
629 |
MODES = ('common', 'repository', 'Any', 'web') |
|
630 |
MCOMPAT = {'all-in-one': MODES, |
|
631 |
'repository': ('common', 'repository', 'Any'), |
|
632 |
'twisted' : ('common', 'web'),} |
|
633 |
@classmethod |
|
634 |
def accept_mode(cls, mode): |
|
635 |
#assert mode in cls.MODES, mode |
|
636 |
return mode in cls.MCOMPAT[cls.name] |
|
1446 | 637 |
|
0 | 638 |
# default configuration methods ########################################### |
1446 | 639 |
|
0 | 640 |
def default_application_id(self): |
641 |
"""return the application identifier, useful for option which need this |
|
642 |
as default value |
|
643 |
""" |
|
644 |
return self.appid |
|
645 |
||
646 |
def default_log_file(self): |
|
647 |
"""return default path to the log file of the application'server""" |
|
648 |
if self.mode == 'dev': |
|
649 |
basepath = '/tmp/%s-%s' % (basename(self.appid), self.name) |
|
650 |
path = basepath + '.log' |
|
651 |
i = 1 |
|
652 |
while exists(path) and i < 100: # arbitrary limit to avoid infinite loop |
|
653 |
try: |
|
654 |
file(path, 'a') |
|
655 |
break |
|
656 |
except IOError: |
|
657 |
path = '%s-%s.log' % (basepath, i) |
|
658 |
i += 1 |
|
659 |
return path |
|
660 |
return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name) |
|
1446 | 661 |
|
0 | 662 |
def default_pid_file(self): |
663 |
"""return default path to the pid file of the application'server""" |
|
664 |
return join(self.runtime_dir(), '%s-%s.pid' % (self.appid, self.name)) |
|
1446 | 665 |
|
0 | 666 |
# instance methods used to get application specific resources ############# |
1446 | 667 |
|
0 | 668 |
def __init__(self, appid): |
669 |
self.appid = appid |
|
670 |
CubicWebNoAppConfiguration.__init__(self) |
|
671 |
self._cubes = None |
|
672 |
self._site_loaded = set() |
|
673 |
self.load_file_configuration(self.main_config_file()) |
|
674 |
||
675 |
def adjust_sys_path(self): |
|
676 |
CubicWebNoAppConfiguration.adjust_sys_path(self) |
|
677 |
# adding apphome to python path is not usually necessary in production |
|
678 |
# environments, but necessary for tests |
|
679 |
if self.apphome and not self.apphome in sys.path: |
|
680 |
sys.path.insert(0, self.apphome) |
|
681 |
||
682 |
@property |
|
683 |
def apphome(self): |
|
684 |
return join(self.registry_dir(), self.appid) |
|
1446 | 685 |
|
0 | 686 |
@property |
687 |
def appdatahome(self): |
|
688 |
return join(self.instance_data_dir(), self.appid) |
|
1446 | 689 |
|
0 | 690 |
def init_cubes(self, cubes): |
1681
1586c0ed9a92
nicer assertion message
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1521
diff
changeset
|
691 |
assert self._cubes is None, self._cubes |
0 | 692 |
self._cubes = self.reorder_cubes(cubes) |
693 |
# load cubes'__init__.py file first |
|
694 |
for cube in cubes: |
|
695 |
__import__('cubes.%s' % cube) |
|
696 |
self.load_site_cubicweb() |
|
697 |
# reload config file in cases options are defined in cubes __init__ |
|
698 |
# or site_cubicweb files |
|
699 |
self.load_file_configuration(self.main_config_file()) |
|
700 |
# configuration initialization hook |
|
701 |
self.load_configuration() |
|
1446 | 702 |
|
0 | 703 |
def cubes(self): |
704 |
"""return the list of cubes used by this instance |
|
705 |
||
706 |
result is ordered from the top level cubes to inner dependencies |
|
707 |
cubes |
|
708 |
""" |
|
709 |
assert self._cubes is not None |
|
710 |
return self._cubes |
|
1446 | 711 |
|
0 | 712 |
def cubes_path(self): |
713 |
"""return the list of path to cubes used by this instance, from outer |
|
714 |
most to inner most cubes |
|
715 |
""" |
|
716 |
return [self.cube_dir(p) for p in self.cubes()] |
|
717 |
||
718 |
def add_cubes(self, cubes): |
|
719 |
"""add given cubes to the list of used cubes""" |
|
720 |
if not isinstance(cubes, list): |
|
721 |
cubes = list(cubes) |
|
722 |
self._cubes = self.reorder_cubes(list(self._cubes) + cubes) |
|
1446 | 723 |
|
0 | 724 |
def main_config_file(self): |
725 |
"""return application's control configuration file""" |
|
726 |
return join(self.apphome, '%s.conf' % self.name) |
|
1446 | 727 |
|
0 | 728 |
def save(self): |
729 |
"""write down current configuration""" |
|
730 |
self.generate_config(open(self.main_config_file(), 'w')) |
|
731 |
||
732 |
@cached |
|
733 |
def instance_md5_version(self): |
|
734 |
import md5 |
|
735 |
infos = [] |
|
736 |
for pkg in self.cubes(): |
|
737 |
version = self.cube_version(pkg) |
|
738 |
infos.append('%s-%s' % (pkg, version)) |
|
739 |
return md5.new(';'.join(infos)).hexdigest() |
|
1446 | 740 |
|
0 | 741 |
def load_site_cubicweb(self): |
742 |
"""load (web?) application's specific site_cubicweb file""" |
|
743 |
for path in reversed([self.apphome] + self.cubes_path()): |
|
744 |
sitefile = join(path, 'site_cubicweb.py') |
|
745 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
746 |
self._load_site_cubicweb(sitefile) |
|
747 |
self._site_loaded.add(sitefile) |
|
748 |
else: |
|
749 |
sitefile = join(path, 'site_erudi.py') |
|
750 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
751 |
self._load_site_cubicweb(sitefile) |
|
752 |
self._site_loaded.add(sitefile) |
|
753 |
self.warning('site_erudi.py is deprecated, should be renamed to site_cubicweb.py') |
|
1446 | 754 |
|
0 | 755 |
def _load_site_cubicweb(self, sitefile): |
2220
64aace08ae2f
add __file__ to context before loading site_cubicweb.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
756 |
context = {'__file__': sitefile} |
0 | 757 |
execfile(sitefile, context, context) |
758 |
self.info('%s loaded', sitefile) |
|
759 |
# cube specific options |
|
760 |
if context.get('options'): |
|
761 |
self.register_options(context['options']) |
|
762 |
self.load_defaults() |
|
1446 | 763 |
|
0 | 764 |
def load_configuration(self): |
765 |
"""load application's configuration files""" |
|
766 |
super(CubicWebConfiguration, self).load_configuration() |
|
767 |
if self.apphome and self.set_language: |
|
768 |
# init gettext |
|
769 |
self._set_language() |
|
1446 | 770 |
|
0 | 771 |
def init_log(self, logthreshold=None, debug=False, force=False): |
772 |
"""init the log service""" |
|
773 |
if not force and hasattr(self, '_logging_initialized'): |
|
774 |
return |
|
775 |
self._logging_initialized = True |
|
776 |
CubicWebNoAppConfiguration.init_log(self, logthreshold, debug, |
|
777 |
logfile=self.get('log-file')) |
|
778 |
# read a config file if it exists |
|
779 |
logconfig = join(self.apphome, 'logging.conf') |
|
780 |
if exists(logconfig): |
|
781 |
logging.fileConfig(logconfig) |
|
782 |
||
783 |
def available_languages(self, *args): |
|
784 |
"""return available translation for an application, by looking for |
|
785 |
compiled catalog |
|
786 |
||
787 |
take *args to be usable as a vocabulary method |
|
788 |
""" |
|
789 |
from glob import glob |
|
790 |
yield 'en' # ensure 'en' is yielded even if no .mo found |
|
791 |
for path in glob(join(self.apphome, 'i18n', |
|
792 |
'*', 'LC_MESSAGES', 'cubicweb.mo')): |
|
793 |
lang = path.split(os.sep)[-3] |
|
794 |
if lang != 'en': |
|
795 |
yield lang |
|
1446 | 796 |
|
0 | 797 |
def _set_language(self): |
798 |
"""set language for gettext""" |
|
799 |
from gettext import translation |
|
800 |
path = join(self.apphome, 'i18n') |
|
801 |
for language in self.available_languages(): |
|
802 |
self.info("loading language %s", language) |
|
803 |
try: |
|
804 |
tr = translation('cubicweb', path, languages=[language]) |
|
805 |
self.translations[language] = tr.ugettext |
|
806 |
except (ImportError, AttributeError, IOError): |
|
807 |
self.exception('localisation support error for language %s', |
|
1446 | 808 |
language) |
809 |
||
0 | 810 |
def vregistry_path(self): |
811 |
"""return a list of files or directories where the registry will look |
|
812 |
for application objects |
|
813 |
""" |
|
814 |
templpath = list(reversed(self.cubes_path())) |
|
815 |
if self.apphome: # may be unset in tests |
|
816 |
templpath.append(self.apphome) |
|
817 |
return self.build_vregistry_path(templpath) |
|
818 |
||
819 |
def set_sources_mode(self, sources): |
|
820 |
if not 'all' in sources: |
|
821 |
print 'warning: ignoring specified sources, requires a repository '\ |
|
822 |
'configuration' |
|
1446 | 823 |
|
0 | 824 |
def migration_handler(self): |
825 |
"""return a migration handler instance""" |
|
826 |
from cubicweb.common.migration import MigrationHelper |
|
827 |
return MigrationHelper(self, verbosity=self.verbosity) |
|
828 |
||
829 |
def i18ncompile(self, langs=None): |
|
830 |
from cubicweb.common import i18n |
|
831 |
if langs is None: |
|
832 |
langs = self.available_languages() |
|
833 |
i18ndir = join(self.apphome, 'i18n') |
|
834 |
if not exists(i18ndir): |
|
835 |
create_dir(i18ndir) |
|
836 |
sourcedirs = [join(path, 'i18n') for path in self.cubes_path()] |
|
837 |
sourcedirs.append(self.i18n_lib_dir()) |
|
838 |
return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs) |
|
839 |
||
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
840 |
def sendmails(self, msgs): |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
841 |
"""msgs: list of 2-uple (message object, recipients)""" |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
842 |
server, port = self['smtp-host'], self['smtp-port'] |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
843 |
SMTP_LOCK.acquire() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
844 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
845 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
846 |
smtp = SMTP(server, port) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
847 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
848 |
self.exception("can't connect to smtp server %s:%s (%s)", |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
849 |
server, port, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
850 |
return |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
851 |
heloaddr = '%s <%s>' % (self['sender-name'], self['sender-addr']) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
852 |
for msg, recipients in msgs: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
853 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
854 |
smtp.sendmail(heloaddr, recipients, msg.as_string()) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
855 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
856 |
self.exception("error sending mail to %s (%s)", |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
857 |
recipients, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
858 |
smtp.close() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
859 |
finally: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
860 |
SMTP_LOCK.release() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
861 |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
862 |
set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration')) |
1446 | 863 |
|
0 | 864 |
# alias to get a configuration instance from an application id |
1446 | 865 |
application_configuration = CubicWebConfiguration.config_for |
0 | 866 |