""" * Version: 0.1.0.0.0 * Purposes: * Intenral Utilities for pingcap.pe.flash module. """ import importlib_resources, time, random, csv, re, boto3, os, datetime, hashlib import pingcap.pe.flash.ai_editor as editor from botocore.exceptions import ( EndpointConnectionError, ClientError, NoCredentialsError, ) def _load_terms_en(team: str) -> list: en_terms_file = importlib_resources.open_text("pingcap.pe.flash", "en-terms.csv") return sorted([e[1] for e in [*csv.reader(en_terms_file)]]) def load_1gram_terms_en(team: str) -> list: ret = [e for e in _load_terms_en(team) if re.match("^[0-9A-Za-z_/-]+$", e)] return ret[1:] def capitalize_all_first_letters(line: str) -> str: """ Private routine. Literally means making the first English letter in line capitalized. """ wl = [] for w in line.split(" "): if len(w) > 0: pw = w[0].upper() + w[1:] wl += [pw] else: continue return " ".join(wl) # def _need_a_space(input_sentence: str, hints: str) -> str: # """Add spaces for sentence. # Args: # input_sentence (str): _description_ # Returns: # str: _description_ # """ # if not hints.endswith("zh"): # return input_sentence # Turn off the adding space action for Flash. # else: # if ( # len(input_sentence) == 0 or len(input_sentence) == 1 # ): # zero or 1 length cannot be handled here # return input_sentence # output_character_array = [input_sentence[0]] # char_count = len(input_sentence) # for x in range(char_count - 1): # process x and x+1 # left = input_sentence[x] # right = input_sentence[x + 1] # if ( # is_char_cjk(left) != is_char_cjk(right) # and left not in ["-", "_"] # and right not in ["-", "_"] # ): # output_character_array += [" ", input_sentence[x + 1]] # else: # output_character_array += [input_sentence[x + 1]] # return "".join(output_character_array) def count_char_in_str(c: str, str_line: str) -> int: """ :param c: The character to count. :param str_line: The string for counting the character c. :returns: Occurance count of the chatacter c. """ count = 0 for i in str_line: if i == c: count += 1 return count def get_md5(input: str) -> str: return hashlib.md5(input.encode("utf-8")).hexdigest() def get_boto_session(profile_name=None) -> boto3.Session: """ Get the boto3 session. Works with profile configured in runtime. No profile? The us-west-2 will be your default region. :param profile_name: The configured AWS profile name. :returns: The Boto session. """ if profile_name == None: region_code = os.environ.get("REGION", "us-west-2") session = boto3.Session(region_name=region_code) return session else: session = boto3.Session(profile_name=profile_name) return session def file_upload_to_s3_with_ts( file_path: str, bucket_name: str, key_prefix: str, session: boto3.Session = None ): if session == None: session = get_boto_session("default") s3_client = session.client("s3") file_basename = os.path.basename(file_path) object_name = ( str(datetime.datetime.now()) .replace(" ", "T") .replace(":", "-") .replace(".", "-") + "-" + file_basename ) try: s3_client.upload_file(file_path, bucket_name, key_prefix + "/" + object_name) except EndpointConnectionError as ex: print(speak_result("util.file_upload_to_s3_with_ts Exception: " + ex)) except ClientError as ex: print(speak_result("util.file_upload_to_s3_with_ts Exception: " + ex)) except NoCredentialsError as ex: print(speak_result("util.file_upload_to_s3_with_ts Exception: " + ex)) def is_char_cjk(c: str) -> bool: """ To check whether the input character is a CJK unicode character or not. It is used for read only features right now, it is saved for the future. :param c: The character for test. :returns: The test on single character whether it is a CJK unicode or not. """ if "\u4e00" <= c <= "\u9fff": # C return True elif "\u30a0" <= c <= "\u30ff": # JK return True elif "\u3040" <= c <= "\u309f": # JH return True elif "\uac00" <= c <= "\ud7ff": # K return True return False def get_random_string(len: int) -> str: START = 33 random_string = "" for _ in range(len): random_integer = random.randint(START, START + 92) random_string += chr(random_integer) return random_string def get_name_by_datetime(tag) -> str: return tag + "-" + datetime.datetime.now().strftime("%G-%m-%d-%H-%M-%S.%f")[:-3] def speak_title(in_str) -> str: """ Return formatted string for a better show-off. """ ceil = "-" * 3 + "-" * (len(in_str) - 1) + "/" + "-" * 3 floor = "-" * 3 + "/" + "-" * (len(in_str) - 1) + "-" * 3 center = "| " + in_str + " |" return "\n" + ceil + "\n" + center + "\n" + floor + "\n" def speak_result(in_str: str) -> str: """ """ indent = "\n >> " content = in_str return indent + content def speak_2nd_title(in_str) -> str: """ Return formatted string for a better show-off. """ ceil = " " + "-" * 3 + "-" * (len(in_str) - 1) + "/" + "-" * 3 floor = " " + "-" * 3 + "/" + "-" * (len(in_str) - 1) + "-" * 3 center = " | " + in_str + " |" return ceil + "\n" + center + "\n" + floor def speak_2nd_result(in_str) -> str: indent = " [] " content = in_str return (indent + content).rstrip(", ") def speak_3rd_title(in_str) -> str: """ Return formatted string for a better show-off. """ ceil = " " + "-" * 3 + "-" * (len(in_str) - 1) + "/" + "-" * 3 floor = " " + "-" * 3 + "/" + "-" * (len(in_str) - 1) + "-" * 3 center = " | " + in_str + " |" return ceil + "\n" + center + "\n" + floor def speak_3rd_result(in_str) -> str: indent = " + " content = in_str return (indent + content).rstrip(", ") def speak_4th_result(in_str) -> str: indent = " * " content = in_str return (indent + content).rstrip(", ")