Re: Migration docutils 0.12 to 0.13

Jean Baptiste Favre <[email protected]>
Newsgroups gmane.text.docutils.user
Message-ID <[email protected]>
Still have the same error.
The full traceback is attached, sorry to have forgotten it before.

My subclass is as follow:

from docutils import nodes
from docutils.parsers.rst import states
from docutils.utils import unescape

# Customize parser.inliner in the only way that Sphinx supports.
# docutils.parsers.rst.Parser takes an instance of states.Inliner or a
# subclass but Sphinx initializes it from
# SphinxStandaloneReader.set_parser('restructuredtext') which is called
# from Publisher.set_components() and initializes the parser without
# arguments.

BaseInliner = states.Inliner
class Inliner(BaseInliner):

  def init_customizations(self, settings):
    BaseInliner.init_customizations(self, settings)

    issue_pattern = re.compile(u'''
      {start_string_prefix}
      TS-\d+
      {end_string_suffix}'''.format(
        start_string_prefix=self.start_string_prefix,
        end_string_suffix=self.end_string_suffix),
      re.VERBOSE | re.UNICODE)

    self.implicit_dispatch.append((issue_pattern, self.issue_reference))

  def issue_reference(self, match, lineno):
    text = match.group(0)

    rawsource = unescape(text, True)
    text = unescape(text, False)

    refuri = 'https://issues.apache.org/jira/browse/' + text

    return [nodes.reference(rawsource, text, refuri=refuri)]

states.Inliner = Inliner


Thanks,
Jean Baptiste

On 02/01/2017 22:53, David Goodger wrote:
> On Mon, Jan 2, 2017 at 3:26 PM, Jean Baptiste Favre
> <[email protected]> wrote:
>> Hello David,
>>
>> Thanks for your answer.
>> Unfortunatly, the patch doesn't help.
>>
>> I still have a "KeyError: 'non_unescaped_whitespace_escape_before'" in
>> docutils/parsers/rst/states.py at line 533.
> 
> In future, please send a full traceback. We need to know the context
> (what called what).
> 
>> I guess it's the same kind of problem and I should redefine all local
>> attributes (from lines 655 to 687) as class attributes as you did in the
>> patch.
> 
> I think I see what the problem is now. docutils/parsers/rst/states.py,
> Inliner.init_customizations contains the following line::
> 
>     args.update(vars(self.__class__))
> 
> The "vars" function gets the __dict__ from the object's class. But
> you're using a subclass, so vars returns the __dict__ of the subclass,
> without the the superclass's attributes, which is what is needed. It's
> a bit of metaprogramming gone wrong. It's not safe for subclassing,
> which is a bug. It's already a bit of a kludgey shortcut. We could
> replace it with::
> 
>     args.update(vars(Inliner))
> 
> That's a bit smelly, but I see it's done elsewhere in the code. Try
> making that change (patch attached).
> 
> We may have to explicitly refer to all the class attributes. I'll think on it.
> 
> David Goodger
> 
>> I'll test it asap and report,
>> Cheers,
>> Jean Baptiste
>>
>>
>> On 02/01/2017 21:24, David Goodger wrote:
>>> On Sun, Jan 1, 2017 at 4:17 PM, Jean Baptiste Favre
>>> <[email protected]> wrote:
>>>> Hello,
>>>> I tried another workaround.
>>>> Instead of using a custom class, I overrided init_cutomizations method
>>>> using:
>>>>
>>>>   class Inliner(BaseInliner):
>>>>     def __init(self):
>>>>       BaseInliner.__init__(self)
>>>>
>>>>     def init_customizations(self, settings):
>>>>       BaseInliner.init_customizations(self, settings)
>>>>
>>>>       issue_pattern = re.compile(u'''
>>>>         {start_string_prefix}
>>>>         TS-\d+
>>>>         {end_string_suffix}'''.format(
>>>>           start_string_prefix=self.start_string_prefix,
>>>>           end_string_suffix=self.end_string_suffix),
>>>>         re.VERBOSE | re.UNICODE)
>>>>
>>>>       self.implicit_dispatch.append((issue_pattern, self.issue_reference))
>>>>
>>>> I don't call explicitly init_customizations, but it's called: during
>>>> run, I still get the error:
>>>>
>>>>   Exception occurred:
>>>>     File
>>>> "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line
>>>> 530, in init_customizations
>>>>         """ % args, re.VERBOSE | re.UNICODE),
>>>>   KeyError: 'non_unescaped_whitespace_escape_before'
>>>>   The full traceback has been saved in /tmp/sphinx-err-wLtHoF.log, if
>>>> you want to report the issue to the developers.
>>>>   Please also report this if it was a user error, so that a better error
>>>> message can be provided next time.
>>>>   A bug report can be filed in the tracker at
>>>> <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
>>>>   make[6]: *** [man] Error 1
>>>>
>>>> What did I do wrong ?
>>>
>>> You didn't do anything wrong that I can see. There was an internal
>>> change to Docutils in revision 7942 that refactored some code, and a
>>> side effect was to remove some attributes from the Inliner class (they
>>> became locals instead). Your code depends on these class attributes.
>>>
>>> Try applying the attached patch and let us know how it works. The
>>> missing class attributes will be present as instance attributes, which
>>> should be close enough.
>>>
>>> Günter, I think the attached patch should also be rolled into a 0.13.2
>>> bugfix release.
>>>
>>> David Goodger
>>> <http://python.net/~goodger>
>>>
>>>
>>>> On 01/01/2017 20:59, Guenter Milde wrote:
>>>>> On 2016-12-31, Jean Baptiste Favre wrote:
>>>>>> Hello,
>>>>>> I'm facing an issue with Trafficserver documentation [1] which doesn't
>>>>>> build with docutils 0.13.1
>>>>>
>>>>>> Error comes from a custom Inliner class which is defined in doc/conf.py
>>>>>> of Trafficserver project(starting line 163).
>>>>>> This allow to transform text like "(TS-XXXX)" in a link to
>>>>>> trafficserver's Jira
>>>>>
>>>>>> This custom Inliner used to work with docutils 0.12. It fails on 0.13.1
>>>>>> with following error:
>>>>>
>>>>>>   Exception occurred:
>>>>>>     File "conf.py", line 185, in __init__
>>>>>>       start_string_prefix=self.start_string_prefix,
>>>>>>   AttributeError: Inliner instance has no attribute 'start_string_prefix'
>>>>>
>>>>>> Since docutils 0.13, start_string_prefix isn't statically defined. We
>>>>>> have to call init_customiations for that.
>>>>>
>>>>> Yes, this changed with the implementation of the long expected feature
>>>>>
>>>>>   - Apply [ 103 ] Recognize inline markups without word boundaries.
>>>>>
>>>>> and the new configuration setting "character_level_inline_markup".
>>>>>
>>>>>
>>>>>> First problem: one  must pass settings param when calling
>>>>>> init_customizations, but I can't find any format or structure for it.
>>>>>
>>>>>> Second problem: I tried a workaround, creating a InlinerSettings class
>>>>>> with minimal properties so that init_customizations could pass:
>>>>>
>>>>>>   class InlinerSettings:
>>>>>>     character_level_inline_markup=None
>>>>>>     pep_references=None
>>>>>>     rfc_references=None
>>>>>
>>>>>> But, then, I faced another error:
>>>>>
>>>>> ...
>>>>>
>>>>> "settings" is a an object returned from the option parser (optparse module).
>>>>>
>>>>> There are others facing similar problems, e.g. Python distutils.
>>>>> There, I learned the trick is to use
>>>>>
>>>>>   settings = frontend.OptionParser(components=(Parser,)).get_default_values()
>>>>>
>>>>>   -- https://hg.python.org/cpython/rev/db09d760b965
>>>>>
>>>>>
>>>>>> Third problem: since the above tries didn't worked, I'd a look on custom
>>>>>> directives and roles.
>>>>>> But, it does not seems to be allowed to define a role with a custom regex.
>>>>>> This would means I have to rewrite all "(TS-XXXX)" expression into ":TS:
>>>>>> XXXX".
>>>>>
>>>>> This is the "minimal-invasive" approach, it would be work now but might save
>>>>> issues later, as it depends less on Docutils internals.
>>>>>
>>>>>> Is there any way to achieve the migration in a compatible way with
>>>>>> docutils 0.12 *and* without rewriting the docuemntation ?
>>>>>
>>>>> If the above example does not lead to a solution, you could also consider to
>>>>> copy the definition of start_string_prefix ...
>>>>> from states.py (importing utils.punctuation_chars first)
>>>>>
>>>>> Günter
>>
>>
>> ------------------------------------------------------------------------------
>> Check out the vibrant tech community on one of the world's most
>> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>> _______________________________________________
>> Docutils-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/docutils-users
>>
>> Please use "Reply All" to reply to the list.
>>
>>
>> ------------------------------------------------------------------------------
>> Check out the vibrant tech community on one of the world's most 
>> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>>
>>
>> _______________________________________________
>> Docutils-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/docutils-users
>>
>> Please use "Reply All" to reply to the list.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
sphinx-err-uqeJwO.log (text/x-log, 3.1 KB)
# Sphinx version: 1.4.9
# Python version: 2.7.13 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.8
# Last messages:
#   Running Sphinx v1.4.9
#   making output directory...
#   loading pickled environment...
#   not yet created
#   building [mo]: targets for 0 po files that are out of date
#   building [man]: all manpages
#   updating environment:
#   456 added, 0 changed, 0 removed
#   reading sources... [  0%] admin-guide/configuration/cache-basics.en
# Loaded extensions:
#   sphinx.ext.graphviz (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/graphviz.pyc
#   sphinx.ext.autodoc (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/autodoc.pyc
#   sphinx.ext.intersphinx (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/intersphinx.pyc
#   alabaster (0.7.8) from /usr/lib/python2.7/dist-packages/alabaster/__init__.pyc
#   traffic-server (unknown version) from /home/dev/trafficserver-debian/doc/ext/traffic-server.py
#   sphinx.ext.viewcode (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/viewcode.pyc
#   sphinx.ext.coverage (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/coverage.pyc
#   sphinx.ext.todo (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/todo.pyc
#   sphinx.ext.imgmath (1.4.9) from /usr/lib/python2.7/dist-packages/sphinx/ext/imgmath.pyc
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/sphinx/cmdline.py", line 244, in main
    app.build(opts.force_all, filenames)
  File "/usr/lib/python2.7/dist-packages/sphinx/application.py", line 297, in build
    self.builder.build_update()
  File "/usr/lib/python2.7/dist-packages/sphinx/builders/__init__.py", line 246, in build_update
    self.build(['__all__'], to_build)
  File "/usr/lib/python2.7/dist-packages/sphinx/builders/__init__.py", line 265, in build
    self.doctreedir, self.app))
  File "/usr/lib/python2.7/dist-packages/sphinx/environment.py", line 569, in update
    self._read_serial(docnames, app)
  File "/usr/lib/python2.7/dist-packages/sphinx/environment.py", line 589, in _read_serial
    self.read_doc(docname, app)
  File "/usr/lib/python2.7/dist-packages/sphinx/environment.py", line 742, in read_doc
    pub.publish()
  File "/usr/lib/python2.7/dist-packages/docutils/core.py", line 217, in publish
    self.settings)
  File "/usr/lib/python2.7/dist-packages/sphinx/io.py", line 49, in read
    self.parse()
  File "/usr/lib/python2.7/dist-packages/docutils/readers/__init__.py", line 78, in parse
    self.parser.parse(self.input, document)
  File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.py", line 185, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 158, in run
    inliner.init_customizations(document.settings)
  File "conf.py", line 185, in init_customizations
    BaseInliner.init_customizations(self, settings)
  File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 533, in init_customizations
    """ % args, re.VERBOSE | re.UNICODE),
KeyError: 'non_unescaped_whitespace_escape_before'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.