From 178d631a1b873ad5b1e1eb9e110ab9c86deb75c8 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 10 Nov 2019 01:01:17 -0400 Subject: [PATCH] Organize global substitution code Signed-off-by: Roberto Rosario --- docs/__init__.py | 0 docs/callbacks.py | 8 +++++ docs/conf.py | 88 +++++++++++------------------------------------ docs/patches.py | 36 +++++++++++++++++++ docs/utils.py | 21 +++++++++++ 5 files changed, 86 insertions(+), 67 deletions(-) create mode 100644 docs/__init__.py create mode 100644 docs/callbacks.py create mode 100644 docs/patches.py create mode 100644 docs/utils.py diff --git a/docs/__init__.py b/docs/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docs/callbacks.py b/docs/callbacks.py new file mode 100644 index 0000000000..e6434d9c9a --- /dev/null +++ b/docs/callbacks.py @@ -0,0 +1,8 @@ +from __future__ import unicode_literals + + +def get_source_read_callback(substitutions): + def global_substitution_function(app, docname, source): + for old, new in substitutions: + source[0] = source[0].replace(old, new) + return global_substitution_function diff --git a/docs/conf.py b/docs/conf.py index f2d5458e5c..a0ec0a5f37 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -14,17 +14,19 @@ from __future__ import unicode_literals # serve to show the default. import os -import inspect import sys from docutils.parsers.rst import directives -import docutils.parsers.rst.directives.misc -from sphinx.directives.other import Include as SphinxInclude sys.path.insert(0, os.path.abspath('..')) +sys.path.insert(1, os.path.abspath('.')) import mayan +import callbacks +import patches +import utils + # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. @@ -263,69 +265,21 @@ extlinks = { } -def load_env_file(filename='../config.env'): - result = {} - with open(filename) as file_object: - for line in file_object: - if not line.startswith('#'): - key, value = line.strip().split('=') - result[key] = value - - return result - - -def generate_substitutions(dictionary): - result = [] - - for key, value in dictionary.items(): - result.append(('|{}|'.format(key), value)) - - return result - - -def global_substitution_function(app, docname, source): - for old, new in global_subtitutions_list: - source[0] = source[0].replace(old, new) - - -def monkey_patch_include(): - """ - Monkey patch docutil's Include directive to support global substitutions. - The Include class doesn't have a hook to modify the content before - inserting it back, so we add a call to our own transformation - method. We patch the base Include class, recreate Sphinx's Include class, - and register it as the new main include directive. - All this avoids copy and paste of the original code here. - """ - source_code = ''.join(inspect.getsourcelines( - docutils.parsers.rst.directives.misc.Include)[0] - ) - source_code = source_code.replace( - 'self.state_machine.insert_input(include_lines, path)', - 'include_lines=self.global_substitution(lines=include_lines)\n self.state_machine.insert_input(include_lines, path)', - ) - exec(source_code, docutils.parsers.rst.directives.misc.__dict__) - - class Include(docutils.parsers.rst.directives.misc.Include, SphinxInclude): - def global_substitution(self, lines): - result = [] - for line in lines: - for old, new in global_subtitutions_list: - line = line.replace(old, new) - result.append(line) - return result - - return Include - - def setup(app): - app.add_stylesheet('css/custom.css') - app.connect('source-read', global_substitution_function) - directives.register_directive('include', monkey_patch_include()) + environment_variables = utils.load_env_file() + environment_variables['DOCKER_MAYAN_IMAGE_VERSION'] = mayan.__version__ + substitutions = utils.generate_substitutions( + dictionary=environment_variables + ) - -environment_variables = load_env_file() -environment_variables['DOCKER_MAYAN_IMAGE_VERSION'] = mayan.__version__ -global_subtitutions_list = generate_substitutions( - dictionary=environment_variables -) + app.add_stylesheet(filename='css/custom.css') + app.connect( + event='source-read', callback=callbacks.get_source_read_callback( + substitutions=substitutions + ) + ) + directives.register_directive( + name='include', directive=patches.monkey_patch_include( + substitutions=substitutions + ) + ) diff --git a/docs/patches.py b/docs/patches.py new file mode 100644 index 0000000000..ad8d31d733 --- /dev/null +++ b/docs/patches.py @@ -0,0 +1,36 @@ +from __future__ import unicode_literals + +import inspect + +import docutils.parsers.rst.directives.misc +from sphinx.directives.other import Include as SphinxInclude + + +def monkey_patch_include(substitutions): + """ + Monkey patch docutil's Include directive to support global substitutions. + The Include class doesn't have a hook to modify the content before + inserting it back, so we add a call to our own transformation + method. We patch the base Include class, recreate Sphinx's Include class, + and register it as the new main include directive. + All this avoids copy and paste of the original code here. + """ + source_code = ''.join(inspect.getsourcelines( + docutils.parsers.rst.directives.misc.Include)[0] + ) + source_code = source_code.replace( + 'self.state_machine.insert_input(include_lines, path)', + 'include_lines=self.global_substitution(lines=include_lines)\n self.state_machine.insert_input(include_lines, path)', + ) + exec(source_code, docutils.parsers.rst.directives.misc.__dict__) + + class Include(docutils.parsers.rst.directives.misc.Include, SphinxInclude): + def global_substitution(self, lines): + result = [] + for line in lines: + for old, new in substitutions: + line = line.replace(old, new) + result.append(line) + return result + + return Include diff --git a/docs/utils.py b/docs/utils.py new file mode 100644 index 0000000000..bed070d934 --- /dev/null +++ b/docs/utils.py @@ -0,0 +1,21 @@ +from __future__ import unicode_literals + + +def load_env_file(filename='../config.env'): + result = {} + with open(filename) as file_object: + for line in file_object: + if not line.startswith('#'): + key, value = line.strip().split('=') + result[key] = value + + return result + + +def generate_substitutions(dictionary): + result = [] + + for key, value in dictionary.items(): + result.append(('|{}|'.format(key), value)) + + return result