def set_deform_translation(self, path=None, production_path=None, base_static_path=None): def translator(term): return get_localizer(get_current_request()).translate(term) deform = _import_module('deform') path = self.is_production_environ and production_path or path if path: deform_template_dir = resource_filename(*path.split(':', 1)) zpt_renderer = deform.ZPTRendererFactory( [deform_template_dir], translator=translator) deform.Form.set_default_renderer(zpt_renderer) if base_static_path: if not base_static_path.endswith('/'): base_static_path += '/' for versions in deform.widget.default_resources.values(): for resources in versions.values(): for resource_type, resource in resources.items(): new_resources = [ r.replace('deform:static/', base_static_path, 1) for r in maybe_list(resource)] if not is_nonstr_iter(resource): resources[resource_type] = new_resources[0] else: resources[resource_type] = tuple(new_resources) self.add_translation_dirs('deform:locale') if not base_static_path: self.add_static_view('deform', 'deform:static')
def get_handler_object(req_spec, resp_spec, **kw): handler_spec = HANDLERS[resp_spec['type']] if not resp_spec.get('handler'): with handler_lock: if not resp_spec.get('handler'): module = six._import_module(handler_spec['module']) cls = getattr(module, handler_spec['cls']) resp_spec['handler'] = cls.from_resp_spec( req_spec, resp_spec, **kw) return resp_spec['handler']
def load_mapping(pattern, func_str): global _mappings try: mod, func_name = func_str.rsplit('.', 1) # mod = __import__(mod, None, None, ['']) mod = six._import_module(mod) func = getattr(mod, func_name) _mappings.append(r"^%s\Z" % pattern) _mappings.append(func) except Exception as e: xutils.print_stacktrace()
def __init__(self, *args, **kwargs): super(BaseMailerSessionManager, self).__init__(*args, **kwargs) pyramid_mailer = _import_module('pyramid_mailer') self.mailer = pyramid_mailer.mailer_factory_from_settings(self.settings, prefix='') pyramid_mailer_message = _import_module('pyramid_mailer.message') self.message_cls = pyramid_mailer_message.Message self.attachment_cls = pyramid_mailer_message.Attachment if self.settings.get('queue_path'): make_dir(self.settings['queue_path']) make_dir(join_paths(self.settings['queue_path'], 'cur')) make_dir(join_paths(self.settings['queue_path'], 'tmp')) make_dir(join_paths(self.settings['queue_path'], 'new')) sendmail_queue = _import_module('repoze.sendmail.queue') self.queue_processor = sendmail_queue.QueueProcessor self.transaction = _import_module('transaction') self.__dict__.setdefault('__middlewares__', []).append(RepozeTMMiddleware)
def add_rule(pattern, func_str): global _rules try: mod, func_name = func_str.rsplit('.', 1) # mod = __import__(mod, None, None, ['']) mod = six._import_module("handlers.search." + mod) func = getattr(mod, func_name) func.modfunc = func_str rule = BaseRule(r"^%s\Z" % pattern, func) _rules.append(rule) except Exception as e: xutils.print_exc()
def __init__(self, *args, **kwargs): super(BaseJobsManager, self).__init__(*args, **kwargs) self.save_reports = asbool(self.settings.get('save_reports', True)) self.server_domain_name = self.settings.get('server_domain_name') self.active = bool( not self.server_domain_name or self.server_domain_name == DOMAIN_NAME) self.domain_names = set(self.settings.get('domain_names', '')) self.domain_names.add(DOMAIN_NAME) try: self.transaction = _import_module('transaction') except ImportError: self.transaction = None if self.active: temporary_dir = gettempdir() domain_start_filename = 'jobs domain %s started' % DOMAIN_NAME domain_start_file_path = join_paths(temporary_dir, domain_start_filename) lock_key = 'jobs monitor start check' self.config.cache.lock(lock_key, timeout=10) try: start_thread = not isfile(domain_start_file_path) if not start_thread: try: with open(domain_start_file_path, 'r') as f: process_id = int(f.read()) except (IOError, ValueError): start_thread = True else: try: getpgid(process_id) except OSError as error: if error.errno is errno.ESRCH: start_thread = True else: raise if start_thread: with open(domain_start_file_path, 'w') as f: f.write(str(PROCESS_ID)) finally: self.config.cache.unlock(lock_key) # Start only one Thread for each domain if start_thread: start_system_thread('jobs_monitor', self.run_monitor) print_('Running jobs monitor on PID %s' % PROCESS_ID)
def __init__(self, *args, **kwargs): super(BaseSQLSessionManager, self).__init__(*args, **kwargs) self.transaction = _import_module('transaction') session_extension = self.settings.get('session_extension') if session_extension is not None: session_extension = get_object_on_path(session_extension) self.db_session = initialize_sql( self.__database_name__, self.settings['sql_path'], encoding=self.settings.get('encoding', 'utf8'), mysql_engine=self.settings.get('mysql_engine') or 'InnoDB', session_extension=session_extension, debug=asbool(self.settings.get('debug', False)))
def __init__( self, url, expire=None, **settings): # Lock settings lock_settings = {} for key in list(settings.keys()): if key.startswith('lock_'): lock_settings[key.split('lock_', 1)[1]] = settings.pop(key) lock_settings.update(settings) self.memcache_module = _import_module('memcache') self.memcache = self.memcache_module.Client(url.split(';'), **settings) self.expire = maybe_integer(expire) self.lockme = LockMeMemcached(url, **lock_settings)
def __init__(self, *args, **kwargs): super(BaseStorageSessionManager, self).__init__(*args, **kwargs) make_dir(self.settings['folder_path']) if issubclass(self.session, BaseStorageWithImageSession): self.image_cls = _import_module('PIL.Image') self.resize_quality = self.image_cls.ANTIALIAS self.resizes = {} for key, value in self.settings.items(): if key.startswith('resize.'): blocks = key.split('.', 3) if len(blocks) == 4: resize_type, application_code, name = blocks[1:] if resize_type in ('width', 'height'): self.resizes.setdefault(application_code, {}).setdefault(name, {})[resize_type] = int(value) self.tinypng_api = self.settings.get('tinypng_api') self.tinypng_locked_months = []
def load_model_dir(self, parent=xconfig.HANDLERS_DIR): dirname = parent.replace(".", "/") if not os.path.exists(dirname): return for filename in os.listdir(dirname): try: filepath = os.path.join(dirname, filename) if os.path.isdir(filepath): self.load_model_dir(parent + "." + filename) continue name, ext = os.path.splitext(filename) if os.path.isfile(filepath) and ext == ".py": modname = parent + "." + name old_mod = sys.modules.get(modname) if old_mod is not None: if hasattr(old_mod, "unload"): old_mod.unload() if self.report_unload: log("del %s" % modname) del sys.modules[modname] # reload module # Py3: __import__(name, globals=None, locals=None, fromlist=(), level=0) # Py2: __import__(name, globals={}, locals={}, fromlist=[], level=-1) # fromlist不为空(任意真值*-*)可以得到子模块,比如__import__("os.path", fromlist=1)返回<module "ntpath" ...> # 参考Python源码import.c即可 # <code>has_from = PyObject_IsTrue(fromlist);</code>实际上是个Bool值 # level=0表示绝对路径,-1是默认的 # mod = __import__(modname, fromlist=1, level=0) # six的这种方式也不错 mod = six._import_module(modname) self.load_model(mod, modname) except Exception as e: self.failed_mods.append([filepath, e]) log("Fail to load module '%s'" % filepath) log("Model traceback (most recent call last):") xutils.print_exc() self.report_failed()
def test_import_module(): from logging import handlers m = six._import_module("logging.handlers") assert m is handlers
count += 1 print_('Process %s stopped!' % process_id) def register_thread(name, thread): # Clean up function clean_dead_threads() process_id = getpid() if name in ALIVE_THREADS[process_id]: raise KeyError('Thread "%s" already started' % name) else: ALIVE_THREADS[process_id][name] = thread # Register on python default atexit.register(exit_system) # Register uwsgi if exists try: uwsgi = _import_module('uwsgi') except ImportError: pass else: def after_fork(): PROCESS_RUNNING.add(getpid()) uwsgi.atexit = exit_system uwsgi.post_fork_hook = after_fork