{ "info": { "author": "Daniil Minukhin", "author_email": "ddddsa@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Console", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Documentation", "Topic :: Utilities" ], "description": "![](https://img.shields.io/pypi/v/foliantcontrib.utils.preprocessor_ext.svg)\n\n# Overview\n\nExtension for basic Preprocessor class with useful methods and functions.\n\n# Usage\n\nUsually when we create preprocessors we inherit from `BasePreprocessor`:\n\n```python\n\nclass Preprocessor(BasePreprocessor):\n ...\n```\n\nTo use all features of preprocessor_ext we should inherit from `BasePreprocessorExt` instead:\n\n```python\n\nclass Preprocessor(BasePreprocessorExt):\n ...\n```\n\n# Features\n\n## Simplified tag processing workflow\n\nUsually to process tags in Markdown-files we write something like this:\n\n```python\nclass Preprocessor(BasePreprocessor):\n ...\n def _process_tags(content):\n def sub(match):\n # process the tag\n return processed_string\n\n self.pattern.sub(sub, content)\n\n def apply(self):\n self.logger.info('Applying preprocessor')\n for markdown_file_path in self.working_dir.rglob('*.md'):\n with open(markdown_file_path, encoding='utf8') as markdown_file:\n content = markdown_file.read()\n\n processed_content = self._process_tags(content)\n\n if processed_content:\n with open(markdown_file_path, 'w') as markdown_file:\n markdown_file.write(processed_content)\n self.logger.info('Preprocessor applied')\n```\n\nBasePreprocessorExt saves us from writing these many lines of the code which appear unchanged in most preprocessors.\n\nSo now instead of all that code we will write:\n\n```python\nclass Preprocessor(BasePreprocessorExt):\n ...\n def _process_tag(match):\n # process the tag\n return processed_string\n\n def apply(self):\n self._process_tags_for_all_files(func=self._process_tag)\n self.logger.info('Preprocessor applied')\n```\n\nAs a bonus when using this workflow we get additional capabilities of logging and outputting warnings.\n\n## Issuing warnings\n\nWhen using `_process_tags_for_all_files` method to process tags we can also take advantage of `_warning` method.\n\nWhat this method does:\n1. Prints warning message to user and adds the md-file name to this message.\n2. Logs this message.\n3. May also add to logged message context of the tag where the problem occured.\n4. May also add to logged message the error traceback.\n5. If debug=true, #3 and #4 are also added to the message which is printed to user.\n\nSimple example:\n\n```python\nclass Preprocessor(BasePreprocessorExt):\n ...\n def _process_tag(match):\n try:\n config = open(self.options.get('config'))\n except FileNotFoundError as e:\n self._warning('Config file not found! Using default', error=e)\n ...\n```\n\nHere if the exception gets triggered, user will see something like this in console:\n\n```bash\nParsing config\nDone\n\nApplying preprocessor my_preprocessor\nWARNING: [index.md] Config file not found! Using default\n\nDone\n\nApplying preprocessor _unescape\nDone\n\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nResult: slug.pre\n\n```\n\nAs we see, we've only supplied the message, but the preprocessor also added the `WARNING:` prefix and current file name to console. If we'd run the make command in debug mode (with `-d --debug` flag), we would also see full traceback of the error. In any case, traceback is stored in log.\n\n## Getting tag context\n\nSometimes we want to show the context of the tag, where we met some problems. By context I mean some words before the tag, some contents of the tag body and some words after the tag. It's really useful for debugging large md-files, with context you usually can identify the place in the document which causes errors.\n\nFor this purpose `BasePreprocessorExt` class has a handy method called `get_tag_context`. Give it the match object you are currently working with and it will return you the string with tag context.\n\nFor example:\n\n```python\nclass Preprocessor(BasePreprocessorExt):\n ...\n def _process_tag(match):\n try:\n config = open(self.options.get('config'))\n except FileNotFoundError as e:\n context = self.get_tag_context(match)\n print(f'Config not found, check the tag:\\n{context}')\n ...\n```\n\nThis will print:\n\n```bash\nParsing config\nDone\n\nApplying preprocessor my_preprocessor\n\nConfig not found, check the tag:\n...amet, consectetur adipisicing elit. Dolores ipsum non nisi voluptatum alias.\n\n\n Tag body consectetur adipisicing elit. Voluptatem.\n\n\nEnd of document.\n```\n\nNow user can easily understand where's the problem in his document.\n\n`get_tag_context` function accepts two parameters:\n\n`limit` (default: `100`) \u2014 number of characters included in context from before the tag, after the tag, and of the tag body;\n`full_tag` (default: `False`) \u2014 if this is True, the tag body is copied into context without cropping (useful for relatively small expected tag bodies).\n\n## Sending context to warning\n\nOne last thing to use the full power of `BasePreprocessorExt` warnings:\n\n`self._warning` accepts the context parameter. You can send there the context string. The context is only shown to user in the console if the debug mode is on, but it is always saved in the log file.\n\nExample:\n\n```python\nclass Preprocessor(BasePreprocessorExt):\n ...\n def _process_tag(match):\n try:\n config = open(self.options.get('config'))\n except FileNotFoundError as e:\n self._warning('Config file not found! Using default',\n context=self.get_context(match),\n error=e)\n ...\n```\n\nNow if we catch this exception in normal mode, we will only get the md-filename and the message in the console. But if we run it in debug mode, we will get a full python traceback and the context of the tag. And a happy user.\n\n## allow_fail decorator\n\nOften we don't want the whole preprocessor to crash if there are some problems in just one tag of the document. We can easily achieve this using the `allow_fail` decorator, which is included in the `preprocessor_ext` module. Decorate your function, which is then sent to `_process_tags_for_all_files` method:\n\n```python\nfrom foliant.preprocessors.utils.preprocessor_ext import (BasePreprocessorExt,\n allow_fail)\n\n\nclass Preprocessor(BasePreprocessorExt):\n ...\n @allow_fail()\n def _process_tag(match):\n # process the tag\n return processed_string\n\n def apply(self):\n self._process_tags_for_all_files(func=self._process_tag)\n self.logger.info('Preprocessor applied')\n```\n\nNow in case _any_ error occurs in the `_process_tag` function, preprocessor will issue warning, show it to user, save it into log and skip the tag.\n\nThe `allow_fail` decorator accepts one argument, the error message, which will be shown to user in case of exception. It defaults to: _Failed to process tag. Skipping._\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/foliant-docs/foliantcontrib.utils.preprocessor_ext", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "foliantcontrib.utils.preprocessor-ext", "package_url": "https://pypi.org/project/foliantcontrib.utils.preprocessor-ext/", "platform": "any", "project_url": "https://pypi.org/project/foliantcontrib.utils.preprocessor-ext/", "project_urls": { "Homepage": "https://github.com/foliant-docs/foliantcontrib.utils.preprocessor_ext" }, "release_url": "https://pypi.org/project/foliantcontrib.utils.preprocessor-ext/1.0.2/", "requires_dist": [ "foliant (>=1.0.8)", "PyYAML" ], "requires_python": "", "summary": "Extension to basic preprocessor class with useful methods and functions.", "version": "1.0.2" }, "last_serial": 5790244, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "b6c54b711159a4526182db85cdfe0552", "sha256": "0ce14b5ae2b447ebb185b95572988da7a7d15754a1eccb91cc50a8a7434a320c" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "b6c54b711159a4526182db85cdfe0552", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6898, "upload_time": "2019-05-20T09:22:02", "url": "https://files.pythonhosted.org/packages/77/af/2bc0407a06fdb4aff29de52da0a6d3bd36c4a5b702c5f2a60b40bbfe4f83/foliantcontrib.utils.preprocessor_ext-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "38d62241acb8883639fba343ce06cc96", "sha256": "94c574162a335a78cd54a1b95da7379a098493fc96a98f33d4dc1d54432e19e0" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.0.tar.gz", "has_sig": false, "md5_digest": "38d62241acb8883639fba343ce06cc96", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5776, "upload_time": "2019-05-20T09:22:05", "url": "https://files.pythonhosted.org/packages/a3/d1/b0ea88fd6a5c79077a4d1e2c18feedd3f71b67debf082dcf3510896775a0/foliantcontrib.utils.preprocessor_ext-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "5805229d739c260dc7ae3ff7688857c0", "sha256": "cf3f28bd50b2ad09e9ce850b73f232a22e0fd8c44f02fcd19502dce69fc37bf8" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "5805229d739c260dc7ae3ff7688857c0", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6902, "upload_time": "2019-06-14T13:23:59", "url": "https://files.pythonhosted.org/packages/11/ab/1688bfd2661c36d05e8dcbe348b94e20e50eb83f1ff722a52b19946bb1bd/foliantcontrib.utils.preprocessor_ext-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "eaef62dae6c33e9b0b7e15ec42995b69", "sha256": "5cb71d9852a81a52f2814df18016006ccb48cc6e8b0308e4efd76f1cd385d2bf" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.1.tar.gz", "has_sig": false, "md5_digest": "eaef62dae6c33e9b0b7e15ec42995b69", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5773, "upload_time": "2019-06-14T13:24:01", "url": "https://files.pythonhosted.org/packages/f5/cf/8636787c1cec3e76441a18b62cf776aa1aad9908371bc4e46546aa8c250e/foliantcontrib.utils.preprocessor_ext-1.0.1.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "ee4176e9b0c24d8277da0ad73b978f09", "sha256": "e4f39de7090e0484f15a22149cafd3cbc988fb8637e1111b2a21adc2ef7cc143" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "ee4176e9b0c24d8277da0ad73b978f09", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6654, "upload_time": "2019-07-29T10:17:18", "url": "https://files.pythonhosted.org/packages/73/36/b4b671e075084bfb97214cfdddc3f2e4498f83e38bf1d3ef9b3e51010f4e/foliantcontrib.utils.preprocessor_ext-1.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1a5bb8126b574d26a2969034232e4eb7", "sha256": "16a2882ccad40293f3a3a3eb5e113f4a4c3f6df9f92ac75304b8145b06cab3a1" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.2.tar.gz", "has_sig": false, "md5_digest": "1a5bb8126b574d26a2969034232e4eb7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5528, "upload_time": "2019-07-29T10:17:20", "url": "https://files.pythonhosted.org/packages/1e/4a/844e01cf4881924781a21b031f57a4fde71cea74256465e80fd9db50991c/foliantcontrib.utils.preprocessor_ext-1.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "ee4176e9b0c24d8277da0ad73b978f09", "sha256": "e4f39de7090e0484f15a22149cafd3cbc988fb8637e1111b2a21adc2ef7cc143" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "ee4176e9b0c24d8277da0ad73b978f09", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6654, "upload_time": "2019-07-29T10:17:18", "url": "https://files.pythonhosted.org/packages/73/36/b4b671e075084bfb97214cfdddc3f2e4498f83e38bf1d3ef9b3e51010f4e/foliantcontrib.utils.preprocessor_ext-1.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "1a5bb8126b574d26a2969034232e4eb7", "sha256": "16a2882ccad40293f3a3a3eb5e113f4a4c3f6df9f92ac75304b8145b06cab3a1" }, "downloads": -1, "filename": "foliantcontrib.utils.preprocessor_ext-1.0.2.tar.gz", "has_sig": false, "md5_digest": "1a5bb8126b574d26a2969034232e4eb7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5528, "upload_time": "2019-07-29T10:17:20", "url": "https://files.pythonhosted.org/packages/1e/4a/844e01cf4881924781a21b031f57a4fde71cea74256465e80fd9db50991c/foliantcontrib.utils.preprocessor_ext-1.0.2.tar.gz" } ] }