def select(self): """ 选择菜单 :return: """ current_level,current_menu = self.display() exit_flag = False while not exit_flag: _input = input('输入选择的编码:\n>>') if _input == '0': log.info('退出菜单' ) if len(self.menu_list) == 1: exit_flag = True else: self.menu_list.pop() #删除一级目录 return self.select() #递归调用 elif _input in current_level : log.info('选择菜单:%s'% current_level[_input]) if isinstance(current_menu[current_level[_input]],str): #判断value是否是字符串 是则到底 返回 return current_menu[current_level[_input]] else: self.menu_list.append(current_menu[current_level[_input]]) #添加当前级别目录 return self.select() #递归调用 选择当前级别目录 else: print("""您输入编号不存在。请输入正确的编号!~\n""") log.warn('输入编号错误 %s' %_input) return self.select()
def get_application(api_module_name): try: api_module = importlib.import_module(api_module_name) except ImportError, e: #this is only import error, not necessary to log the trace log.exception(e) log.warn('module {0} is not found'.format(api_module_name)) return None
def process(child_run): while True: pid = os.fork() if pid == 0: # child atexit.register(log.info, 0, 'process exit') signal.signal(signal.SIGTERM, lambda sig, stack: exit(0)) child_run() return pid, status = os.waitpid(pid, 0) log.warn(0, 'child:%d exit with code: %d', pid, status) if status != 0: time.sleep(1) log.info(0, 'reboot process')
def close_socket(s): if not isinstance(s, socket.socket): log.warn('it is not socket') return try: s.shutdown(socket.SHUT_RDWR) log.trace(0, 'socket close: %d', s.fileno()) return except Exception as exc: log.error(exc, 'socket.shutdown(%d) fail', s.fileno()) try: s.close() return except Exception as exc: log.error(exc, 'socket.close(%d) fail', s.fileno())
def __wrap_other_function(self, mo): try: mo_name = mo.__name__ self_mod_name = self.mod.__name__ if mo_name == self_mod_name: return for kk, vv in mo.__dict__.items(): if kk.startswith("__"): # or not inspect.ismodule(vv) continue full_func_name = "%s.%s" % (mo_name, kk) if inspect.ismodule( vv) and vv is self.mod: # inspect.ismodule(vv) and self.module_func_origin[full_func_name] = vv setattr(mo, kk, self.mod) self.add_wrapped_func('__mod_match__', full_func_name, vv) log.info("other code profile for import as %s at %s", kk, mo_name) elif inspect.isfunction(vv): vv_mo_name = vv.__module__ matched_patt = self.does_match_function_aspect(vv_mo_name + '.' + kk) if not matched_patt: continue # self.module_func_origin[full_func_name] = vv setattr(mo, kk, profile_aspect()(vv)) self.add_wrapped_func(matched_patt, full_func_name, vv) log.info("other code profile for from import %s at %s", full_func_name, mo_name) except: all_module_names.remove(mo.__name__) log.warn("%s error: %s", mo.__name__, utils.current_exception_info())
def wrap_method(self): if not does_profile_switch_on() or not does_local_ip_matched(): log.info( 'code_profile_switch is False or does_local_ip_matched is False' ) return log.info('start profile method for %s.%s pattern is %s', self.mod.__name__, self.clazz_name, self.method_pattern) self.__wrap_method_internal() # aspect functions that are imported classes and objects by from .. import imported_modules = [] for na in all_module_names.copy(): if na == self.mod.__name__ or get_module(na) is None: continue mo = get_module(na) imported_modules.append(na) # log.info("search imported clazz %s", na) for kk, vv in mo.__dict__.items(): try: if kk.startswith("__") or inspect.ismodule(vv): continue if inspect.isclass(vv) and vv in self.aspect_clazzs: self.__wrap_other_clazz_method(kk, vv, mo) else: self.__wrap_other_object_method(kk, vv) except: all_module_names.remove(na) log.warn("%s error: %s", na, utils.current_exception_info()) log.info("search imported module %s", str(imported_modules)) log.info('end profile method for %s.%s pattern is %s', self.mod.__name__, self.clazz_name, self.method_pattern)
def save(filename, output_dir, content): final_content = "" if isinstance(content, list): if len(content) == 1: final_content = content[0] else: i = 0 for part in content: i = i + 1 final_content = final_content + "\n" + str(i) + "*" * 100 + "\n" + part else: final_content = content file = join_char(output_dir, filename, "/") if not os.path.exists(os.path.dirname(file)): try: os.makedirs(os.path.dirname(file)) except OSError as exc: if exc.errno != errno.EEXIST: raise pass try: with open(file, "w") as f: f.write(final_content) info("[+] Saved to: " + file, bold=True) except IsADirectoryError: file = join_char(file, "index", "/") with open(file, "w") as f: f.write(final_content) except PermissionError: warn(f"Permission denied saving {file}")
def on_timeout(self): c = self.conn log.warn(0, '*%d has timeout', c.index) self.close()
def get_application(api_module_name): try: api_module = importlib.import_module(api_module_name) except ImportError, e: #this is only import error, not necessary to log the trace log.exception(e) log.warn('module {0} is not found'.format(api_module_name)) return None for attr_name in dir(api_module): app = getattr(api_module, attr_name) if isinstance(app, Application): log.info('{0} found in {1}'.format(attr_name, api_module_name)) app.module_name = api_module_name return app log.warn('no application found in {0}'.format(api_module_name)) return None def init_app(app, app_type='api'): if app is None: return None if app_type == 'api': app.isapi = True default_error_handler = handler.api_default_error_handler error_handlers = handler.api_error_handlers else: app.isapi = False default_error_handler = handler.page_default_error_handler error_handlers = handler.page_error_handlers if default_error_handler is not None and not hasattr(
def on_timeout(self): c0 = self.c0 c1 = self.c1 log.warn(0, 'bridge timeout *%d(%s) *%d(%s)', c0.index, c0.addr.text, c1.index, c1.addr.text) self.close()
def on_timeout(self): c = self.conn_proxy log.warn(0, '*%d timeout', c.index) self.close()
def get_application(api_module_name): try: api_module = importlib.import_module(api_module_name) except ImportError, e: #this is only import error, not necessary to log the trace log.exception(e) log.warn('module {0} is not found'.format(api_module_name)) return None for attr_name in dir(api_module): app = getattr(api_module, attr_name) if isinstance(app, Application): log.info('{0} found in {1}'.format(attr_name, api_module_name)) app.module_name = api_module_name return app log.warn('no application found in {0}'.format(api_module_name)) return None def init_app(app, app_type='api'): if app is None: return None if app_type == 'api': app.isapi = True default_error_handler = handler.api_default_error_handler error_handlers = handler.api_error_handlers else: app.isapi = False default_error_handler = handler.page_default_error_handler error_handlers = handler.page_error_handlers if default_error_handler is not None and not hasattr(app, '_default_error_handler'):
def on_rtimer(self): log.warn(0, 'connect has close') self.close()