Check if Sphinx doc called the script

I am currently trying to generate sphinx documentation for scripts which use the ArcGIS arcpy library.

I am running into an issue when sphinx tries to run the scripts while generating the documentation, as arcpy scripts take input parameters from the arcgis gui. Since sphinx is calling the scripts without the gui, these parameters are empty and are causing Tracebacks such as:

C:VersionControlPythonScriptsSourcesrc_buildscript_export_pdf.rst:4: WARNING:     autodoc: failed to import module u'gis.scripts.script_export_pdf'; the following exception was raised:
Traceback (most recent call last):
  File "C:VersionControlPythonScriptsSourcesrclibPython27ArcGIS10.1libsite-packagessphinxextautodoc.py", line 335, in import_object
    __import__(self.modname)
  File "C:VersionControlPythonScriptsSourcesrcgisscriptsscript_export_pdf.py", line 76, in <module>
    mxd.ExportToPDF(in_mxds, out_folder, overwrite, current)
  File "C:VersionControlPythonScriptsSourcesrcgismappingmxd.py", line 315, in ExportToPDF
    _ExportToPDF(arcpy.mapping.MapDocument(m), out_folder, overwrite)
  File "C:Program Files (x86)ArcGISDesktop10.1arcpyarcpyarcobjectsmixins.py", line 609, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.

I get around this issue in unittests by setting a variable when the test begins which the script checks for and sets test values in the parameters, I am wondering if there is a similar workaround with sphinx?

Answer

The solution I came up with, while probably no-where near ideal, is to simply check

if 'sphinx' in sys.modules:
    in_mxds = [r"C:/test.mxd"]
else:
    in_mxds = arcpy.GetParameterAsText(1)

This will ensure the script is not trying to get a parameter from the GUI which isn’t set when generating sphinx documents.

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .