def test_logging_init(self): self.logger.debug("debug") self.assertEqual(self.stream.getvalue().strip(), "[{0}]: debug".format(color_msg("blue", "DEBUG"))) self.stream.truncate(0) self.logger.info("info") self.assertEqual(self.stream.getvalue().strip(), "[{0}]: info".format(color_msg("green", "INFO"))) self.stream.truncate(0) self.logger.warning("warning") self.assertEqual( self.stream.getvalue().strip(), "[{0}]: warning".format(color_msg("yellow", "WARNING"))) self.stream.truncate(0) self.logger.error("error") self.assertEqual(self.stream.getvalue().strip(), "[{0}]: error".format(color_msg("red", "ERROR"))) self.stream.truncate(0) self.logger.critical("critical") self.assertEqual( self.stream.getvalue().strip(), "[{0}]: critical".format(color_msg("bgred", "CRITICAL"))) self.stream.truncate(0)
def parse_configs(config_file): base_dir = osp.dirname(osp.dirname(osp.realpath(__file__))) if not utils.check_path_exists(config_file): sys.exit(utils.color_msg("error", "{} not exists".format(config_file))) try: with open(config_file, "rb") as fd: configs = yaml.load(fd) except yaml.YAMLError, e: msg = "Yaml format error in {}:\n{}".format( config_file, unicode(str(e), "utf-8") ) sys.exit(utils.color_msg("error", msg))
def format(self, record): try: msg = super(ANSIFormatter, self).format(record) except: # for python2.6 # Formatter is old-style class in python2.6 and type is classobj # another trick: http://stackoverflow.com/a/18392639/1276501 msg = Formatter.format(self, record) lvl2color = { "DEBUG": "blue", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bgred" } rln = record.levelname if rln in lvl2color: return "[{0}]: {1}".format( utils.color_msg(lvl2color[rln], rln), msg ) else: return msg
def get_meta_datas(self, meta_yaml, mdown_file): """Get meta datas and validate them :param meta_yaml: Meta info in yaml format """ try: meta_datas = yaml.load(meta_yaml) except yaml.YAMLError, e: msg = "Yaml format error in {}:\n{}".format( mdown_file, unicode(str(e), "utf-8") ) sys.exit(utils.color_msg("error", msg))
def get_meta_and_content(self, mdown_file): """Split the markdown file texts by triple-dashed lines. The content in the middle of triple-dashed lines is meta datas, which use Yaml format. The other content is the markdown texts. """ with codecs.open(mdown_file, "rb", "utf-8") as fd: text_lists = fd.readlines() meta_notation = "---\n" if text_lists[0] != meta_notation: msg = utils.color_msg( "error", "[{0}] First line must be triple-dashed!".format(mdown_file), ) sys.exit(msg) meta_lists = [] meta_end_flag = False idx = 1 max_idx = len(text_lists) while not meta_end_flag: meta_lists.append(text_lists[idx]) idx += 1 if idx >= max_idx: sys.exit(utils.color_msg( "error", "[{0}] doesn't have end triple-dashed!".format(mdown_file), )) if text_lists[idx] == meta_notation: meta_end_flag = True content_lists = text_lists[idx+1:] meta_yaml = "".join(meta_lists) contents = "".join(content_lists) return (meta_yaml, contents)
def output_to_file(self, html): """Write generated html to file""" catalog, mdown = self.get_catalog_and_mdown(self.mdown_file) output_catalog_path = osp.join(self.site_settings["destination"], catalog) if not utils.check_path_exists(output_catalog_path): print(utils.color_msg( "info", "The output catalog %s not exists, create it" \ % output_catalog_path) ) os.mkdir(output_catalog_path) mdown_name = osp.splitext(mdown)[0] output_file = osp.join(output_catalog_path, mdown_name+".html") with codecs.open(output_file, "wb", "utf-8") as fd: fd.write(html)
def format(self, record): lvl2color = { "DEBUG" : "blue", "INFO" : "green", "WARNING" : "yellow", "ERROR" : "red", "CRITICAL" : "bgred" } msg = record.getMessage() rln = record.levelname if rln in lvl2color: return "[{}]: {}".format(utils.color_msg(lvl2color[rln], rln), msg.encode('utf-8')) else: return msg
def format(self, record): lvl2color = { "DEBUG": "blue", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "bgred" } msg = record.getMessage() rln = record.levelname if rln in lvl2color: return "[{}]: {}".format(utils.color_msg(lvl2color[rln], rln), msg.encode('utf-8')) else: return msg
def test_logging_init(self): l2c = { "debug": "blue", "info": "green", "warning": "yellow", "error": "red", "critical": "bgred" } for level in l2c: # self.handler.flush() self.stream.truncate(0) func = getattr(self.logger, level) func(level) expected_output = "[{0}]: {1}" \ .format(color_msg(l2c[level], level.upper()), level) self.assertEqual(self.stream.getvalue().strip(), expected_output)
def test_logging_init(self): l2c = { "debug": "blue", "info": "green", "warning": "yellow", "error": "red", "critical": "bgred" } for level in l2c: # self.handler.flush() self.stream.truncate(0) # in python 3.x, truncate(0) would not change the current file pos # via <http://stackoverflow.com/a/4330829/1276501> self.stream.seek(0) func = getattr(self.logger, level) func(level) expected_output = "[{0}]: {1}" \ .format(color_msg(l2c[level], level.upper()), level) stream_output = self.stream.getvalue().strip() if is_py2: stream_output = unicode(stream_output) self.assertEqual(stream_output, expected_output)