File: generate_doc

package info (click to toggle)
morse-simulator 1.4-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 187,116 kB
  • sloc: ansic: 108,311; python: 25,694; cpp: 786; makefile: 126; xml: 34; sh: 7
file content (779 lines) | stat: -rwxr-xr-x 27,144 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#! /usr/bin/env python
"""
This tools import various components of MORSE and generates a set of
documentation in RST format based on the Python source.

It generates doc for:
    - the component itself, based on the component class docstring,
    - the data fields exposed by the components and created with add_data,
    - the configurable parameters created with add_property
    - the services exported by the conmponent
    - the abstraction levels exposed by the component
    - the interfaces and serialization types for each input/output

The tool also generates simple doc page for each environments available with
MORSE
"""

import os
import sys
import pkgutil
import inspect
from collections import OrderedDict
#
# helpers
#

def get_classes_from_module(module_name):
    __import__(module_name)
    # Predicate to make sure the classes only come from the module in question
    def predicate(member):
        return inspect.isclass(member) and member.__module__.startswith(module_name)
    # fetch all members of module name matching 'pred'
    return inspect.getmembers(sys.modules[module_name], predicate)

def get_submodules(module_name):
    """ Get a list of submodules from a module name.
    Not recursive, don't return nor look in subpackages """
    __import__(module_name)
    module = sys.modules[module_name]
    module_path = getattr(module, '__path__')
    return [name for _, name, ispkg in pkgutil.iter_modules(module_path) if not ispkg]

def get_subclasses(module_name, skip_submodules=[]):
    subclasses = []
    submodules = get_submodules(module_name)
    for submodule_name in submodules:
        if submodule_name in skip_submodules:
            pass
        submodule = "%s.%s"%(module_name, submodule_name)
        try:
            submodule_classes = get_classes_from_module(submodule)
            for _, klass in submodule_classes:
                subclasses.append(klass)
        except Exception:
            # can not import some resources
            pass
    return subclasses

modules = [
    "morse.actuators",
    "morse.sensors",
    "morse.modifiers",
    "morse.robots",
]

import sys, os, codecs
import fnmatch
from copy import copy
import glob

from morse.core.actuator import Actuator
from morse.core.robot import Robot
from morse.core.sensor import Sensor
from morse.builder.creator import SensorCreator, ActuatorCreator, ComponentCreator
from morse.modifiers.abstract_modifier import AbstractModifier

from morse.builder.data import MORSE_DATASTREAM_DICT

sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

PREFIX = "."
MEDIA_PATH = "../../media"
print("MORSE source is located at " + os.environ['MORSESOURCE'])
DATA_PATH = os.path.join(os.environ['MORSESOURCE'], "data")


# docstring role that, if present, prevent automatic generation of a code sample
NOAUTOEXAMPLE = ":noautoexample:"

# documentation of special parameters
special_doc = {}

def underline(text, char = '='):
    return text + '\n' + (char * len(text) + '\n')

def insert_code(code):

    return ".. code-block:: python\n\n%s\n\n" % code

def insert_image(name):
    matches = []
    for root, dirnames, filenames in os.walk(MEDIA_PATH):
      for filename in fnmatch.filter(filenames, '%s.*' % name):
            matches.append(os.path.join(root, filename))

    if matches:
        file = matches[0]
        print("Found image %s for the component %s" % (file, name))
        # take the first file found
        return ".. image:: ../%s\n  :align: center\n  :width: 600\n\n" % file

    return ""

def extract_examples(doc):
    """
    Returns a set of examples extracted (ie, removed) from a docstring.
    Examples must be presented in a `.. example::` block.

    :returns: a tuple (examples list, input doc with examples blocks removed)
    """
    examples = []

    remaining_doc = []

    in_example = False
    example = []

    for line in doc:

        # examples are in an indented block (but may contain empty lines)
        # when we leave such a block, the example is complete, we add it
        # to the 'examples' list.
        if in_example and line and line[0] != " ":
            in_example = False
            # remove the first element, which is always an empty line 
            # (RST syntax for blocks)
            examples.append("\n".join(example[1:]))

        if in_example:
            example.append(line)

        # check if we enter an example block at the end to avoid adding the
        # line '.. example::' to 'example'.
        if line.strip() == ".. example::":
            in_example = True
            example = []
        else:
            # we do not want to add '.. example::' to the remaining doc either
            if not in_example:
                remaining_doc.append(line)

    return examples, remaining_doc

def parse_docstring(doc):
    """ Parses the doc string, and return the doc without :param *: or :return:
    but with a list of params, return values and their associated doc.

    Also replace known keywords by hyperlinks.
    Also replace :see:/:sees: by 'See also:'
    """

    generate_example = True

    # Try to 'safely' remove leading spaces introduced by natural Python
    # docstring formatting. We can not simply strip leading withspaces,
    # because they may be significant for rst (like in ..note:)
    if not doc:
        print("No docstring found! Bouhouh!")
        return ("", None, None, None, None)

    orig = doc.split('\n')
    if (orig[0].strip()):
        print("XXX Invalid docstring (first line of MORSE docstrings " \
              "must be empty):\n%s" % doc)
        return (doc, None, None, None, None)

    new = [""]
    
    # Try to determine indentation level reading number of space on the
    # first line

    trailing_space = 0
    for i, c in enumerate(orig[1]):
        if c != ' ':
            trailing_space = i
            break

    for l in orig[1:]:
        new.append(l[trailing_space:])

    examples, new = extract_examples(new)

    doc = "\n".join(new)

    # Pre-processing
    if NOAUTOEXAMPLE in doc:
        generate_example = False
        doc = doc.replace(NOAUTOEXAMPLE, "")


    doc = doc.replace(":see:", "\n**See also:**")
    doc = doc.replace(":sees:", "\n**See also:**")
    r = doc.split(":param ", 1)
    doc = r[0]
    paramsdoc = None

    if len(r) == 1:
        parts = doc.split(":return", 1)
        if len(parts) == 2:
            doc = parts[0]
            returndoc = parts[1].split(':', 1)[1]
            returndoc = returndoc.replace("\n", " ")
            return (doc, None, returndoc, generate_example, examples)
        else:
            return (doc, None, None, generate_example, examples)
    else:
        parts= r[1].split(":return", 1)

    returndoc = None
    paramsdoc = parts[0].split(":param ")

    paramsdoc = [param.replace("\n", " ").split(':', 1) for param in paramsdoc]
    paramsdoc = [[x,y.strip()] for x, y in paramsdoc]

    if len(parts) == 2:
        returndoc = parts[1].split(':', 1)[1]
        returndoc = returndoc.replace("\n", " ")


    return (doc, paramsdoc,returndoc, generate_example, examples)


# contains only classes that implement components and have
# an explicit name (ie, MyComponent._name = ...)
components = {}

def process_component_class(klass, builder_component = False):
    """
    :param builder_component: should be set to true for components that are pure
    Builder scripts (ie, no specific implementation in MORSE/{actuators|sensors})
    """
    global components

    print("process %s"%str(klass))
    if issubclass(klass, (Actuator, ActuatorCreator)):
        ctype = 'actuator'
    elif issubclass(klass, (Sensor, SensorCreator)):
        ctype = 'sensor'
    elif issubclass(klass, AbstractModifier):
        ctype = 'modifier'
    elif issubclass(klass, Robot):
        ctype = 'robot'
    else:
        print('not subclass %s'%str(klass))
        return

    component = klass

    if hasattr(component, '_name'):
        name = component._name
        
        try:
            parent_name = component.mro()[1]._name
            if name == parent_name:
                print("Component %s does not declare its own _name (name identical to parent <%s>). Skipping it." % (component.__name__, name))
                return
        except AttributeError:
            pass
    else:
        print("Component %s does not declare a _name. Skipping it." % component.__name__)
        return
    
    if hasattr(component, '_short_desc'):
        desc = getattr(component, '_short_desc')
    else:
        desc = ""
    
    modulename = klass.__name__.lower() if builder_component else sys.modules[klass.__module__].__name__

    components[name] = {'klassname' : klass.__name__,
                        'object': component,
                        'type': ctype,
                        'desc': desc,
                        'module': modulename,
                        'doc': component.__doc__,
                        'builder_component': builder_component,
                        'services':[]} # default to no services. Those are filled in later on


# browse morse components modules
for module in modules:
    print("browse %s classes"%module)
    for klass in get_subclasses(module):
        process_component_class(klass)



# Extract levels, data_fields and properties
for name, props in components.items():
    c = props['object'] # component class
    for cls in reversed(c.__mro__):
        for key in ['levels', 'data_fields', 'properties']:
            attribute = '_' + key
            if hasattr(cls, attribute):
                if not key in components[name]:
                    components[name][key] = copy(getattr(cls, attribute))
                else:
                    components[name][key].update(getattr(cls, attribute))

# Then, extract services
for name, props in components.items():
    c = props['object'] # component class
    services = {}
    for fn in [getattr(c, fn) for fn in dir(c)]:
        if hasattr(fn, "_morse_service"):
            print("Found service '" + fn.__name__ + "' in component " + name)

            services[fn.__name__] = {'async': fn._morse_service_is_async,
                                     'doc': fn.__doc__}

    components[name]['services'] = services

# 'Orphans' are components that only exist as Builder scripts (they do not have
# explicit components implementations in morse/{actuators|sensors}).
# We need to generate documentation for them as well.
orphans = []

from morse.builder import *
for klass in get_subclasses('morse.builder'):
    if issubclass(klass, (ComponentCreator)):
        # _classpath should be manually set when subclassing a *Creator
        if klass._classpath:

            try:
                if klass._classpath == klass.mro()[1]._classpath:
                    print("Component %s does not declare its own _classpath (identical to parent <%s>). Adding it to orphans." % (klass.__name__, klass.mro()[1].__name__))
                    orphans.append(klass)
                    continue
            except AttributeError:
                pass

        elif klass not in [SensorCreator, ActuatorCreator, ComponentCreator]:
            print("%s has no _classpath. Adding it to orphans." % klass.__name__)
            orphans.append(klass)

# Document orphans as well as possible
print("\n\nDocumenting orphan components\n")
for cmpt in orphans:
    process_component_class(cmpt, 
                            builder_component = True)

# Retrieve serializers documentation
def get_interface_doc(classpath):

    if not isinstance(classpath, str):
        return None, None, None

    modulename, classname = classpath.rsplit(".", 1)

    try:
        __import__(modulename)
    except ImportError as detail:
        print("WARNING! Interface module not found: %s. Maybe you did not install the required middleware?" % detail)
        return classname, None, None
    except OSError:
        return classname, None, None

    module = sys.modules[modulename]

    try:
        klass = getattr(module, classname)
    except AttributeError as detail:
        raise Exception("Serialization class not found: %s" % detail)
        return None

    return (klass._type_name, klass.__doc__, klass._type_url)


# Finally, generate doc

def print_code_snippet(out, name, props):

    out.write(".. cssclass:: examples morse-section\n\n")
    title = "Examples"
    out.write(underline(title, '-') + '\n')

    out.write("\nThe following examples show how to use this component in a *Builder* script:\n\n")

def generate_builder_example(out, props):
    args = {'var': props['klassname'].lower(), 'name': props['klassname'], 'type': props['type']}

    code = """
    from morse.builder import *
    """

    if props['type'] != 'robot':
        code += """
    # adds a default robot (the MORSE mascott!)
    robot = Morsy()
    """

    code += """
    # creates a new instance of the %(type)s
    %(var)s = %(name)s()

    # place your component at the correct location
    %(var)s.translate(<x>, <y>, <z>)
    %(var)s.rotate(<rx>, <ry>, <rz>)
    """ % args

    if "levels" in props:
        code += """
    # select a specific abstraction level (cf below), or skip it to use default level
    %(var)s.level(<level>)
    """ % args

    if props['type'] != 'robot':
        code += """
    robot.append(%(var)s)
    """ % args

    code += """
    # define one or several communication interface, like 'socket'
    %(var)s.add_interface(<interface>)

    env = Environment('empty')
    """ % args

    out.write(insert_code(code))

def print_files(out, name, props):
    out.write(".. cssclass:: files morse-section\n\n")
    if props['type'] == 'modifier':
        title = "Sources of examples"
        out.write(underline(title, '-') + '\n')
    else:
        title = "Other sources of examples"
        out.write(underline(title, '+') + '\n')

    module_name = components[name]['module'].split('.')[-1]

    out.write("- `Source code <../../_modules/" +
              components[name]['module'].replace('.', '/') +
              ".html>`_\n")
    out.write("- `Unit-test <../../_modules/base/" +
                module_name + "_testing.html>`_\n")

    out.write("\n\n")

def supported_interfaces(cmpt, level = "default", tabs = 0):

    def iface_type(type_name, value, type_url):
        if not type_name: 
            return ""
        else:
            if type_url:
                return " as `%s <%s>`_ (:py:mod:`%s`)" % (type_name, type_url, value)
            else:
                return " as %s (:py:mod:`%s`)" % (type_name, value)

    if not cmpt in MORSE_DATASTREAM_DICT:
        return "(attention, no interface support!)"
    if not level in MORSE_DATASTREAM_DICT[cmpt]:
        return "(attention, no interface support!)"

    interfaces = ""
    for interface, values in MORSE_DATASTREAM_DICT[cmpt][level].items():

        # we find a dict when we fallback on default serialization
        if isinstance(values, dict): 
            values = values[interface]

        # If it is a single string, make a list, otherwise, it is
        # already a list.
        if isinstance(values, str):
            values = [values]

        ifaces = []
        for value in values:
            type_name, iface_doc, type_url = get_interface_doc(value)
            ifaces.append(iface_type(type_name, value, type_url))
        interfaces += "\t" * tabs + "- :tag:`%s` %s\n" % (interface, ' or'.join(ifaces))

    return interfaces



def print_levels(out, name, props):

        try:
            levels = props['levels']
        except KeyError:
            return False

        out.write(".. cssclass:: levels morse-section\n\n")
        title = "Available functional levels"
        out.write(underline(title, '-') + '\n')
        out.write("\n*Functional levels* are predefined *abstraction* or *realism* levels for the %s.\n\n" % props["type"])


        for name, level in levels.items():
            out.write('\n- ``' + name + '``' + (' (default level)' if level[2] else '' ) + ' ' + level[1] + "\n")

            out.write("\tAt this level, the %s %s these datafields at each simulation step:\n\n" % ( props["type"], "exports" if props["type"] == "sensor" else "reads"))

            for fieldname, prop in props["data_fields"].items():
                if name is "all" or name in prop[3]:
                    out.write('\t- ``' + fieldname + '`` (' + (prop[1] + ', ' if prop[1] else '' ) + 'initial value: ``' + str(prop[0]) + '``): ' + prop[2] + "\n")

            out.write("\n\t*Interface support:*\n\n%s\n\n" % supported_interfaces(props["object"].__module__ + '.' + props["object"].__name__, name, tabs = 1))

        out.write("\n\n")
        return True

def print_data(out, name, props):

        out.write(".. cssclass:: fields morse-section\n\n")
        title = "Data fields"
        out.write(underline(title, '-') + '\n')

        if not "data_fields" in props:
            out.write("No data field documented (see above for possible notes).\n\n")
            return

        prop = props['data_fields']
        out.write("\nThis %s %s these datafields at each simulation step:\n\n" % ( props["type"], "exports" if props["type"] == "sensor" else "reads"))

        for name, prop in prop.items():
            out.write('- ``' + name + '`` (' + (prop[1] + ', ' if prop[1] else '' ) + 'initial value: ``' + str(prop[0]) + '``)\n\t' + prop[2] + "\n")

        out.write("\n*Interface support:*\n\n%s" % supported_interfaces(props["object"].__module__ + '.' + props["object"].__name__))

        out.write("\n\n")

def print_properties(out, name, props):

        out.write(".. cssclass:: properties morse-section\n\n")
        title = "Configuration parameters for " + name
        out.write(underline(title, '-') + '\n')

        if props['type'] == 'robot':
            out.write("\nYou can :\n\n- set the mass of the robot using the builder method :py:meth:`morse.builder.morsebuilder.Robot.set_mass()`\n- set the friction coefficient of the robot using the builder method :py:meth:`morse.builder.morsebuilder.Robot.set_friction()`\n\n")

        try:
            prop = props['properties']
        except KeyError:
            out.write("*No configurable parameter.*\n\n")
            return

        if props['type'] != 'modifier':
            out.write("\nYou can set these properties in your scripts with ``<component>.properties(<property1>=..., <property2>=...)``.\n\n")
        else:
            out.write("\nYou can set these parameters in your scripts with ``<component>.alter('%s', <property1>=..., <property2>=...)``.\n\n"
                      % name)

        for name, prop in prop.items():
            out.write('- ``' + name + '`` (' + (prop[1] + ', ' if prop[1] else '' ) + 'default: ``' + 
          ('"' + prop[0] + '"' if isinstance(prop[0], str) else str(prop[0])) + '``)\n\t' + prop[2] + "\n")

        out.write("\n\n")

def print_services(out, name, props):

        out.write(".. cssclass:: services morse-section\n\n")
        title = "Services for " + name
        out.write(underline(title, '-') + '\n')

        services = props['services']

        if not services:
            out.write("*This component does not expose any service.*\n\n")
            return

        for name, serv in services.items():
            out.write('- ``' + name + '(')

            doc = params = returndoc = generate_example = examples = None

            if serv['doc']:
                doc, params, returndoc, generate_example, examples = parse_docstring(serv['doc'])

            if params:
                out.write(", ".join([p for p,d in params]))

            if serv['async']:
                out.write(')`` (non blocking)')
            else:
                out.write(')`` (blocking)')

            if doc:
                out.write(doc.replace("\n", "\n    "))
                if params:
                    out.write("\n  - Parameters\n\n")
                    for p, d in params:
                        out.write("    - ``" + p + "``: " + d + "\n")
                if returndoc:
                    out.write("\n  - Return value\n\n")
                    out.write("   " + returndoc)
                    out.write("\n")
            else:
                out.write("\n    (no documentation yet)")
            out.write("\n")

        out.write("\n\n")



if not os.path.exists(PREFIX):
    os.makedirs(PREFIX)

for directory in ['actuators', 'sensors', 'modifiers', 'robots', 'environments']:
    path = os.path.join(PREFIX, directory)
    if not os.path.exists(path):
        os.makedirs(path)

for name, props in components.items():
    module = (props['module'].split('.'))[-1]
    with open(os.path.join(PREFIX, props['type'] + 's', module + ".rst"), 'w',
              encoding='utf-8') as out:
        out.write(underline(name) + '\n')

        # if an image if available, use it
        out.write(insert_image(module))

        if props['desc']:
            out.write("\n**" + props['desc'] + "**\n\n")

        doc, params, returndoc, generate_example, examples = parse_docstring(props['doc'])
        out.write(doc + "\n\n")

        if not props["builder_component"]:
            print_properties(out, name, props)
            if not print_levels(out, name, props) and \
                props['type'] not in ['modifier', 'robot']:
                print_data(out, name, props)

            if props['type'] != 'modifier':
                print_services(out, name, props)
                if generate_example or examples:
                    print_code_snippet(out, name, props)
                    if generate_example:
                        generate_builder_example(out, props)
                    for example in examples:
                        out.write(insert_code(example))

            print_files(out, name, props)

        out.write('\n\n*(This page has been auto-generated from MORSE module %s.)*\n' % (props['object'].__module__) )

environments = glob.glob(os.path.join(DATA_PATH + "/environments/*.blend"))
environments += glob.glob(os.path.join(DATA_PATH + "/environments/**/*.blend"))
environments = [env.split(DATA_PATH + "/environments/")[1].split(".blend")[0] for env in environments]

print("Found environments: %s" % environments)

for env in environments:
    path = env
    name = env.split("/")[1] if "/" in env else env

    with open(os.path.join(PREFIX, 'environments',  name + ".rst"), 'w',
              encoding='utf-8') as out:
        out.write(underline("<%s> environment" % name) + '\n')

        # if an image if available, use it
        out.write(insert_image(name))

        out.write(underline("To use it", "-") + "\n")
        out.write(".. code-block:: python\n\n  from morse.builder import *\n\n")
        out.write("  # your robots...\n\n  env = Environment('%s')\n" % path)
        out.write('\n\n*(This page has been auto-generated from MORSE available environments.)*\n')



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Matrix generation tool """
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

def add_csv_line(out, l):
    out.write(', '.join(l) + '\n')

def print_csv_data(out, name, datastreams, props):
        if 'levels' in props:
            module = (props['object'].__module__.split('.'))[-1]
            add_csv_line(out, [":doc:`" + props['type'] + 's/' + module + "`,"])
            for name, level in props['levels'].items():
                s = supported_interfaces_csv(props, datastreams, name)
                out.write(s + '\n')
        else:
            s = supported_interfaces_csv(props, datastreams)
            out.write(s + '\n')

def supported_interfaces_csv(props, datastreams, level = "default"):
    def csv_format(type_name, type_url):
        if not type_name: 
            return "?"
        else:
            if type_url:
                return "`%s <%s>`_" % (type_name, type_url)
            else:
                return "%s" % (type_name)

    if props['builder_component']:
        module = props['module']
        cmpt = getattr(props['object'], '_classpath', 'to be or not to be ...')
    else:
        module = (props['object'].__module__.split('.'))[-1]
        cmpt = props["object"].__module__ + '.' +  props["object"].__name__

    module_name = ":doc:`" + props['type'] + 's/' + module + "`"
    if (level != 'default'):
        module_name = module_name + " (" + level + " level )"
    supported_interfaces = [module_name]

    if not cmpt in MORSE_DATASTREAM_DICT or not level in MORSE_DATASTREAM_DICT[cmpt]:
        for ds in datastreams:
            supported_interfaces.append('✘')
    else:
        for ds in datastreams:
            values = MORSE_DATASTREAM_DICT[cmpt][level].get(ds, None)
            if not values:
                supported_interfaces.append('✘')
            if values:
                if (isinstance(values, dict)):
                    supported_interfaces.append('✔')
                    continue

                if (isinstance(values, str)):
                    values = [values]

                ifaces = []
                for value in values:
                    type_name, iface_doc, type_url = get_interface_doc(value)
                    ifaces.append(csv_format(type_name, type_url))
                supported_interfaces.append('✔ ' + ' or  '.join(ifaces))

    return ' ,'.join(supported_interfaces)

def generate_matrix(filename):

    # Format:
    #     middleware: [support datastreams, support services]
    middlewares = OrderedDict()
    
    middlewares['socket']= ['✔','✔']
    middlewares['ros']= ['✔ (topics)','✔ (services + actions)']
    middlewares['yarp']= ['✔ (ports)','✔']
    middlewares['pocolibs']= ['✔ (posters)','✔ (requests)']
    middlewares['moos']= ['✔ (database)','✘']
    middlewares['mavlink']= ['✔','✘']
    middlewares['hla']=['✔ (attribute update)','✘']
    middlewares['text']= ['✔','✘']

    with open(filename, 'w', encoding='utf-8') as out:
        sensors_list = []
        actuators_list = []
        for name, props in components.items():
            if props['type'] == 'sensor':
                sensors_list.append(name)

            if props['type'] == 'actuator':
                actuators_list.append(name)

        sensors_list.sort()
        actuators_list.sort()
        
        add_csv_line(out, [''] + [':tag:`' + d + '`' for d in middlewares.keys()])
        add_csv_line(out, [''] + [':doc:`Support notes <middlewares/' + d + '>`' for d in middlewares.keys()])
        add_csv_line(out, ['Communications,'])
        add_csv_line(out, ['*Datastreams*'] + [mw[0] for name, mw in middlewares.items()])
        add_csv_line(out, ['*Services*'] + [mw[1] for name, mw in middlewares.items()])
        add_csv_line(out, ['Sensors,'])
        for name in sensors_list:
            print_csv_data(out, name, middlewares.keys(), components[name])
        add_csv_line(out, ['Actuators,'])
        for name in actuators_list:
            print_csv_data(out, name, middlewares.keys(), components[name])


generate_matrix(os.path.join(PREFIX, 'compatibility_matrix.csv'))