1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 Application Init
18 ================
19
20 Meta config setup and managed applications helper.
21 """
22 __author__ = u"Andr\xe9 Malo"
23 __docformat__ = "restructuredtext en"
24
25 import os as _os
26 import sys as _sys
27
28
29 -def config(configfile, opts=None, dump=False):
30 """ initialize the application """
31
32 from wtf import config as _config
33 if configfile is None:
34 config = _config.Config(
35 _os.path.normpath(_os.path.abspath(_os.getcwd()))
36 )
37 else:
38 config = _config.load(configfile)
39
40 if dump:
41 _config.dump(config)
42 _sys.exit(0)
43
44 if 'wtf' in config and 'pythonpath' in config.wtf:
45 _sys.path = list(config.wtf.pythonpath) + _sys.path
46
47 if opts is not None and opts.checkinterval:
48 checkinterval = opts.checkinterval
49 elif 'wtf' in config:
50 checkinterval = config.wtf('checkinterval', 0)
51 else:
52 checkinterval = 0
53 if checkinterval:
54 _sys.setcheckinterval(checkinterval)
55
56 if opts is not None and opts.max_descriptors:
57 from wtf import cmdline as _cmdline
58 max_descriptors = opts.max_descriptors
59 exc = _cmdline.CommandlineError
60 elif 'wtf' in config:
61 max_descriptors = max(-1, int(config.wtf('max_descriptors', 0)))
62 exc = _config.ConfigurationError
63 else:
64 max_descriptors = 0
65 if max_descriptors:
66 try:
67 import resource as _resource
68 except ImportError:
69 raise exc(
70 "Cannot set max descriptors: resource module not available"
71 )
72 else:
73 try:
74 name = _resource.RLIMIT_NOFILE
75 except AttributeError:
76 try:
77 name = _resource.RLIMIT_OFILE
78 except AttributeError:
79 raise exc(
80 "Cannot set max descriptors: no rlimit constant found"
81 )
82 _resource.setrlimit(name, (max_descriptors, max_descriptors))
83
84 return config
85
86
88 """ Create a managed application """
89 from wtf import app as _app
90 return _app.factory(config(configfile), None, None)
91