def event(): """ The program runs the entrance """ loop = asyncio.new_event_loop() threads = threading.Thread(target=start_loop, args=(loop, )) """ .setDaemon(True) ensure that the main thread withdraws from the child thread and exits. """ threads.setDaemon(True) threads.start() result = asyncio.run_coroutine_threadsafe(subintervel(loop), loop) asyncio.run_coroutine_threadsafe(controller(loop), loop) try: while result.result(): """ When the thread returns True, stop the current event loop, and then restart a cycle of events. """ loop.stop() logger.warning('正在重启,等待... ...') time.sleep(5) event() except KeyboardInterrupt: """ Guarantee the normal withdrawal of the event cycle. """ logger.warning('-----------Event loop will exit-------------------') loop.stop() sys.exit(0)
def func(self): # headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"} while True: url, savepath = self.taskqueue.get() block = 1 << 14 # 16k try: req = request.Request(url, headers=USER_AGENT) with request.urlopen(req, timeout=60) as response: with open(savepath, "wb") as f: for data in iter(partial(response.read, block), b""): f.write(data) except socket.timeout: logger.warning("下载超时:{}".format(url)) os.remove(savepath) self.count += 1 self.taskqueue.put((url, savepath)) continue except Exception as e: logger.error("出错:{}".format(e)) logger.error("出错url:{}".format(url)) self.count += 1 self.taskqueue.put((url, savepath)) finally: # response.close() self.taskqueue.task_done() self.count -= 1 logger.info("下载完成:{}".format(savepath)) logger.debug("当前队列任务数:{}".format(self.count))
def get_ms_corresponding_help(handler_input) -> str: """Returns corresponding help message depending on mode. TODO: - Add other help pointers & corresponding functions. - Add corresponding prompts, etc for other functions. """ attr = handler_input.attributes_manager.session_attributes help_pointer = attr.get('help_pointer', 'overview') logger.debug(f"help_pointer {help_pointer}") HELP_MS_POINTER_DICT = { 'overview': HelpUtils.get_ms_help_overview, 'user_profile': HelpUtils.get_ms_user_profile, 'act_descript': HelpUtils.get_ms_act_descript, 'free_play': HelpUtils.get_ms_free_play, 'fp_input': HelpUtils.get_ms_fp_table_input, 'custom_practice': HelpUtils.get_ms_custom_practice, 'survival_mode': HelpUtils.get_ms_survival_mode, 'speed_challenge': HelpUtils.get_ms_speed_challenge, } try: ms_help_func = HELP_MS_POINTER_DICT[help_pointer] except KeyError: logger.warning( f"get_ms_corresponding_help: KeyError {help_pointer}") ms_help_func = HelpUtils.get_ms_act_descript return ms_help_func()
def fetch_data(cmdline_again, keys): identifier = cmdline_again.strip('/') lhostmatch = identifier.find("aos://16777343") if lhostmatch != -1 and keys[0] == 'name': # If player plays on localhost then just pass this info and done return ['(Playing on localhost)', '-', '-', '-', '-', '-'] else: try: # Request json serverlist that buildandshoot hosts. request = urllib.request.urlopen("https://services.buildandshoot.com/serverlist.json", context=SSLCONTEXT) except urllib.error.URLError: logger.warning('No internet connection.') time.sleep(5) return data = request.read() encoding = request.info().get_content_charset('utf-8') serverlist = json.loads(data.decode(encoding)) try: for server_num in range(0, len(serverlist)+1): presence_info = [] if serverlist[server_num]['identifier'] == identifier: current_server = serverlist[server_num] for variable in keys: presence_info.append(current_server[variable]) if len(presence_info) == 6: return presence_info except IndexError: return ['(Server is not broadcasting to master server)', '-', '-', '-', '-', '-']
def create_backup_of_shortcuts(config, user, dry_run=False): def _create_directory_if_needed(directory): if os.path.exists(directory): return logger.debug("Creating directory: %s" % directory) os.makedirs(directory) backup_dir = backup_directory(config) if backup_dir is None: logger.info("No backups directory specified, so not backing up shortcuts.vdf before overwriting. See config.txt for more info") return _create_directory_if_needed(backup_dir) if not os.path.isdir(backup_dir): logger.warning("Backup directory path is something other than a directory. Skipping backups") return backup_path = shortcuts_backup_path(backup_dir, user) # Make sure the user-specific backups dir exists _create_directory_if_needed(os.path.dirname(backup_path)) shortcuts.write_shortcuts(backup_path, shortcuts.get_shortcuts(user))
def get_clause(tup_data) -> str: """Helper func: returns clause from tup_data using recursion.""" if not tup_data: return '' elif isinstance(tup_data, str): return tup_data elif isinstance(tup_data, (int, float)): return Pauser.get_p_level(tup_data) elif isinstance(tup_data, (tuple, list)): if isinstance(tup_data[0], str): ## List of strings, return choice. return random.choice(tup_data) else: # Use Tuples in tuple to create message speech_list = [] for clause_list in tup_data: clause = get_clause(clause_list) speech_list.append(clause) return str_joiner.join(speech_list) else: logger.warning(f"get_clause: Unrecognized data type {tup_data}")
def watch(self): """ return: callback function """ while True: for key, event_ in self._selector.select(): logger.debug("self._selector.select()") devfd = key.data hotkey = self._hotkey_seq_dict try: for e in devfd.events(): if e.matches(ev.evbit("EV_KEY")): logger.debug( f"key: {e.code.name} value: {e.value}") logger.debug(f"hotkey: {hotkey}") logger.debug("-" * 60) # 这个事件在hotkey seq if e.value == 1 and e.code in hotkey: hotkey = hotkey.get(e.code) if hasattr(hotkey, "__call__"): return hotkey elif e.value == 0 and e.code in hotkey: hotkey = self._hotkey_seq_dict except EventsDroppedException: logger.warning("EventsDroppedException") for e in devfd.sync(): logger.debug(e) logger.debug("从这里返回的????")
def post(self, request): role = request.user.role if role == 3: form = ModerationForm(request.POST) if form.is_valid(): text = clear_text(form.cleaned_data['text']) title = clear_text(form.cleaned_data['title']) user = request.user.id moderation = ModerationPost(title=title, text=text, moderation_status=user) moderation.save() logger.info( f'{request.user.username} created the post that was sent to moderation' ) return HttpResponseRedirect('/news') logger.warning(f'form by user {request.user.username} [NOT VALID]') return HttpResponse('[POST CREATE FORM NOT VALID]') elif role == 1 or 2: form = PostCreateForm(request.POST) if form.is_valid(): text = clear_text(form.cleaned_data['text']) title = clear_text(form.cleaned_data['title']) user = User.objects.get(id=request.user.id) post = Post(title=title, text=text, user=user) post.save() return HttpResponseRedirect('/news') return HttpResponse('[POST CREATE FORM NOT VALID]') return HttpResponse('[ERROR]')
def save_options(file_path): """ Saves all parameters set in a file. """ if os.path.exists(file_path): logger.warning("Parameter file will be overwritten") with open(file_path, "w") as writer: writer.write(f"Command: {' '.join(sys.argv)}\n\n") for entry in vars(args): writer.write(f"{entry}: {getattr(args, entry)}\n")
def incorrect_query_tables_parse(handler_input, query_tables: str, times_tables: list): """Controls attributes & logs when likely wrong parsing for query tables.""" attr = handler_input.attributes_manager.session_attributes attr['inform_query_tables_format'] = True logger.warning(f"query tables: {query_tables}") logger.warning(f"times tables: {times_tables}") return
def should_use_user_override(self, override): if override is None: return False if override == "": return False if not self.filesystem.path_exists(override): logger.warning("config.txt specifies a Steam userdata directory that doesn't exist. Ignoring.") return False return False
async def verify_ip_valid(session, proxy_item): try: async with session.get('https://api.ipify.org?format=json', proxy='http://{}'.format(str(proxy_item)), timeout=30) as resp: data = await resp.json() return proxy_item.ip == data['ip'] except (ServerTimeoutError, ClientProxyConnectionError, ClientOSError): return False except Exception as exc: logger.warning('请求时发生错误, {}:{}'.format(type(exc), exc))
def diffsha1(sha, filename): if path.exists(filename): fn_sha = sha1sum(filename) else: logger.warning("{} 不在 ...".format(filename)) return False if sha == fn_sha: return True else: return False
def should_use_user_override(self, override): if override is None: return False if override == "": return False if not self.filesystem.path_exists(override): logger.warning( "config.txt specifies a Steam userdata directory that doesn't exist. Ignoring." ) return False return False
def get_mode_stats(handler_input, mode: str) -> tuple: """Returns a tuple (int, int) of the mode statistics.""" attr = handler_input.attributes_manager.session_attributes mode_stats = attr.get('mode_stats', dict()) if mode not in mode_stats.keys(): logger.warning(f"get_mode_stats {mode} not found in keys.") return (0, 0) mode_correct, mode_incorrect = mode_stats[mode] return (int(mode_correct), int(mode_incorrect))
def get_data_list(_json): if not _json: return False, False if _json.get('code') == '3001': logger.warning('提取频繁请按照规定频率提取!') return False, False if _json.get('code') == '3006': logger.warning('提取数量用完!') mark_proxy_use_queue.put(1) return False, True if _json.get('code') != '0': return False, False _data_list = _json.get('msg') return _data_list, False
def post(self, request, id): """ Create comment """ form = CommentForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] user = request.user post = Post.objects.get(id=id) comment = Comment(text=text, user=user, post=post) comment.save() comment_notification.delay(post.user.email) return HttpResponseRedirect('/news') msg = f'Comment was not created' logger.warning(msg + f'by user {user.username}') return HttpResponse(msg)
def get_all_champions_links() -> Union[set, bool]: """ Get links to all champions to update the database """ try: database.connection() query = ChampionLink.select().dicts() champions = {champion['link_name'] for champion in query} return champions except Exception as e: logger.warning(f"DB!{__name__}: {e}") return False finally: database.close()
def get_champion_db(link_name: str) -> Union[Model, bool]: """ Get champion from database(cache). """ try: database.connect() champion = Champion.get(link_name=link_name) return champion except DoesNotExist: return False except Exception as e: logger.warning(f"DB!{__name__}: {e}") finally: database.close()
def create_new_player(handler_input, player_name: str) -> None: """Creates new player and adds it to the players_dict""" attr = handler_input.attributes_manager.session_attributes players_dict = attr.get('players_dict', {}) if player_name in players_dict.keys(): logger.warning("create_new_player: Player already found.") return new_player = Player(player_name) new_player_dict = PlayerEncoder.encode_player_to_dict(new_player) players_dict[player_name] = new_player_dict attr['players_dict'] = players_dict return
def process(self, handler_input, response): # type (HandlerInput, Response) if ( (handler_input.request_envelope.session.new == True) or is_request_type("SessionEndedRequest")(handler_input) or not response ): return try: prompt = response.reprompt.output_speech.ssml LastPrompt.save_last_prompt_str(handler_input, prompt) except Exception as e: logger.warning(f"CacheRepromptInterceptor {e}") return
def save_player_obj(handler_input, player_obj: object): """Saves player_object in players_dict""" attr = handler_input.attributes_manager.session_attributes players_dict = attr.get('players_dict', {}) player_name = player_obj.name if player_name == "": logger.warning("save_player_obj: Discarded empty player obj.") return player_dict_format = PlayerEncoder.encode_player_to_dict( player_obj) players_dict[ player_name] = player_dict_format attr['players_dict'] = players_dict return
def load_player_obj(handler_input) -> object: """Loads player object from players_dict""" attr = handler_input.attributes_manager.session_attributes players_dict = attr.get('players_dict', {}) current_player = attr.get('current_player', None) if not current_player: logger.warning("load_player_obj: No player object") return Player(name="") elif current_player not in players_dict.keys(): logger.warning("load_player_obj: Player not found.") return Player(name="") player_dict_format = players_dict[current_player] player_obj = PlayerEncoder.decode_dict_to_player(player_dict_format) return player_obj
def importTRLmotion3to2(filename, path=MOCAP_PATH): ''' Imports motion data from TerrainRLSim motion files. It assumes that the character type is the `biped3d.txt`. ''' file_path = os.path.join(path, filename) with open(file_path, 'r') as fp: data = json.load(fp) if 'Loop' in data and data['Loop'] is not True: logger.warning('This data was not defined to loop around') if 'Frames' not in data: raise ValueError( 'The file `%s` does not conform to the correct TRL MoCap format' % file_path) frames = np.array(data['Frames']) duration = frames[:-1, 0].sum() torso = frames[:, 1:4] torso[:, 2] += data.get('ZCompensation', 1.0) # torso[:, 1] += data.get('YCompensation', -0.76) torso[:, 1] = 0 # skipping the root and torso orientation (always assuming upright position for now) joints = frames[:, 12:] joints_2d = np.vstack([ _get_orientations_from_quaternion_seq(joints[:, :4]), joints[:, 4], _get_orientations_from_quaternion_seq(joints[:, 5:9]), _get_orientations_from_quaternion_seq(joints[:, 9:13]), joints[:, 13], _get_orientations_from_quaternion_seq(joints[:, 14:18]), ]).transpose() points = np.hstack([torso, joints_2d]) periodic = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=np.bool) print('error: %.4f' % ((points[-1, periodic] - points[0, periodic])**2).sum()) points[-1, periodic] = points[0, periodic] return RepeatingPath( duration=duration, # * 10 points=points, periodic=periodic, )
def deserialize(cls, dict_data: Dict) -> 'PageMark': """ transforms a dict into an objet. may trigger warnings if object does not have the correct format """ deserialized_page_mark = cls(serie_id=dict_data['serie_id']) warning_message_elem = [] if 'serie_name' not in dict_data: warning_message_elem.append('"serie_name" attribute is missing.') else: deserialized_page_mark.serie_name = dict_data['serie_name'] if 'latest_update' in dict_data: try: deserialized_page_mark.latest_update = dateutil.parser.parse( dict_data['latest_update']) except KeyError: pass except ValueError as e: warning_message_elem.append( f'"latest_update" attribute has unrecognized format. Parsing error {e}. ' f'Value was : {dict_data.get("latest_update")}') chapter_marks = list() for index_position, mark in enumerate( dict_data.get('chapter_marks', [])): try: chapter = Chapter.deserialize(mark) if not chapter.is_valid(): warning_message_elem.append( f'chapter_mark" attribute is invalid at position. {index_position},' f' with key values "{str(mark)}"') chapter_marks.append(chapter) except TypeError: warning_message_elem.append( f' chapter_mark" attribute is invalid at position. {index_position},' f' with key values "{str(mark)}"') deserialized_page_mark.chapter_marks = sorted(chapter_marks, reverse=True) if warning_message_elem: warning_message = f'Corrupted PageMark document for serie id {deserialized_page_mark.serie_id}, ' \ f'and serie name {dict_data.get("serie_name", "")} ' + '\n'.join(warning_message_elem) logger.warning(warning_message) return deserialized_page_mark
def parse2dict(self, current_user, html): if not html: return {} """ xpath //script/[@id="js-initialData"] """ # 数据清洗 soup = BeautifulSoup(html, "html.parser") html_data = soup.find("script", {"id": "js-initialData"}) if html_data is None: logger.warning("html data is None!") return {} json_data = json.loads(html_data.text) users = json_data["initialState"]["entities"]["users"] users.pop(current_user, None) return users
def test_payment_list(self,a): # 银行端退款查询接口 new_headers = {"Connection": "keep-alive", "Referer": "https://gray3.ubank365.com/anhui/admin/bank/index.html", "Content-Type": "application/json", "userId": str(tokens_id), "token": tokens } url = "https://gray3.ubank365.com/api/bank/refund/bossPayment/list" res = common.requests_common.post_http(url=url, headers=new_headers, payload=a) try: self.assertIn("'message': '操作成功'",str(res)) logger.info("退款查询接口,测试成功!") except AssertionError: logger.warning("测试失败!") raise
def process(self, handler_input, response): # type (HandlerInput, Response) -> None ## can probably move this to a utility function for other interceptors too if ((handler_input.request_envelope.session.new == True) or (is_request_type("SessionEndedRequest")(handler_input)) or not response): return logger.debug("INTER: CacheResponseForRepeatInterceptor") try: handler_input.attributes_manager.session_attributes[ 'recent_response'] = response except Exception as e: logger.warning(f"""INTER: CacheResponseForRepeatInterceptor Could not cache repeated response: {e}""") pass return
def test_fundOrderId(self): #查看退款进度详情 new_headers = {"Connection": "keep-alive", "Referer": "https://gray3.ubank365.com/anhui/admin/bank/index.html", "Content-Type": "application/json", "userId": str(tokens_id), "token": tokens } url = "https://gray3.ubank365.com/api/bank/refund/auditDetails" x = {"refundOrderId":"9FEB7AD48000"} res = common.requests_common.get_http(url=url, params= x, headers=new_headers) try: self.assertIn("'reason': '商品破损已拒签'",str(res)) logger.info("退款进度详情,测试成功!") except AssertionError: logger.warning("测试失败") raise
def get_resolved_value(handler_input, slot_name: str) -> str: """Returns resolved value for the slot.""" try: slot = handler_input.request_envelope.request.intent.slots if not slot or not slot.get(slot_name, None): logger.warning(f"get_resolved_value: {slot_name} not found.") return None slot = slot[slot_name] if ( str(slot.resolutions.resolutions_per_authority[0].status.code) != "StatusCode.ER_SUCCESS_MATCH"): logger.warning(f"get_resolved_value: {slot_name} not successfully matched.") return None slot_value = slot.resolutions.resolutions_per_authority[0].values[0].value.name except AttributeError: return None return slot_value
def make_ms_pause_level_list(*args) -> list: """Returns list of the arguments to be added to speech_list. Transforms all int/float args into p_levels then adds to the list. NOTE: This function is useful to preserve list format so the str.join method only has to operate once. """ logger.debug(f"make_ms_pause_level_list: args {args}") speech_list = [] if not (args): return [] for arg in (args): if isinstance(arg, str): speech_list.append(arg) elif isinstance(arg, (float, int)): speech_list.append(Pauser.get_p_level(arg)) else: logger.warning( f"make_ms_pause_level_list: Unrecognized argument {arg}") return speech_list