1
2 u"""
3 :Copyright:
4
5 Copyright 2012 - 2014
6 Andr\xe9 Malo or his licensors, as applicable
7
8 :License:
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 ==================
23 Application Init
24 ==================
25
26 Meta config setup and managed applications helper.
27 """
28 __author__ = u"Andr\xe9 Malo"
29 __docformat__ = "restructuredtext en"
30
31 import os as _os
32 import sys as _sys
33
34
35 -def config(configfile, opts=None, dump=False):
36 """
37 Initialize the configuration and apply basic parameters
38
39 The basic parameters are:
40
41 - The python path
42 - sys.checkinterval
43 - max descriptors
44
45 :Parameters:
46 `configfile` : ``str``
47 Config file name (relative to the current working directory)
48
49 `opt`
50 Commandline options
51
52 `dump` : ``bool``
53 Dump config and exit? If true, the effective config is dumped and the
54 process exits with return code 0. Default: false
55
56 :Return: Effective config
57 :Rtype: ``
58 """
59
60
61 from wtf import config as _config
62
63 if configfile is None:
64 conf = _config.Config(
65 _os.path.normpath(_os.path.abspath(_os.getcwd()))
66 )
67 else:
68 conf = _config.load(configfile)
69
70 if dump:
71 _config.dump(conf)
72 _sys.exit(0)
73
74 if 'wtf' in conf and 'pythonpath' in conf.wtf:
75 _sys.path = list(conf.wtf.pythonpath) + _sys.path
76
77 if opts is not None and opts.checkinterval:
78 checkinterval = opts.checkinterval
79 elif 'wtf' in conf:
80 checkinterval = conf.wtf('checkinterval', 0)
81 else:
82 checkinterval = 0
83 if checkinterval:
84 _sys.setcheckinterval(checkinterval)
85
86 if opts is not None and opts.max_descriptors:
87 from wtf import cmdline as _cmdline
88 max_descriptors = opts.max_descriptors
89 exc = _cmdline.CommandlineError
90 elif 'wtf' in conf:
91 max_descriptors = max(-1, int(conf.wtf('max_descriptors', 0)))
92 exc = _config.ConfigurationError
93 else:
94 max_descriptors = 0
95 if max_descriptors:
96 try:
97 import resource as _resource
98 except ImportError:
99 raise exc(
100 "Cannot set max descriptors: resource module not available"
101 )
102 else:
103 try:
104 name = _resource.RLIMIT_NOFILE
105 except AttributeError:
106 try:
107 name = _resource.RLIMIT_OFILE
108 except AttributeError:
109 raise exc(
110 "Cannot set max descriptors: no rlimit constant found"
111 )
112 _resource.setrlimit(name, (max_descriptors, max_descriptors))
113
114 return conf
115
116
118 """
119 Create a managed application
120
121 :Parameters:
122 `configfile` : ``str``
123 Config file name
124
125 :Return: Application factory
126 :Rtype: callable
127 """
128 from wtf import app as _app
129
130 return _app.factory(config(configfile), None, None)
131