From: Thanh Ha Date: Thu, 25 Jan 2018 01:41:17 +0000 (-0500) Subject: Move code out of __init__ and into conf.py module X-Git-Tag: v0.2.1~25 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F18%2F8518%2F3;p=releng%2Fdocs-conf.git Move code out of __init__ and into conf.py module This allows us to use __init__.py to store __version__ and other metadata strings and be reused in local documentation and setup.py to shore a single version file. The problem with having code in __init__.py is that the code will be executed even if all the documentation code wants to pull is the metadata variable. Change-Id: I7382c81508be42a41c99f392f8006cfdf9887258 Signed-off-by: Thanh Ha --- diff --git a/docs/conf.py b/docs/conf.py index cc5b98c..369cfc2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -11,12 +11,12 @@ ############################################################################## import os -import pkg_resources import sys # Sys.path for RTD to resolve docs_conf package sys.path.insert(0, os.path.abspath('..')) -from docs_conf import * +from docs_conf import __version__ +from docs_conf.conf import * -version=pkg_resources.require("lfdocs-conf")[0].version -release=version +version=__version__ +release=__version__ diff --git a/docs_conf/__init__.py b/docs_conf/__init__.py index 6def52a..b42d3d5 100644 --- a/docs_conf/__init__.py +++ b/docs_conf/__init__.py @@ -13,97 +13,6 @@ Configure sphinx-doc through an YAML file. """ - -import imp -import os.path - -import sphinx_bootstrap_theme -import yaml - - -def _merge_yaml(x, y): - """Merge dictionary 'y' into 'x'. - - This transaction will overwrite existing data values in "y" with values - from "x". - """ - z = x.copy() - z.update(y) - return z - - -def collect_project_and_config(): - """Pull project and configuration by merging all config sources. - - Order of precedence: - - 1) local conf.yaml - 2) defaults/PROJECT.yaml - 3) defaults/default.yaml - - Return the project name and merged configs from the calling project - and per-project defaults. - """ - if not os.path.isfile('conf.yaml'): - raise IOError("No conf.yaml file found at: {}".format(os.getcwd())) - - with open('conf.yaml', 'r') as f: - local_config = yaml.safe_load(f) - - project_cfg = local_config.get('project_cfg', None) - - _, docs_path, _ = imp.find_module('docs_conf') - - default_cfg = os.path.join(docs_path, 'defaults', 'default.yaml') - with open(os.path.join(docs_path, default_cfg), 'r') as f: - effective_config = yaml.safe_load(f) - - project_cfg_file = os.path.join(docs_path, 'defaults', '{}.yaml'.format(project_cfg)) - if os.path.isfile(project_cfg_file): - with open(os.path.join(docs_path, project_cfg_file), 'r') as f: - _project_cfg_data = yaml.safe_load(f) - effective_config = _merge_yaml(effective_config, _project_cfg_data) - - effective_config = _merge_yaml(effective_config, local_config) - - return effective_config - - -cfg = collect_project_and_config() - -# Parse the config and pull in sphinx conf.py settings -project = cfg.get('project') -release = cfg.get('release') -version = cfg.get('version') -author = cfg.get('author') -copyright = cfg.get('copyright') - -needs_sphinx = cfg.get('needs_sphinx', '1.0') -exclude_patterns = cfg.get('exclude_patterns', []) -extensions = cfg.get('extensions', []) -language = cfg.get('language', None) -master_doc = cfg.get('master_doc', 'index') -pygments_style = cfg.get('pygments_style', 'sphinx') -source_suffix = cfg.get('source_suffix', '.rst') -templates_path = cfg.get('templates_path', ['_templates']) -todo_include_todos = cfg.get('todo_include_todos', False) - -html_extra_path = cfg.get('html_extra_path', []) -html_favicon = cfg.get('html_favicon', 'favicon.ico') -html_logo = cfg.get('html_logo', '_static/logo.png') -html_sidebars = cfg.get('html_sidebars', {'**': ['localtoc.html', 'relations.html'], }) -html_static_path = cfg.get('html_static_path', ['_static']) -html_theme = cfg.get('html_theme', 'bootstrap') -html_theme_options = cfg.get('html_theme_options', { - 'bootswatch_theme': "cerulean", - 'navbar_sidebarrel': False, - 'source_link_position': "footer", -}) -html_theme_path = cfg.get('html_theme_path', sphinx_bootstrap_theme.get_html_theme_path()) -htmlhelp_basename = cfg.get('htmlhelp_basename', 'DocsConf') - -intersphinx_mapping = { - 'global-jjb': ('http://global-jjb.releng.linuxfoundation.org/en/latest/', None), - 'lftools': ('http://lftools.releng.linuxfoundation.org/en/latest/', None), - 'python': ('https://docs.python.org/', None), -} +__author__ = 'Linux Foundation Releng' +__summary__ = 'Linux Foundation DocsConf' +__version__ = '0.2.0-dev' diff --git a/docs_conf/conf.py b/docs_conf/conf.py new file mode 100644 index 0000000..6def52a --- /dev/null +++ b/docs_conf/conf.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2017-2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""LF Sphinx Docs Config. + +Configure sphinx-doc through an YAML file. +""" + +import imp +import os.path + +import sphinx_bootstrap_theme +import yaml + + +def _merge_yaml(x, y): + """Merge dictionary 'y' into 'x'. + + This transaction will overwrite existing data values in "y" with values + from "x". + """ + z = x.copy() + z.update(y) + return z + + +def collect_project_and_config(): + """Pull project and configuration by merging all config sources. + + Order of precedence: + + 1) local conf.yaml + 2) defaults/PROJECT.yaml + 3) defaults/default.yaml + + Return the project name and merged configs from the calling project + and per-project defaults. + """ + if not os.path.isfile('conf.yaml'): + raise IOError("No conf.yaml file found at: {}".format(os.getcwd())) + + with open('conf.yaml', 'r') as f: + local_config = yaml.safe_load(f) + + project_cfg = local_config.get('project_cfg', None) + + _, docs_path, _ = imp.find_module('docs_conf') + + default_cfg = os.path.join(docs_path, 'defaults', 'default.yaml') + with open(os.path.join(docs_path, default_cfg), 'r') as f: + effective_config = yaml.safe_load(f) + + project_cfg_file = os.path.join(docs_path, 'defaults', '{}.yaml'.format(project_cfg)) + if os.path.isfile(project_cfg_file): + with open(os.path.join(docs_path, project_cfg_file), 'r') as f: + _project_cfg_data = yaml.safe_load(f) + effective_config = _merge_yaml(effective_config, _project_cfg_data) + + effective_config = _merge_yaml(effective_config, local_config) + + return effective_config + + +cfg = collect_project_and_config() + +# Parse the config and pull in sphinx conf.py settings +project = cfg.get('project') +release = cfg.get('release') +version = cfg.get('version') +author = cfg.get('author') +copyright = cfg.get('copyright') + +needs_sphinx = cfg.get('needs_sphinx', '1.0') +exclude_patterns = cfg.get('exclude_patterns', []) +extensions = cfg.get('extensions', []) +language = cfg.get('language', None) +master_doc = cfg.get('master_doc', 'index') +pygments_style = cfg.get('pygments_style', 'sphinx') +source_suffix = cfg.get('source_suffix', '.rst') +templates_path = cfg.get('templates_path', ['_templates']) +todo_include_todos = cfg.get('todo_include_todos', False) + +html_extra_path = cfg.get('html_extra_path', []) +html_favicon = cfg.get('html_favicon', 'favicon.ico') +html_logo = cfg.get('html_logo', '_static/logo.png') +html_sidebars = cfg.get('html_sidebars', {'**': ['localtoc.html', 'relations.html'], }) +html_static_path = cfg.get('html_static_path', ['_static']) +html_theme = cfg.get('html_theme', 'bootstrap') +html_theme_options = cfg.get('html_theme_options', { + 'bootswatch_theme': "cerulean", + 'navbar_sidebarrel': False, + 'source_link_position': "footer", +}) +html_theme_path = cfg.get('html_theme_path', sphinx_bootstrap_theme.get_html_theme_path()) +htmlhelp_basename = cfg.get('htmlhelp_basename', 'DocsConf') + +intersphinx_mapping = { + 'global-jjb': ('http://global-jjb.releng.linuxfoundation.org/en/latest/', None), + 'lftools': ('http://lftools.releng.linuxfoundation.org/en/latest/', None), + 'python': ('https://docs.python.org/', None), +} diff --git a/setup.py b/setup.py index 6c9ff32..bc3e72d 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,11 @@ """ Setup for Docs Configuration """ -__author__ = 'Linux Foundation Releng' -__summary__ = 'Linux Foundation DocsConf' -__version__ = '0.2.0-dev' - - from setuptools import setup, find_packages +from docs_conf import __author__ +from docs_conf import __version__ + with open('requirements.txt') as f: install_reqs = f.read().splitlines() diff --git a/tests/test_simple.py b/tests/test_simple.py index 0ffcf44..3f30995 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -24,7 +24,7 @@ def config(tmpdir): """ # Create the base 'conf.py' confpy = tmpdir.join('conf.py') - confpy.write("from docs_conf import *") + confpy.write("from docs_conf.conf import *") # Create conf.cfg file with test defaults # TODO: Make this dynamic so each test can set their own conf.cfg