Use 'sphinx.util.logging'

This resolves a deprecation warning. It also allows us to stop storing
the 'app' variable just to enable logging.

Change-Id: I53c0f0d5cc61e32a8f1ac1d48816d2c56d82cc41
This commit is contained in:
Stephen Finucane
2018-04-20 17:20:21 +01:00
parent d6805491ed
commit 9cebd346dc

View File

@@ -20,6 +20,7 @@ from docutils.parsers.rst.directives.tables import Table
from docutils.statemachine import ViewList from docutils.statemachine import ViewList
import pbr.version import pbr.version
import six import six
from sphinx.util import logging
from sphinx.util.osutil import copyfile from sphinx.util.osutil import copyfile
import yaml import yaml
@@ -30,6 +31,8 @@ from os_api_ref.http_codes import HTTPResponseCodeDirective
__version__ = pbr.version.VersionInfo( __version__ = pbr.version.VersionInfo(
'os_api_ref').version_string() 'os_api_ref').version_string()
LOG = logging.getLogger(__name__)
# This is to allow for a graceful swap from oslosphinx to openstackdocstheme # This is to allow for a graceful swap from oslosphinx to openstackdocstheme
THEME = 'openstackdocstheme' THEME = 'openstackdocstheme'
@@ -167,8 +170,6 @@ class RestMethodDirective(rst.Directive):
lineno = self.state_machine.abs_line_number() lineno = self.state_machine.abs_line_number()
target = nodes.target() target = nodes.target()
section = nodes.section(classes=["detail-control"]) section = nodes.section(classes=["detail-control"])
# env = self.state.document.settings.env
# env.app.info("Parent %s" % self.state.parent.attributes)
node = rest_method() node = rest_method()
@@ -219,25 +220,22 @@ class RestParametersDirective(Table):
return YAML_CACHE[fpath] return YAML_CACHE[fpath]
lookup = {} lookup = {}
# self.app.info("Fpath: %s" % fpath)
try: try:
with open(fpath, 'r') as stream: with open(fpath, 'r') as stream:
lookup = ordered_load(stream) lookup = ordered_load(stream)
except IOError: except IOError:
self.app.warn( LOG.warning("Parameters file not found, %s", fpath,
"Parameters file %s not found" % fpath, location=(self.env.docname, None))
(self.env.docname, None))
return return
except yaml.YAMLError as exc: except yaml.YAMLError as exc:
self.app.warn(exc) LOG.exception(exc_info=exc)
raise raise
if lookup: if lookup:
self._check_yaml_sorting(fpath, lookup) self._check_yaml_sorting(fpath, lookup)
else: else:
self.app.warn( LOG.warning("Parameters file is empty, %s", fpath,
"Parameters file is empty %s" % fpath, location=(self.env.docname, None))
(self.env.docname, None))
return return
YAML_CACHE[fpath] = lookup YAML_CACHE[fpath] = lookup
@@ -265,13 +263,11 @@ class RestParametersDirective(Table):
# use of an invalid 'in' value # use of an invalid 'in' value
if value['in'] not in sections: if value['in'] not in sections:
self.app.warn( LOG.warning("``%s`` is not a valid value for 'in' (must be "
"``%s`` is not a valid value for 'in' (must be one of: %s)" "one of: %s). (see ``%s``)",
". (see ``%s``)" % ( value['in'],
value['in'], ", ".join(sorted(sections.keys())),
", ".join(sorted(sections.keys())), key)
key)
)
continue continue
if last is None: if last is None:
@@ -281,17 +277,13 @@ class RestParametersDirective(Table):
current_section = value['in'] current_section = value['in']
last_section = last[1]['in'] last_section = last[1]['in']
if sections[current_section] < sections[last_section]: if sections[current_section] < sections[last_section]:
self.app.warn( LOG.warning("Section out of order. All parameters in section "
"Section out of order. All parameters in section ``%s`` " "``%s`` should be after section ``%s``. (see "
"should be after section ``%s``. (see ``%s``)" % ( "``%s``)", last_section, current_section, last[0])
last_section,
current_section,
last[0]))
if (sections[value['in']] == sections[last[1]['in']] and if (sections[value['in']] == sections[last[1]['in']] and
key.lower() < last[0].lower()): key.lower() < last[0].lower()):
self.app.warn( LOG.warning("Parameters out of order ``%s`` should be after "
"Parameters out of order ``%s`` should be after ``%s``" % ( "``%s``", last[0], key)
last[0], key))
last = (key, value) last = (key, value)
def yaml_from_file(self, fpath): def yaml_from_file(self, fpath):
@@ -307,17 +299,14 @@ class RestParametersDirective(Table):
content = "\n".join(self.content) content = "\n".join(self.content)
parsed = yaml.safe_load(content) parsed = yaml.safe_load(content)
# self.app.info("Params loaded is %s" % parsed)
# self.app.info("Lookup table looks like %s" % lookup)
new_content = list() new_content = list()
for paramlist in parsed: for paramlist in parsed:
if not isinstance(paramlist, dict): if not isinstance(paramlist, dict):
self.app.warn( location = (self.state_machine.node.source,
("Invalid parameter definition ``%s``. Expected " self.state_machine.node.line)
"format: ``name: reference``. " LOG.warning("Invalid parameter definition ``%s``. Expected "
" Skipping." % paramlist), "format: ``name: reference``. Skipping.",
(self.state_machine.node.source, paramlist, location=location)
self.state_machine.node.line))
continue continue
for name, ref in paramlist.items(): for name, ref in paramlist.items():
if ref in lookup: if ref in lookup:
@@ -328,11 +317,10 @@ class RestParametersDirective(Table):
# used this way, however it does provide a way to # used this way, however it does provide a way to
# track down where the parameters list is that is # track down where the parameters list is that is
# wrong. So it's good enough for now. # wrong. So it's good enough for now.
self.app.warn( location = (self.state_machine.node.source,
("No field definition for ``%s`` found in ``%s``. " self.state_machine.node.line)
" Skipping." % (ref, fpath)), LOG.warning("No field definition for ``%s`` found in "
(self.state_machine.node.source, "``%s``. Skipping.", ref, fpath)
self.state_machine.node.line))
# Check for path params in stanza # Check for path params in stanza
for i, param in enumerate(self.env.path_params): for i, param in enumerate(self.env.path_params):
@@ -346,19 +334,15 @@ class RestParametersDirective(Table):
# Warn that path parameters are not set in rest_parameter # Warn that path parameters are not set in rest_parameter
# stanza and will not appear in the generated table. # stanza and will not appear in the generated table.
for param in self.env.path_params: for param in self.env.path_params:
self.app.warn( location = (self.state_machine.node.source,
("No path parameter ``%s`` found in rest_parameter" self.state_machine.node.line)
" stanza.\n" LOG.warning("No path parameter ``%s`` found in rest_parameter"
% param.rstrip('}').lstrip('{')), " stanza.\n", param.rstrip('}').lstrip('{'))
(self.state_machine.node.source,
self.state_machine.node.line))
# self.app.info("New content %s" % new_content)
self.yaml = new_content self.yaml = new_content
def run(self): def run(self):
self.env = self.state.document.settings.env self.env = self.state.document.settings.env
self.app = self.env.app
# Make sure we have some content, which should be yaml that # Make sure we have some content, which should be yaml that
# defines some parameters. # defines some parameters.
@@ -395,7 +379,6 @@ class RestParametersDirective(Table):
self.col_widths = self.col_widths[1] self.col_widths = self.col_widths[1]
# Actually convert the yaml # Actually convert the yaml
title, messages = self.make_title() title, messages = self.make_title()
# self.app.info("Title %s, messages %s" % (title, messages))
table_node = self.build_table() table_node = self.build_table()
self.add_name(table_node) self.add_name(table_node)
if title: if title:
@@ -435,7 +418,6 @@ class RestParametersDirective(Table):
rows = [] rows = []
groups = [] groups = []
try: try:
# self.app.info("Parsed content is: %s" % self.yaml)
for key, values in self.yaml: for key, values in self.yaml:
min_version = values.get('min_version', '') min_version = values.get('min_version', '')
max_version = values.get('max_version', '') max_version = values.get('max_version', '')
@@ -463,8 +445,8 @@ class RestParametersDirective(Table):
rows.append(trow) rows.append(trow)
except AttributeError as exc: except AttributeError as exc:
if 'key' in locals(): if 'key' in locals():
self.app.warn("Failure on key: %s, values: %s. %s" % LOG.warning("Failure on key: %s, values: %s. %s",
(key, values, exc)) key, values, exc)
else: else:
rows.append(self.show_no_yaml_error()) rows.append(self.show_no_yaml_error())
return rows, groups return rows, groups
@@ -657,8 +639,8 @@ def copy_assets(app, exception):
) )
if app.builder.name != 'html' or exception: if app.builder.name != 'html' or exception:
return return
app.info('Copying assets: %s' % ', '.join(assets)) LOG.info('Copying assets: %s', ', '.join(assets))
app.info('Copying fonts: %s' % ', '.join(fonts)) LOG.info('Copying fonts: %s', ', '.join(fonts))
for asset in assets: for asset in assets:
dest = os.path.join(app.builder.outdir, '_static', asset) dest = os.path.join(app.builder.outdir, '_static', asset)
source = os.path.abspath(os.path.dirname(__file__)) source = os.path.abspath(os.path.dirname(__file__))