Skip to content
This repository has been archived by the owner on Jan 20, 2018. It is now read-only.

Commit

Permalink
Command line args OK + no debug by default.
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Matthieu BARBIER <[email protected]>
  • Loading branch information
jmbarbier committed Oct 15, 2013
1 parent f1ce11c commit 99a8de4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
23 changes: 18 additions & 5 deletions rst2code.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import sys
from docutils.core import publish_doctree
import logging
logging.basicConfig(level=logging.WARNING)
import re
import shutil, os

Expand Down Expand Up @@ -182,30 +181,36 @@ def __init__(self, msg):
self.msg = msg

def main(argv=None):
global OUTPUT_DIR
if argv is None:
argv = sys.argv[1:]
try:
try:
parser = argparse.ArgumentParser("Write code from rst files")
parser.add_argument('outdir', metavar="OUTPUT_DIR", type=str, nargs=1,
help="Output directory base for code")
parser.add_argument('srcfiles', metavar="SRC_FILES", type=str, nargs="+",
parser.add_argument('srcfiles', metavar="SRC_FILES", type=str, nargs=argparse.REMAINDER,
help="Source files (.rst files)")
parser.add_argument('--debug', '-d')
parser.add_argument('--debug', '-d', action='store_true')
args = parser.parse_args(argv)

except Exception as msg:
raise Usage(msg)


# process arguments
DEBUG = args.debug
OUTPUT_DIR = args.outdir
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)
OUTPUT_DIR = args.outdir[0]
SRC_FILES = args.srcfiles

for filename in SRC_FILES:
scan_file(filename)
process_blocks()
clean_output_dir()
write_files()
return 0

except Usage as err:
Expand All @@ -220,9 +225,16 @@ def main(argv=None):


def sphinx_get_doctree(app, doctree, docname):

if app.config.rst2code_debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)

logging.debug("Got doctree")
scan_doctree(doctree,docname)
def sphinx_build_finished(app, exception):
global OUTPUT_DIR
env = app.builder.env
OUTPUT_DIR = app.config.rst2code_output_dir
process_blocks()
Expand All @@ -231,5 +243,6 @@ def sphinx_build_finished(app, exception):
def setup(app):
app.add_config_value("rst2code_output_dir", "./src", "env")
app.add_config_value("rst2code_max_iterations", 10, "env")
app.add_config_value("rst2code_debug", False, "env")
app.connect('doctree-resolved',sphinx_get_doctree)
app.connect('build-finished', sphinx_build_finished)
14 changes: 10 additions & 4 deletions source/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ of input files ::
self.msg = msg

def main(argv=None):
global OUTPUT_DIR
if argv is None:
argv = sys.argv[1:]
try:
try:
parser = argparse.ArgumentParser("Write code from rst files")
parser.add_argument('outdir', metavar="OUTPUT_DIR", type=str, nargs=1,
help="Output directory base for code")
parser.add_argument('srcfiles', metavar="SRC_FILES", type=str, nargs="+",
parser.add_argument('srcfiles', metavar="SRC_FILES", type=str, nargs=argparse.REMAINDER,
help="Source files (.rst files)")
parser.add_argument('--debug', '-d')
parser.add_argument('--debug', '-d', action='store_true')
args = parser.parse_args(argv)
except Exception as msg:
raise Usage(msg)
# process arguments
DEBUG = args.debug
OUTPUT_DIR = args.outdir
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)
OUTPUT_DIR = args.outdir[0]
SRC_FILES = args.srcfiles
for filename in SRC_FILES:
scan_file(filename)
process_blocks()
clean_output_dir()
write_files()
return 0
except Usage as err:
Expand Down
1 change: 0 additions & 1 deletion source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Logging can be enabled for debug / trace purposes ::

#@@imports@@
import logging
logging.basicConfig(level=logging.WARNING)

Code blocks
-----------
Expand Down
8 changes: 8 additions & 0 deletions source/sphinx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ gives us processed doctree ::

#@@sphinx_extension@@
def sphinx_get_doctree(app, doctree, docname):

if app.config.rst2code_debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)

logging.debug("Got doctree")
scan_doctree(doctree,docname)

We wait for "build-finished" event to create source code files (SEE? maybe we should do it before ?) ::

#@@sphinx_extension@@
def sphinx_build_finished(app, exception):
global OUTPUT_DIR
env = app.builder.env
OUTPUT_DIR = app.config.rst2code_output_dir
process_blocks()
Expand All @@ -27,6 +34,7 @@ To use rst2code within sphinx, [configuration]... So we need a "setup" function
def setup(app):
app.add_config_value("rst2code_output_dir", "./src", "env")
app.add_config_value("rst2code_max_iterations", 10, "env")
app.add_config_value("rst2code_debug", False, "env")
app.connect('doctree-resolved',sphinx_get_doctree)
app.connect('build-finished', sphinx_build_finished)

0 comments on commit 99a8de4

Please sign in to comment.