def get_releases(self):
        full_path = os.path.join(ROOT_DIR, "CHANGELOG.rst")
        with open(full_path) as f:
            changelog = f.read()
        with mock.patch.object(sys, "stderr"):
            parser = rst.Parser()
            settings = frontend.OptionParser(
                components=(rst.Parser, )).get_default_values()
            document = utils.new_document(changelog, settings)
            parser.parse(changelog, document)
            changelog = document.children
        if len(changelog) != 1:
            self.fail("'%s' file should contain one global section "
                      "with subsections for each release." % full_path)

        releases = []
        for node in changelog[0].children:
            if not isinstance(node, nodes.section):
                continue
            title = node.astext().split("\n", 1)[0]
            result = self.RE_RELEASE.match(title)
            if result:
                releases.append(result.groupdict()["version"])
        if not releases:
            self.fail("'%s' doesn't mention any releases..." % full_path)
        return releases
Пример #2
0
def new_document():
    """There is a similar utility in docutils.utils but it supposes a file source (right?), and does not work straight for (at least) html output (?)"""

    from docutils import frontend  #these are only needed here
    from docutils.utils import Reporter

    source = None
    settings = frontend.OptionParser().get_default_values()

    #these needed for getting a html out - where are the defaults?
    settings.xml_declaration = False
    settings.embed_stylesheet = False
    settings.stylesheet_path = False
    settings.stylesheet = None
    settings.initial_header_level = "1"

    #an attempt to make docutils.nodes.NodeVisitor accept notebook classes

    #this is one-to-one from docutils.utils new_document
    reporter = Reporter(source,
                        settings.report_level,
                        settings.halt_level,
                        stream=settings.warning_stream,
                        debug=settings.debug,
                        encoding=settings.error_encoding,
                        error_handler=settings.error_encoding_error_handler)

    document = nodes.document(settings, reporter)
    return document
Пример #3
0
 def document(self):
     if self._doc is None:
         # Use the rst parsers document output to do as much of the
         # validation as we can without resorting to custom logic (this
         # parser is what sphinx and others use anyway so it's hopefully
         # mature).
         parser_cls = docutils_parser.get_parser_class("rst")
         parser = parser_cls()
         defaults = {
             'halt_level': 5,
             'report_level': 5,
             'quiet': True,
             'file_insertion_enabled': False,
             'traceback': True,
             # Development use only.
             'dump_settings': False,
             'dump_internals': False,
             'dump_transforms': False,
         }
         opt = frontend.OptionParser(components=[parser], defaults=defaults)
         doc = utils.new_document(source_path=self.filename,
                                  settings=opt.get_default_values())
         parser.parse(self.contents, doc)
         self._doc = doc
     return self._doc
Пример #4
0
    def _check_rst_data(self, data):
        """Returns warnings when the provided data doesn't compile."""
        # the include and csv_table directives need this to be a path
        source_path = self.distribution.script_name or "setup.py"
        parser = Parser()
        settings = frontend.OptionParser(
            components=(Parser, )).get_default_values()
        settings.tab_width = 4
        settings.pep_references = None
        settings.rfc_references = None
        reporter = SilentReporter(
            source_path,
            settings.report_level,
            settings.halt_level,
            stream=settings.warning_stream,
            debug=settings.debug,
            encoding=settings.error_encoding,
            error_handler=settings.error_encoding_error_handler,
        )

        document = nodes.document(settings, reporter, source=source_path)
        document.note_source(source_path, -1)
        try:
            parser.parse(data, document)
        except AttributeError as e:
            reporter.messages.append(
                (-1, "Could not finish the parsing: %s." % e, "", {}))

        return reporter.messages
Пример #5
0
def directive_factory(src_path):
    """Create directive instance out of docutils/sphinx process.

    Used in testing or command-line process
    """
    from sphinx_vcs_changelog.changelog import ChangelogWriter

    state_machine = statemachine.StateMachine(
        state_classes=[statemachine.StateWS], initial_state='StateWS')
    base_dir = path.dirname(src_path)

    _instance = object.__new__(ChangelogWriter)
    _instance.name = DIRECTIVE_CHANGELOG
    _instance.arguments = []
    _instance.options = {}
    _instance.content = ""
    _instance.lineno = 123
    _instance.content_offset = 123
    _instance.block_text = ""
    _instance.state = state_machine.get_state()
    _instance.state_machine = state_machine

    document = new_document(src_path)
    document.settings.update({'env': Values(defaults={'srcdir': base_dir})},
                             frontend.OptionParser())
    setattr(_instance.state, 'document', document)
    _instance.prepare()
    return _instance
Пример #6
0
    def _check_rst_data(self, data):
        """Returns warnings when the provided data doesn't compile."""
        source_path = StringIO()
        parser = Parser()
        settings = frontend.OptionParser(components=(Parser,)).get_default_values()
        settings.tab_width = 4
        settings.pep_references = None
        settings.rfc_references = None
        reporter = SilentReporter(source_path,
                          settings.report_level,
                          settings.halt_level,
                          stream=settings.warning_stream,
                          debug=settings.debug,
                          encoding=settings.error_encoding,
                          error_handler=settings.error_encoding_error_handler)

        document = nodes.document(settings, reporter, source=source_path)
        document.note_source(source_path, -1)
        try:
            parser.parse(data, document)
        except AttributeError as e:
            reporter.messages.append(
                (-1, 'Could not finish the parsing: %s.' % e, '', {}))

        return reporter.messages
def new_document(source_path, settings=None):
    """
    Return a new empty document object.

    :Parameters:
        `source_path` : string
            The path to or description of the source text of the document.
        `settings` : optparse.Values object
            Runtime settings.  If none are provided, a default core set will
            be used.  If you will use the document object with any Docutils
            components, you must provide their default settings as well.  For
            example, if parsing, at least provide the parser settings,
            obtainable as follows::

                settings = docutils.frontend.OptionParser(
                    components=(docutils.parsers.rst.Parser,)
                    ).get_default_values()
    """
    from docutils import frontend
    if settings is None:
        settings = frontend.OptionParser().get_default_values()
    source_path = decode_path(source_path)
    reporter = new_reporter(source_path, settings)
    document = nodes.document(settings, reporter, source=source_path)
    document.note_source(source_path, -1)
    return document
Пример #8
0
def parse_text(text):
    parser = rst.Parser()
    settings = frontend.OptionParser(
        components=(rst.Parser, )).get_default_values()
    document = utils.new_document(text, settings)
    parser.parse(text, document)
    return document.children
Пример #9
0
class ParserTestCase(CustomTestCase):
    """
    Output checker for the parser.

    Should probably be called ParserOutputChecker, but I can deal with
    that later when/if someone comes up with a category of parser test
    cases that have nothing to do with the input and output of the parser.
    """

    parser = rst.Parser()
    """Parser shared by all ParserTestCases."""

    option_parser = frontend.OptionParser(components=(parser, ))
    settings = option_parser.get_default_values()
    settings.report_level = 5
    settings.halt_level = 5
    settings.debug = package_unittest.debug

    def test_parser(self):
        if self.run_in_debugger:
            pdb.set_trace()
        document = utils.new_document('test data', self.settings)
        self.parser.parse(self.input, document)
        output = document.pformat()
        self.compare_output(self.input, output, self.expected)
Пример #10
0
class TransformTestCase(CustomTestCase):
    """
    Output checker for the transform.

    Should probably be called TransformOutputChecker, but I can deal with
    that later when/if someone comes up with a category of transform test
    cases that have nothing to do with the input and output of the transform.
    """

    option_parser = frontend.OptionParser(components=(rst.Parser, ))
    settings = option_parser.get_default_values()
    settings.report_level = 1
    settings.halt_level = 5
    settings.debug = package_unittest.debug
    settings.warning_stream = DevNull()

    def __init__(self, *args, **kwargs):
        self.transforms = kwargs['transforms']
        """List of transforms to perform for this test case."""

        self.parser = kwargs['parser']
        """Input parser for this test case."""

        del kwargs['transforms'], kwargs['parser']  # only wanted here
        CustomTestCase.__init__(self, *args, **kwargs)

    def supports(self, format):
        return 1

    def test_transforms(self):
        if self.run_in_debugger:
            pdb.set_trace()
        document = utils.new_document('test data', self.settings)
        self.parser.parse(self.input, document)
        # Don't do a ``populate_from_components()`` because that would
        # enable the Transformer's default transforms.
        document.transformer.add_transforms(self.transforms)
        document.transformer.add_transform(universal.TestMessages)
        document.transformer.components['writer'] = self
        document.transformer.apply_transforms()
        output = document.pformat()
        self.compare_output(self.input, output, self.expected)

    def test_transforms_verbosely(self):
        if self.run_in_debugger:
            pdb.set_trace()
        print '\n', self.id
        print '-' * 70
        print self.input
        document = utils.new_document('test data', self.settings)
        self.parser.parse(self.input, document)
        print '-' * 70
        print document.pformat()
        for transformClass in self.transforms:
            transformClass(document).apply()
        output = document.pformat()
        print '-' * 70
        print output
        self.compare_output(self.input, output, self.expected)
Пример #11
0
 def __init__(self, app):
     env = sphinx.environment.BuildEnvironment(app)
     env.setup(app)
     env.temp_data['docname'] = 'mock-doc'
     settings = frontend.OptionParser(
         components=(parsers.rst.Parser, )).get_default_values()
     settings.env = env
     self.document = utils.new_document('', settings)
Пример #12
0
def setup_module():
    global app, settings, parser
    app = TestApp(cleanenv=True)
    optparser = frontend.OptionParser(components=(rst.Parser, HTMLWriter,
                                                  LaTeXWriter))
    settings = optparser.get_default_values()
    settings.env = app.builder.env
    parser = rst.Parser()
Пример #13
0
class ErrorReportingTests(unittest.TestCase):
    """
    Test cases where error reporting can go wrong.

    Do not test the exact output (as this varies with the locale), just
    ensure that the correct exception is thrown.
    """

    # These tests fail with a 'problematic locale' and
    # (revision < 7035) and Python-2.

    parser = parsers.rst.Parser()
    """Parser shared by all ParserTestCases."""

    option_parser = frontend.OptionParser(components=(parsers.rst.Parser,))
    settings = option_parser.get_default_values()
    settings.report_level = 1
    settings.halt_level = 1
    settings.warning_stream = ''
    document = utils.new_document('test data', settings)

    def setUp(self):
        if testlocale:
            locale.setlocale(locale.LC_ALL, testlocale)

    def tearDown(self):
        if testlocale:
            locale.setlocale(locale.LC_ALL, oldlocale)

    def test_include(self):
        source = ('.. include:: bogus.txt')
        self.assertRaises(utils.SystemMessage,
                          self.parser.parse, source, self.document)

    def test_raw_file(self):
        source = ('.. raw:: html\n'
                  '   :file: bogus.html\n')
        self.assertRaises(utils.SystemMessage,
                          self.parser.parse, source, self.document)

    def test_raw_url(self):
        source = ('.. raw:: html\n'
                  '   :url: http://bogus.html\n')
        self.assertRaises(utils.SystemMessage,
                          self.parser.parse, source, self.document)

    def test_csv_table(self):
        source = ('.. csv-table:: external file\n'
                  '   :file: bogus.csv\n')
        self.assertRaises(utils.SystemMessage,
                          self.parser.parse, source, self.document)

    def test_csv_table_url(self):
        source = ('.. csv-table:: external URL\n'
                  '   :url: ftp://bogus.csv\n')
        self.assertRaises(utils.SystemMessage,
                          self.parser.parse, source, self.document)
Пример #14
0
 def __init__(self):
     env = sphinx.environment.BuildEnvironment(None, None, MockConfig())
     CPPDomain(env)
     CDomain(env)
     env.temp_data['docname'] = 'mock-doc'
     settings = frontend.OptionParser(
         components=(parsers.rst.Parser, )).get_default_values()
     settings.env = env
     self.document = utils.new_document('', settings)
Пример #15
0
def setup_module():
    global settings, parser
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        # DeprecationWarning: The frontend.OptionParser class will be replaced
        # by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
        optparser = frontend.OptionParser(components=(rst.Parser, ))
    settings = optparser.get_default_values()
    parser = rst.Parser()
Пример #16
0
def create_new_document():
    with warnings.catch_warnings():
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        # DeprecationWarning: The frontend.OptionParser class will be replaced
        # by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
        settings = frontend.OptionParser(
            components=(rst.Parser, )).get_default_values()
    settings.id_prefix = 'id'
    document = new_document('dummy.txt', settings)
    return document
Пример #17
0
class PEPParserTestCase(ParserTestCase):
    """PEP-specific parser test case."""

    parser = rst.Parser(rfc2822=1, inliner=pep.Inliner())
    """Parser shared by all PEPParserTestCases."""

    option_parser = frontend.OptionParser(components=(parser, pep.Reader))
    settings = option_parser.get_default_values()
    settings.report_level = 5
    settings.halt_level = 5
    settings.debug = package_unittest.debug
Пример #18
0
    def from_rst(cls, text):
        self = cls()

        parser = rst.Parser()
        settings = frontend.OptionParser(
            components=(rst.Parser, )).get_default_values()
        document = utils.new_document(text, settings)
        parser.parse(text, document)

        self._children.extend(self._parse_elements(document.children))
        return self
Пример #19
0
def getTree(rstTxt):
    """From a given RST string, parses into an abstract document tree, which is
       then returned.
    """
    RstParser = parsers.get_parser_class("rst")
    settings = frontend.OptionParser(
        components=(RstParser, )).get_default_values()
    doc = utils.new_document("", settings)
    psr = RstParser()
    psr.parse(rstTxt, doc)
    return doc
def run_parser(source, parser_class):
    inliner = InlinerLSP(doc_text=source)
    parser = parser_class(inliner=inliner)
    option_parser = frontend.OptionParser(components=(rst.Parser,))
    settings = option_parser.get_default_values()
    settings.report_level = 5
    settings.halt_level = 5
    # settings.debug = package_unittest.debug
    document = utils.new_document("test data", settings)
    parser.parse(source, document)
    return document
Пример #21
0
def settings(app):
    texescape.init()  # otherwise done by the latex builder
    optparser = frontend.OptionParser(components=(RstParser, HTMLWriter,
                                                  LaTeXWriter))
    settings = optparser.get_default_values()
    settings.env = app.builder.env
    settings.env.temp_data['docname'] = 'dummy'
    domain_context = sphinx_domains(settings.env)
    domain_context.enable()
    yield settings
    domain_context.disable()
Пример #22
0
def parse_text(text):
    parser = rst.Parser()
    settings = frontend.OptionParser(
        components=(rst.Parser, )).get_default_values()
    document = utils.new_document(text, settings)
    try:
        parser.parse(text, document)
    except Exception as e:
        print(f"WARNING: {e}")
        return []
    return document.children
Пример #23
0
def setup_module():
    global app, settings, parser
    texescape.init()  # otherwise done by the latex builder
    app = TestApp()
    optparser = frontend.OptionParser(components=(rst.Parser, HTMLWriter,
                                                  LaTeXWriter))
    settings = optparser.get_default_values()
    settings.env = app.builder.env
    settings.env.patch_lookup_functions()
    settings.env.temp_data['docname'] = 'dummy'
    parser = rst.Parser()
Пример #24
0
class RecommonmarkParserTestCase(ParserTestCase):
    """Recommonmark-specific parser test case."""

    parser = recommonmark_wrapper.Parser()
    """Parser shared by all RecommonmarkParserTestCases."""

    option_parser = frontend.OptionParser(
        components=(recommonmark_wrapper.Parser, ))
    settings = option_parser.get_default_values()
    settings.report_level = 5
    settings.halt_level = 5
    settings.debug = package_unittest.debug
Пример #25
0
    def __init__(self, app):
        from breathe.project import ProjectInfoFactory
        from breathe.parser import DoxygenParserFactory

        env = sphinx.environment.BuildEnvironment(app)
        env.setup(app)
        env.temp_data["docname"] = "mock-doc"
        env.temp_data["breathe_project_info_factory"] = ProjectInfoFactory(app)
        env.temp_data["breathe_parser_factory"] = DoxygenParserFactory(app)
        settings = frontend.OptionParser(components=(parsers.rst.Parser,)).get_default_values()
        settings.env = env
        self.document = utils.new_document("", settings)
Пример #26
0
    def test_inputrestrictions(self):
        parser_class = parsers.get_parser_class('rst')
        parser = parser_class()
        document = utils.new_document('test data', frontend.OptionParser(
                    components=(parser, )).get_default_values())

        if sys.version_info < (3,):
            # supplying string input is supported, but only if ascii-decodable
            self.assertRaises(UnicodeDecodeError,
                              parser.parse, b'hol%s' % chr(224), document)
        else:
            # input must be unicode at all times
            self.assertRaises(TypeError, parser.parse, b'hol', document)
Пример #27
0
 def __init__(self):
     settings = frontend.OptionParser().get_default_values()
     settings.report_level = 1
     Reporter.__init__(
         self,
         source='sphinxcontrib.clearquest',
         report_level=settings.report_level,
         halt_level=settings.halt_level,
         stream=settings.warning_stream,
         debug=settings.debug,
         encoding=settings.error_encoding,
         error_handler=settings.error_encoding_error_handler
     )
Пример #28
0
def settings(app):
    texescape.init()  # otherwise done by the latex builder
    optparser = frontend.OptionParser(components=(RstParser, HTMLWriter,
                                                  LaTeXWriter))
    settings = optparser.get_default_values()
    settings.smart_quotes = True
    settings.env = app.builder.env
    settings.env.temp_data['docname'] = 'dummy'
    settings.contentsname = 'dummy'
    settings.rfc_base_url = 'http://tools.ietf.org/html/'
    domain_context = sphinx_domains(settings.env)
    domain_context.enable()
    yield settings
    domain_context.disable()
Пример #29
0
    def __init__(self, src='sphinxcontrib.xyz'):
        settings = frontend.OptionParser().get_default_values()

        settings.report_level = 1

        Reporter.__init__(self,
                          src,
                          settings.report_level,
                          settings.halt_level,
                          stream=settings.warning_stream,
                          debug=settings.debug,
                          encoding=settings.error_encoding,
                          error_handler=settings.error_encoding_error_handler)

        self.log = logging.getLogger(src)
Пример #30
0
def new_document(source_path, settings=None):
    """
    Return a new empty document object.

    :Parameters:
        `source` : string
            The path to or description of the source text of the document.
        `settings` : optparse.Values object
            Runtime settings.  If none provided, a default set will be used.
    """
    if settings is None:
        settings = frontend.OptionParser().get_default_values()
    reporter = new_reporter(source_path, settings)
    document = nodes.document(settings, reporter, source=source_path)
    document.note_source(source_path, -1)
    return document