def __user_setting(self): message_style = self.__config.get(MESSAGE_STYLE, setting()) time_style = self.__config.get(TIME_STYLE, setting()) flag_style = self.__config.get(FLAG_STYLE, setting()) flag = '{0}{1}{2}'.format(flag_style[0], f"{self.__flag_name.center(self.__flag_len, ' ')}", flag_style[1]) time = '{0}{1}{2}'.format(time_style[0], self.__get_time(), time_style[1]) return flag, time, message_style
def __init__(self, *args, default=''): self.default = default data = {} for i in args: if isinstance(i, CPStyle): data[i.name] = i.value self.style = setting(**data) # list
def load_config(self): """获取py文件中的配置""" # template 检查 template = self.config.get('TEMPLATE') if not template: raise PrintMeError( f"'{self.config.filename}' Can't find TEMPLATE = '' ") self.raw_template = template self.term = re.findall(r'(?<=\{)[^}]*(?=\})+', template) for t in self.term: if t.strip() == '': raise PrintMeError('Unknown {} ! ') if "message" not in self.term: raise PrintMeError('Template muse have {message} ! ') term_wrap = { i: "{%s}{%s}{%s}" % (i + '0', i, i + '1') for i in self.term } self.template = template.format(**term_wrap) # style map for level in self.level_list: default = self.default[level] = {} style = self.box[level] = {} terms = self.config.get(level, make_level_default( self.term)) # 获取每个level里的字段 for t in self.term: cfg = terms.get( t, make_default()) # 获取每个字段的default,fore,mode,back default.update({t: cfg.get("DEFAULT")}) sett = setting(fore=cfg.get('fore'), back=cfg.get('back'), mode=cfg.get('mode')) style.update({f'{t}0': sett[0], f'{t}1': sett[1]})
def set_config(self): for k, v in self.config.items(): default = self.default[k] = {} style = self.box[k] = {} for t in self.term: default.update({t: v[t].pop("DEFAULT", "")}) sett = setting(**v[t]) style.update({f'{t}0': sett[0], f'{t}1': sett[1]})
def cword(*args, **kwargs): """ :param args: 字符 :param kwargs: 颜色参数 :return: list 彩色字符 """ mode = kwargs.get('mode') fore = kwargs.get('fore', Fore.CYAN) back = kwargs.get('back') style = setting(mode=mode, fore=fore, back=back) str_temp = [] for s in args: str_temp.append(f'{style[0]}{s}{style[1]}') return str_temp[0] if len(str_temp) == 1 else str_temp
def cword(*args): """ :param args: 字符 :param kwargs: 颜色参数 :return: list 彩色字符 """ data = {} temp = [] for i in args: if isinstance(i, CPStyle): data[i.name] = i.value else: temp.append(i) style = setting(**data) str_temp = [] for s in temp: str_temp.append('{s0}{s}{s1}'.format(s0=style[0], s=s, s1=style[1])) return str_temp[0] if len(str_temp) == 1 else str_temp
def cprint(*args, **kwargs): """ :param args: 字符 :param kwargs: 颜色参数 :return: list 彩色字符 sep=' ', end='\n', file=None """ if not args: return print() mode = kwargs.get('mode') fore = kwargs.get('fore', Fore.CYAN) back = kwargs.get('back') sep = kwargs.get('sep', " ") end = kwargs.get('end', '\n') file = kwargs.get('file', None) style = setting(mode=mode, fore=fore, back=back) str_temp = [] for s in args: str_temp.append(f'{style[0]}{s}{style[1]}') if kwargs.get('show', True): print(*str_temp, sep=sep, end=end, file=file) return str_temp if len(str_temp) > 1 else str_temp[0]
def flag_style(self, mode='', fore='', back=''): self.__config[FLAG_STYLE] = setting(mode=mode, fore=fore, back=back) return self
def time_style(self, mode='', fore='', back=''): self.__config[TIME_STYLE] = setting(mode=mode, fore=fore, back=back) return self
def message_style(self, mode='', fore='', back=''): self.__config[MESSAGE_STYLE] = setting(mode=mode, fore=fore, back=back) return self