views:

441

answers:

3

This question is even harder today because I haven't had any luck using the search function on the Sphinx homepage today.

I have a group of modules that I want to be documented from the docstrings. However, these are not pure Python scripts. They won't compile as is, because they are run from a C# application that creates a new variable in the executing scope.

To the Python compiler, it looks like I have an undefined method (which, technically I do, until C# creates the IronPython script engine and creates the method).

When I run:

sphinx-build -b html output/html

I get:

NameError: name 'injected_method' is not defined

How do I get Sphinx to ignore compilation errors and just generate my documentation?

EDIT:

If anybody knows if an alternative to Sphinx (like Epydoc) does not have to compile the Python script to get the function signatures and docstrings, that would be helpful as well. Sphinx is the best looking documentation generator, but I'll abandon it if I have to.

A: 

Perhaps you could define injected_method as a empty function so that the documentation will work. You'll need to make sure that the definition of injected_method that you're injecting happens after the new injected_method stub.

#By empty function I mean a function that looks like this
def injected_method():
  pass
David Locke
I suppose that would work, but I'm not sure how to get it to occur after the method is injected. I have to create the ScriptScope (where I inject my custom methods) before then running the script within that ScriptScope.Also, I'd rather not have dummy methods in my source.It might come to something like this if I can't get around the compiler errors, though...
cgyDeveloper
+3  A: 

Well, you could try:

  • Wrapping the usage of injected_method in a try/except.
  • Writing a script that filters out all python-code that is run on import time, and feeds the result into Sphinx.
  • You could....ok, I have no more ideas. :)
Lennart Regebro
I couldn't get it to hide behind a try/except. I suppose I could filter out all the injected code before running Sphinx, that would work.
cgyDeveloper
A: 

Okay, I found a way to get around the Errors.

When setting up the embedded scripting environment, instead of using:

ScriptScope.SetVariable("injected_method", myMethod);

I am now using:

ScriptRuntime.Globals.SetVariable("injected_method", myMethod);

And then, in the script:

import injected_method

Then I created a dummy injected_method.py file in my search path, which is blank. I delete the dummy file during the build of my C# project to avoid any conflicts.

cgyDeveloper