"""An api for generating documentation from the codebase
"""

from os.path import dirname, join

import subprocess

def generate_documentation(dirs, output_dir):
    """Use doxygen to generate the documentation

    Positional arguments:
    dirs - the directories that doxygen should scan for documentation
    output_dir - location of the documentation after the return of this function
    """
    with open(join(dirname(__file__), "Doxyfile")) as doxyfile:
        proc = subprocess.Popen(["doxygen", "-"], stdin=subprocess.PIPE)
        proc.stdin.write(doxyfile.read())
        proc.stdin.write("OUTPUT_DIRECTORY={}\n".format(output_dir))
        proc.stdin.write("INPUT={}".format(" ".join(dirs)))
        proc.stdin.close()
        proc.wait()

if __name__ == "__main__":
    import sys
    from os.path import abspath, dirname, join
    # Be sure that the tools directory is in the search path
    ROOT = abspath(join(dirname(__file__), ".."))
    sys.path.insert(0, ROOT)

    from tools.toolchains.gcc import GCC_ARM
    from tools.targets import TARGET_MAP
    toolchain = GCC_ARM(TARGET_MAP["Super_Target"])
    resources = toolchain.scan_resources(".")
    for src in resources.headers:
        with open(src) as fd:
            contents = fd.read()
        with open(src, "w+") as fd:
            fd.write("/** \\addtogroup feature_mbed_os */\n/** @{{*/\n{:s}\n/** @}}*/\n".format(contents))
    for name, res in resources.features.iteritems():
        for src in res.headers:
            with open(src) as fd:
                contents = fd.read()
            with open(src, "w+") as fd:
                fd.write("/** \\addtogroup feature_{:s} */\n/** @{{*/\n{:s}\n/** @}}*/\n".format(name.lower(),contents))

    generate_documentation(sum(map(lambda x:x.headers, resources.features.values()), resources.headers),
                           join(dirname(dirname(__file__)), "mbed-docs"))
    print resources

@@ -88,16 +88,35 @@
 me, join
+%0Afrom os import sep
 %0A%0Aimport
@@ -409,16 +409,31 @@
     %22%22%22%0A
+    print dirs%0A
     with
@@ -1220,32 +1220,185 @@
 ources.headers:%0A
+        if len(src.split(sep)) %3E 2:%0A            name = src.split(sep)%5B1%5D%0A            if name == %22features%22:%0A                name = src.split(sep)%5B2%5D%0A    
         with ope
@@ -1415,32 +1415,36 @@
 fd:%0A            
+    
 contents = fd.re
@@ -1440,32 +1440,36 @@
 nts = fd.read()%0A
+    
         with ope
@@ -1492,32 +1492,36 @@
 fd:%0A            
+    
 fd.write(%22/** %5C%5C
@@ -1535,23 +1535,12 @@
 oup 
-feature_mbed_os
+%7B:s%7D
  */%5C
@@ -1577,16 +1577,21 @@
 .format(
+name,
 contents
@@ -1832,23 +1832,23 @@
 togroup 
-feature
+FEATURE
 _%7B:s%7D */
@@ -1894,16 +1894,8 @@
 name
-.lower()
 ,con
@@ -1930,16 +1930,53 @@
 ntation(
+filter(lambda x: %22targets%22 not in x, 
 sum(map(
@@ -2043,16 +2043,17 @@
 headers)
+)
 ,%0A      
@@ -2124,24 +2124,4 @@
 %22))%0A
-    print resources%0A

Switch how we do groups