Jean-Baptiste/email_parser
20
1from functools import wraps2import logging3import os4from time import time5import configparser6 7timer_functions = {}8 9# Loading configuration from config file10config = configparser.ConfigParser()11config.read(os.path.join(os.path.dirname(__file__), 'config.ini'))12 13 14def timing(f):15 @wraps(f)16 def wrap(*args, **kw):17 ts = time()18 result = f(*args, **kw)19 te = time()20 if f.__name__ in timer_functions.keys():21 current_elapsed_time = timer_functions[f.__name__]22 else:23 current_elapsed_time = 024 timer_functions[f.__name__] = current_elapsed_time + (te - ts)25 logging.debug('func:%r took: %2.4f sec' % \26 (f.__name__, te - ts))27 return result28 return wrap29 30 31def f_read_config(path=None):32 """ read config file from specified file path33 34 :param path: file path35 :return: configparser object36 """37 # Loading configuration from config file38 config = configparser.ConfigParser()39 if path is None:40 path = os.path.join(os.path.dirname(__file__), 'config.ini')41 config.read(path, encoding='utf-8')42 return config43 44def f_setup_logger(level_sysout=logging.INFO, level_file=logging.DEBUG, folder_path="logs"):45 """Setup logger46 47 By default we display only INFO in console, and write everything in file48 49 Args:50 level_sysout: Level that is displayed in console (default INFO)51 level_file: Level that is written in file (default DEBUG)52 53 Returns:54 Nothing55 56 """57 if not os.path.isdir(folder_path):58 os.mkdir(folder_path)59 60 for handler in logging.root.handlers[:]:61 logging.root.removeHandler(handler)62 63 file_handler = logging.FileHandler(filename=os.path.join(folder_path, "amf_uce_nlp_{}.log".format(time())),64 encoding='utf-8')65 sysout_handler = logging.StreamHandler()66 file_handler.setLevel(level_file)67 sysout_handler.setLevel(level_sysout)68 logging.basicConfig(handlers=[file_handler, sysout_handler], level=logging.DEBUG,69 format='%(asctime)s (%(levelname)s) %(message)s', datefmt='%m/%d/%y %I:%M:%S %p')70 71 72def get_model_full_path(model_name):73 path_models = config["DEFAULT"]["path_models"]74 return os.path.join(os.path.dirname(__file__), path_models, model_name)75 76 77def f_normalize_text(text):78 """Repair common mojibake patterns while leaving normal Unicode text unchanged."""79 if not isinstance(text, str) or text == "":80 return text81 82 suspicious_markers = ("Ã", "’", "“", "â€\x9d", "–", "—", "Â")83 if not any(marker in text for marker in suspicious_markers):84 return text85 86 for source_encoding in ("latin-1", "cp1252"):87 try:88 repaired = text.encode(source_encoding).decode("utf-8")89 except (UnicodeEncodeError, UnicodeDecodeError):90 continue91 if repaired != text:92 return repaired93 return text94 