Best Python code snippet using localstack_python
brushtask.py
Source:brushtask.py
...223 avg_upspeed = int(uploaded / dltime)224 # ä¸è½½é225 downloaded = torrent.get("downloaded")226 total_downloaded += downloaded227 if self.__check_remove_rule(remove_rule=remove_rule,228 seeding_time=seeding_time,229 ratio=ratio,230 uploaded=uploaded,231 avg_upspeed=avg_upspeed):232 log.info("ãBRUSHã%s åç§è¾¾å°å ç§æ¡ä»¶ï¼å é¤ä¸è½½ä»»å¡..." % torrent.get('name'))233 if torrent_id not in delete_ids:234 delete_ids.append(torrent_id)235 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))236 # æ£æ¥ä¸è½½ä¸ç¶æç237 torrents = downloader.get_torrents(ids=torrent_ids, status=["downloading"])238 for torrent in torrents:239 # ID240 torrent_id = torrent.get("hash")241 # ä¸è½½èæ¶ ç§242 dltime = int(time.time() - torrent.get("added_on"))243 # ä¸ä¼ é Byte244 uploaded = torrent.get("uploaded") or 0245 total_uploaded += uploaded246 # å¹³åä¸ä¼ é度 Byte/s247 avg_upspeed = int(uploaded / dltime)248 # ä¸è½½é249 downloaded = torrent.get("downloaded")250 total_downloaded += downloaded251 if self.__check_remove_rule(remove_rule=remove_rule,252 dltime=dltime,253 avg_upspeed=avg_upspeed):254 log.info("ãBRUSHã%s ä¸è½½èæ¶è¾¾å°å ç§æ¡ä»¶ï¼å é¤ä¸è½½ä»»å¡..." % torrent.get('name'))255 if torrent_id not in delete_ids:256 delete_ids.append(torrent_id)257 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))258 # transmission259 else:260 # æ£æ¥å®æç¶æ261 downloader = Transmission(user_config=downloader_cfg)262 torrents = downloader.get_torrents(ids=torrent_ids, status=["seeding", "seed_pending"])263 for torrent in torrents:264 # ID265 torrent_id = torrent.id266 # åç§æ¶é´267 date_done = torrent.date_done if torrent.date_done else torrent.date_added268 dltime = (datetime.now().astimezone() - torrent.date_added).seconds269 seeding_time = (datetime.now().astimezone() - date_done).seconds270 # ä¸è½½é271 downloaded = int(torrent.total_size * torrent.progress / 100)272 total_downloaded += downloaded273 # å享ç274 ratio = torrent.ratio or 0275 # ä¸ä¼ é276 uploaded = int(downloaded * torrent.ratio)277 total_uploaded += uploaded278 # å¹³åä¸ä¼ é度279 avg_upspeed = int(uploaded / dltime)280 if self.__check_remove_rule(remove_rule=remove_rule,281 seeding_time=seeding_time,282 ratio=ratio,283 uploaded=uploaded,284 avg_upspeed=avg_upspeed):285 log.info("ãBRUSHã%s åç§è¾¾å°å ç§æ¡ä»¶ï¼å é¤ä¸è½½ä»»å¡..." % torrent.name)286 if torrent_id not in delete_ids:287 delete_ids.append(torrent_id)288 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))289 # æ£æ¥ä¸è½½ç¶æ290 torrents = downloader.get_torrents(ids=torrent_ids,291 status=["downloading", "download_pending", "stopped"])292 for torrent in torrents:293 # ID294 torrent_id = torrent.id295 # ä¸è½½èæ¶296 dltime = (datetime.now().astimezone() - torrent.date_added).seconds297 # ä¸è½½é298 downloaded = int(torrent.total_size * torrent.progress / 100)299 total_downloaded += downloaded300 # ä¸ä¼ é301 uploaded = int(downloaded * torrent.ratio)302 total_uploaded += uploaded303 # å¹³åä¸ä¼ é度304 avg_upspeed = int(uploaded / dltime)305 if self.__check_remove_rule(remove_rule=remove_rule,306 dltime=dltime,307 avg_upspeed=avg_upspeed):308 log.info("ãBRUSHã%s ä¸è½½èæ¶è¾¾å°å ç§æ¡ä»¶ï¼å é¤ä¸è½½ä»»å¡..." % torrent.name)309 if torrent_id not in delete_ids:310 delete_ids.append(torrent_id)311 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))312 # æ´æ°ä¸ä¼ ä¸è½½éåå é¤ç§åæ°313 add_brushtask_upload_count(brush_id=taskid,314 upload_size=total_uploaded,315 download_size=total_downloaded,316 remove_count=len(delete_ids))317 # æ´æ°ç§åç¶æ为已å é¤318 update_brushtask_torrent_state(update_torrents)319 # å é¤ç§å320 if delete_ids:321 downloader.delete_torrents(delete_file=True, ids=delete_ids)322 log.info("ãBRUSHãä»»å¡ %s å
±å é¤ %s 个å·æµä¸è½½ä»»å¡" % (task_name, len(delete_ids)))323 except Exception as e:324 log.console(str(e) + " - " + traceback.format_exc())325 def __is_allow_new_torrent(self, taskid, taskname, downloadercfg, seedsize, dlcount):326 """327 æ£æ¥æ¯å¦è¿è½æ·»å æ°çä¸è½½328 """329 if not taskid:330 return False331 # å¤æ大å°332 total_size = get_brushtask_totalsize(taskid)333 if seedsize:334 if int(seedsize) * 1024 ** 3 <= int(total_size):335 log.warn("ãBRUSHãå·æµä»»å¡ %s 已达å°ä¿ç§ä½ç§¯ %sGBï¼ä¸åæ°å¢ä¸è½½" % (taskname, seedsize))336 return False337 # æ£æ¥æ£å¨ä¸è½½çä»»å¡æ°338 if dlcount:339 downloading_count = self.__get_downloading_count(downloadercfg)340 if downloading_count is None:341 log.error("ãBRUSHãä»»å¡ %s ä¸è½½å¨ %s æ æ³è¿æ¥" % (taskname, downloadercfg.get("name")))342 return False343 if int(downloading_count) > int(dlcount):344 log.warn("ãBRUSHãä¸è½½å¨ %s æ£å¨ä¸è½½ä»»å¡æ°ï¼%sï¼è¶
è¿è®¾å®ä¸éï¼æä¸æ·»å ä¸è½½" % (downloadercfg.get("name"), downloading_count))345 return False346 return True347 @staticmethod348 def __get_downloader_config(dlid):349 """350 è·åä¸è½½å¨çåæ°351 """352 if not dlid:353 return None354 downloader_info = get_user_downloaders(dlid)355 if downloader_info:356 userconfig = {"id": downloader_info[0][0],357 "name": downloader_info[0][1],358 "type": downloader_info[0][2],359 "host": downloader_info[0][3],360 "port": downloader_info[0][4],361 "username": downloader_info[0][5],362 "password": downloader_info[0][6],363 "save_dir": downloader_info[0][7]}364 return userconfig365 return None366 def __get_downloading_count(self, downloadercfg):367 """368 æ¥è¯¢å½åæ£å¨ä¸è½½çä»»å¡æ°369 """370 if not downloadercfg:371 return 0372 if downloadercfg.get("type") == self._qb_client:373 downloader = Qbittorrent(user_config=downloadercfg)374 if not downloader.qbc:375 return None376 dlitems = downloader.get_downloading_torrents()377 if dlitems is not None:378 return int(len(dlitems))379 else:380 downloader = Transmission(user_config=downloadercfg)381 if not downloader.trc:382 return None383 dlitems = downloader.get_downloading_torrents()384 if dlitems is not None:385 return int(len(dlitems))386 return None387 def __download_torrent(self, downloadercfg, title, enclosure, size, taskid, transfer, taskname):388 """389 æ·»å ä¸è½½ä»»å¡ï¼æ´æ°ä»»å¡æ°æ®390 :param downloadercfg: ä¸è½½å¨çææåæ°391 :param title: ç§åå称392 :param enclosure: ç§åå°å393 :param size: ç§å大å°394 :param taskid: ä»»å¡ID395 :param transfer: æ¯å¦è¦è½¬ç§»ï¼ä¸ºFalseæ¶ç´æ¥æ·»å å·²æ´ççæ ç¾396 :param taskname: ä»»å¡å称397 """398 if not downloadercfg:399 return False400 # æ ç¾401 tag = "å·²æ´ç" if not transfer else None402 # ä¸è½½ä»»å¡ID403 download_id = None404 # æ·»å ä¸è½½405 if downloadercfg.get("type") == self._qb_client:406 # åå§åä¸è½½å¨407 downloader = Qbittorrent(user_config=downloadercfg)408 if not downloader.qbc:409 log.error("ãBRUSHãä»»å¡ %s ä¸è½½å¨ %s æ æ³è¿æ¥" % (taskname, downloadercfg.get("name")))410 return False411 torrent_tag = str(round(datetime.now().timestamp()))412 if tag:413 tag = [tag, torrent_tag]414 else:415 tag = torrent_tag416 ret = downloader.add_torrent(content=enclosure, mtype=None, tag=tag, is_paused=True)417 if ret:418 # QBæ·»å ä¸è½½åéè¦æ¶é´ï¼éè¯5次æ¯æ¬¡çå¾
5ç§419 for i in range(1, 6):420 sleep(5)421 download_id = downloader.get_last_add_torrentid_by_tag(tag)422 if download_id is None:423 continue424 else:425 downloader.remove_torrents_tag(download_id, torrent_tag)426 downloader.start_torrents(download_id)427 downloader.torrents_set_force_start(download_id)428 break429 else:430 # åå§åä¸è½½å¨431 downloader = Transmission(user_config=downloadercfg)432 if not downloader.trc:433 log.error("ãBRUSHãä»»å¡ %s ä¸è½½å¨ %s æ æ³è¿æ¥" % (taskname, downloadercfg.get("name")))434 return False435 ret = downloader.add_torrent(content=enclosure, mtype=None)436 if ret:437 download_id = ret.id438 if download_id and tag:439 downloader.set_torrent_tag(tid=download_id, tag=tag)440 if not download_id:441 log.warn("ãBRUSHã%s æ·»å ä¸è½½ä»»å¡åºé" % title)442 return False443 else:444 log.info("ãBRUSHãæåæ·»å ä¸è½½ï¼%s" % title)445 # æå
¥ç§åæ°æ®446 if insert_brushtask_torrent(brush_id=taskid,447 title=title,448 enclosure=enclosure,449 downloader=downloadercfg.get("id"),450 download_id=download_id,451 size=size):452 # æ´æ°ä¸è½½æ¬¡æ°453 add_brushtask_download_count(brush_id=taskid)454 else:455 log.info("ãBRUSHã%s å·²ä¸è½½è¿" % title)456 return True457 @staticmethod458 def __check_rss_rule(rss_rule, title, description, torrent_url, torrent_size, cookie):459 """460 æ£æ¥ç§åæ¯å¦ç¬¦åå·æµè¿æ»¤æ¡ä»¶461 :param rss_rule: è¿æ»¤æ¡ä»¶åå
¸462 :param title: ç§åå称463 :param description: ç§åå¯æ é¢464 :param torrent_url: ç§å页é¢å°å465 :param torrent_size: ç§å大å°466 :param cookie: Cookie467 :return: æ¯å¦å½ä¸468 """469 if not rss_rule:470 return True471 # æ£æ¥ç§å大å°472 try:473 if rss_rule.get("size"):474 rule_sizes = rss_rule.get("size").split("#")475 if rule_sizes[0]:476 if len(rule_sizes) > 1 and rule_sizes[1]:477 min_max_size = rule_sizes[1].split(',')478 min_size = min_max_size[0]479 if len(min_max_size) > 1:480 max_size = min_max_size[1]481 else:482 max_size = 0483 if rule_sizes[0] == "gt" and float(torrent_size) < float(min_size) * 1024 ** 3:484 return False485 if rule_sizes[0] == "lt" and float(torrent_size) > float(min_size) * 1024 ** 3:486 return False487 if rule_sizes[0] == "bw" and not float(min_size) * 1024 ** 3 < float(torrent_size) < float(488 max_size) * 1024 ** 3:489 return False490 # æ£æ¥å
å«è§å491 if rss_rule.get("include"):492 if not re.search(r"%s" % rss_rule.get("include"), "%s %s" % (title, description), re.IGNORECASE):493 return False494 # æ£æ¥æé¤è§å495 if rss_rule.get("exclude"):496 if re.search(r"%s" % rss_rule.get("exclude"), "%s %s" % (title, description), re.IGNORECASE):497 return False498 attr_type = Torrent.check_torrent_attr(torrent_url=torrent_url, cookie=cookie)499 log.debug("ãBRUSHã%s 解æ详æ
, %s" % (title, attr_type))500 # æ£æ¥å
è´¹ç¶æ501 if rss_rule.get("free") == "FREE":502 if not attr_type.is_free():503 return False504 elif rss_rule.get("free") == "2XFREE":505 if not attr_type.is_free2x():506 return False507 # æ£æ¥HRç¶æ508 if rss_rule.get("hr"):509 if attr_type.is_hr():510 return False511 log.debug("ãBRUSHã%s `å¤æåç§æ°, å½ååç§äººæ°%s, å½å人æ°éå¼%s" % (title, attr_type.peer_count, rss_rule.get("peercount")))512 if rss_rule.get("peercount"):513 if attr_type.peer_count > int(rss_rule.get("peercount")):514 return False515 except Exception as err:516 log.console(str(err) + " - " + traceback.format_exc())517 return True518 @staticmethod519 def __check_remove_rule(remove_rule, seeding_time=None, ratio=None, uploaded=None, dltime=None, avg_upspeed=None):520 """521 æ£æ¥æ¯å¦ç¬¦åå ç§è§å522 :param remove_rule: å ç§è§å523 :param seeding_time: åç§æ¶é´524 :param ratio: å享ç525 :param uploaded: ä¸ä¼ é526 :param dltime: ä¸è½½èæ¶527 :param avg_upspeed: ä¸ä¼ å¹³åé度528 """529 if not remove_rule:530 return False531 try:532 if remove_rule.get("time") and seeding_time:533 rule_times = remove_rule.get("time").split("#")...
text_filter.py
Source:text_filter.py
1import re2import pdb3# Add rules as patterns.4# remove : remove matched substring from string5# change : change matched substring as substute resource6#7#8# Order of adding rule -> Order of filtering9# To filter with rules, call filter() method10class TextFilter:11 def __init__(self):12 self.rules = []13 self.rules_for_detect = []14 # Private inner classes15 class Remove_rule:16 def __init__(self, pattern):17 self.pattern = re.compile(pattern)18 def filter(self, sentence):19 return self.pattern.sub('', sentence)20 class Replace_rule:21 def __init__(self, pattern, substitute, flags=None, unicode=False):22 if unicode:23 self.pattern = re.compile(pattern, re.UNICODE)24 else:25 self.pattern = re.compile(pattern)26 self.substitute = substitute27 self.flags = flags28 def filter(self, sentence):29 if self.flags is not None:30 return self.pattern.sub(self.substitute, sentence, self.flags)31 else:32 # print(sentence)33 # if 'include' in sentence:34 # pdb.set_trace()35 return self.pattern.sub(self.substitute, sentence)36 class Detection_rule:37 def __init__(self, detect_field, pattern):38 self.detect_field = detect_field39 self.pattern = re.compile(pattern)40 def detect(self, sentence):41 out = self.pattern.search(sentence)42 data = dict()43 if out is not None:44 data[self.detect_field] = True45 return data46 data[self.detect_field] = False47 return data48 # regex sub, string replace, string pos ì¡ê³ ì»·, 리무ë¸...49 def filter(self, sentence):50 for rule in self.rules:51 sentence = rule.filter(sentence)52 sentence = sentence.strip()53 return sentence54 def detect(self, sentence):55 check_dict = dict()56 for rule in self.rules_for_detect:57 out = rule.detect(sentence)58 check_dict.update(out)59 return check_dict60 """61 ìí¤í¼ëì ì ì²ë¦¬ë¥¼ ìí Replace/ Remove Rule (doc open, close)62 """63 def detect_doc_open_pattern(self):64 pattern = r"^<doc.*>$"65 self.rules_for_detect.append(self.Detection_rule('doc_open', pattern))66 def detect_doc_close_pattern(self):67 pattern = r"</doc>"68 self.rules_for_detect.append(self.Detection_rule('doc_close', pattern))69 def change_match_pattern_to_subs(self, pattern, substitute):70 self.rules.append(self.Replace_rule(pattern, substitute))71 def remove_match_pattern(self, pattern):72 self.rules.append(self.Remove_rule(pattern))73 def remove_from_pattern_to_space(self, pattern):74 pattern = pattern + r'.*?($|[ \t\n\r])'75 self.rules.append(self.Remove_rule(pattern))76 def remove_from_space_to_pattern(self, pattern):77 pass78 def remove_from_pattern_to_pattern(self, pattern_start, pattern_end):79 pattern = pattern_start + r'.*?' + pattern_end80 self.rules.append(self.Remove_rule(pattern))81 def remove_http_url(self):82 pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&#+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'83 self.rules.append(self.Remove_rule(pattern))84 def remove_ftp_url(self):85 pattern = r'ftp://(?:[a-zA-Z]|[0-9]|[$-_@.&#+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'86 self.rules.append(self.Remove_rule(pattern))87 def remove_unstructured_url(self):88 pass89 def remove_emails(self):90 pattern = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'91 self.rules.append(self.Remove_rule(pattern))92 def remove_phone_number(self):93 pattern = r'[0-9]+(-[0-9]+)+'94 self.rules.append(self.Remove_rule(pattern))95 # íê¸, íê¸ì모, ìì´ëì문ì, ì«ì, í¤ë³´ë ë´ í¹ì문ì ì ì¸íê³ ëª¨ë ìì 96 def remove_trash_char(self):97 pattern = r"[^ê°-í£a-zA-Z0-9ã±-ã
£\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\|\\\[\]\{\}\;\:\'\"\,\.\<\>\?\/\t ]+"98 self.rules.append(self.Remove_rule(pattern))99 # handle wiki100 def remove_doc_open_tag(self):101 pattern = r"^<doc.*>$"102 self.rules.append(self.Remove_rule(pattern))103 # handle wiki104 def remove_doc_close_tag(self):105 pattern = r"</doc>"106 self.rules.append(self.Remove_rule(pattern))107 """108 ë무ìí¤ ì ì²ë¦¬ë¥¼ ìí Replace/ Remove Rule109 """110 # handle namu-wiki111 def _replace_chinese(self):112 pattern = u'[âº-âºâº-⻳â¼-â¿ã
ãã¡-ã©ã¸-ãºã»ã-䶵ä¸-é¿è±-鶴侮-é »ä¸¦-é¾]'113 substitute = ''114 flags=None115 self.rules.append(self.Replace_rule(pattern, substitute, flags, unicode=True))116 # handle namu-wiki117 def _replace_japanese(self):118 pattern = u'[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]'119 substitute = ''120 flags=None121 self.rules.append(self.Replace_rule(pattern, substitute, flags, unicode=True))122 # remove html123 def _replace_html(self):124 pattern = r"\{\{\{#\!html[^\}]*\}\}\}"125 substitute = ''126 flags=re.IGNORECASE|re.MULTILINE|re.DOTALL127 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))128 # remove redirect129 def _replace_redirect(self):130 pattern = r"#redirect .*"131 substitute = ''132 flags=re.IGNORECASE133 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))134 # remove ë¶ë¥135 def _replace_tag0(self):136 pattern = r"í:.*"137 substitute = ''138 flags = None139 self.rules.append(self.Replace_rule(pattern, substitute, flags=flags))140 # remove ë¶ë¥141 def _replace_tag1(self):142 pattern = r"\[\[ë¶ë¥:.*"143 substitute = ''144 flags=None145 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))146 # remove íì¼147 def _replace_tag2(self):148 pattern = r"\[\[íì¼:.*"149 substitute = ''150 flags=None151 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))152 # remove ìì문ì153 def _replace_tag3(self):154 pattern = r"\* ìì 문ì ?:.*"155 substitute = ''156 flags=None157 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))158 # remove youtube159 def _replace_tag4(self):160 pattern = r"\[youtube\(\w+\)\]"161 substitute = ''162 flags=re.IGNORECASE163 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))164 # remove include165 def _replace_tag5(self):166 pattern = r"\[(i|I)nclude\(([^\]|]*)(\|[^]]*)?\]"167 substitute = r'\1'168 flags=re.IGNORECASE169 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))170 # remove link171 def _replace_tag6(self):172 pattern = r"\[\[(?:[^\]|]*\|)?([^\]|]+)\]\]"173 substitute = r'\1'174 flags=None175 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))176 # remove ê°ì£¼177 def _replace_tag7(self):178 # pattern = r"\[([^\]]*)\]"179 pattern = r"\[.*\]"180 substitute = ''181 flags = None182 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))183 # remove text color/size184 def _replace_tag8(self):185 pattern = r"\{\{\{([^\ }|]*) ([^\}|]*)\}\}\}"186 substitute = r'\2'187 flags = None188 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))189 # remove text bold190 def _replace_tag9(self):191 pattern = r"'''([^']*)'''"192 substitute = r'\1'193 flags=None194 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))195 # remove strike-through196 def _replace_tag10(self):197 pattern = r"(~~|--)([^']*)(~~|--)"198 substitute = ''199 flags=None200 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))201 # remove table202 def _replace_tag11(self):203 pattern = r"\|[\S\s]*\|"204 substitute = ''205 flags=None206 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))207 # ë¬¸ë¨ ì ê±°208 def _replace_tag12(self):209 pattern = r"=(.*)="210 substitute = ''211 flags=None212 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))213 # ëìì ì ê±°214 def _replace_tag13(self):215 pattern = r"width.*\/iframe>"216 substitute = ''217 flags=None218 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))219 # html tag220 def _replace_tag14(self):221 pattern = r"<.*?>"222 substitute = ''223 flags=None224 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))225 # html tag226 def _replace_tag15(self):227 pattern = r"\{\{\|([^|]*)\|\}\}"228 substitute = ''229 flags=None230 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))231 # ìì íí ì ê±°232 def _replace_tag16(self):233 pattern = r"\\[^ê°-í£]*"234 substitute = ''235 flags=None236 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))237 # ê´í¸ ë´ë¶ ë´ì© ì ê±°238 def _replace_tag17(self):239 pattern = r"\([^)]*\)"240 substitute = ''241 flags=None242 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))243 # include tag ì ê±°244 def _replace_tag18(self):245 pattern = r"\[(i|I)nclude.*\]"246 substitute = ''247 flags=None248 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))249 def replace_namu_tags(self):250 self._replace_tag18()251 self._replace_chinese()252 self._replace_japanese()253 self._replace_html()254 self._replace_redirect()255 self._replace_tag0()256 self._replace_tag1()257 self._replace_tag2()258 self._replace_tag3()259 self._replace_tag4()260 self._replace_tag5()261 self._replace_tag6()262 self._replace_tag7()263 self._replace_tag8()264 self._replace_tag9()265 self._replace_tag10()266 self._replace_tag11()267 self._replace_tag12()268 self._replace_tag13()269 self._replace_tag14()270 self._replace_tag15()271 self._replace_tag16()272 self._replace_tag17()273 def spacing_on_both_side_of_mark(self):274 pattern = r'[\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\[\{\]\}\\\|\;\:\'\"\,\.\/\<\>\?]'275 substitute = r' \g<0> '276 self.rules.append(self.Replace_rule(pattern, substitute))277 def spacing_on_both_side_of_hangul_jamo(self):278 pattern = r'[ã±-ã
£]+'279 substitute = r' \g<0> '280 self.rules.append(self.Replace_rule(pattern, substitute))281 # ë¬¸ì¥ ë´ë¶ì 공백(' ', \n \t \r)ì´ ì¬ë¬ë² ìë ê²½ì° space íëë¡ ì¹í282 def strip_inside(self):283 pattern = r'[ \t\n\r]+'284 substitute = ' '...
iparrest.py
Source:iparrest.py
...78 if block:79 deny_rule('10.1.1.68', 150)80 deny_rule('10.1.1.6', 151)81 else:82 remove_rule(150)83 remove_rule(151)84 85def block_xbox(block):86 if block:87 deny_rule('10.1.1.90', 152)88 deny_rule('10.1.1.14', 153)89 else:90 remove_rule(152)91 remove_rule(153)92 93def block_ghome(block):94 if block:95 deny_rule('10.1.1.75', 154)96 deny_rule('10.1.1.76', 155)97 else:98 remove_rule(154)99 remove_rule(155)100 101def block_tv(block):102 if block:103 deny_rule('10.1.1.128', 156)104 else:105 remove_rule(156)106 107def clear_list():108 global black_list109 110 for ip, rule_number in black_list:111 remove_rule(rule_number)112 black_list = {}113 114def block_ip(ip, block):115 global black_list116 if block and ip not in black_list.keys():117 block_addr(ip)118 else:119 unblock_addr(ip)120 del black_list[ip]121 122def reset_fw():123 global ed_blocked, xbox_blocked, ghome_blocked, tv_blocked, reject_number, root, debug124 head = """125#!/bin/sh126 127#/sbin/natd -config /etc/natd.conf128 129/sbin/ipfw -f flush130/sbin/ipfw add 100 divert natd all from any to any via $cfg->{iface}\n131#/sbin/ipfw add allow tcp from any to any 18081 in\n132#/sbin/ipfw add fwd 10.1.1.103, 18081 tcp from any to any 18081 in\n133\n"""134 tail = """135/sbin/ipfw add 50000 allow gre from any to any keep-state136/sbin/ipfw add 50100 pass all from any to any\n"""137 if ed_blocked:138 head += '/sbin/ipfw add 150 deny ip from 10.1.1.68 to any\n'139 head += '/sbin/ipfw add 151 deny ip from 10.1.1.6 to any\n'140 if xbox_blocked:141 head += '/sbin/ipfw add 152 deny ip from 10.1.1.90 to any\n'142 head += '/sbin/ipfw add 153 deny ip from 10.1.1.14 to any\n'143 if ghome_blocked:144 head += '/sbin/ipfw add 154 deny ip from 10.1.1.75 to any\n'145 head += '/sbin/ipfw add 155 deny ip from 10.1.1.76 to any\n'146 if tv_blocked:147 head += '/sbin/ipfw add 156 deny ip from 10.1.1.128 to any\n'148 if root and not debug:149 tmp_natd = '/tmp/natd.sh'150 natd = '/usr/local/etc/rc.d/fw.sh'151 else:152 tmp_natd = './natd.sh'153 natd = './fw.sh'154 try:155 f = open(tmp_natd, 'w')156 f.write(head)157 f.write(tail)158 f.close()159 except Exception as err:160 print_out("error in 'reset_fw' : " + err)161 except:162 print_out("unexpected error in 'reset_fw' : " + sys.exc_info()[0])163 reject_number = 1000164 165 now = datetime.now()166 timestamp = str(datetime.timestamp(now))167 168 cmd = "mv {0} {1}.{2}~".format(natd, natd, timestamp)169 try:170 if root and not debug:171 os.system(cmd)172 else:173 print_out(cmd)174 cmd = 'mv {0} {1}'.format(tmp_natd, natd)175 if root and not debug:176 os.system(cmd)177 else:178 print_out(cmd)179 os.chmod(natd, stat.stat.S_ISUID)180 181 cmd = natd182 if root and not debug:183 os.system(cmd)184 else:185 print_out(cmd)186 except Exception as err:187 print_out("error in 'reset_fw' : " + err)188 except:189 print_out("unexpected error in 'reset_fw' : " + sys.exc_info()[0])190def handle_cmd(cmd, ip):191 print_out("iparrest command found : " + cmd + " " + ip)192 193 if 'add-white':194 add_white(ip)195 update_fw_file()196 elif 'show-list':197 show_list()198 update_fw_file()199 elif 'block-ed':200 block_ed(True)201 update_fw_file()202 elif 'unblock-ed':203 block_ed(False)204 update_fw_file()205 elif 'block-xbox':206 block_xbox(True)207 update_fw_file()208 elif 'unblock-xbox':209 block_xbox(False)210 update_fw_file()211 elif 'block-ghome':212 block_ghome(True)213 update_fw_file()214 elif 'unblock-ghome':215 block_ghome(False)216 update_fw_file()217 elif 'block-tv':218 block_tv(True)219 update_fw_file()220 elif 'unblock-tv':221 block_tv(False)222 update_fw_file()223 elif 'clear-list':224 clear_list()225 update_fw_file()226 elif 'unblock-ip':227 block_ip(ip, False)228 update_fw_file()229 elif 'block-ip':230 block_ip(ip, True)231 update_fw_file()232 elif 'reset-fw':233 reset_fw()234 update_fw_file()235 else:236 print_out('unknown command : ' + cmd)237def unblock_addr(ip):238 global remove_rule, black_list, cfg239 if ip in black_list.keys():240 rule_number = black_list[ip]241 cmd = remove_rule.format(cfg['ipfw'], rule_number)242 print_out(cmd)243 del black_list[ip]244def remove_rule(rule_number):245 global remove_rule, cfg246 cmd = remove_rule.format(cfg['ipfw'], rule_number)247 print_out(cmd)248def block_addr(ip):249 global reject_rule, reject_number, cfg, black_list250 cmd = reject_rule.format(cfg['ipfw'], ++reject_number, ip, cfg['if'])251 print_out(cmd)252 253 black_list[ip] = reject_number254 255def deny_addr(ip, rule_number):256 global deny_rule, cfg257 cmd = deny_rule.format(cfg['ipfw'], rule_number, ip)258 print_out(cmd)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!