Check if Sphinx doc called the script

前端 未结 4 2127
予麋鹿
予麋鹿 2021-01-18 15:41

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 scrip

相关标签:
4条回答
  • 2021-01-18 15:55

    Since Sphinx 1.3 there is a simple solution for this issue. Just add

    autodoc_mock_imports = ['arcpy']
    

    to your conf.py. This can be used when some external dependencies are not met at build time and break the building process. See Sphinx: how to exclude imports in automodule?.

    It's unfortunate that Esri has very poor arcpy documentation and mostly ignores Python standards.

    0 讨论(0)
  • 2021-01-18 16:09

    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.

    0 讨论(0)
  • 2021-01-18 16:12

    If your project is importing sphinx (in my case a sphinx extension), the following may also work for you

    import os
    import sys
    if os.path.basename(sys.argv[0]) == "sphinx-build":
        # code for when sphinx is running
    else:
        # code for regular application
    

    I'm not sure if that will work on Windows or if it should be something like

    if os.path.basename(sys.argv[0]) in ["sphinx-build", "sphinx-build.exe"]:
    
    0 讨论(0)
  • 2021-01-18 16:17

    I do something like this. In my conf.py I add a new variable to the builtins module like:

    # anywhere in conf.py before any of your modules are imported
    import builtins
    builtins.__sphinx_build__ = True
    

    Then in my module code I can write a check like:

    try:
        from some_dependency import SomeClass
    except ImportError:
        try:
            if __sphinx_build__:
                class SomeClass:
                    """Mock the class"""
        except NameError:
            raise ImportError('some_dependency')
    
    0 讨论(0)
提交回复
热议问题