import os, re, subprocess, json, collections
from sphinx.addnodes import toctree
from docutils import io, nodes, statemachine, utils
from docutils.parsers.rst import Directive
from jinja2 import Environment, PackageLoader

# Maintain a cache of previously loaded examples
example_cache = {}

# Maintain a cache of previously loaded service descriptions
description_cache = {}


def setup(app):
    """
    see: http://sphinx.pocoo.org/ext/appapi.html
    this is the primary extension point for Sphinx
    """
    from sphinx.application import Sphinx
    if not isinstance(app, Sphinx): return

    app.add_directive('service', ServiceIntro)
    app.add_directive('apiref', ServiceApiRef)
    app.add_directive('indexlinks', ServiceIndexLinks)
    app.add_directive('example', ExampleDirective)


class ServiceDescription():
    """
    Loads the service description for a given source file
    """