Package wtf :: Module init
[hide private]
[frames] | no frames]

Source Code for Module wtf.init

 1  # -*- coding: ascii -*- 
 2  # 
 3  # Copyright 2012 Andr\xe9 Malo or his licensors, as applicable 
 4  # 
 5  # Licensed under the Apache License, Version 2.0 (the "License"); 
 6  # you may not use this file except in compliance with the License. 
 7  # You may obtain a copy of the License at 
 8  # 
 9  #     http://www.apache.org/licenses/LICENSE-2.0 
10  # 
11  # Unless required by applicable law or agreed to in writing, software 
12  # distributed under the License is distributed on an "AS IS" BASIS, 
13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  # See the License for the specific language governing permissions and 
15  # limitations under the License. 
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 # pylint: disable = R0912, W0621 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
87 -def managed_app(configfile):
88 """ Create a managed application """ 89 from wtf import app as _app 90 return _app.factory(config(configfile), None, None)
91