用有用的方法和函数扩展到基本预处理器类。

foliantcontrib.utils.preprocessor-ext的Python项目详细描述


概述

用有用的方法和函数扩展基本预处理器类。

用法

通常当我们创建预处理器时,我们从BasePreprocessor

classPreprocessor(BasePreprocessor):...

要使用预处理器扩展的所有功能,我们应该从BasePreprocessorExt继承:

classPreprocessor(BasePreprocessorExt):...

功能

简化的标签处理流程

通常为了处理标记文件中的标记,我们编写如下内容:

classPreprocessor(BasePreprocessor):...def_process_tags(content):defsub(match):# process the tagreturnprocessed_stringself.pattern.sub(sub,content)defapply(self):self.logger.info('Applying preprocessor')formarkdown_file_pathinself.working_dir.rglob('*.md'):withopen(markdown_file_path,encoding='utf8')asmarkdown_file:content=markdown_file.read()processed_content=self._process_tags(content)ifprocessed_content:withopen(markdown_file_path,'w')asmarkdown_file:markdown_file.write(processed_content)self.logger.info('Preprocessor applied')

BasePreprocessorExt使我们不必编写这些在大多数预处理器中看起来没有变化的代码行。

因此,现在我们将不再编写这些代码:

classPreprocessor(BasePreprocessorExt):...def_process_tag(match):# process the tagreturnprocessed_stringdefapply(self):self._process_tags_for_all_files(func=self._process_tag)self.logger.info('Preprocessor applied')

作为一个额外的好处,当使用这个工作流时,我们可以获得额外的记录和输出警告的功能。

发出警告

当使用_process_tags_for_all_files方法处理标记时,我们还可以利用_warning方法。

此方法的作用:

  1. 向用户打印警告消息并将MD文件名添加到此消息中。
  2. 记录此消息。
  3. 也可以添加到发生问题的标记的日志消息上下文中。
  4. 也可以在记录的消息中添加错误回溯。
  5. 如果debug=true,3和4也会添加到打印给用户的消息中。

简单示例:

classPreprocessor(BasePreprocessorExt):...def_process_tag(match):try:config=open(self.options.get('config'))exceptFileNotFoundErrorase:self._warning('Config file not found! Using default',error=e)...

在这里,如果触发异常,用户将在控制台中看到类似的内容:

Parsing config
Done

Applying preprocessor my_preprocessor
WARNING: [index.md] Config file not found! Using default

Done

Applying preprocessor _unescape
Done

────────────────────
Result: slug.pre

如我们所见,我们只提供了消息,但是预处理器也向控制台添加了WARNING:前缀和当前文件名。如果我们在调试模式下运行make命令(使用-d --debug标志),我们还会看到错误的完整回溯。在任何情况下,回溯都存储在日志中。

获取标记上下文

有时我们想显示标签的上下文,在那里我们遇到了一些问题。我所说的上下文是指标签前的一些单词,标签正文的一些内容,标签后的一些单词。这对于调试大型md文件非常有用,使用上下文通常可以确定文档中导致错误的位置。

为此,BasePreprocessorExt类有一个方便的方法,名为get_tag_context。给它一个您当前正在使用的匹配对象,它将返回带有标记上下文的字符串。

例如:

classPreprocessor(BasePreprocessorExt):...def_process_tag(match):try:config=open(self.options.get('config'))exceptFileNotFoundErrorase:context=self.get_tag_context(match)print(f'Config not found, check the tag:\n{context}')...

这将打印:

Parsing config
Done

Applying preprocessor my_preprocessor

Config not found, check the tag:
...amet, consectetur adipisicing elit. Dolores ipsum non nisi voluptatum alias.

<my_tag param="value"config="wrong/path">
    Tag body consectetur adipisicing elit. Voluptatem.
</template>

End of document.

现在用户可以很容易地理解文档中的问题所在。

get_tag_context函数接受两个参数:

limit(默认值:100)-从标记之前、标记之后和标记主体的上下文中包含的字符数; full_tag(默认值:False)-如果这是真的,标记体将被复制到上下文中而不被裁剪(对于相对较小的预期标记体很有用)。

将上下文发送到警告

最后一件事是使用BasePreprocessorExt警告的全部功能:

self._warning接受上下文参数。你可以把上下文字符串发送到那里。只有在调试模式打开时,上下文才会在控制台中显示给用户,但它始终保存在日志文件中。

示例:

classPreprocessor(BasePreprocessorExt):...def_process_tag(match):try:config=open(self.options.get('config'))exceptFileNotFoundErrorase:self._warning('Config file not found! Using default',context=self.get_context(match),error=e)...

现在,如果我们在正常模式下捕获此异常,我们将只在控制台中获取md文件名和消息。但是如果我们在调试模式下运行它,我们将得到一个完整的python回溯和标记的上下文。一个快乐的用户。

允许'u fail decorator

如果文档的一个标记中有问题,我们通常不希望整个预处理器崩溃。我们可以使用allow_faildecorator轻松实现这一点,该decorator包含在preprocessor_ext模块中。修饰函数,然后将其发送到_process_tags_for_all_files方法:

fromfoliant.preprocessors.utils.preprocessor_extimport(BasePreprocessorExt,allow_fail)classPreprocessor(BasePreprocessorExt):...@allow_fail()def_process_tag(match):# process the tagreturnprocessed_stringdefapply(self):self._process_tags_for_all_files(func=self._process_tag)self.logger.info('Preprocessor applied')

现在,如果_process_tag函数中发生任何错误,预处理器将发出警告,显示给用户,保存到日志中并跳过标记。

装饰器allow_fail接受一个参数,即错误消息,如果出现异常,该参数将显示给用户。它默认为:无法处理标记。跳过。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何通过InjectMocks注释注入真实对象   多线程如何减少Java parallelStream中的#个线程?   java检测所有安卓摄像头   java中多个构造函数的参数   Spark SQL load json抛出错误java。lang.NoClassDefFoundError:scala/collection/GenTraversableOnce$class   java如何在ServerSocket中获得挂起的请求数或接受请求而不阻塞?   java JaxWsPortProxyFactoryBean查询超时   java Spring MVC LightAdmin配置   java类类型列表不一致性   安卓自定义视图,以在Java中动态插入到布局中   如果我使用安卓,java如何使用replace()方法。支持v4。应用程序。碎片   为什么Java中的数组不能使用类型擦除?   基于java JBoss EJB的Web服务日期格式   如何在java中实现负载均衡器   java OSGI OBR存储库托管?